From 53e376d836133a660223198c7bb8308fb912375e Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Mon, 20 Oct 2003 09:20:11 +0000 Subject: * Refactored the source tree. --- src/archive.cc | 340 --------------- src/archive.hh | 60 --- src/boost/Makefile.am | 3 + src/boost/format.hpp | 71 ++++ src/boost/format/Makefile.am | 6 + src/boost/format/exceptions.hpp | 96 +++++ src/boost/format/feed_args.hpp | 247 +++++++++++ src/boost/format/format_class.hpp | 135 ++++++ src/boost/format/format_fwd.hpp | 49 +++ src/boost/format/format_implementation.cc | 256 +++++++++++ src/boost/format/free_funcs.cc | 71 ++++ src/boost/format/group.hpp | 680 ++++++++++++++++++++++++++++++ src/boost/format/internals.hpp | 167 ++++++++ src/boost/format/internals_fwd.hpp | 65 +++ src/boost/format/macros_default.hpp | 48 +++ src/boost/format/parsing.cc | 454 ++++++++++++++++++++ src/db.cc | 425 ------------------- src/db.hh | 89 ---- src/dotgraph.cc | 135 ------ src/dotgraph.hh | 8 - src/exec.cc | 144 ------- src/exec.hh | 21 - src/expr.cc | 222 ---------- src/expr.hh | 66 --- src/fix.cc | 488 --------------------- src/fix/fix.cc | 488 +++++++++++++++++++++ src/globals.cc | 8 - src/globals.hh | 29 -- src/hash.cc | 124 ------ src/hash.hh | 51 --- src/libmain/shared.cc | 79 ++++ src/libmain/shared.hh | 15 + src/libnix/archive.cc | 340 +++++++++++++++ src/libnix/archive.hh | 60 +++ src/libnix/db.cc | 425 +++++++++++++++++++ src/libnix/db.hh | 89 ++++ src/libnix/exec.cc | 144 +++++++ src/libnix/exec.hh | 21 + src/libnix/expr.cc | 222 ++++++++++ src/libnix/expr.hh | 66 +++ src/libnix/globals.cc | 8 + src/libnix/globals.hh | 29 ++ src/libnix/hash.cc | 124 ++++++ src/libnix/hash.hh | 51 +++ src/libnix/md5.c | 448 ++++++++++++++++++++ src/libnix/md5.h | 151 +++++++ src/libnix/normalise.cc | 469 +++++++++++++++++++++ src/libnix/normalise.hh | 46 ++ src/libnix/pathlocks.cc | 90 ++++ src/libnix/pathlocks.hh | 24 ++ src/libnix/references.cc | 110 +++++ src/libnix/references.hh | 10 + src/libnix/store.cc | 405 ++++++++++++++++++ src/libnix/store.hh | 72 ++++ src/libnix/test-builder-1.sh | 3 + src/libnix/test-builder-2.sh | 9 + src/libnix/test.cc | 162 +++++++ src/libnix/util.cc | 253 +++++++++++ src/libnix/util.hh | 116 +++++ src/md5.c | 448 -------------------- src/md5.h | 151 ------- src/nix-hash.cc | 20 - src/nix-hash/nix-hash.cc | 20 + src/nix-help.txt | 32 -- src/nix.cc | 304 ------------- src/nix/dotgraph.cc | 135 ++++++ src/nix/dotgraph.hh | 8 + src/nix/nix-help.txt | 32 ++ src/nix/nix.cc | 304 +++++++++++++ src/normalise.cc | 469 --------------------- src/normalise.hh | 46 -- src/pathlocks.cc | 90 ---- src/pathlocks.hh | 24 -- src/references.cc | 110 ----- src/references.hh | 10 - src/shared.cc | 79 ---- src/shared.hh | 15 - src/store.cc | 405 ------------------ src/store.hh | 72 ---- src/test-builder-1.sh | 3 - src/test-builder-2.sh | 9 - src/test-expr-1.nix | 1 - src/test.cc | 162 ------- src/util.cc | 253 ----------- src/util.hh | 116 ----- 85 files changed, 7376 insertions(+), 5029 deletions(-) delete mode 100644 src/archive.cc delete mode 100644 src/archive.hh create mode 100644 src/boost/Makefile.am create mode 100644 src/boost/format.hpp create mode 100644 src/boost/format/Makefile.am create mode 100644 src/boost/format/exceptions.hpp create mode 100644 src/boost/format/feed_args.hpp create mode 100644 src/boost/format/format_class.hpp create mode 100644 src/boost/format/format_fwd.hpp create mode 100644 src/boost/format/format_implementation.cc create mode 100644 src/boost/format/free_funcs.cc create mode 100644 src/boost/format/group.hpp create mode 100644 src/boost/format/internals.hpp create mode 100644 src/boost/format/internals_fwd.hpp create mode 100644 src/boost/format/macros_default.hpp create mode 100644 src/boost/format/parsing.cc delete mode 100644 src/db.cc delete mode 100644 src/db.hh delete mode 100644 src/dotgraph.cc delete mode 100644 src/dotgraph.hh delete mode 100644 src/exec.cc delete mode 100644 src/exec.hh delete mode 100644 src/expr.cc delete mode 100644 src/expr.hh delete mode 100644 src/fix.cc create mode 100644 src/fix/fix.cc delete mode 100644 src/globals.cc delete mode 100644 src/globals.hh delete mode 100644 src/hash.cc delete mode 100644 src/hash.hh create mode 100644 src/libmain/shared.cc create mode 100644 src/libmain/shared.hh create mode 100644 src/libnix/archive.cc create mode 100644 src/libnix/archive.hh create mode 100644 src/libnix/db.cc create mode 100644 src/libnix/db.hh create mode 100644 src/libnix/exec.cc create mode 100644 src/libnix/exec.hh create mode 100644 src/libnix/expr.cc create mode 100644 src/libnix/expr.hh create mode 100644 src/libnix/globals.cc create mode 100644 src/libnix/globals.hh create mode 100644 src/libnix/hash.cc create mode 100644 src/libnix/hash.hh create mode 100644 src/libnix/md5.c create mode 100644 src/libnix/md5.h create mode 100644 src/libnix/normalise.cc create mode 100644 src/libnix/normalise.hh create mode 100644 src/libnix/pathlocks.cc create mode 100644 src/libnix/pathlocks.hh create mode 100644 src/libnix/references.cc create mode 100644 src/libnix/references.hh create mode 100644 src/libnix/store.cc create mode 100644 src/libnix/store.hh create mode 100755 src/libnix/test-builder-1.sh create mode 100755 src/libnix/test-builder-2.sh create mode 100644 src/libnix/test.cc create mode 100644 src/libnix/util.cc create mode 100644 src/libnix/util.hh delete mode 100644 src/md5.c delete mode 100644 src/md5.h delete mode 100644 src/nix-hash.cc create mode 100644 src/nix-hash/nix-hash.cc delete mode 100644 src/nix-help.txt delete mode 100644 src/nix.cc create mode 100644 src/nix/dotgraph.cc create mode 100644 src/nix/dotgraph.hh create mode 100644 src/nix/nix-help.txt create mode 100644 src/nix/nix.cc delete mode 100644 src/normalise.cc delete mode 100644 src/normalise.hh delete mode 100644 src/pathlocks.cc delete mode 100644 src/pathlocks.hh delete mode 100644 src/references.cc delete mode 100644 src/references.hh delete mode 100644 src/shared.cc delete mode 100644 src/shared.hh delete mode 100644 src/store.cc delete mode 100644 src/store.hh delete mode 100755 src/test-builder-1.sh delete mode 100755 src/test-builder-2.sh delete mode 100644 src/test-expr-1.nix delete mode 100644 src/test.cc delete mode 100644 src/util.cc delete mode 100644 src/util.hh (limited to 'src') diff --git a/src/archive.cc b/src/archive.cc deleted file mode 100644 index 9039ad7db43e..000000000000 --- a/src/archive.cc +++ /dev/null @@ -1,340 +0,0 @@ -#include -#include -#include - -#include -#include -#include -#include -#include - -#include "archive.hh" -#include "util.hh" - - -static string archiveVersion1 = "nix-archive-1"; - - -static void writePadding(unsigned int len, DumpSink & sink) -{ - if (len % 8) { - unsigned char zero[8]; - memset(zero, 0, sizeof(zero)); - sink(zero, 8 - (len % 8)); - } -} - - -static void writeInt(unsigned int n, DumpSink & sink) -{ - unsigned char buf[8]; - memset(buf, 0, sizeof(buf)); - buf[0] = n & 0xff; - buf[1] = (n >> 8) & 0xff; - buf[2] = (n >> 16) & 0xff; - buf[3] = (n >> 24) & 0xff; - sink(buf, sizeof(buf)); -} - - -static void writeString(const string & s, DumpSink & sink) -{ - unsigned int len = s.length(); - writeInt(len, sink); - sink((const unsigned char *) s.c_str(), len); - writePadding(len, sink); -} - - -static void dump(const string & path, DumpSink & sink); - - -static void dumpEntries(const Path & path, DumpSink & sink) -{ - DIR * dir = opendir(path.c_str()); - if (!dir) throw SysError("opening directory " + path); - - vector names; - - struct dirent * dirent; - while (errno = 0, dirent = readdir(dir)) { - string name = dirent->d_name; - if (name == "." || name == "..") continue; - names.push_back(name); - } - if (errno) throw SysError("reading directory " + path); - - sort(names.begin(), names.end()); - - for (vector::iterator it = names.begin(); - it != names.end(); it++) - { - writeString("entry", sink); - writeString("(", sink); - writeString("name", sink); - writeString(*it, sink); - writeString("node", sink); - dump(path + "/" + *it, sink); - writeString(")", sink); - } - - closedir(dir); /* !!! close on exception */ -} - - -static void dumpContents(const Path & path, unsigned int size, - DumpSink & sink) -{ - writeString("contents", sink); - writeInt(size, sink); - - int fd = open(path.c_str(), O_RDONLY); - if (fd == -1) throw SysError(format("opening file `%1%'") % path); - - unsigned char buf[65536]; - - unsigned int total = 0; - ssize_t n; - while ((n = read(fd, buf, sizeof(buf)))) { - if (n == -1) throw SysError("reading file " + path); - total += n; - sink(buf, n); - } - - if (total != size) - throw SysError("file changed while reading it: " + path); - - writePadding(size, sink); - - close(fd); /* !!! close on exception */ -} - - -static void dump(const Path & path, DumpSink & sink) -{ - struct stat st; - if (lstat(path.c_str(), &st)) - throw SysError(format("getting attributes of path `%1%'") % path); - - writeString("(", sink); - - if (S_ISREG(st.st_mode)) { - writeString("type", sink); - writeString("regular", sink); - if (st.st_mode & S_IXUSR) { - writeString("executable", sink); - writeString("", sink); - } - dumpContents(path, st.st_size, sink); - } - - else if (S_ISDIR(st.st_mode)) { - writeString("type", sink); - writeString("directory", sink); - dumpEntries(path, sink); - } - - else if (S_ISLNK(st.st_mode)) { - writeString("type", sink); - writeString("symlink", sink); - char buf[st.st_size]; - if (readlink(path.c_str(), buf, st.st_size) != st.st_size) - throw SysError("reading symbolic link " + path); - writeString("target", sink); - writeString(string(buf, st.st_size), sink); - } - - else throw Error("unknown file type: " + path); - - writeString(")", sink); -} - - -void dumpPath(const Path & path, DumpSink & sink) -{ - writeString(archiveVersion1, sink); - dump(path, sink); -} - - -static Error badArchive(string s) -{ - return Error("bad archive: " + s); -} - - -static void readPadding(unsigned int len, RestoreSource & source) -{ - if (len % 8) { - unsigned char zero[8]; - unsigned int n = 8 - (len % 8); - source(zero, n); - for (unsigned int i = 0; i < n; i++) - if (zero[i]) throw badArchive("non-zero padding"); - } -} - -static unsigned int readInt(RestoreSource & source) -{ - unsigned char buf[8]; - source(buf, sizeof(buf)); - if (buf[4] || buf[5] || buf[6] || buf[7]) - throw Error("implementation cannot deal with > 32-bit integers"); - return - buf[0] | - (buf[1] << 8) | - (buf[2] << 16) | - (buf[3] << 24); -} - - -static string readString(RestoreSource & source) -{ - unsigned int len = readInt(source); - char buf[len]; - source((unsigned char *) buf, len); - readPadding(len, source); - return string(buf, len); -} - - -static void skipGeneric(RestoreSource & source) -{ - if (readString(source) == "(") { - while (readString(source) != ")") - skipGeneric(source); - } -} - - -static void restore(const Path & path, RestoreSource & source); - - -static void restoreEntry(const Path & path, RestoreSource & source) -{ - string s, name; - - s = readString(source); - if (s != "(") throw badArchive("expected open tag"); - - while (1) { - s = readString(source); - - if (s == ")") { - break; - } else if (s == "name") { - name = readString(source); - } else if (s == "node") { - if (s == "") throw badArchive("entry name missing"); - restore(path + "/" + name, source); - } else { - throw badArchive("unknown field " + s); - skipGeneric(source); - } - } -} - - -static void restoreContents(int fd, const Path & path, RestoreSource & source) -{ - unsigned int size = readInt(source); - unsigned int left = size; - unsigned char buf[65536]; - - while (left) { - unsigned int n = sizeof(buf); - if (n > left) n = left; - source(buf, n); - if (write(fd, buf, n) != (ssize_t) n) - throw SysError("writing file " + path); - left -= n; - } - - readPadding(size, source); -} - - -static void restore(const Path & path, RestoreSource & source) -{ - string s; - - s = readString(source); - if (s != "(") throw badArchive("expected open tag"); - - enum { tpUnknown, tpRegular, tpDirectory, tpSymlink } type = tpUnknown; - int fd = -1; /* !!! close on exception */ - - while (1) { - s = readString(source); - - if (s == ")") { - break; - } - - else if (s == "type") { - if (type != tpUnknown) - throw badArchive("multiple type fields"); - string t = readString(source); - - if (t == "regular") { - type = tpRegular; - fd = open(path.c_str(), O_CREAT | O_EXCL | O_WRONLY, 0666); - if (fd == -1) - throw SysError("creating file " + path); - } - - else if (t == "directory") { - type = tpDirectory; - if (mkdir(path.c_str(), 0777) == -1) - throw SysError("creating directory " + path); - } - - else if (t == "symlink") { - type = tpSymlink; - } - - else throw badArchive("unknown file type " + t); - - } - - else if (s == "contents" && type == tpRegular) { - restoreContents(fd, path, source); - } - - else if (s == "executable" && type == tpRegular) { - readString(source); - struct stat st; - if (fstat(fd, &st) == -1) - throw SysError("fstat"); - if (fchmod(fd, st.st_mode | (S_IXUSR | S_IXGRP | S_IXOTH)) == -1) - throw SysError("fchmod"); - } - - else if (s == "entry" && type == tpDirectory) { - restoreEntry(path, source); - } - - else if (s == "target" && type == tpSymlink) { - string target = readString(source); - if (symlink(target.c_str(), path.c_str()) == -1) - throw SysError("creating symlink " + path); - } - - else { - throw badArchive("unknown field " + s); - skipGeneric(source); - } - - } - - if (fd != -1) close(fd); -} - - -void restorePath(const Path & path, RestoreSource & source) -{ - if (readString(source) != archiveVersion1) - throw badArchive("expected Nix archive"); - restore(path, source); -} - diff --git a/src/archive.hh b/src/archive.hh deleted file mode 100644 index 67e236668a06..000000000000 --- a/src/archive.hh +++ /dev/null @@ -1,60 +0,0 @@ -#include - -#include "util.hh" - - -/* dumpPath creates a Nix archive of the specified path. The format - is as follows: - - IF path points to a REGULAR FILE: - dump(path) = attrs( - [ ("type", "regular") - , ("contents", contents(path)) - ]) - - IF path points to a DIRECTORY: - dump(path) = attrs( - [ ("type", "directory") - , ("entries", concat(map(f, sort(entries(path))))) - ]) - where f(fn) = attrs( - [ ("name", fn) - , ("file", dump(path + "/" + fn)) - ]) - - where: - - attrs(as) = concat(map(attr, as)) + encN(0) - attrs((a, b)) = encS(a) + encS(b) - - encS(s) = encN(len(s)) + s + (padding until next 64-bit boundary) - - encN(n) = 64-bit little-endian encoding of n. - - contents(path) = the contents of a regular file. - - sort(strings) = lexicographic sort by 8-bit value (strcmp). - - entries(path) = the entries of a directory, without `.' and - `..'. - - `+' denotes string concatenation. */ - -struct DumpSink -{ - virtual void operator () (const unsigned char * data, unsigned int len) = 0; -}; - -void dumpPath(const Path & path, DumpSink & sink); - - -struct RestoreSource -{ - /* The callee should store exactly *len bytes in the buffer - pointed to by data. It should block if that much data is not - yet available, or throw an error if it is not going to be - available. */ - virtual void operator () (unsigned char * data, unsigned int len) = 0; -}; - -void restorePath(const Path & path, RestoreSource & source); diff --git a/src/boost/Makefile.am b/src/boost/Makefile.am new file mode 100644 index 000000000000..9ea79c660cf7 --- /dev/null +++ b/src/boost/Makefile.am @@ -0,0 +1,3 @@ +SUBDIRS = format + +EXTRA_DIST = format.hpp diff --git a/src/boost/format.hpp b/src/boost/format.hpp new file mode 100644 index 000000000000..a287048ed338 --- /dev/null +++ b/src/boost/format.hpp @@ -0,0 +1,71 @@ +// -*- C++ -*- +// Boost general library 'format' --------------------------- +// See http://www.boost.org for updates, documentation, and revision history. + +// (C) Samuel Krempp 2001 +// krempp@crans.ens-cachan.fr +// Permission to copy, use, modify, sell and +// distribute this software is granted provided this copyright notice appears +// in all copies. This software is provided "as is" without express or implied +// warranty, and with no claim as to its suitability for any purpose. + +// ideas taken from Rüdiger Loos's format class +// and Karl Nelson's ofstream + +// ---------------------------------------------------------------------------- +// format.hpp : primary header +// ---------------------------------------------------------------------------- + +#ifndef BOOST_FORMAT_HPP +#define BOOST_FORMAT_HPP + +#include +#include +#include +#include + +#include +//#define BOOST_NO_STD_LOCALE +//#define BOOST_NO_LOCALE_ISIDIGIT +//#include + +#include + +namespace boost +{ + template void throw_exception(E const & e) + { + throw e; + } +} + +#define BOOST_ASSERT(expr) assert(expr) + + +// **** Forward declarations ---------------------------------- +#include // basic_format, and other frontends +#include // misc forward declarations for internal use + + +// **** Auxiliary structs (stream_format_state , and format_item ) +#include + +// **** Format class interface -------------------------------- +#include + +// **** Exceptions ----------------------------------------------- +#include + +// **** Implementation ------------------------------------------- +//#include // member functions + +#include // class for grouping arguments + +#include // argument-feeding functions +//#include // format-string parsing (member-)functions + +// **** Implementation of the free functions ---------------------- +//#include + + +#endif // BOOST_FORMAT_HPP diff --git a/src/boost/format/Makefile.am b/src/boost/format/Makefile.am new file mode 100644 index 000000000000..5badba27cf6d --- /dev/null +++ b/src/boost/format/Makefile.am @@ -0,0 +1,6 @@ +noinst_LIBRARIES = libformat.a + +libformat_a_SOURCES = format_implementation.cc free_funcs.cc parsing.cc + +EXTRA_DIST = exceptions.hpp feed_args.hpp format_class.hpp format_fwd.hpp \ + group.hpp internals.hpp internals_fwd.hpp macros_default.hpp diff --git a/src/boost/format/exceptions.hpp b/src/boost/format/exceptions.hpp new file mode 100644 index 000000000000..79e452449ef8 --- /dev/null +++ b/src/boost/format/exceptions.hpp @@ -0,0 +1,96 @@ +// -*- C++ -*- +// Boost general library 'format' --------------------------- +// See http://www.boost.org for updates, documentation, and revision history. + +// (C) Samuel Krempp 2001 +// krempp@crans.ens-cachan.fr +// Permission to copy, use, modify, sell and +// distribute this software is granted provided this copyright notice appears +// in all copies. This software is provided "as is" without express or implied +// warranty, and with no claim as to its suitability for any purpose. + +// ideas taken from Rüdiger Loos's format class +// and Karl Nelson's ofstream (also took its parsing code as basis for printf parsing) + +// ------------------------------------------------------------------------------ +// exceptions.hpp +// ------------------------------------------------------------------------------ + + +#ifndef BOOST_FORMAT_EXCEPTIONS_HPP +#define BOOST_FORMAT_EXCEPTIONS_HPP + + +#include + + +namespace boost { + +namespace io { + +// **** exceptions ----------------------------------------------- + +class format_error : public std::exception +{ +public: + format_error() {} + virtual const char *what() const throw() + { + return "boost::format_error: " + "format generic failure"; + } +}; + +class bad_format_string : public format_error +{ +public: + bad_format_string() {} + virtual const char *what() const throw() + { + return "boost::bad_format_string: " + "format-string is ill-formed"; + } +}; + +class too_few_args : public format_error +{ +public: + too_few_args() {} + virtual const char *what() const throw() + { + return "boost::too_few_args: " + "format-string refered to more arguments than were passed"; + } +}; + +class too_many_args : public format_error +{ +public: + too_many_args() {} + virtual const char *what() const throw() + { + return "boost::too_many_args: " + "format-string refered to less arguments than were passed"; + } +}; + + +class out_of_range : public format_error +{ +public: + out_of_range() {} + virtual const char *what() const throw() + { + return "boost::out_of_range: " + "tried to refer to an argument (or item) number which is out of range, " + "according to the format string."; + } +}; + + +} // namespace io + +} // namespace boost + + +#endif // BOOST_FORMAT_EXCEPTIONS_HPP diff --git a/src/boost/format/feed_args.hpp b/src/boost/format/feed_args.hpp new file mode 100644 index 000000000000..ba107dce66d5 --- /dev/null +++ b/src/boost/format/feed_args.hpp @@ -0,0 +1,247 @@ +// -*- C++ -*- +// Boost general library 'format' --------------------------- +// See http://www.boost.org for updates, documentation, and revision history. + +// (C) Samuel Krempp 2001 +// krempp@crans.ens-cachan.fr +// Permission to copy, use, modify, sell and +// distribute this software is granted provided this copyright notice appears +// in all copies. This software is provided "as is" without express or implied +// warranty, and with no claim as to its suitability for any purpose. + +// ideas taken from Rüdiger Loos's format class +// and Karl Nelson's ofstream + +// ---------------------------------------------------------------------------- +// feed_args.hpp : functions for processing each argument +// (feed, feed_manip, and distribute) +// ---------------------------------------------------------------------------- + + +#ifndef BOOST_FORMAT_FEED_ARGS_HPP +#define BOOST_FORMAT_FEED_ARGS_HPP + +#include "boost/format/format_class.hpp" +#include "boost/format/group.hpp" + +//#include "boost/throw_exception.hpp" + +namespace boost { +namespace io { +namespace detail { +namespace { + + inline + void empty_buf(BOOST_IO_STD ostringstream & os) { + static const std::string emptyStr; + os.str(emptyStr); + } + + void do_pad( std::string & s, + std::streamsize w, + const char c, + std::ios::fmtflags f, + bool center) + // applies centered / left / right padding to the string s. + // Effects : string s is padded. + { + std::streamsize n=w-s.size(); + if(n<=0) { + return; + } + if(center) + { + s.reserve(w); // allocate once for the 2 inserts + const std::streamsize n1 = n /2, n0 = n - n1; + s.insert(s.begin(), n0, c); + s.append(n1, c); + } + else + { + if(f & std::ios::left) { + s.append(n, c); + } + else { + s.insert(s.begin(), n, c); + } + } + } // -do_pad(..) + + + template inline + void put_head(BOOST_IO_STD ostream& , const T& ) { + } + + template inline + void put_head( BOOST_IO_STD ostream& os, const group1& x ) { + os << group_head(x.a1_); // send the first N-1 items, not the last + } + + template inline + void put_last( BOOST_IO_STD ostream& os, const T& x ) { + os << x ; + } + + template inline + void put_last( BOOST_IO_STD ostream& os, const group1& x ) { + os << group_last(x.a1_); // this selects the last element + } + +#ifndef BOOST_NO_OVERLOAD_FOR_NON_CONST + template inline + void put_head( BOOST_IO_STD ostream& , T& ) { + } + + template inline + void put_last( BOOST_IO_STD ostream& os, T& x ) { + os << x ; + } +#endif + + + + +template +void put( T x, + const format_item& specs, + std::string & res, + BOOST_IO_STD ostringstream& oss_ ) +{ + // does the actual conversion of x, with given params, into a string + // using the *supplied* strinstream. (the stream state is important) + + typedef std::string string_t; + typedef format_item format_item_t; + + stream_format_state prev_state(oss_); + + specs.state_.apply_on(oss_); + + // in case x is a group, apply the manip part of it, + // in order to find width + put_head( oss_, x ); + empty_buf( oss_); + + const std::streamsize w=oss_.width(); + const std::ios::fmtflags fl=oss_.flags(); + const bool internal = (fl & std::ios::internal) != 0; + const bool two_stepped_padding = internal + && ! ( specs.pad_scheme_ & format_item_t::spacepad ) + && specs.truncate_ < 0 ; + + + if(! two_stepped_padding) + { + if(w>0) // handle simple padding via do_pad, not natively in stream + oss_.width(0); + put_last( oss_, x); + res = oss_.str(); + + if (specs.truncate_ >= 0) + res.erase(specs.truncate_); + + // complex pads : + if(specs.pad_scheme_ & format_item_t::spacepad) + { + if( res.size()==0 || ( res[0]!='+' && res[0]!='-' )) + { + res.insert(res.begin(), 1, ' '); // insert 1 space at pos 0 + } + } + if(w > 0) // need do_pad + { + do_pad(res,w,oss_.fill(), fl, (specs.pad_scheme_ & format_item_t::centered) !=0 ); + } + } + else // 2-stepped padding + { + put_last( oss_, x); // oss_.width() may result in padding. + res = oss_.str(); + + if (specs.truncate_ >= 0) + res.erase(specs.truncate_); + + if( res.size() - w > 0) + { // length w exceeded + // either it was multi-output with first output padding up all width.. + // either it was one big arg and we are fine. + empty_buf( oss_); + oss_.width(0); + put_last(oss_, x ); + string_t tmp = oss_.str(); // minimal-length output + std::streamsize d; + if( (d=w - tmp.size()) <=0 ) + { + // minimal length is already >= w, so no padding (cool!) + res.swap(tmp); + } + else + { // hum.. we need to pad (it was necessarily multi-output) + typedef typename string_t::size_type size_type; + size_type i = 0; + while( i( d ), oss_.fill()); + res.swap( tmp ); + } + } + else + { // okay, only one thing was printed and padded, so res is fine. + } + } + + prev_state.apply_on(oss_); + empty_buf( oss_); + oss_.clear(); +} // end- put(..) + + +} // local namespace + + + + + +template +void distribute(basic_format& self, T x) + // call put(x, ..) on every occurence of the current argument : +{ + if(self.cur_arg_ >= self.num_args_) + { + if( self.exceptions() & too_many_args_bit ) + boost::throw_exception(too_many_args()); // too many variables have been supplied ! + else return; + } + for(unsigned long i=0; i < self.items_.size(); ++i) + { + if(self.items_[i].argN_ == self.cur_arg_) + { + put (x, self.items_[i], self.items_[i].res_, self.oss_ ); + } + } +} + +template +basic_format& feed(basic_format& self, T x) +{ + if(self.dumped_) self.clear(); + distribute (self, x); + ++self.cur_arg_; + if(self.bound_.size() != 0) + { + while( self.cur_arg_ < self.num_args_ && self.bound_[self.cur_arg_] ) + ++self.cur_arg_; + } + + // this arg is finished, reset the stream's format state + self.state0_.apply_on(self.oss_); + return self; +} + + +} // namespace detail +} // namespace io +} // namespace boost + + +#endif // BOOST_FORMAT_FEED_ARGS_HPP diff --git a/src/boost/format/format_class.hpp b/src/boost/format/format_class.hpp new file mode 100644 index 000000000000..6875623acb47 --- /dev/null +++ b/src/boost/format/format_class.hpp @@ -0,0 +1,135 @@ +// -*- C++ -*- +// Boost general library 'format' --------------------------- +// See http://www.boost.org for updates, documentation, and revision history. + +// (C) Samuel Krempp 2001 +// krempp@crans.ens-cachan.fr +// Permission to copy, use, modify, sell and +// distribute this software is granted provided this copyright notice appears +// in all copies. This software is provided "as is" without express or implied +// warranty, and with no claim as to its suitability for any purpose. + +// ideas taken from Rüdiger Loos's format class +// and Karl Nelson's ofstream (also took its parsing code as basis for printf parsing) + +// ------------------------------------------------------------------------------ +// format_class.hpp : class interface +// ------------------------------------------------------------------------------ + + +#ifndef BOOST_FORMAT_CLASS_HPP +#define BOOST_FORMAT_CLASS_HPP + +#include +#include + +#include +#include + +#include + +namespace boost { + +class basic_format +{ +public: + typedef std::string string_t; + typedef BOOST_IO_STD ostringstream internal_stream_t; +private: + typedef BOOST_IO_STD ostream stream_t; + typedef io::detail::stream_format_state stream_format_state; + typedef io::detail::format_item format_item_t; + +public: + basic_format(const char* str); + basic_format(const string_t& s); +#ifndef BOOST_NO_STD_LOCALE + basic_format(const char* str, const std::locale & loc); + basic_format(const string_t& s, const std::locale & loc); +#endif // no locale + basic_format(const basic_format& x); + basic_format& operator= (const basic_format& x); + + basic_format& clear(); // empty the string buffers (except bound arguments, see clear_binds() ) + + // pass arguments through those operators : + template basic_format& operator%(const T& x) + { + return io::detail::feed(*this,x); + } + +#ifndef BOOST_NO_OVERLOAD_FOR_NON_CONST + template basic_format& operator%(T& x) + { + return io::detail::feed(*this,x); + } +#endif + + + // system for binding arguments : + template + basic_format& bind_arg(int argN, const T& val) + { + return io::detail::bind_arg_body(*this, argN, val); + } + basic_format& clear_bind(int argN); + basic_format& clear_binds(); + + // modify the params of a directive, by applying a manipulator : + template + basic_format& modify_item(int itemN, const T& manipulator) + { + return io::detail::modify_item_body(*this, itemN, manipulator) ; + } + + // Choosing which errors will throw exceptions : + unsigned char exceptions() const; + unsigned char exceptions(unsigned char newexcept); + + // final output + string_t str() const; + friend BOOST_IO_STD ostream& + operator<< ( BOOST_IO_STD ostream& , const basic_format& ); + + + template friend basic_format& + io::detail::feed(basic_format&, T); + + template friend + void io::detail::distribute(basic_format&, T); + + template friend + basic_format& io::detail::modify_item_body(basic_format&, int, const T&); + + template friend + basic_format& io::detail::bind_arg_body(basic_format&, int, const T&); + +// make the members private only if the friend templates are supported +private: + + // flag bits, used for style_ + enum style_values { ordered = 1, // set only if all directives are positional directives + special_needs = 4 }; + + // parse the format string : + void parse(const string_t&); + + int style_; // style of format-string : positional or not, etc + int cur_arg_; // keep track of wich argument will come + int num_args_; // number of expected arguments + mutable bool dumped_; // true only after call to str() or << + std::vector items_; // vector of directives (aka items) + string_t prefix_; // piece of string to insert before first item + + std::vector bound_; // stores which arguments were bound + // size = num_args OR zero + internal_stream_t oss_; // the internal stream. + stream_format_state state0_; // reference state for oss_ + unsigned char exceptions_; +}; // class basic_format + + +} // namespace boost + + +#endif // BOOST_FORMAT_CLASS_HPP diff --git a/src/boost/format/format_fwd.hpp b/src/boost/format/format_fwd.hpp new file mode 100644 index 000000000000..97c55f6684c3 --- /dev/null +++ b/src/boost/format/format_fwd.hpp @@ -0,0 +1,49 @@ +// -*- C++ -*- +// Boost general library 'format' --------------------------- +// See http://www.boost.org for updates, documentation, and revision history. + +// (C) Samuel Krempp 2001 +// krempp@crans.ens-cachan.fr +// Permission to copy, use, modify, sell and +// distribute this software is granted provided this copyright notice appears +// in all copies. This software is provided "as is" without express or implied +// warranty, and with no claim as to its suitability for any purpose. + +// ideas taken from Rüdiger Loos's format class +// and Karl Nelson's ofstream (also took its parsing code as basis for printf parsing) + +// ------------------------------------------------------------------------------ +// format_fwd.hpp : forward declarations, for primary header format.hpp +// ------------------------------------------------------------------------------ + +#ifndef BOOST_FORMAT_FWD_HPP +#define BOOST_FORMAT_FWD_HPP + +#include +#include + +namespace boost { + +class basic_format; + +typedef basic_format format; + +namespace io { +enum format_error_bits { bad_format_string_bit = 1, + too_few_args_bit = 2, too_many_args_bit = 4, + out_of_range_bit = 8, + all_error_bits = 255, no_error_bits=0 }; + +// Convertion: format to string +std::string str(const basic_format& ) ; + +} // namespace io + + +BOOST_IO_STD ostream& +operator<<( BOOST_IO_STD ostream&, const basic_format&); + + +} // namespace boost + +#endif // BOOST_FORMAT_FWD_HPP diff --git a/src/boost/format/format_implementation.cc b/src/boost/format/format_implementation.cc new file mode 100644 index 000000000000..41cb5fc9fa2d --- /dev/null +++ b/src/boost/format/format_implementation.cc @@ -0,0 +1,256 @@ +// -*- C++ -*- +// Boost general library format --------------------------- +// See http://www.boost.org for updates, documentation, and revision history. + +// (C) Samuel Krempp 2001 +// krempp@crans.ens-cachan.fr +// Permission to copy, use, modify, sell and +// distribute this software is granted provided this copyright notice appears +// in all copies. This software is provided "as is" without express or implied +// warranty, and with no claim as to its suitability for any purpose. + +// ideas taken from Rüdiger Loos's format class +// and Karl Nelson's ofstream + +// ---------------------------------------------------------------------------- +// format_implementation.hpp Implementation of the basic_format class +// ---------------------------------------------------------------------------- + + +#ifndef BOOST_FORMAT_IMPLEMENTATION_HPP +#define BOOST_FORMAT_IMPLEMENTATION_HPP + +//#include +//#include +#include + +namespace boost { + +// -------- format:: ------------------------------------------- +basic_format::basic_format(const char* str) + : style_(0), cur_arg_(0), num_args_(0), dumped_(false), + items_(), oss_(), exceptions_(io::all_error_bits) +{ + state0_.set_by_stream(oss_); + string_t emptyStr; + if( !str) str = emptyStr.c_str(); + parse( str ); +} + +#ifndef BOOST_NO_STD_LOCALE +basic_format::basic_format(const char* str, const std::locale & loc) + : style_(0), cur_arg_(0), num_args_(0), dumped_(false), + items_(), oss_(), exceptions_(io::all_error_bits) +{ + oss_.imbue( loc ); + state0_.set_by_stream(oss_); + string_t emptyStr; + if( !str) str = emptyStr.c_str(); + parse( str ); +} + +basic_format::basic_format(const string_t& s, const std::locale & loc) + : style_(0), cur_arg_(0), num_args_(0), dumped_(false), + items_(), oss_(), exceptions_(io::all_error_bits) +{ + oss_.imbue( loc ); + state0_.set_by_stream(oss_); + parse(s); +} +#endif //BOOST_NO_STD_LOCALE + +basic_format::basic_format(const string_t& s) + : style_(0), cur_arg_(0), num_args_(0), dumped_(false), + items_(), oss_(), exceptions_(io::all_error_bits) +{ + state0_.set_by_stream(oss_); + parse(s); +} + +basic_format:: basic_format(const basic_format& x) + : style_(x.style_), cur_arg_(x.cur_arg_), num_args_(x.num_args_), dumped_(false), + items_(x.items_), prefix_(x.prefix_), bound_(x.bound_), + oss_(), // <- we obviously can't copy x.oss_ + state0_(x.state0_), exceptions_(x.exceptions_) +{ + state0_.apply_on(oss_); +} + +basic_format& basic_format::operator= (const basic_format& x) +{ + if(this == &x) + return *this; + state0_ = x.state0_; + state0_.apply_on(oss_); + + // plus all the other (trivial) assignments : + exceptions_ = x.exceptions_; + items_ = x.items_; + prefix_ = x.prefix_; + bound_=x.bound_; + style_=x.style_; + cur_arg_=x.cur_arg_; + num_args_=x.num_args_; + dumped_=x.dumped_; + return *this; +} + + +unsigned char basic_format::exceptions() const +{ + return exceptions_; +} + +unsigned char basic_format::exceptions(unsigned char newexcept) +{ + unsigned char swp = exceptions_; + exceptions_ = newexcept; + return swp; +} + + +basic_format& basic_format ::clear() + // empty the string buffers (except bound arguments, see clear_binds() ) + // and make the format object ready for formatting a new set of arguments +{ + BOOST_ASSERT( bound_.size()==0 || num_args_ == static_cast(bound_.size()) ); + + for(unsigned long i=0; i num_args_ || bound_.size()==0 || !bound_[argN-1] ) + { + if( exceptions() & io::out_of_range_bit ) + boost::throw_exception(io::out_of_range()); // arg not in range. + else return *this; + } + bound_[argN-1]=false; + clear(); + return *this; +} + + + +std::string basic_format::str() const +{ + dumped_=true; + if(items_.size()==0) + return prefix_; + if( cur_arg_ < num_args_) + if( exceptions() & io::too_few_args_bit ) + boost::throw_exception(io::too_few_args()); // not enough variables have been supplied ! + + unsigned long sz = prefix_.size(); + unsigned long i; + for(i=0; i < items_.size(); ++i) + sz += items_[i].res_.size() + items_[i].appendix_.size(); + string_t res; + res.reserve(sz); + + res += prefix_; + for(i=0; i < items_.size(); ++i) + { + const format_item_t& item = items_[i]; + res += item.res_; + if( item.argN_ == format_item_t::argN_tabulation) + { + BOOST_ASSERT( item.pad_scheme_ & format_item_t::tabulation); + std::streamsize n = item.state_.width_ - res.size(); + if( n > 0 ) + res.append( n, item.state_.fill_ ); + } + res += item.appendix_; + } + return res; +} + +namespace io { +namespace detail { + +template +basic_format& bind_arg_body( basic_format& self, + int argN, + const T& val) + // bind one argument to a fixed value + // this is persistent over clear() calls, thus also over str() and << +{ + if(self.dumped_) self.clear(); // needed, because we will modify cur_arg_.. + if(argN<1 || argN > self.num_args_) + { + if( self.exceptions() & io::out_of_range_bit ) + boost::throw_exception(io::out_of_range()); // arg not in range. + else return self; + } + if(self.bound_.size()==0) + self.bound_.assign(self.num_args_,false); + else + BOOST_ASSERT( self.num_args_ == static_cast(self.bound_.size()) ); + int o_cur_arg = self.cur_arg_; + self.cur_arg_ = argN-1; // arrays begin at 0 + + self.bound_[self.cur_arg_]=false; // if already set, we unset and re-sets.. + self.operator%(val); // put val at the right place, because cur_arg is set + + + // Now re-position cur_arg before leaving : + self.cur_arg_ = o_cur_arg; + self.bound_[argN-1]=true; + if(self.cur_arg_ == argN-1 ) + // hum, now this arg is bound, so move to next free arg + { + while(self.cur_arg_ < self.num_args_ && self.bound_[self.cur_arg_]) ++self.cur_arg_; + } + // In any case, we either have all args, or are on a non-binded arg : + BOOST_ASSERT( self.cur_arg_ >= self.num_args_ || ! self.bound_[self.cur_arg_]); + return self; +} + +template +basic_format& modify_item_body( basic_format& self, + int itemN, + const T& manipulator) + // applies a manipulator to the format_item describing a given directive. + // this is a permanent change, clear or clear_binds won't cancel that. +{ + if(itemN<1 || itemN >= static_cast(self.items_.size() )) + { + if( self.exceptions() & io::out_of_range_bit ) + boost::throw_exception(io::out_of_range()); // item not in range. + else return self; + } + self.items_[itemN-1].ref_state_.apply_manip( manipulator ); + self.items_[itemN-1].state_ = self.items_[itemN-1].ref_state_; + return self; +} + +} // namespace detail + +} // namespace io + +} // namespace boost + + + +#endif // BOOST_FORMAT_IMPLEMENTATION_HPP diff --git a/src/boost/format/free_funcs.cc b/src/boost/format/free_funcs.cc new file mode 100644 index 000000000000..b2ac01774bdf --- /dev/null +++ b/src/boost/format/free_funcs.cc @@ -0,0 +1,71 @@ +// -*- C++ -*- +// Boost general library 'format' --------------------------- +// See http://www.boost.org for updates, documentation, and revision history. + +// (C) Samuel Krempp 2001 +// krempp@crans.ens-cachan.fr +// Permission to copy, use, modify, sell and +// distribute this software is granted provided this copyright notice appears +// in all copies. This software is provided "as is" without express or implied +// warranty, and with no claim as to its suitability for any purpose. + +// ideas taken from Rüdiger Loos's format class +// and Karl Nelson's ofstream (also took its parsing code as basis for printf parsing) + +// ------------------------------------------------------------------------------ +// free_funcs.hpp : implementation of the free functions declared in namespace format +// ------------------------------------------------------------------------------ + +#ifndef BOOST_FORMAT_FUNCS_HPP +#define BOOST_FORMAT_FUNCS_HPP + +#include "boost/format.hpp" +//#include "boost/throw_exception.hpp" + +namespace boost { + +namespace io { + inline + std::string str(const basic_format& f) + // adds up all pieces of strings and converted items, and return the formatted string + { + return f.str(); + } +} // - namespace io + +BOOST_IO_STD ostream& +operator<<( BOOST_IO_STD ostream& os, + const boost::basic_format& f) + // effect: "return os << str(f);" but we can try to do it faster +{ + typedef boost::basic_format format_t; + if(f.items_.size()==0) + os << f.prefix_; + else { + if(f.cur_arg_ < f.num_args_) + if( f.exceptions() & io::too_few_args_bit ) + boost::throw_exception(io::too_few_args()); // not enough variables have been supplied ! + if(f.style_ & format_t::special_needs) + os << f.str(); + else { + // else we dont have to count chars output, so we dump directly to os : + os << f.prefix_; + for(unsigned long i=0; i +inline +BOOST_IO_STD ostream& +operator << ( BOOST_IO_STD ostream& os, + const group0& ) +{ + return os; +} + +template +struct group1 +{ + T1 a1_; + group1(T1 a1) + : a1_(a1) + {} +}; + +template +inline +BOOST_IO_STD ostream& +operator << (BOOST_IO_STD ostream& os, + const group1& x) +{ + os << x.a1_; + return os; +} + + + + +template +struct group2 +{ + T1 a1_; + T2 a2_; + group2(T1 a1,T2 a2) + : a1_(a1),a2_(a2) + {} +}; + +template +inline +BOOST_IO_STD ostream& +operator << (BOOST_IO_STD ostream& os, + const group2& x) +{ + os << x.a1_<< x.a2_; + return os; +} + +template +struct group3 +{ + T1 a1_; + T2 a2_; + T3 a3_; + group3(T1 a1,T2 a2,T3 a3) + : a1_(a1),a2_(a2),a3_(a3) + {} +}; + +template +inline +BOOST_IO_STD ostream& +operator << (BOOST_IO_STD ostream& os, + const group3& x) +{ + os << x.a1_<< x.a2_<< x.a3_; + return os; +} + +template +struct group4 +{ + T1 a1_; + T2 a2_; + T3 a3_; + T4 a4_; + group4(T1 a1,T2 a2,T3 a3,T4 a4) + : a1_(a1),a2_(a2),a3_(a3),a4_(a4) + {} +}; + +template +inline +BOOST_IO_STD ostream& +operator << (BOOST_IO_STD ostream& os, + const group4& x) +{ + os << x.a1_<< x.a2_<< x.a3_<< x.a4_; + return os; +} + +template +struct group5 +{ + T1 a1_; + T2 a2_; + T3 a3_; + T4 a4_; + T5 a5_; + group5(T1 a1,T2 a2,T3 a3,T4 a4,T5 a5) + : a1_(a1),a2_(a2),a3_(a3),a4_(a4),a5_(a5) + {} +}; + +template +inline +BOOST_IO_STD ostream& +operator << (BOOST_IO_STD ostream& os, + const group5& x) +{ + os << x.a1_<< x.a2_<< x.a3_<< x.a4_<< x.a5_; + return os; +} + +template +struct group6 +{ + T1 a1_; + T2 a2_; + T3 a3_; + T4 a4_; + T5 a5_; + T6 a6_; + group6(T1 a1,T2 a2,T3 a3,T4 a4,T5 a5,T6 a6) + : a1_(a1),a2_(a2),a3_(a3),a4_(a4),a5_(a5),a6_(a6) + {} +}; + +template +inline +BOOST_IO_STD ostream& +operator << (BOOST_IO_STD ostream& os, + const group6& x) +{ + os << x.a1_<< x.a2_<< x.a3_<< x.a4_<< x.a5_<< x.a6_; + return os; +} + +template +struct group7 +{ + T1 a1_; + T2 a2_; + T3 a3_; + T4 a4_; + T5 a5_; + T6 a6_; + T7 a7_; + group7(T1 a1,T2 a2,T3 a3,T4 a4,T5 a5,T6 a6,T7 a7) + : a1_(a1),a2_(a2),a3_(a3),a4_(a4),a5_(a5),a6_(a6),a7_(a7) + {} +}; + +template +inline +BOOST_IO_STD ostream& +operator << (BOOST_IO_STD ostream& os, + const group7& x) +{ + os << x.a1_<< x.a2_<< x.a3_<< x.a4_<< x.a5_<< x.a6_<< x.a7_; + return os; +} + +template +struct group8 +{ + T1 a1_; + T2 a2_; + T3 a3_; + T4 a4_; + T5 a5_; + T6 a6_; + T7 a7_; + T8 a8_; + group8(T1 a1,T2 a2,T3 a3,T4 a4,T5 a5,T6 a6,T7 a7,T8 a8) + : a1_(a1),a2_(a2),a3_(a3),a4_(a4),a5_(a5),a6_(a6),a7_(a7),a8_(a8) + {} +}; + +template +inline +BOOST_IO_STD ostream& +operator << (BOOST_IO_STD ostream& os, + const group8& x) +{ + os << x.a1_<< x.a2_<< x.a3_<< x.a4_<< x.a5_<< x.a6_<< x.a7_<< x.a8_; + return os; +} + +template +struct group9 +{ + T1 a1_; + T2 a2_; + T3 a3_; + T4 a4_; + T5 a5_; + T6 a6_; + T7 a7_; + T8 a8_; + T9 a9_; + group9(T1 a1,T2 a2,T3 a3,T4 a4,T5 a5,T6 a6,T7 a7,T8 a8,T9 a9) + : a1_(a1),a2_(a2),a3_(a3),a4_(a4),a5_(a5),a6_(a6),a7_(a7),a8_(a8),a9_(a9) + {} +}; + +template +inline +BOOST_IO_STD ostream& +operator << (BOOST_IO_STD ostream& os, + const group9& x) +{ + os << x.a1_<< x.a2_<< x.a3_<< x.a4_<< x.a5_<< x.a6_<< x.a7_<< x.a8_<< x.a9_; + return os; +} + +template +struct group10 +{ + T1 a1_; + T2 a2_; + T3 a3_; + T4 a4_; + T5 a5_; + T6 a6_; + T7 a7_; + T8 a8_; + T9 a9_; + T10 a10_; + group10(T1 a1,T2 a2,T3 a3,T4 a4,T5 a5,T6 a6,T7 a7,T8 a8,T9 a9,T10 a10) + : a1_(a1),a2_(a2),a3_(a3),a4_(a4),a5_(a5),a6_(a6),a7_(a7),a8_(a8),a9_(a9),a10_(a10) + {} +}; + +template +inline +BOOST_IO_STD ostream& +operator << (BOOST_IO_STD ostream& os, + const group10& x) +{ + os << x.a1_<< x.a2_<< x.a3_<< x.a4_<< x.a5_<< x.a6_<< x.a7_<< x.a8_<< x.a9_<< x.a10_; + return os; +} + + + + +template +inline +group1 +group_head( group2 const& x) +{ + return group1 (x.a1_); +} + +template +inline +group1 +group_last( group2 const& x) +{ + return group1 (x.a2_); +} + + + +template +inline +group2 +group_head( group3 const& x) +{ + return group2 (x.a1_,x.a2_); +} + +template +inline +group1 +group_last( group3 const& x) +{ + return group1 (x.a3_); +} + + + +template +inline +group3 +group_head( group4 const& x) +{ + return group3 (x.a1_,x.a2_,x.a3_); +} + +template +inline +group1 +group_last( group4 const& x) +{ + return group1 (x.a4_); +} + + + +template +inline +group4 +group_head( group5 const& x) +{ + return group4 (x.a1_,x.a2_,x.a3_,x.a4_); +} + +template +inline +group1 +group_last( group5 const& x) +{ + return group1 (x.a5_); +} + + + +template +inline +group5 +group_head( group6 const& x) +{ + return group5 (x.a1_,x.a2_,x.a3_,x.a4_,x.a5_); +} + +template +inline +group1 +group_last( group6 const& x) +{ + return group1 (x.a6_); +} + + + +template +inline +group6 +group_head( group7 const& x) +{ + return group6 (x.a1_,x.a2_,x.a3_,x.a4_,x.a5_,x.a6_); +} + +template +inline +group1 +group_last( group7 const& x) +{ + return group1 (x.a7_); +} + + + +template +inline +group7 +group_head( group8 const& x) +{ + return group7 (x.a1_,x.a2_,x.a3_,x.a4_,x.a5_,x.a6_,x.a7_); +} + +template +inline +group1 +group_last( group8 const& x) +{ + return group1 (x.a8_); +} + + + +template +inline +group8 +group_head( group9 const& x) +{ + return group8 (x.a1_,x.a2_,x.a3_,x.a4_,x.a5_,x.a6_,x.a7_,x.a8_); +} + +template +inline +group1 +group_last( group9 const& x) +{ + return group1 (x.a9_); +} + + + +template +inline +group9 +group_head( group10 const& x) +{ + return group9 (x.a1_,x.a2_,x.a3_,x.a4_,x.a5_,x.a6_,x.a7_,x.a8_,x.a9_); +} + +template +inline +group1 +group_last( group10 const& x) +{ + return group1 (x.a10_); +} + + + + + +} // namespace detail + + + +// helper functions + + +inline detail::group1< detail::group0 > +group() { return detail::group1< detail::group0 > ( detail::group0() ); } + +template +inline +detail::group1< detail::group2 > + group(T1 a1, Var const& var) +{ + return detail::group1< detail::group2 > + ( detail::group2 + (a1, var) + ); +} + +template +inline +detail::group1< detail::group3 > + group(T1 a1,T2 a2, Var const& var) +{ + return detail::group1< detail::group3 > + ( detail::group3 + (a1,a2, var) + ); +} + +template +inline +detail::group1< detail::group4 > + group(T1 a1,T2 a2,T3 a3, Var const& var) +{ + return detail::group1< detail::group4 > + ( detail::group4 + (a1,a2,a3, var) + ); +} + +template +inline +detail::group1< detail::group5 > + group(T1 a1,T2 a2,T3 a3,T4 a4, Var const& var) +{ + return detail::group1< detail::group5 > + ( detail::group5 + (a1,a2,a3,a4, var) + ); +} + +template +inline +detail::group1< detail::group6 > + group(T1 a1,T2 a2,T3 a3,T4 a4,T5 a5, Var const& var) +{ + return detail::group1< detail::group6 > + ( detail::group6 + (a1,a2,a3,a4,a5, var) + ); +} + +template +inline +detail::group1< detail::group7 > + group(T1 a1,T2 a2,T3 a3,T4 a4,T5 a5,T6 a6, Var const& var) +{ + return detail::group1< detail::group7 > + ( detail::group7 + (a1,a2,a3,a4,a5,a6, var) + ); +} + +template +inline +detail::group1< detail::group8 > + group(T1 a1,T2 a2,T3 a3,T4 a4,T5 a5,T6 a6,T7 a7, Var const& var) +{ + return detail::group1< detail::group8 > + ( detail::group8 + (a1,a2,a3,a4,a5,a6,a7, var) + ); +} + +template +inline +detail::group1< detail::group9 > + group(T1 a1,T2 a2,T3 a3,T4 a4,T5 a5,T6 a6,T7 a7,T8 a8, Var const& var) +{ + return detail::group1< detail::group9 > + ( detail::group9 + (a1,a2,a3,a4,a5,a6,a7,a8, var) + ); +} + +template +inline +detail::group1< detail::group10 > + group(T1 a1,T2 a2,T3 a3,T4 a4,T5 a5,T6 a6,T7 a7,T8 a8,T9 a9, Var const& var) +{ + return detail::group1< detail::group10 > + ( detail::group10 + (a1,a2,a3,a4,a5,a6,a7,a8,a9, var) + ); +} + + +#ifndef BOOST_NO_OVERLOAD_FOR_NON_CONST + +template +inline +detail::group1< detail::group2 > + group(T1 a1, Var& var) +{ + return detail::group1< detail::group2 > + ( detail::group2 + (a1, var) + ); +} + +template +inline +detail::group1< detail::group3 > + group(T1 a1,T2 a2, Var& var) +{ + return detail::group1< detail::group3 > + ( detail::group3 + (a1,a2, var) + ); +} + +template +inline +detail::group1< detail::group4 > + group(T1 a1,T2 a2,T3 a3, Var& var) +{ + return detail::group1< detail::group4 > + ( detail::group4 + (a1,a2,a3, var) + ); +} + +template +inline +detail::group1< detail::group5 > + group(T1 a1,T2 a2,T3 a3,T4 a4, Var& var) +{ + return detail::group1< detail::group5 > + ( detail::group5 + (a1,a2,a3,a4, var) + ); +} + +template +inline +detail::group1< detail::group6 > + group(T1 a1,T2 a2,T3 a3,T4 a4,T5 a5, Var& var) +{ + return detail::group1< detail::group6 > + ( detail::group6 + (a1,a2,a3,a4,a5, var) + ); +} + +template +inline +detail::group1< detail::group7 > + group(T1 a1,T2 a2,T3 a3,T4 a4,T5 a5,T6 a6, Var& var) +{ + return detail::group1< detail::group7 > + ( detail::group7 + (a1,a2,a3,a4,a5,a6, var) + ); +} + +template +inline +detail::group1< detail::group8 > + group(T1 a1,T2 a2,T3 a3,T4 a4,T5 a5,T6 a6,T7 a7, Var& var) +{ + return detail::group1< detail::group8 > + ( detail::group8 + (a1,a2,a3,a4,a5,a6,a7, var) + ); +} + +template +inline +detail::group1< detail::group9 > + group(T1 a1,T2 a2,T3 a3,T4 a4,T5 a5,T6 a6,T7 a7,T8 a8, Var& var) +{ + return detail::group1< detail::group9 > + ( detail::group9 + (a1,a2,a3,a4,a5,a6,a7,a8, var) + ); +} + +template +inline +detail::group1< detail::group10 > + group(T1 a1,T2 a2,T3 a3,T4 a4,T5 a5,T6 a6,T7 a7,T8 a8,T9 a9, Var& var) +{ + return detail::group1< detail::group10 > + ( detail::group10 + (a1,a2,a3,a4,a5,a6,a7,a8,a9, var) + ); +} + + +#endif //end- #ifndef BOOST_NO_OVERLOAD_FOR_NON_CONST + + +} // namespace io + +} // namespace boost + + +#endif // BOOST_FORMAT_GROUP_HPP diff --git a/src/boost/format/internals.hpp b/src/boost/format/internals.hpp new file mode 100644 index 000000000000..d25eb4c864c4 --- /dev/null +++ b/src/boost/format/internals.hpp @@ -0,0 +1,167 @@ +// -*- C++ -*- +// Boost general library 'format' --------------------------- +// See http://www.boost.org for updates, documentation, and revision history. + +// (C) Samuel Krempp 2001 +// krempp@crans.ens-cachan.fr +// Permission to copy, use, modify, sell and +// distribute this software is granted provided this copyright notice appears +// in all copies. This software is provided "as is" without express or implied +// warranty, and with no claim as to its suitability for any purpose. + +// ideas taken from Rüdiger Loos's format class +// and Karl Nelson's ofstream + +// ---------------------------------------------------------------------------- +// internals.hpp : internal structs. included by format.hpp +// stream_format_state, and format_item +// ---------------------------------------------------------------------------- + + +#ifndef BOOST_FORMAT_INTERNALS_HPP +#define BOOST_FORMAT_INTERNALS_HPP + + +#include +#include + +namespace boost { +namespace io { +namespace detail { + + +// -------------- +// set of params that define the format state of a stream + +struct stream_format_state +{ + typedef std::ios basic_ios; + + std::streamsize width_; + std::streamsize precision_; + char fill_; + std::ios::fmtflags flags_; + + stream_format_state() : width_(-1), precision_(-1), fill_(0), flags_(std::ios::dec) {} + stream_format_state(basic_ios& os) {set_by_stream(os); } + + void apply_on(basic_ios & os) const; //- applies format_state to the stream + template void apply_manip(T manipulator) //- modifies state by applying manipulator. + { apply_manip_body( *this, manipulator) ; } + void reset(); //- sets to default state. + void set_by_stream(const basic_ios& os); //- sets to os's state. +}; + + + +// -------------- +// format_item : stores all parameters that can be defined by directives in the format-string + +struct format_item +{ + enum pad_values { zeropad = 1, spacepad =2, centered=4, tabulation = 8 }; + + enum arg_values { argN_no_posit = -1, // non-positional directive. argN will be set later. + argN_tabulation = -2, // tabulation directive. (no argument read) + argN_ignored = -3 // ignored directive. (no argument read) + }; + typedef BOOST_IO_STD ios basic_ios; + typedef detail::stream_format_state stream_format_state; + typedef std::string string_t; + typedef BOOST_IO_STD ostringstream internal_stream_t; + + + int argN_; //- argument number (starts at 0, eg : %1 => argN=0) + // negative values are used for items that don't process + // an argument + string_t res_; //- result of the formatting of this item + string_t appendix_; //- piece of string between this item and the next + + stream_format_state ref_state_;// set by parsing the format_string, is only affected by modify_item + stream_format_state state_; // always same as ref_state, _unless_ modified by manipulators 'group(..)' + + // non-stream format-state parameters + signed int truncate_; //- is >=0 for directives like %.5s (take 5 chars from the string) + unsigned int pad_scheme_; //- several possible padding schemes can mix. see pad_values + + format_item() : argN_(argN_no_posit), truncate_(-1), pad_scheme_(0) {} + + void compute_states(); // sets states according to truncate and pad_scheme. +}; + + + +// ----------------------------------------------------------- +// Definitions +// ----------------------------------------------------------- + +// --- stream_format_state:: ------------------------------------------- +inline +void stream_format_state::apply_on(basic_ios & os) const + // set the state of this stream according to our params +{ + if(width_ != -1) + os.width(width_); + if(precision_ != -1) + os.precision(precision_); + if(fill_ != 0) + os.fill(fill_); + os.flags(flags_); +} + +inline +void stream_format_state::set_by_stream(const basic_ios& os) + // set our params according to the state of this stream +{ + flags_ = os.flags(); + width_ = os.width(); + precision_ = os.precision(); + fill_ = os.fill(); +} + +template inline +void apply_manip_body( stream_format_state& self, + T manipulator) + // modify our params according to the manipulator +{ + BOOST_IO_STD stringstream ss; + self.apply_on( ss ); + ss << manipulator; + self.set_by_stream( ss ); +} + +inline +void stream_format_state::reset() + // set our params to standard's default state +{ + width_=-1; precision_=-1; fill_=0; + flags_ = std::ios::dec; +} + + +// --- format_items:: ------------------------------------------- +inline +void format_item::compute_states() + // reflect pad_scheme_ on state_ and ref_state_ + // because some pad_schemes has complex consequences on several state params. +{ + if(pad_scheme_ & zeropad) + { + if(ref_state_.flags_ & std::ios::left) + { + pad_scheme_ = pad_scheme_ & (~zeropad); // ignore zeropad in left alignment + } + else + { + ref_state_.fill_='0'; + ref_state_.flags_ |= std::ios::internal; + } + } + state_ = ref_state_; +} + + +} } } // namespaces boost :: io :: detail + + +#endif // BOOST_FORMAT_INTERNALS_HPP diff --git a/src/boost/format/internals_fwd.hpp b/src/boost/format/internals_fwd.hpp new file mode 100644 index 000000000000..a8ebf7c3abc1 --- /dev/null +++ b/src/boost/format/internals_fwd.hpp @@ -0,0 +1,65 @@ +// -*- C++ -*- +// Boost general library 'format' --------------------------- +// See http://www.boost.org for updates, documentation, and revision history. + +// (C) Samuel Krempp 2001 +// krempp@crans.ens-cachan.fr +// Permission to copy, use, modify, sell and +// distribute this software is granted provided this copyright notice appears +// in all copies. This software is provided "as is" without express or implied +// warranty, and with no claim as to its suitability for any purpose. + +// ideas taken from Rüdiger Loos's format class +// and Karl Nelson's ofstream (also took its parsing code as basis for printf parsing) + +// ------------------------------------------------------------------------------ +// internals_fwd.hpp : forward declarations, for internal headers +// ------------------------------------------------------------------------------ + +#ifndef BOOST_FORMAT_INTERNAL_FWD_HPP +#define BOOST_FORMAT_INTERNAL_FWD_HPP + +#include "boost/format/format_fwd.hpp" + + +namespace boost { +namespace io { + +namespace detail { + struct stream_format_state; + struct format_item; +} + + +namespace detail { + + // these functions were intended as methods, + // but MSVC have problems with template member functions : + + // defined in format_implementation.hpp : + template + basic_format& modify_item_body( basic_format& self, + int itemN, const T& manipulator); + + template + basic_format& bind_arg_body( basic_format& self, + int argN, const T& val); + + template + void apply_manip_body( stream_format_state& self, + T manipulator); + + // argument feeding (defined in feed_args.hpp ) : + template + void distribute(basic_format& self, T x); + + template + basic_format& feed(basic_format& self, T x); + +} // namespace detail + +} // namespace io +} // namespace boost + + +#endif // BOOST_FORMAT_INTERNAL_FWD_HPP diff --git a/src/boost/format/macros_default.hpp b/src/boost/format/macros_default.hpp new file mode 100644 index 000000000000..4fd84a163fb3 --- /dev/null +++ b/src/boost/format/macros_default.hpp @@ -0,0 +1,48 @@ +// -*- C++ -*- +// Boost general library 'format' --------------------------- +// See http://www.boost.org for updates, documentation, and revision history. + +// (C) Samuel Krempp 2001 +// krempp@crans.ens-cachan.fr +// Permission to copy, use, modify, sell and +// distribute this software is granted provided this copyright notice appears +// in all copies. This software is provided "as is" without express or implied +// warranty, and with no claim as to its suitability for any purpose. + +// ideas taken from Rüdiger Loos's format class +// and Karl Nelson's ofstream (also took its parsing code as basis for printf parsing) + +// ------------------------------------------------------------------------------ +// macros_default.hpp : configuration for the format library +// provides default values for the stl workaround macros +// ------------------------------------------------------------------------------ + +#ifndef BOOST_FORMAT_MACROS_DEFAULT_HPP +#define BOOST_FORMAT_MACROS_DEFAULT_HPP + +// *** This should go to "boost/config/suffix.hpp". + +#ifndef BOOST_IO_STD +# define BOOST_IO_STD std:: +#endif + +// **** Workaround for io streams, stlport and msvc. +#ifdef BOOST_IO_NEEDS_USING_DECLARATION +namespace boost { + using std::char_traits; + using std::basic_ostream; + using std::basic_ostringstream; + namespace io { + using std::basic_ostream; + namespace detail { + using std::basic_ios; + using std::basic_ostream; + using std::basic_ostringstream; + } + } +} +#endif + +// ------------------------------------------------------------------------------ + +#endif // BOOST_FORMAT_MACROS_DEFAULT_HPP diff --git a/src/boost/format/parsing.cc b/src/boost/format/parsing.cc new file mode 100644 index 000000000000..1e12dea9bfc6 --- /dev/null +++ b/src/boost/format/parsing.cc @@ -0,0 +1,454 @@ +// -*- C++ -*- +// Boost general library 'format' --------------------------- +// See http://www.boost.org for updates, documentation, and revision history. + +// (C) Samuel Krempp 2001 +// krempp@crans.ens-cachan.fr +// Permission to copy, use, modify, sell and +// distribute this software is granted provided this copyright notice appears +// in all copies. This software is provided "as is" without express or implied +// warranty, and with no claim as to its suitability for any purpose. + +// ideas taken from Rudiger Loos's format class +// and Karl Nelson's ofstream (also took its parsing code as basis for printf parsing) + +// ------------------------------------------------------------------------------ +// parsing.hpp : implementation of the parsing member functions +// ( parse, parse_printf_directive) +// ------------------------------------------------------------------------------ + + +#ifndef BOOST_FORMAT_PARSING_HPP +#define BOOST_FORMAT_PARSING_HPP + + +#include +//#include +//#include + + +namespace boost { +namespace io { +namespace detail { + + template inline + bool wrap_isdigit(char c, Stream &os) + { +#ifndef BOOST_NO_LOCALE_ISIDIGIT + return std::isdigit(c, os.rdbuf()->getloc() ); +# else + using namespace std; + return isdigit(c); +#endif + } //end- wrap_isdigit(..) + + template inline + Res str2int(const std::string& s, + std::string::size_type start, + BOOST_IO_STD ios &os, + const Res = Res(0) ) + // Input : char string, with starting index + // a basic_ios& merely to call its widen/narrow member function in the desired locale. + // Effects : reads s[start:] and converts digits into an integral n, of type Res + // Returns : n + { + Res n = 0; + while(start= buf.size() ) return; + if(buf[ *pos_p]=='*') { + ++ (*pos_p); + while (*pos_p < buf.size() && wrap_isdigit(buf[*pos_p],os)) ++(*pos_p); + if(buf[*pos_p]=='$') ++(*pos_p); + } + } + + + inline void maybe_throw_exception( unsigned char exceptions) + // auxiliary func called by parse_printf_directive + // for centralising error handling + // it either throws if user sets the corresponding flag, or does nothing. + { + if(exceptions & io::bad_format_string_bit) + boost::throw_exception(io::bad_format_string()); + } + + + + bool parse_printf_directive(const std::string & buf, + std::string::size_type * pos_p, + detail::format_item * fpar, + BOOST_IO_STD ios &os, + unsigned char exceptions) + // Input : a 'printf-directive' in the format-string, starting at buf[ *pos_p ] + // a basic_ios& merely to call its widen/narrow member function in the desired locale. + // a bitset'excpetions' telling whether to throw exceptions on errors. + // Returns : true if parse somehow succeeded (possibly ignoring errors if exceptions disabled) + // false if it failed so bad that the directive should be printed verbatim + // Effects : - *pos_p is incremented so that buf[*pos_p] is the first char after the directive + // - *fpar is set with the parameters read in the directive + { + typedef format_item format_item_t; + BOOST_ASSERT( pos_p != 0); + std::string::size_type &i1 = *pos_p, + i0; + fpar->argN_ = format_item_t::argN_no_posit; // if no positional-directive + + bool in_brackets=false; + if(buf[i1]=='|') + { + in_brackets=true; + if( ++i1 >= buf.size() ) { + maybe_throw_exception(exceptions); + return false; + } + } + + // the flag '0' would be picked as a digit for argument order, but here it's a flag : + if(buf[i1]=='0') + goto parse_flags; + + // handle argument order (%2$d) or possibly width specification: %2d + i0 = i1; // save position before digits + while (i1 < buf.size() && wrap_isdigit(buf[i1], os)) + ++i1; + if (i1!=i0) + { + if( i1 >= buf.size() ) { + maybe_throw_exception(exceptions); + return false; + } + int n=str2int(buf,i0, os, int(0) ); + + // %N% case : this is already the end of the directive + if( buf[i1] == '%' ) + { + fpar->argN_ = n-1; + ++i1; + if( in_brackets) + maybe_throw_exception(exceptions); + // but don't return. maybe "%" was used in lieu of '$', so we go on. + else return true; + } + + if ( buf[i1]=='$' ) + { + fpar->argN_ = n-1; + ++i1; + } + else + { + // non-positionnal directive + fpar->ref_state_.width_ = n; + fpar->argN_ = format_item_t::argN_no_posit; + goto parse_precision; + } + } + + parse_flags: + // handle flags + while ( i1 ref_state_.flags_ |= std::ios::left; + break; + case '=': + fpar->pad_scheme_ |= format_item_t::centered; + break; + case ' ': + fpar->pad_scheme_ |= format_item_t::spacepad; + break; + case '+': + fpar->ref_state_.flags_ |= std::ios::showpos; + break; + case '0': + fpar->pad_scheme_ |= format_item_t::zeropad; + // need to know alignment before really setting flags, + // so just add 'zeropad' flag for now, it will be processed later. + break; + case '#': + fpar->ref_state_.flags_ |= std::ios::showpoint | std::ios::showbase; + break; + default: + goto parse_width; + } + ++i1; + } // loop on flag. + if( i1>=buf.size()) { + maybe_throw_exception(exceptions); + return true; + } + + parse_width: + // handle width spec + skip_asterisk(buf, &i1, os); // skips 'asterisk fields' : *, or *N$ + i0 = i1; // save position before digits + while (i1ref_state_.width_ = str2int( buf,i0, os, std::streamsize(0) ); } + + parse_precision: + if( i1>=buf.size()) { + maybe_throw_exception(exceptions); + return true; + } + // handle precision spec + if (buf[i1]=='.') + { + ++i1; + skip_asterisk(buf, &i1, os); + i0 = i1; // save position before digits + while (i1ref_state_.precision_ = 0; + else + fpar->ref_state_.precision_ = str2int(buf,i0, os, std::streamsize(0) ); + } + + // handle formatting-type flags : + while( i1=buf.size()) { + maybe_throw_exception(exceptions); + return true; + } + + if( in_brackets && buf[i1]=='|' ) + { + ++i1; + return true; + } + switch (buf[i1]) + { + case 'X': + fpar->ref_state_.flags_ |= std::ios::uppercase; + case 'p': // pointer => set hex. + case 'x': + fpar->ref_state_.flags_ &= ~std::ios::basefield; + fpar->ref_state_.flags_ |= std::ios::hex; + break; + + case 'o': + fpar->ref_state_.flags_ &= ~std::ios::basefield; + fpar->ref_state_.flags_ |= std::ios::oct; + break; + + case 'E': + fpar->ref_state_.flags_ |= std::ios::uppercase; + case 'e': + fpar->ref_state_.flags_ &= ~std::ios::floatfield; + fpar->ref_state_.flags_ |= std::ios::scientific; + + fpar->ref_state_.flags_ &= ~std::ios::basefield; + fpar->ref_state_.flags_ |= std::ios::dec; + break; + + case 'f': + fpar->ref_state_.flags_ &= ~std::ios::floatfield; + fpar->ref_state_.flags_ |= std::ios::fixed; + case 'u': + case 'd': + case 'i': + fpar->ref_state_.flags_ &= ~std::ios::basefield; + fpar->ref_state_.flags_ |= std::ios::dec; + break; + + case 'T': + ++i1; + if( i1 >= buf.size()) + maybe_throw_exception(exceptions); + else + fpar->ref_state_.fill_ = buf[i1]; + fpar->pad_scheme_ |= format_item_t::tabulation; + fpar->argN_ = format_item_t::argN_tabulation; + break; + case 't': + fpar->ref_state_.fill_ = ' '; + fpar->pad_scheme_ |= format_item_t::tabulation; + fpar->argN_ = format_item_t::argN_tabulation; + break; + + case 'G': + fpar->ref_state_.flags_ |= std::ios::uppercase; + break; + case 'g': // 'g' conversion is default for floats. + fpar->ref_state_.flags_ &= ~std::ios::basefield; + fpar->ref_state_.flags_ |= std::ios::dec; + + // CLEAR all floatield flags, so stream will CHOOSE + fpar->ref_state_.flags_ &= ~std::ios::floatfield; + break; + + case 'C': + case 'c': + fpar->truncate_ = 1; + break; + case 'S': + case 's': + fpar->truncate_ = fpar->ref_state_.precision_; + fpar->ref_state_.precision_ = -1; + break; + case 'n' : + fpar->argN_ = format_item_t::argN_ignored; + break; + default: + maybe_throw_exception(exceptions); + } + ++i1; + + if( in_brackets ) + { + if( i1= buf.size() ) { + if(exceptions() & io::bad_format_string_bit) + boost::throw_exception(io::bad_format_string()); // must not end in "bla bla %" + else break; // stop there, ignore last '%' + } + if(buf[i1+1] == buf[i1] ) { i1+=2; continue; } // escaped "%%" / "##" + ++i1; + + // in case of %N% directives, dont count it double (wastes allocations..) : + while(i1 < buf.size() && io::detail::wrap_isdigit(buf[i1],oss_)) ++i1; + if( i1 < buf.size() && buf[i1] == arg_mark ) ++ i1; + + ++num_items; + } + items_.assign( num_items, format_item_t() ); + + // B: Now the real parsing of the format string : + num_items=0; + i1 = 0; + string_t::size_type i0 = i1; + bool special_things=false; + int cur_it=0; + while( (i1=buf.find(arg_mark,i1)) != string::npos ) + { + string_t & piece = (cur_it==0) ? prefix_ : items_[cur_it-1].appendix_; + + if( buf[i1+1] == buf[i1] ) // escaped mark, '%%' + { + piece += buf.substr(i0, i1-i0) + buf[i1]; + i1+=2; i0=i1; + continue; + } + BOOST_ASSERT( static_cast(cur_it) < items_.size() || cur_it==0); + + if(i1!=i0) piece += buf.substr(i0, i1-i0); + ++i1; + + bool parse_ok; + parse_ok = io::detail::parse_printf_directive(buf, &i1, &items_[cur_it], oss_, exceptions()); + if( ! parse_ok ) continue; // the directive will be printed verbatim + + i0=i1; + items_[cur_it].compute_states(); // process complex options, like zeropad, into stream params. + + int argN=items_[cur_it].argN_; + if(argN == format_item_t::argN_ignored) + continue; + if(argN ==format_item_t::argN_no_posit) + ordered_args=false; + else if(argN == format_item_t::argN_tabulation) special_things=true; + else if(argN > max_argN) max_argN = argN; + ++num_items; + ++cur_it; + } // loop on %'s + BOOST_ASSERT(cur_it == num_items); + + // store the final piece of string + string_t & piece = (cur_it==0) ? prefix_ : items_[cur_it-1].appendix_; + piece += buf.substr(i0); + + if( !ordered_args) + { + if(max_argN >= 0 ) // dont mix positional with non-positionnal directives + { + if(exceptions() & io::bad_format_string_bit) + boost::throw_exception(io::bad_format_string()); + // else do nothing. => positionnal arguments are processed as non-positionnal + } + // set things like it would have been with positional directives : + int non_ordered_items = 0; + for(int i=0; i< num_items; ++i) + if(items_[i].argN_ == format_item_t::argN_no_posit) + { + items_[i].argN_ = non_ordered_items; + ++non_ordered_items; + } + max_argN = non_ordered_items-1; + } + + // C: set some member data : + items_.resize(num_items); + + if(special_things) style_ |= special_needs; + num_args_ = max_argN + 1; + if(ordered_args) style_ |= ordered; + else style_ &= ~ordered; +} + +} // namespace boost + + +#endif // BOOST_FORMAT_PARSING_HPP diff --git a/src/db.cc b/src/db.cc deleted file mode 100644 index 2f53ca3b5231..000000000000 --- a/src/db.cc +++ /dev/null @@ -1,425 +0,0 @@ -#include -#include -#include - -#include - -#include "db.hh" -#include "util.hh" -#include "pathlocks.hh" - - -/* Wrapper class to ensure proper destruction. */ -class DestroyDbc -{ - Dbc * dbc; -public: - DestroyDbc(Dbc * _dbc) : dbc(_dbc) { } - ~DestroyDbc() { dbc->close(); /* close() frees dbc */ } -}; - - -static void rethrow(DbException & e) -{ - throw Error(e.what()); -} - - -Transaction::Transaction() - : txn(0) -{ -} - - -Transaction::Transaction(Database & db) -{ - db.requireEnv(); - try { - db.env->txn_begin(0, &txn, 0); - } catch (DbException e) { rethrow(e); } -} - - -Transaction::~Transaction() -{ - if (txn) abort(); -} - - -void Transaction::commit() -{ - if (!txn) throw Error("commit called on null transaction"); - debug(format("committing transaction %1%") % (void *) txn); - DbTxn * txn2 = txn; - txn = 0; - try { - txn2->commit(0); - } catch (DbException e) { rethrow(e); } -} - - -void Transaction::abort() -{ - if (!txn) throw Error("abort called on null transaction"); - debug(format("aborting transaction %1%") % (void *) txn); - DbTxn * txn2 = txn; - txn = 0; - try { - txn2->abort(); - } catch (DbException e) { rethrow(e); } -} - - -void Transaction::moveTo(Transaction & t) -{ - if (t.txn) throw Error("target txn already exists"); - t.txn = txn; - txn = 0; -} - - -void Database::requireEnv() -{ - if (!env) throw Error("database environment not open"); -} - - -Db * Database::getDb(TableId table) -{ - map::iterator i = tables.find(table); - if (i == tables.end()) - throw Error("unknown table id"); - return i->second; -} - - -Database::Database() - : env(0) - , nextId(1) -{ -} - - -Database::~Database() -{ - close(); -} - - -int getAccessorCount(int fd) -{ - if (lseek(fd, 0, SEEK_SET) == -1) - throw SysError("seeking accessor count"); - char buf[128]; - int len; - if ((len = read(fd, buf, sizeof(buf) - 1)) == -1) - throw SysError("reading accessor count"); - buf[len] = 0; - int count; - if (sscanf(buf, "%d", &count) != 1) { - debug(format("accessor count is invalid: `%1%'") % buf); - return -1; - } - return count; -} - - -void setAccessorCount(int fd, int n) -{ - if (lseek(fd, 0, SEEK_SET) == -1) - throw SysError("seeking accessor count"); - string s = (format("%1%") % n).str(); - const char * s2 = s.c_str(); - if (write(fd, s2, strlen(s2)) != (ssize_t) strlen(s2) || - ftruncate(fd, strlen(s2)) != 0) - throw SysError("writing accessor count"); -} - - -void openEnv(DbEnv * env, const string & path, u_int32_t flags) -{ - env->open(path.c_str(), - DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN | - DB_CREATE | flags, - 0666); -} - - -void Database::open(const string & path) -{ - if (env) throw Error(format("environment already open")); - - try { - - debug(format("opening database environment")); - - - /* Create the database environment object. */ - env = new DbEnv(0); - - env->set_lg_bsize(32 * 1024); /* default */ - env->set_lg_max(256 * 1024); /* must be > 4 * lg_bsize */ - env->set_lk_detect(DB_LOCK_DEFAULT); - env->set_flags(DB_TXN_WRITE_NOSYNC, 1); - - - /* The following code provides automatic recovery of the - database environment. Recovery is necessary when a process - dies while it has the database open. To detect this, - processes atomically increment a counter when the open the - database, and decrement it when they close it. If we see - that counter is > 0 but no processes are accessing the - database---determined by attempting to obtain a write lock - on a lock file on which all accessors have a read lock---we - must run recovery. Note that this also ensures that we - only run recovery when there are no other accessors (which - could cause database corruption). */ - - /* !!! close fdAccessors / fdLock on exception */ - - /* Open the accessor count file. */ - string accessorsPath = path + "/accessor_count"; - fdAccessors = ::open(accessorsPath.c_str(), O_RDWR | O_CREAT, 0666); - if (fdAccessors == -1) - throw SysError(format("opening file `%1%'") % accessorsPath); - - /* Open the lock file. */ - string lockPath = path + "/access_lock"; - fdLock = ::open(lockPath.c_str(), O_RDWR | O_CREAT, 0666); - if (fdLock == -1) - throw SysError(format("opening lock file `%1%'") % lockPath); - - /* Try to acquire a write lock. */ - debug(format("attempting write lock on `%1%'") % lockPath); - if (lockFile(fdLock, ltWrite, false)) { /* don't wait */ - - debug(format("write lock granted")); - - /* We have a write lock, which means that there are no - other readers or writers. */ - - int n = getAccessorCount(fdAccessors); - setAccessorCount(fdAccessors, 1); - - if (n != 0) { - msg(lvlTalkative, format("accessor count is %1%, running recovery") % n); - - /* Open the environment after running recovery. */ - openEnv(env, path, DB_RECOVER); - } - - else - /* Open the environment normally. */ - openEnv(env, path, 0); - - /* Downgrade to a read lock. */ - debug(format("downgrading to read lock on `%1%'") % lockPath); - lockFile(fdLock, ltRead, true); - - } else { - /* There are other accessors. */ - debug(format("write lock refused")); - - /* Acquire a read lock. */ - debug(format("acquiring read lock on `%1%'") % lockPath); - lockFile(fdLock, ltRead, true); /* wait indefinitely */ - - /* Increment the accessor count. */ - lockFile(fdAccessors, ltWrite, true); - int n = getAccessorCount(fdAccessors) + 1; - setAccessorCount(fdAccessors, n); - debug(format("incremented accessor count to %1%") % n); - lockFile(fdAccessors, ltNone, true); - - /* Open the environment normally. */ - openEnv(env, path, 0); - } - - } catch (DbException e) { rethrow(e); } -} - - -void Database::close() -{ - if (!env) return; - - /* Close the database environment. */ - debug(format("closing database environment")); - - try { - - for (map::iterator i = tables.begin(); - i != tables.end(); i++) - { - debug(format("closing table %1%") % i->first); - Db * db = i->second; - db->close(DB_NOSYNC); - delete db; - } - -// env->txn_checkpoint(0, 0, 0); - env->close(0); - - } catch (DbException e) { rethrow(e); } - - delete env; - - /* Decrement the accessor count. */ - lockFile(fdAccessors, ltWrite, true); - int n = getAccessorCount(fdAccessors) - 1; - setAccessorCount(fdAccessors, n); - debug(format("decremented accessor count to %1%") % n); - lockFile(fdAccessors, ltNone, true); - - ::close(fdAccessors); - ::close(fdLock); -} - - -TableId Database::openTable(const string & tableName) -{ - requireEnv(); - TableId table = nextId++; - - try { - - Db * db = new Db(env, 0); - - try { - db->open(0, tableName.c_str(), 0, - DB_HASH, DB_CREATE | DB_AUTO_COMMIT, 0666); - } catch (...) { - delete db; - throw; - } - - tables[table] = db; - - } catch (DbException e) { rethrow(e); } - - return table; -} - - -bool Database::queryString(const Transaction & txn, TableId table, - const string & key, string & data) -{ - try { - Db * db = getDb(table); - - Dbt kt((void *) key.c_str(), key.length()); - Dbt dt; - - int err = db->get(txn.txn, &kt, &dt, 0); - if (err) return false; - - if (!dt.get_data()) - data = ""; - else - data = string((char *) dt.get_data(), dt.get_size()); - - } catch (DbException e) { rethrow(e); } - - return true; -} - - -bool Database::queryStrings(const Transaction & txn, TableId table, - const string & key, Strings & data) -{ - string d; - - if (!queryString(txn, table, key, d)) - return false; - - string::iterator it = d.begin(); - - while (it != d.end()) { - - if (it + 4 > d.end()) - throw Error(format("short db entry: `%1%'") % d); - - unsigned int len; - len = (unsigned char) *it++; - len |= ((unsigned char) *it++) << 8; - len |= ((unsigned char) *it++) << 16; - len |= ((unsigned char) *it++) << 24; - - if (it + len > d.end()) - throw Error(format("short db entry: `%1%'") % d); - - string s; - while (len--) s += *it++; - - data.push_back(s); - } - - return true; -} - - -void Database::setString(const Transaction & txn, TableId table, - const string & key, const string & data) -{ - try { - Db * db = getDb(table); - Dbt kt((void *) key.c_str(), key.length()); - Dbt dt((void *) data.c_str(), data.length()); - db->put(txn.txn, &kt, &dt, 0); - } catch (DbException e) { rethrow(e); } -} - - -void Database::setStrings(const Transaction & txn, TableId table, - const string & key, const Strings & data) -{ - string d; - - for (Strings::const_iterator it = data.begin(); - it != data.end(); it++) - { - string s = *it; - unsigned int len = s.size(); - - d += len & 0xff; - d += (len >> 8) & 0xff; - d += (len >> 16) & 0xff; - d += (len >> 24) & 0xff; - - d += s; - } - - setString(txn, table, key, d); -} - - -void Database::delPair(const Transaction & txn, TableId table, - const string & key) -{ - try { - Db * db = getDb(table); - Dbt kt((void *) key.c_str(), key.length()); - db->del(txn.txn, &kt, 0); - /* Non-existence of a pair with the given key is not an - error. */ - } catch (DbException e) { rethrow(e); } -} - - -void Database::enumTable(const Transaction & txn, TableId table, - Strings & keys) -{ - try { - Db * db = getDb(table); - - Dbc * dbc; - db->cursor(txn.txn, &dbc, 0); - DestroyDbc destroyDbc(dbc); - - Dbt kt, dt; - while (dbc->get(&kt, &dt, DB_NEXT) != DB_NOTFOUND) - keys.push_back( - string((char *) kt.get_data(), kt.get_size())); - - } catch (DbException e) { rethrow(e); } -} diff --git a/src/db.hh b/src/db.hh deleted file mode 100644 index 1c681b9b5419..000000000000 --- a/src/db.hh +++ /dev/null @@ -1,89 +0,0 @@ -#ifndef __DB_H -#define __DB_H - -#include -#include -#include - -#include - -#include "util.hh" - -using namespace std; - - -class Database; - - -class Transaction -{ - friend class Database; - -private: - DbTxn * txn; - -public: - Transaction(); - Transaction(Database & _db); - ~Transaction(); - - void abort(); - void commit(); - - void moveTo(Transaction & t); -}; - - -#define noTxn Transaction() - - -typedef unsigned int TableId; /* table handles */ - - -class Database -{ - friend class Transaction; - -private: - DbEnv * env; - - int fdLock; - int fdAccessors; - - TableId nextId; - map tables; - - void requireEnv(); - - Db * getDb(TableId table); - -public: - Database(); - ~Database(); - - void open(const string & path); - void close(); - - TableId openTable(const string & table); - - bool queryString(const Transaction & txn, TableId table, - const string & key, string & data); - - bool queryStrings(const Transaction & txn, TableId table, - const string & key, Strings & data); - - void setString(const Transaction & txn, TableId table, - const string & key, const string & data); - - void setStrings(const Transaction & txn, TableId table, - const string & key, const Strings & data); - - void delPair(const Transaction & txn, TableId table, - const string & key); - - void enumTable(const Transaction & txn, TableId table, - Strings & keys); -}; - - -#endif /* !__DB_H */ diff --git a/src/dotgraph.cc b/src/dotgraph.cc deleted file mode 100644 index 36daf7f9966d..000000000000 --- a/src/dotgraph.cc +++ /dev/null @@ -1,135 +0,0 @@ -#include "dotgraph.hh" -#include "normalise.hh" - - -static string dotQuote(const string & s) -{ - return "\"" + s + "\""; -} - - -static string nextColour() -{ - static int n = 0; - static string colours[] = - { "black", "red", "green", "blue" - , "magenta", "burlywood" }; - return colours[n++ % (sizeof(colours) / sizeof(string))]; -} - - -static string makeEdge(const string & src, const string & dst) -{ - format f = format("%1% -> %2% [color = %3%];\n") - % dotQuote(src) % dotQuote(dst) % dotQuote(nextColour()); - return f.str(); -} - - -static string makeNode(const string & id, const string & label, - const string & colour) -{ - format f = format("%1% [label = %2%, shape = box, " - "style = filled, fillcolor = %3%];\n") - % dotQuote(id) % dotQuote(label) % dotQuote(colour); - return f.str(); -} - - -static string symbolicName(const string & path) -{ - string p = baseNameOf(path); - if (isHash(string(p, 0, Hash::hashSize * 2)) && - p[Hash::hashSize * 2] == '-') - p = string(p, Hash::hashSize * 2 + 1); - return p; -} - - -string pathLabel(const Path & nePath, const string & elemPath) -{ - return (string) nePath + "-" + elemPath; -} - - -void printClosure(const Path & nePath, const NixExpr & fs) -{ - PathSet workList(fs.closure.roots); - PathSet doneSet; - - for (PathSet::iterator i = workList.begin(); i != workList.end(); i++) { - cout << makeEdge(pathLabel(nePath, *i), nePath); - } - - while (!workList.empty()) { - Path path = *(workList.begin()); - workList.erase(path); - - if (doneSet.find(path) == doneSet.end()) { - doneSet.insert(path); - - ClosureElems::const_iterator elem = fs.closure.elems.find(path); - if (elem == fs.closure.elems.end()) - throw Error(format("bad closure, missing path `%1%'") % path); - - for (StringSet::const_iterator i = elem->second.refs.begin(); - i != elem->second.refs.end(); i++) - { - workList.insert(*i); - cout << makeEdge(pathLabel(nePath, *i), pathLabel(nePath, path)); - } - - cout << makeNode(pathLabel(nePath, path), - symbolicName(path), "#ff0000"); - } - } -} - - -void printDotGraph(const PathSet & roots) -{ - PathSet workList(roots); - PathSet doneSet; - - cout << "digraph G {\n"; - - while (!workList.empty()) { - Path nePath = *(workList.begin()); - workList.erase(nePath); - - if (doneSet.find(nePath) == doneSet.end()) { - doneSet.insert(nePath); - - NixExpr ne = exprFromPath(nePath); - - string label, colour; - - if (ne.type == NixExpr::neDerivation) { - for (PathSet::iterator i = ne.derivation.inputs.begin(); - i != ne.derivation.inputs.end(); i++) - { - workList.insert(*i); - cout << makeEdge(*i, nePath); - } - - label = "derivation"; - colour = "#00ff00"; - for (StringPairs::iterator i = ne.derivation.env.begin(); - i != ne.derivation.env.end(); i++) - if (i->first == "name") label = i->second; - } - - else if (ne.type == NixExpr::neClosure) { - label = ""; - colour = "#00ffff"; - printClosure(nePath, ne); - } - - else abort(); - - cout << makeNode(nePath, label, colour); - } - } - - cout << "}\n"; -} diff --git a/src/dotgraph.hh b/src/dotgraph.hh deleted file mode 100644 index a5c5248f144e..000000000000 --- a/src/dotgraph.hh +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef __DOTGRAPH_H -#define __DOTGRAPH_H - -#include "expr.hh" - -void printDotGraph(const PathSet & roots); - -#endif /* !__DOTGRAPH_H */ diff --git a/src/exec.cc b/src/exec.cc deleted file mode 100644 index 2e092b2e0dd6..000000000000 --- a/src/exec.cc +++ /dev/null @@ -1,144 +0,0 @@ -#include - -#include -#include -#include -#include -#include - -#include "exec.hh" -#include "util.hh" -#include "globals.hh" - - -class AutoDelete -{ - string path; - bool del; -public: - - AutoDelete(const string & p) : path(p) - { - del = true; - } - - ~AutoDelete() - { - if (del) deletePath(path); - } - - void cancel() - { - del = false; - } -}; - - -static string pathNullDevice = "/dev/null"; - - -/* Run a program. */ -void runProgram(const string & program, - const Strings & args, const Environment & env) -{ - /* Create a log file. */ - string logFileName = nixLogDir + "/run.log"; - string logCommand = - verbosity >= lvlDebug - ? "tee -a " + logFileName + " >&2" - : "cat >> " + logFileName; - /* !!! auto-pclose on exit */ - FILE * logFile = popen(logCommand.c_str(), "w"); /* !!! escaping */ - if (!logFile) - throw SysError(format("creating log file `%1%'") % logFileName); - - /* Create a temporary directory where the build will take - place. */ - string tmpDir = createTempDir(); - - AutoDelete delTmpDir(tmpDir); - - /* Fork a child to build the package. */ - pid_t pid; - switch (pid = fork()) { - - case -1: - throw SysError("unable to fork"); - - case 0: - - try { /* child */ - - if (chdir(tmpDir.c_str()) == -1) - throw SysError(format("changing into to `%1%'") % tmpDir); - - /* Fill in the arguments. */ - const char * argArr[args.size() + 2]; - const char * * p = argArr; - string progName = baseNameOf(program); - *p++ = progName.c_str(); - for (Strings::const_iterator i = args.begin(); - i != args.end(); i++) - *p++ = i->c_str(); - *p = 0; - - /* Fill in the environment. */ - Strings envStrs; - const char * envArr[env.size() + 1]; - p = envArr; - for (Environment::const_iterator i = env.begin(); - i != env.end(); i++) - *p++ = envStrs.insert(envStrs.end(), - i->first + "=" + i->second)->c_str(); - *p = 0; - - /* Dup the log handle into stderr. */ - if (dup2(fileno(logFile), STDERR_FILENO) == -1) - throw SysError("cannot pipe standard error into log file"); - - /* Dup stderr to stdin. */ - if (dup2(STDERR_FILENO, STDOUT_FILENO) == -1) - throw SysError("cannot dup stderr into stdout"); - - /* Reroute stdin to /dev/null. */ - int fdDevNull = open(pathNullDevice.c_str(), O_RDWR); - if (fdDevNull == -1) - throw SysError(format("cannot open `%1%'") % pathNullDevice); - if (dup2(fdDevNull, STDIN_FILENO) == -1) - throw SysError("cannot dup null device into stdin"); - - /* Execute the program. This should not return. */ - execve(program.c_str(), (char * *) argArr, (char * *) envArr); - - throw SysError(format("unable to execute %1%") % program); - - } catch (exception & e) { - cerr << format("build error: %1%\n") % e.what(); - } - _exit(1); - - } - - /* parent */ - - /* Close the logging pipe. Note that this should not cause - the logger to exit until builder exits (because the latter - has an open file handle to the former). */ - pclose(logFile); - - /* Wait for the child to finish. */ - int status; - if (waitpid(pid, &status, 0) != pid) - throw Error("unable to wait for child"); - - if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { - if (keepFailed) { - msg(lvlTalkative, - format("build failed; keeping build directory `%1%'") % tmpDir); - delTmpDir.cancel(); - } - throw Error("unable to build package"); - } -} - - diff --git a/src/exec.hh b/src/exec.hh deleted file mode 100644 index 8d410e404383..000000000000 --- a/src/exec.hh +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef __EXEC_H -#define __EXEC_H - -#include -#include - -#include "util.hh" - -using namespace std; - - -/* A Unix environment is a mapping from strings to strings. */ -typedef map Environment; - - -/* Run a program. */ -void runProgram(const string & program, - const Strings & args, const Environment & env); - - -#endif /* !__EXEC_H */ diff --git a/src/expr.cc b/src/expr.cc deleted file mode 100644 index cead803425ba..000000000000 --- a/src/expr.cc +++ /dev/null @@ -1,222 +0,0 @@ -#include "expr.hh" -#include "globals.hh" -#include "store.hh" - - -string printTerm(ATerm t) -{ - char * s = ATwriteToString(t); - return s; -} - - -Error badTerm(const format & f, ATerm t) -{ - return Error(format("%1%, in `%2%'") % f.str() % printTerm(t)); -} - - -Hash hashTerm(ATerm t) -{ - return hashString(printTerm(t)); -} - - -Path writeTerm(ATerm t, const string & suffix) -{ - /* The id of a term is its hash. */ - Hash h = hashTerm(t); - - Path path = canonPath(nixStore + "/" + - (string) h + suffix + ".nix"); - - if (!isValidPath(path)) { - char * s = ATwriteToString(t); - if (!s) throw Error(format("cannot write aterm to `%1%'") % path); - addTextToStore(path, string(s)); - } - - return path; -} - - -static void parsePaths(ATermList paths, PathSet & out) -{ - while (!ATisEmpty(paths)) { - char * s; - ATerm t = ATgetFirst(paths); - if (!ATmatch(t, "", &s)) - throw badTerm("not a path", t); - out.insert(s); - paths = ATgetNext(paths); - } -} - - -static void checkClosure(const Closure & closure) -{ - if (closure.elems.size() == 0) - throw Error("empty closure"); - - PathSet decl; - for (ClosureElems::const_iterator i = closure.elems.begin(); - i != closure.elems.end(); i++) - decl.insert(i->first); - - for (PathSet::const_iterator i = closure.roots.begin(); - i != closure.roots.end(); i++) - if (decl.find(*i) == decl.end()) - throw Error(format("undefined root path `%1%'") % *i); - - for (ClosureElems::const_iterator i = closure.elems.begin(); - i != closure.elems.end(); i++) - for (PathSet::const_iterator j = i->second.refs.begin(); - j != i->second.refs.end(); j++) - if (decl.find(*j) == decl.end()) - throw Error( - format("undefined path `%1%' referenced by `%2%'") - % *j % i->first); -} - - -/* Parse a closure. */ -static bool parseClosure(ATerm t, Closure & closure) -{ - ATermList roots, elems; - - if (!ATmatch(t, "Closure([], [])", &roots, &elems)) - return false; - - parsePaths(roots, closure.roots); - - while (!ATisEmpty(elems)) { - char * s1; - ATermList refs; - ATerm t = ATgetFirst(elems); - if (!ATmatch(t, "(, [])", &s1, &refs)) - throw badTerm("not a closure element", t); - ClosureElem elem; - parsePaths(refs, elem.refs); - closure.elems[s1] = elem; - elems = ATgetNext(elems); - } - - checkClosure(closure); - return true; -} - - -static bool parseDerivation(ATerm t, Derivation & derivation) -{ - ATermList outs, ins, args, bnds; - char * builder; - char * platform; - - if (!ATmatch(t, "Derive([], [], , , [], [])", - &outs, &ins, &platform, &builder, &args, &bnds)) - { - /* !!! compatibility -> remove eventually */ - if (!ATmatch(t, "Derive([], [], , , [])", - &outs, &ins, &builder, &platform, &bnds)) - return false; - args = ATempty; - } - - parsePaths(outs, derivation.outputs); - parsePaths(ins, derivation.inputs); - - derivation.builder = builder; - derivation.platform = platform; - - while (!ATisEmpty(args)) { - char * s; - ATerm arg = ATgetFirst(args); - if (!ATmatch(arg, "", &s)) - throw badTerm("string expected", arg); - derivation.args.push_back(s); - args = ATgetNext(args); - } - - while (!ATisEmpty(bnds)) { - char * s1, * s2; - ATerm bnd = ATgetFirst(bnds); - if (!ATmatch(bnd, "(, )", &s1, &s2)) - throw badTerm("tuple of strings expected", bnd); - derivation.env[s1] = s2; - bnds = ATgetNext(bnds); - } - - return true; -} - - -NixExpr parseNixExpr(ATerm t) -{ - NixExpr ne; - if (parseClosure(t, ne.closure)) - ne.type = NixExpr::neClosure; - else if (parseDerivation(t, ne.derivation)) - ne.type = NixExpr::neDerivation; - else throw badTerm("not a Nix expression", t); - return ne; -} - - -static ATermList unparsePaths(const PathSet & paths) -{ - ATermList l = ATempty; - for (PathSet::const_iterator i = paths.begin(); - i != paths.end(); i++) - l = ATinsert(l, ATmake("", i->c_str())); - return ATreverse(l); -} - - -static ATerm unparseClosure(const Closure & closure) -{ - ATermList roots = unparsePaths(closure.roots); - - ATermList elems = ATempty; - for (ClosureElems::const_iterator i = closure.elems.begin(); - i != closure.elems.end(); i++) - elems = ATinsert(elems, - ATmake("(, )", - i->first.c_str(), - unparsePaths(i->second.refs))); - - return ATmake("Closure(, )", roots, elems); -} - - -static ATerm unparseDerivation(const Derivation & derivation) -{ - ATermList args = ATempty; - for (Strings::const_iterator i = derivation.args.begin(); - i != derivation.args.end(); i++) - args = ATinsert(args, ATmake("", i->c_str())); - - ATermList env = ATempty; - for (StringPairs::const_iterator i = derivation.env.begin(); - i != derivation.env.end(); i++) - env = ATinsert(env, - ATmake("(, )", - i->first.c_str(), i->second.c_str())); - - return ATmake("Derive(, , , , , )", - unparsePaths(derivation.outputs), - unparsePaths(derivation.inputs), - derivation.platform.c_str(), - derivation.builder.c_str(), - ATreverse(args), - ATreverse(env)); -} - - -ATerm unparseNixExpr(const NixExpr & ne) -{ - if (ne.type == NixExpr::neClosure) - return unparseClosure(ne.closure); - else if (ne.type == NixExpr::neDerivation) - return unparseDerivation(ne.derivation); - else abort(); -} diff --git a/src/expr.hh b/src/expr.hh deleted file mode 100644 index 7d0420935f9d..000000000000 --- a/src/expr.hh +++ /dev/null @@ -1,66 +0,0 @@ -#ifndef __FSTATE_H -#define __FSTATE_H - -extern "C" { -#include -} - -#include "store.hh" - - -/* Abstract syntax of Nix expressions. */ - -struct ClosureElem -{ - PathSet refs; -}; - -typedef map ClosureElems; - -struct Closure -{ - PathSet roots; - ClosureElems elems; -}; - -typedef map StringPairs; - -struct Derivation -{ - PathSet outputs; - PathSet inputs; /* Nix expressions, not actual inputs */ - string platform; - Path builder; - Strings args; - StringPairs env; -}; - -struct NixExpr -{ - enum { neClosure, neDerivation } type; - Closure closure; - Derivation derivation; -}; - - -/* Return a canonical textual representation of an expression. */ -string printTerm(ATerm t); - -/* Throw an exception with an error message containing the given - aterm. */ -Error badTerm(const format & f, ATerm t); - -/* Hash an aterm. */ -Hash hashTerm(ATerm t); - -/* Write an aterm to the Nix store directory, and return its path. */ -Path writeTerm(ATerm t, const string & suffix); - -/* Parse a Nix expression. */ -NixExpr parseNixExpr(ATerm t); - -/* Parse a Nix expression. */ -ATerm unparseNixExpr(const NixExpr & ne); - - -#endif /* !__FSTATE_H */ diff --git a/src/fix.cc b/src/fix.cc deleted file mode 100644 index c1f9c1ad68d0..000000000000 --- a/src/fix.cc +++ /dev/null @@ -1,488 +0,0 @@ -#include -#include - -#include "globals.hh" -#include "normalise.hh" -#include "shared.hh" - - -typedef ATerm Expr; - -typedef map NormalForms; -typedef map PkgPaths; -typedef map PkgHashes; - -struct EvalState -{ - Paths searchDirs; - NormalForms normalForms; - PkgPaths pkgPaths; - PkgHashes pkgHashes; /* normalised package hashes */ - Expr blackHole; - - EvalState() - { - blackHole = ATmake("BlackHole()"); - if (!blackHole) throw Error("cannot build black hole"); - } -}; - - -static Expr evalFile(EvalState & state, const Path & path); -static Expr evalExpr(EvalState & state, Expr e); - - -static Path searchPath(const Paths & searchDirs, const Path & relPath) -{ - if (string(relPath, 0, 1) == "/") return relPath; - - for (Paths::const_iterator i = searchDirs.begin(); - i != searchDirs.end(); i++) - { - Path path = *i + "/" + relPath; - if (pathExists(path)) return path; - } - - throw Error( - format("path `%1%' not found in any of the search directories") - % relPath); -} - - -static Expr substExpr(string x, Expr rep, Expr e) -{ - char * s; - Expr e2; - - if (ATmatch(e, "Var()", &s)) - if (x == s) - return rep; - else - return e; - - ATermList formals; - if (ATmatch(e, "Function([], )", &formals, &e2)) { - while (!ATisEmpty(formals)) { - if (!ATmatch(ATgetFirst(formals), "", &s)) - throw badTerm("not a list of formals", (ATerm) formals); - if (x == (string) s) - return e; - formals = ATgetNext(formals); - } - } - - /* Generically substitute in subterms. */ - - if (ATgetType(e) == AT_APPL) { - AFun fun = ATgetAFun(e); - int arity = ATgetArity(fun); - ATermList args = ATempty; - - for (int i = arity - 1; i >= 0; i--) - args = ATinsert(args, substExpr(x, rep, ATgetArgument(e, i))); - - return (ATerm) ATmakeApplList(fun, args); - } - - if (ATgetType(e) == AT_LIST) { - ATermList in = (ATermList) e; - ATermList out = ATempty; - - while (!ATisEmpty(in)) { - out = ATinsert(out, substExpr(x, rep, ATgetFirst(in))); - in = ATgetNext(in); - } - - return (ATerm) ATreverse(out); - } - - throw badTerm("do not know how to substitute", e); -} - - -static Expr substExprMany(ATermList formals, ATermList args, Expr body) -{ - char * s; - Expr e; - - /* !!! check args against formals */ - - while (!ATisEmpty(args)) { - ATerm tup = ATgetFirst(args); - if (!ATmatch(tup, "(, )", &s, &e)) - throw badTerm("expected an argument tuple", tup); - - body = substExpr(s, e, body); - - args = ATgetNext(args); - } - - return body; -} - - -static PathSet nixExprRootsCached(EvalState & state, const Path & nePath) -{ - PkgPaths::iterator i = state.pkgPaths.find(nePath); - if (i != state.pkgPaths.end()) - return i->second; - else { - PathSet paths = nixExprRoots(nePath); - state.pkgPaths[nePath] = paths; - return paths; - } -} - - -static Hash hashPackage(EvalState & state, NixExpr ne) -{ - if (ne.type == NixExpr::neDerivation) { - PathSet inputs2; - for (PathSet::iterator i = ne.derivation.inputs.begin(); - i != ne.derivation.inputs.end(); i++) - { - PkgHashes::iterator j = state.pkgHashes.find(*i); - if (j == state.pkgHashes.end()) - throw Error(format("don't know expression `%1%'") % (string) *i); - inputs2.insert(j->second); - } - ne.derivation.inputs = inputs2; - } - return hashTerm(unparseNixExpr(ne)); -} - - -static string processBinding(EvalState & state, Expr e, NixExpr & ne) -{ - char * s1; - - if (ATmatch(e, "NixExpr()", &s1)) { - Path nePath(s1); - PathSet paths = nixExprRootsCached(state, nePath); - if (paths.size() != 1) abort(); - Path path = *(paths.begin()); - ne.derivation.inputs.insert(nePath); - return path; - } - - if (ATmatch(e, "", &s1)) - return s1; - - if (ATmatch(e, "True")) return "1"; - - if (ATmatch(e, "False")) return ""; - - ATermList l; - if (ATmatch(e, "[]", &l)) { - string s; - bool first = true; - while (!ATisEmpty(l)) { - if (!first) s = s + " "; else first = false; - s += processBinding(state, evalExpr(state, ATgetFirst(l)), ne); - l = ATgetNext(l); - } - return s; - } - - throw badTerm("invalid package binding", e); -} - - -static Expr evalExpr2(EvalState & state, Expr e) -{ - char * s1; - Expr e1, e2, e3, e4; - ATermList bnds; - - /* Normal forms. */ - if (ATmatch(e, "", &s1) || - ATmatch(e, "[]", &e1) || - ATmatch(e, "True") || - ATmatch(e, "False") || - ATmatch(e, "Function([], )", &e1, &e2) || - ATmatch(e, "NixExpr()", &s1)) - return e; - - try { - Hash pkgHash = hashPackage(state, parseNixExpr(e)); - Path pkgPath = writeTerm(e, ""); - state.pkgHashes[pkgPath] = pkgHash; - return ATmake("NixExpr()", pkgPath.c_str()); - } catch (...) { /* !!! catch parse errors only */ - } - - /* Application. */ - if (ATmatch(e, "Call(, [])", &e1, &e2) || - ATmatch(e, "App(, [])", &e1, &e2)) { - e1 = evalExpr(state, e1); - if (!ATmatch(e1, "Function([], )", &e3, &e4)) - throw badTerm("expecting a function", e1); - return evalExpr(state, - substExprMany((ATermList) e3, (ATermList) e2, e4)); - } - - /* Conditional. */ - if (ATmatch(e, "If(, , )", &e1, &e2, &e3)) { - e1 = evalExpr(state, e1); - Expr x; - if (ATmatch(e1, "True")) x = e2; - else if (ATmatch(e1, "False")) x = e3; - else throw badTerm("expecting a boolean", e1); - return evalExpr(state, x); - } - - /* Ad-hoc function for string matching. */ - if (ATmatch(e, "HasSubstr(, )", &e1, &e2)) { - e1 = evalExpr(state, e1); - e2 = evalExpr(state, e2); - - char * s1, * s2; - if (!ATmatch(e1, "", &s1)) - throw badTerm("expecting a string", e1); - if (!ATmatch(e2, "", &s2)) - throw badTerm("expecting a string", e2); - - return - string(s1).find(string(s2)) != string::npos ? - ATmake("True") : ATmake("False"); - } - - /* Platform constant. */ - if (ATmatch(e, "Platform")) { - return ATmake("", SYSTEM); - } - - /* Fix inclusion. */ - if (ATmatch(e, "IncludeFix()", &s1)) { - Path fileName(s1); - return evalFile(state, s1); - } - - /* Relative files. */ - if (ATmatch(e, "Relative()", &s1)) { - Path srcPath = searchPath(state.searchDirs, s1); - Path dstPath = addToStore(srcPath); - - ClosureElem elem; - NixExpr ne; - ne.type = NixExpr::neClosure; - ne.closure.roots.insert(dstPath); - ne.closure.elems[dstPath] = elem; - - Hash pkgHash = hashPackage(state, ne); - Path pkgPath = writeTerm(unparseNixExpr(ne), ""); - state.pkgHashes[pkgPath] = pkgHash; - - msg(lvlChatty, format("copied `%1%' -> closure `%2%'") - % srcPath % pkgPath); - - return ATmake("NixExpr()", pkgPath.c_str()); - } - - /* Packages are transformed into Nix derivation expressions. */ - if (ATmatch(e, "Package([])", &bnds)) { - - /* Evaluate the bindings and put them in a map. */ - map bndMap; - bndMap["platform"] = ATmake("", SYSTEM); - while (!ATisEmpty(bnds)) { - ATerm bnd = ATgetFirst(bnds); - if (!ATmatch(bnd, "(, )", &s1, &e1)) - throw badTerm("binding expected", bnd); - bndMap[s1] = evalExpr(state, e1); - bnds = ATgetNext(bnds); - } - - /* Gather information for building the derivation - expression. */ - NixExpr ne; - ne.type = NixExpr::neDerivation; - ne.derivation.platform = SYSTEM; - string name; - Path outPath; - Hash outHash; - bool outHashGiven = false; - bnds = ATempty; - - for (map::iterator it = bndMap.begin(); - it != bndMap.end(); it++) - { - string key = it->first; - ATerm value = it->second; - - if (key == "args") { - ATermList args; - if (!ATmatch(value, "[]", &args)) - throw badTerm("list expected", value); - - while (!ATisEmpty(args)) { - Expr arg = evalExpr(state, ATgetFirst(args)); - ne.derivation.args.push_back(processBinding(state, arg, ne)); - args = ATgetNext(args); - } - } - - else { - string s = processBinding(state, value, ne); - ne.derivation.env[key] = s; - - if (key == "build") ne.derivation.builder = s; - if (key == "name") name = s; - if (key == "outPath") outPath = s; - if (key == "id") { - outHash = parseHash(s); - outHashGiven = true; - } - } - - bnds = ATinsert(bnds, - ATmake("(, )", key.c_str(), value)); - } - - if (ne.derivation.builder == "") - throw badTerm("no builder specified", e); - - if (name == "") - throw badTerm("no package name specified", e); - - /* Determine the output path. */ - if (!outHashGiven) outHash = hashPackage(state, ne); - if (outPath == "") - /* Hash the Nix expression with no outputs to produce a - unique but deterministic path name for this package. */ - outPath = - canonPath(nixStore + "/" + ((string) outHash).c_str() + "-" + name); - ne.derivation.env["out"] = outPath; - ne.derivation.outputs.insert(outPath); - - /* Write the resulting term into the Nix store directory. */ - Hash pkgHash = outHashGiven - ? hashString((string) outHash + outPath) - : hashPackage(state, ne); - Path pkgPath = writeTerm(unparseNixExpr(ne), "-d-" + name); - state.pkgHashes[pkgPath] = pkgHash; - - msg(lvlChatty, format("instantiated `%1%' -> `%2%'") - % name % pkgPath); - - return ATmake("NixExpr()", pkgPath.c_str()); - } - - /* BaseName primitive function. */ - if (ATmatch(e, "BaseName()", &e1)) { - e1 = evalExpr(state, e1); - if (!ATmatch(e1, "", &s1)) - throw badTerm("string expected", e1); - return ATmake("", baseNameOf(s1).c_str()); - } - - /* Barf. */ - throw badTerm("invalid expression", e); -} - - -static Expr evalExpr(EvalState & state, Expr e) -{ - Nest nest(lvlVomit, format("evaluating expression: %1%") % printTerm(e)); - - /* Consult the memo table to quickly get the normal form of - previously evaluated expressions. */ - NormalForms::iterator i = state.normalForms.find(e); - if (i != state.normalForms.end()) { - if (i->second == state.blackHole) - throw badTerm("infinite recursion", e); - return i->second; - } - - /* Otherwise, evaluate and memoize. */ - state.normalForms[e] = state.blackHole; - Expr nf = evalExpr2(state, e); - state.normalForms[e] = nf; - return nf; -} - - -static Expr evalFile(EvalState & state, const Path & relPath) -{ - Path path = searchPath(state.searchDirs, relPath); - Nest nest(lvlTalkative, format("evaluating file `%1%'") % path); - Expr e = ATreadFromNamedFile(path.c_str()); - if (!e) - throw Error(format("unable to read a term from `%1%'") % path); - return evalExpr(state, e); -} - - -static Expr evalStdin(EvalState & state) -{ - Nest nest(lvlTalkative, format("evaluating standard input")); - Expr e = ATreadFromFile(stdin); - if (!e) - throw Error(format("unable to read a term from stdin")); - return evalExpr(state, e); -} - - -static void printNixExpr(EvalState & state, Expr e) -{ - ATermList es; - char * s; - if (ATmatch(e, "NixExpr()", &s)) { - cout << format("%1%\n") % s; - } - else if (ATmatch(e, "[]", &es)) { - while (!ATisEmpty(es)) { - printNixExpr(state, evalExpr(state, ATgetFirst(es))); - es = ATgetNext(es); - } - } - else throw badTerm("top level does not evaluate to a (list of) Nix expression(s)", e); -} - - -void run(Strings args) -{ - EvalState state; - Strings files; - bool readStdin = false; - - state.searchDirs.push_back("."); - state.searchDirs.push_back(nixDataDir + "/fix"); - - for (Strings::iterator it = args.begin(); - it != args.end(); ) - { - string arg = *it++; - - if (arg == "--includedir" || arg == "-I") { - if (it == args.end()) - throw UsageError(format("argument required in `%1%'") % arg); - state.searchDirs.push_back(*it++); - } - else if (arg == "--verbose" || arg == "-v") - verbosity = (Verbosity) ((int) verbosity + 1); - else if (arg == "-") - readStdin = true; - else if (arg[0] == '-') - throw UsageError(format("unknown flag `%1%`") % arg); - else - files.push_back(arg); - } - - openDB(); - - if (readStdin) { - Expr e = evalStdin(state); - printNixExpr(state, e); - } - - for (Strings::iterator it = files.begin(); - it != files.end(); it++) - { - Expr e = evalFile(state, *it); - printNixExpr(state, e); - } -} - - -string programId = "fix"; diff --git a/src/fix/fix.cc b/src/fix/fix.cc new file mode 100644 index 000000000000..c1f9c1ad68d0 --- /dev/null +++ b/src/fix/fix.cc @@ -0,0 +1,488 @@ +#include +#include + +#include "globals.hh" +#include "normalise.hh" +#include "shared.hh" + + +typedef ATerm Expr; + +typedef map NormalForms; +typedef map PkgPaths; +typedef map PkgHashes; + +struct EvalState +{ + Paths searchDirs; + NormalForms normalForms; + PkgPaths pkgPaths; + PkgHashes pkgHashes; /* normalised package hashes */ + Expr blackHole; + + EvalState() + { + blackHole = ATmake("BlackHole()"); + if (!blackHole) throw Error("cannot build black hole"); + } +}; + + +static Expr evalFile(EvalState & state, const Path & path); +static Expr evalExpr(EvalState & state, Expr e); + + +static Path searchPath(const Paths & searchDirs, const Path & relPath) +{ + if (string(relPath, 0, 1) == "/") return relPath; + + for (Paths::const_iterator i = searchDirs.begin(); + i != searchDirs.end(); i++) + { + Path path = *i + "/" + relPath; + if (pathExists(path)) return path; + } + + throw Error( + format("path `%1%' not found in any of the search directories") + % relPath); +} + + +static Expr substExpr(string x, Expr rep, Expr e) +{ + char * s; + Expr e2; + + if (ATmatch(e, "Var()", &s)) + if (x == s) + return rep; + else + return e; + + ATermList formals; + if (ATmatch(e, "Function([], )", &formals, &e2)) { + while (!ATisEmpty(formals)) { + if (!ATmatch(ATgetFirst(formals), "", &s)) + throw badTerm("not a list of formals", (ATerm) formals); + if (x == (string) s) + return e; + formals = ATgetNext(formals); + } + } + + /* Generically substitute in subterms. */ + + if (ATgetType(e) == AT_APPL) { + AFun fun = ATgetAFun(e); + int arity = ATgetArity(fun); + ATermList args = ATempty; + + for (int i = arity - 1; i >= 0; i--) + args = ATinsert(args, substExpr(x, rep, ATgetArgument(e, i))); + + return (ATerm) ATmakeApplList(fun, args); + } + + if (ATgetType(e) == AT_LIST) { + ATermList in = (ATermList) e; + ATermList out = ATempty; + + while (!ATisEmpty(in)) { + out = ATinsert(out, substExpr(x, rep, ATgetFirst(in))); + in = ATgetNext(in); + } + + return (ATerm) ATreverse(out); + } + + throw badTerm("do not know how to substitute", e); +} + + +static Expr substExprMany(ATermList formals, ATermList args, Expr body) +{ + char * s; + Expr e; + + /* !!! check args against formals */ + + while (!ATisEmpty(args)) { + ATerm tup = ATgetFirst(args); + if (!ATmatch(tup, "(, )", &s, &e)) + throw badTerm("expected an argument tuple", tup); + + body = substExpr(s, e, body); + + args = ATgetNext(args); + } + + return body; +} + + +static PathSet nixExprRootsCached(EvalState & state, const Path & nePath) +{ + PkgPaths::iterator i = state.pkgPaths.find(nePath); + if (i != state.pkgPaths.end()) + return i->second; + else { + PathSet paths = nixExprRoots(nePath); + state.pkgPaths[nePath] = paths; + return paths; + } +} + + +static Hash hashPackage(EvalState & state, NixExpr ne) +{ + if (ne.type == NixExpr::neDerivation) { + PathSet inputs2; + for (PathSet::iterator i = ne.derivation.inputs.begin(); + i != ne.derivation.inputs.end(); i++) + { + PkgHashes::iterator j = state.pkgHashes.find(*i); + if (j == state.pkgHashes.end()) + throw Error(format("don't know expression `%1%'") % (string) *i); + inputs2.insert(j->second); + } + ne.derivation.inputs = inputs2; + } + return hashTerm(unparseNixExpr(ne)); +} + + +static string processBinding(EvalState & state, Expr e, NixExpr & ne) +{ + char * s1; + + if (ATmatch(e, "NixExpr()", &s1)) { + Path nePath(s1); + PathSet paths = nixExprRootsCached(state, nePath); + if (paths.size() != 1) abort(); + Path path = *(paths.begin()); + ne.derivation.inputs.insert(nePath); + return path; + } + + if (ATmatch(e, "", &s1)) + return s1; + + if (ATmatch(e, "True")) return "1"; + + if (ATmatch(e, "False")) return ""; + + ATermList l; + if (ATmatch(e, "[]", &l)) { + string s; + bool first = true; + while (!ATisEmpty(l)) { + if (!first) s = s + " "; else first = false; + s += processBinding(state, evalExpr(state, ATgetFirst(l)), ne); + l = ATgetNext(l); + } + return s; + } + + throw badTerm("invalid package binding", e); +} + + +static Expr evalExpr2(EvalState & state, Expr e) +{ + char * s1; + Expr e1, e2, e3, e4; + ATermList bnds; + + /* Normal forms. */ + if (ATmatch(e, "", &s1) || + ATmatch(e, "[]", &e1) || + ATmatch(e, "True") || + ATmatch(e, "False") || + ATmatch(e, "Function([], )", &e1, &e2) || + ATmatch(e, "NixExpr()", &s1)) + return e; + + try { + Hash pkgHash = hashPackage(state, parseNixExpr(e)); + Path pkgPath = writeTerm(e, ""); + state.pkgHashes[pkgPath] = pkgHash; + return ATmake("NixExpr()", pkgPath.c_str()); + } catch (...) { /* !!! catch parse errors only */ + } + + /* Application. */ + if (ATmatch(e, "Call(, [])", &e1, &e2) || + ATmatch(e, "App(, [])", &e1, &e2)) { + e1 = evalExpr(state, e1); + if (!ATmatch(e1, "Function([], )", &e3, &e4)) + throw badTerm("expecting a function", e1); + return evalExpr(state, + substExprMany((ATermList) e3, (ATermList) e2, e4)); + } + + /* Conditional. */ + if (ATmatch(e, "If(, , )", &e1, &e2, &e3)) { + e1 = evalExpr(state, e1); + Expr x; + if (ATmatch(e1, "True")) x = e2; + else if (ATmatch(e1, "False")) x = e3; + else throw badTerm("expecting a boolean", e1); + return evalExpr(state, x); + } + + /* Ad-hoc function for string matching. */ + if (ATmatch(e, "HasSubstr(, )", &e1, &e2)) { + e1 = evalExpr(state, e1); + e2 = evalExpr(state, e2); + + char * s1, * s2; + if (!ATmatch(e1, "", &s1)) + throw badTerm("expecting a string", e1); + if (!ATmatch(e2, "", &s2)) + throw badTerm("expecting a string", e2); + + return + string(s1).find(string(s2)) != string::npos ? + ATmake("True") : ATmake("False"); + } + + /* Platform constant. */ + if (ATmatch(e, "Platform")) { + return ATmake("", SYSTEM); + } + + /* Fix inclusion. */ + if (ATmatch(e, "IncludeFix()", &s1)) { + Path fileName(s1); + return evalFile(state, s1); + } + + /* Relative files. */ + if (ATmatch(e, "Relative()", &s1)) { + Path srcPath = searchPath(state.searchDirs, s1); + Path dstPath = addToStore(srcPath); + + ClosureElem elem; + NixExpr ne; + ne.type = NixExpr::neClosure; + ne.closure.roots.insert(dstPath); + ne.closure.elems[dstPath] = elem; + + Hash pkgHash = hashPackage(state, ne); + Path pkgPath = writeTerm(unparseNixExpr(ne), ""); + state.pkgHashes[pkgPath] = pkgHash; + + msg(lvlChatty, format("copied `%1%' -> closure `%2%'") + % srcPath % pkgPath); + + return ATmake("NixExpr()", pkgPath.c_str()); + } + + /* Packages are transformed into Nix derivation expressions. */ + if (ATmatch(e, "Package([])", &bnds)) { + + /* Evaluate the bindings and put them in a map. */ + map bndMap; + bndMap["platform"] = ATmake("", SYSTEM); + while (!ATisEmpty(bnds)) { + ATerm bnd = ATgetFirst(bnds); + if (!ATmatch(bnd, "(, )", &s1, &e1)) + throw badTerm("binding expected", bnd); + bndMap[s1] = evalExpr(state, e1); + bnds = ATgetNext(bnds); + } + + /* Gather information for building the derivation + expression. */ + NixExpr ne; + ne.type = NixExpr::neDerivation; + ne.derivation.platform = SYSTEM; + string name; + Path outPath; + Hash outHash; + bool outHashGiven = false; + bnds = ATempty; + + for (map::iterator it = bndMap.begin(); + it != bndMap.end(); it++) + { + string key = it->first; + ATerm value = it->second; + + if (key == "args") { + ATermList args; + if (!ATmatch(value, "[]", &args)) + throw badTerm("list expected", value); + + while (!ATisEmpty(args)) { + Expr arg = evalExpr(state, ATgetFirst(args)); + ne.derivation.args.push_back(processBinding(state, arg, ne)); + args = ATgetNext(args); + } + } + + else { + string s = processBinding(state, value, ne); + ne.derivation.env[key] = s; + + if (key == "build") ne.derivation.builder = s; + if (key == "name") name = s; + if (key == "outPath") outPath = s; + if (key == "id") { + outHash = parseHash(s); + outHashGiven = true; + } + } + + bnds = ATinsert(bnds, + ATmake("(, )", key.c_str(), value)); + } + + if (ne.derivation.builder == "") + throw badTerm("no builder specified", e); + + if (name == "") + throw badTerm("no package name specified", e); + + /* Determine the output path. */ + if (!outHashGiven) outHash = hashPackage(state, ne); + if (outPath == "") + /* Hash the Nix expression with no outputs to produce a + unique but deterministic path name for this package. */ + outPath = + canonPath(nixStore + "/" + ((string) outHash).c_str() + "-" + name); + ne.derivation.env["out"] = outPath; + ne.derivation.outputs.insert(outPath); + + /* Write the resulting term into the Nix store directory. */ + Hash pkgHash = outHashGiven + ? hashString((string) outHash + outPath) + : hashPackage(state, ne); + Path pkgPath = writeTerm(unparseNixExpr(ne), "-d-" + name); + state.pkgHashes[pkgPath] = pkgHash; + + msg(lvlChatty, format("instantiated `%1%' -> `%2%'") + % name % pkgPath); + + return ATmake("NixExpr()", pkgPath.c_str()); + } + + /* BaseName primitive function. */ + if (ATmatch(e, "BaseName()", &e1)) { + e1 = evalExpr(state, e1); + if (!ATmatch(e1, "", &s1)) + throw badTerm("string expected", e1); + return ATmake("", baseNameOf(s1).c_str()); + } + + /* Barf. */ + throw badTerm("invalid expression", e); +} + + +static Expr evalExpr(EvalState & state, Expr e) +{ + Nest nest(lvlVomit, format("evaluating expression: %1%") % printTerm(e)); + + /* Consult the memo table to quickly get the normal form of + previously evaluated expressions. */ + NormalForms::iterator i = state.normalForms.find(e); + if (i != state.normalForms.end()) { + if (i->second == state.blackHole) + throw badTerm("infinite recursion", e); + return i->second; + } + + /* Otherwise, evaluate and memoize. */ + state.normalForms[e] = state.blackHole; + Expr nf = evalExpr2(state, e); + state.normalForms[e] = nf; + return nf; +} + + +static Expr evalFile(EvalState & state, const Path & relPath) +{ + Path path = searchPath(state.searchDirs, relPath); + Nest nest(lvlTalkative, format("evaluating file `%1%'") % path); + Expr e = ATreadFromNamedFile(path.c_str()); + if (!e) + throw Error(format("unable to read a term from `%1%'") % path); + return evalExpr(state, e); +} + + +static Expr evalStdin(EvalState & state) +{ + Nest nest(lvlTalkative, format("evaluating standard input")); + Expr e = ATreadFromFile(stdin); + if (!e) + throw Error(format("unable to read a term from stdin")); + return evalExpr(state, e); +} + + +static void printNixExpr(EvalState & state, Expr e) +{ + ATermList es; + char * s; + if (ATmatch(e, "NixExpr()", &s)) { + cout << format("%1%\n") % s; + } + else if (ATmatch(e, "[]", &es)) { + while (!ATisEmpty(es)) { + printNixExpr(state, evalExpr(state, ATgetFirst(es))); + es = ATgetNext(es); + } + } + else throw badTerm("top level does not evaluate to a (list of) Nix expression(s)", e); +} + + +void run(Strings args) +{ + EvalState state; + Strings files; + bool readStdin = false; + + state.searchDirs.push_back("."); + state.searchDirs.push_back(nixDataDir + "/fix"); + + for (Strings::iterator it = args.begin(); + it != args.end(); ) + { + string arg = *it++; + + if (arg == "--includedir" || arg == "-I") { + if (it == args.end()) + throw UsageError(format("argument required in `%1%'") % arg); + state.searchDirs.push_back(*it++); + } + else if (arg == "--verbose" || arg == "-v") + verbosity = (Verbosity) ((int) verbosity + 1); + else if (arg == "-") + readStdin = true; + else if (arg[0] == '-') + throw UsageError(format("unknown flag `%1%`") % arg); + else + files.push_back(arg); + } + + openDB(); + + if (readStdin) { + Expr e = evalStdin(state); + printNixExpr(state, e); + } + + for (Strings::iterator it = files.begin(); + it != files.end(); it++) + { + Expr e = evalFile(state, *it); + printNixExpr(state, e); + } +} + + +string programId = "fix"; diff --git a/src/globals.cc b/src/globals.cc deleted file mode 100644 index a292b49aeae0..000000000000 --- a/src/globals.cc +++ /dev/null @@ -1,8 +0,0 @@ -#include "globals.hh" - -string nixStore = "/UNINIT"; -string nixDataDir = "/UNINIT"; -string nixLogDir = "/UNINIT"; -string nixDBPath = "/UNINIT"; - -bool keepFailed = false; diff --git a/src/globals.hh b/src/globals.hh deleted file mode 100644 index 1b4d0bde3ffe..000000000000 --- a/src/globals.hh +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef __GLOBALS_H -#define __GLOBALS_H - -#include - -using namespace std; - -/* Path names. */ - -/* nixStore is the directory where we generally store atomic and - derived files. */ -extern string nixStore; - -extern string nixDataDir; /* !!! fix */ - -/* nixLogDir is the directory where we log various operations. */ -extern string nixLogDir; - -/* nixDBPath is the path name of our Berkeley DB environment. */ -extern string nixDBPath; - - -/* Misc. global flags. */ - -/* Whether to keep temporary directories of failed builds. */ -extern bool keepFailed; - - -#endif /* !__GLOBALS_H */ diff --git a/src/hash.cc b/src/hash.cc deleted file mode 100644 index 752b269125cb..000000000000 --- a/src/hash.cc +++ /dev/null @@ -1,124 +0,0 @@ -#include - -extern "C" { -#include "md5.h" -} - -#include "hash.hh" -#include "archive.hh" - - -Hash::Hash() -{ - memset(hash, 0, sizeof(hash)); -} - - -bool Hash::operator == (const Hash & h2) const -{ - for (unsigned int i = 0; i < hashSize; i++) - if (hash[i] != h2.hash[i]) return false; - return true; -} - - -bool Hash::operator != (const Hash & h2) const -{ - return !(*this == h2); -} - - -bool Hash::operator < (const Hash & h) const -{ - for (unsigned int i = 0; i < hashSize; i++) { - if (hash[i] < h.hash[i]) return true; - if (hash[i] > h.hash[i]) return false; - } - return false; -} - - -Hash::operator string() const -{ - ostringstream str; - for (unsigned int i = 0; i < hashSize; i++) { - str.fill('0'); - str.width(2); - str << hex << (int) hash[i]; - } - return str.str(); -} - - -Hash parseHash(const string & s) -{ - Hash hash; - if (s.length() != Hash::hashSize * 2) - throw Error(format("invalid hash `%1%'") % s); - for (unsigned int i = 0; i < Hash::hashSize; i++) { - string s2(s, i * 2, 2); - if (!isxdigit(s2[0]) || !isxdigit(s2[1])) - throw Error(format("invalid hash `%1%'") % s); - istringstream str(s2); - int n; - str >> hex >> n; - hash.hash[i] = n; - } - return hash; -} - - -bool isHash(const string & s) -{ - if (s.length() != 32) return false; - for (int i = 0; i < 32; i++) { - char c = s[i]; - if (!((c >= '0' && c <= '9') || - (c >= 'a' && c <= 'f'))) - return false; - } - return true; -} - - -Hash hashString(const string & s) -{ - Hash hash; - md5_buffer(s.c_str(), s.length(), hash.hash); - return hash; -} - - -Hash hashFile(const Path & path) -{ - Hash hash; - FILE * file = fopen(path.c_str(), "rb"); - if (!file) - throw SysError(format("file `%1%' does not exist") % path); - int err = md5_stream(file, hash.hash); - fclose(file); - if (err) throw SysError(format("cannot hash file `%1%'") % path); - return hash; -} - - -struct HashSink : DumpSink -{ - struct md5_ctx ctx; - virtual void operator () - (const unsigned char * data, unsigned int len) - { - md5_process_bytes(data, len, &ctx); - } -}; - - -Hash hashPath(const Path & path) -{ - Hash hash; - HashSink sink; - md5_init_ctx(&sink.ctx); - dumpPath(path, sink); - md5_finish_ctx(&sink.ctx, hash.hash); - return hash; -} diff --git a/src/hash.hh b/src/hash.hh deleted file mode 100644 index 0062f987c021..000000000000 --- a/src/hash.hh +++ /dev/null @@ -1,51 +0,0 @@ -#ifndef __HASH_H -#define __HASH_H - -#include - -#include "util.hh" - -using namespace std; - - -struct Hash -{ - static const unsigned int hashSize = 16; - unsigned char hash[hashSize]; - - /* Create a zeroed hash object. */ - Hash(); - - /* Check whether two hash are equal. */ - bool operator == (const Hash & h2) const; - - /* Check whether two hash are not equal. */ - bool operator != (const Hash & h2) const; - - /* For sorting. */ - bool operator < (const Hash & h) const; - - /* Convert a hash code into a hexadecimal representation. */ - operator string() const; -}; - - -/* Parse a hexadecimal representation of a hash code. */ -Hash parseHash(const string & s); - -/* Verify that the given string is a valid hash code. */ -bool isHash(const string & s); - -/* Compute the hash of the given string. */ -Hash hashString(const string & s); - -/* Compute the hash of the given file. */ -Hash hashFile(const Path & path); - -/* Compute the hash of the given path. The hash is defined as - md5(dump(path)). -*/ -Hash hashPath(const Path & path); - - -#endif /* !__HASH_H */ diff --git a/src/libmain/shared.cc b/src/libmain/shared.cc new file mode 100644 index 000000000000..80463308a3f0 --- /dev/null +++ b/src/libmain/shared.cc @@ -0,0 +1,79 @@ +#include +#include + +extern "C" { +#include +} + +#include "globals.hh" +#include "shared.hh" + +#include "config.h" + + +/* Initialize and reorder arguments, then call the actual argument + processor. */ +static void initAndRun(int argc, char * * argv) +{ + /* Setup Nix paths. */ + nixStore = NIX_STORE_DIR; + nixDataDir = NIX_DATA_DIR; + nixLogDir = NIX_LOG_DIR; + nixDBPath = (string) NIX_STATE_DIR + "/db"; + + /* Put the arguments in a vector. */ + Strings args; + while (argc--) args.push_back(*argv++); + args.erase(args.begin()); + + /* Expand compound dash options (i.e., `-qlf' -> `-q -l -f'). */ + for (Strings::iterator it = args.begin(); + it != args.end(); ) + { + string arg = *it; + if (arg.length() > 2 && arg[0] == '-' && arg[1] != '-') { + for (unsigned int i = 1; i < arg.length(); i++) + if (isalpha(arg[i])) + args.insert(it, (string) "-" + arg[i]); + else { + args.insert(it, string(arg, i)); + break; + } + it = args.erase(it); + } else it++; + } + + run(args); +} + + +static char buf[1024]; + +int main(int argc, char * * argv) +{ + /* ATerm setup. */ + ATerm bottomOfStack; + ATinit(argc, argv, &bottomOfStack); + + /* Turn on buffering for cerr. */ + cerr.rdbuf()->pubsetbuf(buf, sizeof(buf)); + + try { + initAndRun(argc, argv); + } catch (UsageError & e) { + msg(lvlError, + format( + "error: %1%\n" + "Try `%2% --help' for more information.") + % e.what() % programId); + return 1; + } catch (Error & e) { + msg(lvlError, format("error: %1%") % e.msg()); + return 1; + } catch (exception & e) { + msg(lvlError, format("error: %1%") % e.what()); + return 1; + } + + return 0; +} diff --git a/src/libmain/shared.hh b/src/libmain/shared.hh new file mode 100644 index 000000000000..8ea637fd21ac --- /dev/null +++ b/src/libmain/shared.hh @@ -0,0 +1,15 @@ +#ifndef __SHARED_H +#define __SHARED_H + +#include + +#include "util.hh" + + +void run(Strings args); + + +extern string programId; + + +#endif /* !__SHARED_H */ diff --git a/src/libnix/archive.cc b/src/libnix/archive.cc new file mode 100644 index 000000000000..9039ad7db43e --- /dev/null +++ b/src/libnix/archive.cc @@ -0,0 +1,340 @@ +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "archive.hh" +#include "util.hh" + + +static string archiveVersion1 = "nix-archive-1"; + + +static void writePadding(unsigned int len, DumpSink & sink) +{ + if (len % 8) { + unsigned char zero[8]; + memset(zero, 0, sizeof(zero)); + sink(zero, 8 - (len % 8)); + } +} + + +static void writeInt(unsigned int n, DumpSink & sink) +{ + unsigned char buf[8]; + memset(buf, 0, sizeof(buf)); + buf[0] = n & 0xff; + buf[1] = (n >> 8) & 0xff; + buf[2] = (n >> 16) & 0xff; + buf[3] = (n >> 24) & 0xff; + sink(buf, sizeof(buf)); +} + + +static void writeString(const string & s, DumpSink & sink) +{ + unsigned int len = s.length(); + writeInt(len, sink); + sink((const unsigned char *) s.c_str(), len); + writePadding(len, sink); +} + + +static void dump(const string & path, DumpSink & sink); + + +static void dumpEntries(const Path & path, DumpSink & sink) +{ + DIR * dir = opendir(path.c_str()); + if (!dir) throw SysError("opening directory " + path); + + vector names; + + struct dirent * dirent; + while (errno = 0, dirent = readdir(dir)) { + string name = dirent->d_name; + if (name == "." || name == "..") continue; + names.push_back(name); + } + if (errno) throw SysError("reading directory " + path); + + sort(names.begin(), names.end()); + + for (vector::iterator it = names.begin(); + it != names.end(); it++) + { + writeString("entry", sink); + writeString("(", sink); + writeString("name", sink); + writeString(*it, sink); + writeString("node", sink); + dump(path + "/" + *it, sink); + writeString(")", sink); + } + + closedir(dir); /* !!! close on exception */ +} + + +static void dumpContents(const Path & path, unsigned int size, + DumpSink & sink) +{ + writeString("contents", sink); + writeInt(size, sink); + + int fd = open(path.c_str(), O_RDONLY); + if (fd == -1) throw SysError(format("opening file `%1%'") % path); + + unsigned char buf[65536]; + + unsigned int total = 0; + ssize_t n; + while ((n = read(fd, buf, sizeof(buf)))) { + if (n == -1) throw SysError("reading file " + path); + total += n; + sink(buf, n); + } + + if (total != size) + throw SysError("file changed while reading it: " + path); + + writePadding(size, sink); + + close(fd); /* !!! close on exception */ +} + + +static void dump(const Path & path, DumpSink & sink) +{ + struct stat st; + if (lstat(path.c_str(), &st)) + throw SysError(format("getting attributes of path `%1%'") % path); + + writeString("(", sink); + + if (S_ISREG(st.st_mode)) { + writeString("type", sink); + writeString("regular", sink); + if (st.st_mode & S_IXUSR) { + writeString("executable", sink); + writeString("", sink); + } + dumpContents(path, st.st_size, sink); + } + + else if (S_ISDIR(st.st_mode)) { + writeString("type", sink); + writeString("directory", sink); + dumpEntries(path, sink); + } + + else if (S_ISLNK(st.st_mode)) { + writeString("type", sink); + writeString("symlink", sink); + char buf[st.st_size]; + if (readlink(path.c_str(), buf, st.st_size) != st.st_size) + throw SysError("reading symbolic link " + path); + writeString("target", sink); + writeString(string(buf, st.st_size), sink); + } + + else throw Error("unknown file type: " + path); + + writeString(")", sink); +} + + +void dumpPath(const Path & path, DumpSink & sink) +{ + writeString(archiveVersion1, sink); + dump(path, sink); +} + + +static Error badArchive(string s) +{ + return Error("bad archive: " + s); +} + + +static void readPadding(unsigned int len, RestoreSource & source) +{ + if (len % 8) { + unsigned char zero[8]; + unsigned int n = 8 - (len % 8); + source(zero, n); + for (unsigned int i = 0; i < n; i++) + if (zero[i]) throw badArchive("non-zero padding"); + } +} + +static unsigned int readInt(RestoreSource & source) +{ + unsigned char buf[8]; + source(buf, sizeof(buf)); + if (buf[4] || buf[5] || buf[6] || buf[7]) + throw Error("implementation cannot deal with > 32-bit integers"); + return + buf[0] | + (buf[1] << 8) | + (buf[2] << 16) | + (buf[3] << 24); +} + + +static string readString(RestoreSource & source) +{ + unsigned int len = readInt(source); + char buf[len]; + source((unsigned char *) buf, len); + readPadding(len, source); + return string(buf, len); +} + + +static void skipGeneric(RestoreSource & source) +{ + if (readString(source) == "(") { + while (readString(source) != ")") + skipGeneric(source); + } +} + + +static void restore(const Path & path, RestoreSource & source); + + +static void restoreEntry(const Path & path, RestoreSource & source) +{ + string s, name; + + s = readString(source); + if (s != "(") throw badArchive("expected open tag"); + + while (1) { + s = readString(source); + + if (s == ")") { + break; + } else if (s == "name") { + name = readString(source); + } else if (s == "node") { + if (s == "") throw badArchive("entry name missing"); + restore(path + "/" + name, source); + } else { + throw badArchive("unknown field " + s); + skipGeneric(source); + } + } +} + + +static void restoreContents(int fd, const Path & path, RestoreSource & source) +{ + unsigned int size = readInt(source); + unsigned int left = size; + unsigned char buf[65536]; + + while (left) { + unsigned int n = sizeof(buf); + if (n > left) n = left; + source(buf, n); + if (write(fd, buf, n) != (ssize_t) n) + throw SysError("writing file " + path); + left -= n; + } + + readPadding(size, source); +} + + +static void restore(const Path & path, RestoreSource & source) +{ + string s; + + s = readString(source); + if (s != "(") throw badArchive("expected open tag"); + + enum { tpUnknown, tpRegular, tpDirectory, tpSymlink } type = tpUnknown; + int fd = -1; /* !!! close on exception */ + + while (1) { + s = readString(source); + + if (s == ")") { + break; + } + + else if (s == "type") { + if (type != tpUnknown) + throw badArchive("multiple type fields"); + string t = readString(source); + + if (t == "regular") { + type = tpRegular; + fd = open(path.c_str(), O_CREAT | O_EXCL | O_WRONLY, 0666); + if (fd == -1) + throw SysError("creating file " + path); + } + + else if (t == "directory") { + type = tpDirectory; + if (mkdir(path.c_str(), 0777) == -1) + throw SysError("creating directory " + path); + } + + else if (t == "symlink") { + type = tpSymlink; + } + + else throw badArchive("unknown file type " + t); + + } + + else if (s == "contents" && type == tpRegular) { + restoreContents(fd, path, source); + } + + else if (s == "executable" && type == tpRegular) { + readString(source); + struct stat st; + if (fstat(fd, &st) == -1) + throw SysError("fstat"); + if (fchmod(fd, st.st_mode | (S_IXUSR | S_IXGRP | S_IXOTH)) == -1) + throw SysError("fchmod"); + } + + else if (s == "entry" && type == tpDirectory) { + restoreEntry(path, source); + } + + else if (s == "target" && type == tpSymlink) { + string target = readString(source); + if (symlink(target.c_str(), path.c_str()) == -1) + throw SysError("creating symlink " + path); + } + + else { + throw badArchive("unknown field " + s); + skipGeneric(source); + } + + } + + if (fd != -1) close(fd); +} + + +void restorePath(const Path & path, RestoreSource & source) +{ + if (readString(source) != archiveVersion1) + throw badArchive("expected Nix archive"); + restore(path, source); +} + diff --git a/src/libnix/archive.hh b/src/libnix/archive.hh new file mode 100644 index 000000000000..67e236668a06 --- /dev/null +++ b/src/libnix/archive.hh @@ -0,0 +1,60 @@ +#include + +#include "util.hh" + + +/* dumpPath creates a Nix archive of the specified path. The format + is as follows: + + IF path points to a REGULAR FILE: + dump(path) = attrs( + [ ("type", "regular") + , ("contents", contents(path)) + ]) + + IF path points to a DIRECTORY: + dump(path) = attrs( + [ ("type", "directory") + , ("entries", concat(map(f, sort(entries(path))))) + ]) + where f(fn) = attrs( + [ ("name", fn) + , ("file", dump(path + "/" + fn)) + ]) + + where: + + attrs(as) = concat(map(attr, as)) + encN(0) + attrs((a, b)) = encS(a) + encS(b) + + encS(s) = encN(len(s)) + s + (padding until next 64-bit boundary) + + encN(n) = 64-bit little-endian encoding of n. + + contents(path) = the contents of a regular file. + + sort(strings) = lexicographic sort by 8-bit value (strcmp). + + entries(path) = the entries of a directory, without `.' and + `..'. + + `+' denotes string concatenation. */ + +struct DumpSink +{ + virtual void operator () (const unsigned char * data, unsigned int len) = 0; +}; + +void dumpPath(const Path & path, DumpSink & sink); + + +struct RestoreSource +{ + /* The callee should store exactly *len bytes in the buffer + pointed to by data. It should block if that much data is not + yet available, or throw an error if it is not going to be + available. */ + virtual void operator () (unsigned char * data, unsigned int len) = 0; +}; + +void restorePath(const Path & path, RestoreSource & source); diff --git a/src/libnix/db.cc b/src/libnix/db.cc new file mode 100644 index 000000000000..2f53ca3b5231 --- /dev/null +++ b/src/libnix/db.cc @@ -0,0 +1,425 @@ +#include +#include +#include + +#include + +#include "db.hh" +#include "util.hh" +#include "pathlocks.hh" + + +/* Wrapper class to ensure proper destruction. */ +class DestroyDbc +{ + Dbc * dbc; +public: + DestroyDbc(Dbc * _dbc) : dbc(_dbc) { } + ~DestroyDbc() { dbc->close(); /* close() frees dbc */ } +}; + + +static void rethrow(DbException & e) +{ + throw Error(e.what()); +} + + +Transaction::Transaction() + : txn(0) +{ +} + + +Transaction::Transaction(Database & db) +{ + db.requireEnv(); + try { + db.env->txn_begin(0, &txn, 0); + } catch (DbException e) { rethrow(e); } +} + + +Transaction::~Transaction() +{ + if (txn) abort(); +} + + +void Transaction::commit() +{ + if (!txn) throw Error("commit called on null transaction"); + debug(format("committing transaction %1%") % (void *) txn); + DbTxn * txn2 = txn; + txn = 0; + try { + txn2->commit(0); + } catch (DbException e) { rethrow(e); } +} + + +void Transaction::abort() +{ + if (!txn) throw Error("abort called on null transaction"); + debug(format("aborting transaction %1%") % (void *) txn); + DbTxn * txn2 = txn; + txn = 0; + try { + txn2->abort(); + } catch (DbException e) { rethrow(e); } +} + + +void Transaction::moveTo(Transaction & t) +{ + if (t.txn) throw Error("target txn already exists"); + t.txn = txn; + txn = 0; +} + + +void Database::requireEnv() +{ + if (!env) throw Error("database environment not open"); +} + + +Db * Database::getDb(TableId table) +{ + map::iterator i = tables.find(table); + if (i == tables.end()) + throw Error("unknown table id"); + return i->second; +} + + +Database::Database() + : env(0) + , nextId(1) +{ +} + + +Database::~Database() +{ + close(); +} + + +int getAccessorCount(int fd) +{ + if (lseek(fd, 0, SEEK_SET) == -1) + throw SysError("seeking accessor count"); + char buf[128]; + int len; + if ((len = read(fd, buf, sizeof(buf) - 1)) == -1) + throw SysError("reading accessor count"); + buf[len] = 0; + int count; + if (sscanf(buf, "%d", &count) != 1) { + debug(format("accessor count is invalid: `%1%'") % buf); + return -1; + } + return count; +} + + +void setAccessorCount(int fd, int n) +{ + if (lseek(fd, 0, SEEK_SET) == -1) + throw SysError("seeking accessor count"); + string s = (format("%1%") % n).str(); + const char * s2 = s.c_str(); + if (write(fd, s2, strlen(s2)) != (ssize_t) strlen(s2) || + ftruncate(fd, strlen(s2)) != 0) + throw SysError("writing accessor count"); +} + + +void openEnv(DbEnv * env, const string & path, u_int32_t flags) +{ + env->open(path.c_str(), + DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN | + DB_CREATE | flags, + 0666); +} + + +void Database::open(const string & path) +{ + if (env) throw Error(format("environment already open")); + + try { + + debug(format("opening database environment")); + + + /* Create the database environment object. */ + env = new DbEnv(0); + + env->set_lg_bsize(32 * 1024); /* default */ + env->set_lg_max(256 * 1024); /* must be > 4 * lg_bsize */ + env->set_lk_detect(DB_LOCK_DEFAULT); + env->set_flags(DB_TXN_WRITE_NOSYNC, 1); + + + /* The following code provides automatic recovery of the + database environment. Recovery is necessary when a process + dies while it has the database open. To detect this, + processes atomically increment a counter when the open the + database, and decrement it when they close it. If we see + that counter is > 0 but no processes are accessing the + database---determined by attempting to obtain a write lock + on a lock file on which all accessors have a read lock---we + must run recovery. Note that this also ensures that we + only run recovery when there are no other accessors (which + could cause database corruption). */ + + /* !!! close fdAccessors / fdLock on exception */ + + /* Open the accessor count file. */ + string accessorsPath = path + "/accessor_count"; + fdAccessors = ::open(accessorsPath.c_str(), O_RDWR | O_CREAT, 0666); + if (fdAccessors == -1) + throw SysError(format("opening file `%1%'") % accessorsPath); + + /* Open the lock file. */ + string lockPath = path + "/access_lock"; + fdLock = ::open(lockPath.c_str(), O_RDWR | O_CREAT, 0666); + if (fdLock == -1) + throw SysError(format("opening lock file `%1%'") % lockPath); + + /* Try to acquire a write lock. */ + debug(format("attempting write lock on `%1%'") % lockPath); + if (lockFile(fdLock, ltWrite, false)) { /* don't wait */ + + debug(format("write lock granted")); + + /* We have a write lock, which means that there are no + other readers or writers. */ + + int n = getAccessorCount(fdAccessors); + setAccessorCount(fdAccessors, 1); + + if (n != 0) { + msg(lvlTalkative, format("accessor count is %1%, running recovery") % n); + + /* Open the environment after running recovery. */ + openEnv(env, path, DB_RECOVER); + } + + else + /* Open the environment normally. */ + openEnv(env, path, 0); + + /* Downgrade to a read lock. */ + debug(format("downgrading to read lock on `%1%'") % lockPath); + lockFile(fdLock, ltRead, true); + + } else { + /* There are other accessors. */ + debug(format("write lock refused")); + + /* Acquire a read lock. */ + debug(format("acquiring read lock on `%1%'") % lockPath); + lockFile(fdLock, ltRead, true); /* wait indefinitely */ + + /* Increment the accessor count. */ + lockFile(fdAccessors, ltWrite, true); + int n = getAccessorCount(fdAccessors) + 1; + setAccessorCount(fdAccessors, n); + debug(format("incremented accessor count to %1%") % n); + lockFile(fdAccessors, ltNone, true); + + /* Open the environment normally. */ + openEnv(env, path, 0); + } + + } catch (DbException e) { rethrow(e); } +} + + +void Database::close() +{ + if (!env) return; + + /* Close the database environment. */ + debug(format("closing database environment")); + + try { + + for (map::iterator i = tables.begin(); + i != tables.end(); i++) + { + debug(format("closing table %1%") % i->first); + Db * db = i->second; + db->close(DB_NOSYNC); + delete db; + } + +// env->txn_checkpoint(0, 0, 0); + env->close(0); + + } catch (DbException e) { rethrow(e); } + + delete env; + + /* Decrement the accessor count. */ + lockFile(fdAccessors, ltWrite, true); + int n = getAccessorCount(fdAccessors) - 1; + setAccessorCount(fdAccessors, n); + debug(format("decremented accessor count to %1%") % n); + lockFile(fdAccessors, ltNone, true); + + ::close(fdAccessors); + ::close(fdLock); +} + + +TableId Database::openTable(const string & tableName) +{ + requireEnv(); + TableId table = nextId++; + + try { + + Db * db = new Db(env, 0); + + try { + db->open(0, tableName.c_str(), 0, + DB_HASH, DB_CREATE | DB_AUTO_COMMIT, 0666); + } catch (...) { + delete db; + throw; + } + + tables[table] = db; + + } catch (DbException e) { rethrow(e); } + + return table; +} + + +bool Database::queryString(const Transaction & txn, TableId table, + const string & key, string & data) +{ + try { + Db * db = getDb(table); + + Dbt kt((void *) key.c_str(), key.length()); + Dbt dt; + + int err = db->get(txn.txn, &kt, &dt, 0); + if (err) return false; + + if (!dt.get_data()) + data = ""; + else + data = string((char *) dt.get_data(), dt.get_size()); + + } catch (DbException e) { rethrow(e); } + + return true; +} + + +bool Database::queryStrings(const Transaction & txn, TableId table, + const string & key, Strings & data) +{ + string d; + + if (!queryString(txn, table, key, d)) + return false; + + string::iterator it = d.begin(); + + while (it != d.end()) { + + if (it + 4 > d.end()) + throw Error(format("short db entry: `%1%'") % d); + + unsigned int len; + len = (unsigned char) *it++; + len |= ((unsigned char) *it++) << 8; + len |= ((unsigned char) *it++) << 16; + len |= ((unsigned char) *it++) << 24; + + if (it + len > d.end()) + throw Error(format("short db entry: `%1%'") % d); + + string s; + while (len--) s += *it++; + + data.push_back(s); + } + + return true; +} + + +void Database::setString(const Transaction & txn, TableId table, + const string & key, const string & data) +{ + try { + Db * db = getDb(table); + Dbt kt((void *) key.c_str(), key.length()); + Dbt dt((void *) data.c_str(), data.length()); + db->put(txn.txn, &kt, &dt, 0); + } catch (DbException e) { rethrow(e); } +} + + +void Database::setStrings(const Transaction & txn, TableId table, + const string & key, const Strings & data) +{ + string d; + + for (Strings::const_iterator it = data.begin(); + it != data.end(); it++) + { + string s = *it; + unsigned int len = s.size(); + + d += len & 0xff; + d += (len >> 8) & 0xff; + d += (len >> 16) & 0xff; + d += (len >> 24) & 0xff; + + d += s; + } + + setString(txn, table, key, d); +} + + +void Database::delPair(const Transaction & txn, TableId table, + const string & key) +{ + try { + Db * db = getDb(table); + Dbt kt((void *) key.c_str(), key.length()); + db->del(txn.txn, &kt, 0); + /* Non-existence of a pair with the given key is not an + error. */ + } catch (DbException e) { rethrow(e); } +} + + +void Database::enumTable(const Transaction & txn, TableId table, + Strings & keys) +{ + try { + Db * db = getDb(table); + + Dbc * dbc; + db->cursor(txn.txn, &dbc, 0); + DestroyDbc destroyDbc(dbc); + + Dbt kt, dt; + while (dbc->get(&kt, &dt, DB_NEXT) != DB_NOTFOUND) + keys.push_back( + string((char *) kt.get_data(), kt.get_size())); + + } catch (DbException e) { rethrow(e); } +} diff --git a/src/libnix/db.hh b/src/libnix/db.hh new file mode 100644 index 000000000000..1c681b9b5419 --- /dev/null +++ b/src/libnix/db.hh @@ -0,0 +1,89 @@ +#ifndef __DB_H +#define __DB_H + +#include +#include +#include + +#include + +#include "util.hh" + +using namespace std; + + +class Database; + + +class Transaction +{ + friend class Database; + +private: + DbTxn * txn; + +public: + Transaction(); + Transaction(Database & _db); + ~Transaction(); + + void abort(); + void commit(); + + void moveTo(Transaction & t); +}; + + +#define noTxn Transaction() + + +typedef unsigned int TableId; /* table handles */ + + +class Database +{ + friend class Transaction; + +private: + DbEnv * env; + + int fdLock; + int fdAccessors; + + TableId nextId; + map tables; + + void requireEnv(); + + Db * getDb(TableId table); + +public: + Database(); + ~Database(); + + void open(const string & path); + void close(); + + TableId openTable(const string & table); + + bool queryString(const Transaction & txn, TableId table, + const string & key, string & data); + + bool queryStrings(const Transaction & txn, TableId table, + const string & key, Strings & data); + + void setString(const Transaction & txn, TableId table, + const string & key, const string & data); + + void setStrings(const Transaction & txn, TableId table, + const string & key, const Strings & data); + + void delPair(const Transaction & txn, TableId table, + const string & key); + + void enumTable(const Transaction & txn, TableId table, + Strings & keys); +}; + + +#endif /* !__DB_H */ diff --git a/src/libnix/exec.cc b/src/libnix/exec.cc new file mode 100644 index 000000000000..2e092b2e0dd6 --- /dev/null +++ b/src/libnix/exec.cc @@ -0,0 +1,144 @@ +#include + +#include +#include +#include +#include +#include + +#include "exec.hh" +#include "util.hh" +#include "globals.hh" + + +class AutoDelete +{ + string path; + bool del; +public: + + AutoDelete(const string & p) : path(p) + { + del = true; + } + + ~AutoDelete() + { + if (del) deletePath(path); + } + + void cancel() + { + del = false; + } +}; + + +static string pathNullDevice = "/dev/null"; + + +/* Run a program. */ +void runProgram(const string & program, + const Strings & args, const Environment & env) +{ + /* Create a log file. */ + string logFileName = nixLogDir + "/run.log"; + string logCommand = + verbosity >= lvlDebug + ? "tee -a " + logFileName + " >&2" + : "cat >> " + logFileName; + /* !!! auto-pclose on exit */ + FILE * logFile = popen(logCommand.c_str(), "w"); /* !!! escaping */ + if (!logFile) + throw SysError(format("creating log file `%1%'") % logFileName); + + /* Create a temporary directory where the build will take + place. */ + string tmpDir = createTempDir(); + + AutoDelete delTmpDir(tmpDir); + + /* Fork a child to build the package. */ + pid_t pid; + switch (pid = fork()) { + + case -1: + throw SysError("unable to fork"); + + case 0: + + try { /* child */ + + if (chdir(tmpDir.c_str()) == -1) + throw SysError(format("changing into to `%1%'") % tmpDir); + + /* Fill in the arguments. */ + const char * argArr[args.size() + 2]; + const char * * p = argArr; + string progName = baseNameOf(program); + *p++ = progName.c_str(); + for (Strings::const_iterator i = args.begin(); + i != args.end(); i++) + *p++ = i->c_str(); + *p = 0; + + /* Fill in the environment. */ + Strings envStrs; + const char * envArr[env.size() + 1]; + p = envArr; + for (Environment::const_iterator i = env.begin(); + i != env.end(); i++) + *p++ = envStrs.insert(envStrs.end(), + i->first + "=" + i->second)->c_str(); + *p = 0; + + /* Dup the log handle into stderr. */ + if (dup2(fileno(logFile), STDERR_FILENO) == -1) + throw SysError("cannot pipe standard error into log file"); + + /* Dup stderr to stdin. */ + if (dup2(STDERR_FILENO, STDOUT_FILENO) == -1) + throw SysError("cannot dup stderr into stdout"); + + /* Reroute stdin to /dev/null. */ + int fdDevNull = open(pathNullDevice.c_str(), O_RDWR); + if (fdDevNull == -1) + throw SysError(format("cannot open `%1%'") % pathNullDevice); + if (dup2(fdDevNull, STDIN_FILENO) == -1) + throw SysError("cannot dup null device into stdin"); + + /* Execute the program. This should not return. */ + execve(program.c_str(), (char * *) argArr, (char * *) envArr); + + throw SysError(format("unable to execute %1%") % program); + + } catch (exception & e) { + cerr << format("build error: %1%\n") % e.what(); + } + _exit(1); + + } + + /* parent */ + + /* Close the logging pipe. Note that this should not cause + the logger to exit until builder exits (because the latter + has an open file handle to the former). */ + pclose(logFile); + + /* Wait for the child to finish. */ + int status; + if (waitpid(pid, &status, 0) != pid) + throw Error("unable to wait for child"); + + if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { + if (keepFailed) { + msg(lvlTalkative, + format("build failed; keeping build directory `%1%'") % tmpDir); + delTmpDir.cancel(); + } + throw Error("unable to build package"); + } +} + + diff --git a/src/libnix/exec.hh b/src/libnix/exec.hh new file mode 100644 index 000000000000..8d410e404383 --- /dev/null +++ b/src/libnix/exec.hh @@ -0,0 +1,21 @@ +#ifndef __EXEC_H +#define __EXEC_H + +#include +#include + +#include "util.hh" + +using namespace std; + + +/* A Unix environment is a mapping from strings to strings. */ +typedef map Environment; + + +/* Run a program. */ +void runProgram(const string & program, + const Strings & args, const Environment & env); + + +#endif /* !__EXEC_H */ diff --git a/src/libnix/expr.cc b/src/libnix/expr.cc new file mode 100644 index 000000000000..cead803425ba --- /dev/null +++ b/src/libnix/expr.cc @@ -0,0 +1,222 @@ +#include "expr.hh" +#include "globals.hh" +#include "store.hh" + + +string printTerm(ATerm t) +{ + char * s = ATwriteToString(t); + return s; +} + + +Error badTerm(const format & f, ATerm t) +{ + return Error(format("%1%, in `%2%'") % f.str() % printTerm(t)); +} + + +Hash hashTerm(ATerm t) +{ + return hashString(printTerm(t)); +} + + +Path writeTerm(ATerm t, const string & suffix) +{ + /* The id of a term is its hash. */ + Hash h = hashTerm(t); + + Path path = canonPath(nixStore + "/" + + (string) h + suffix + ".nix"); + + if (!isValidPath(path)) { + char * s = ATwriteToString(t); + if (!s) throw Error(format("cannot write aterm to `%1%'") % path); + addTextToStore(path, string(s)); + } + + return path; +} + + +static void parsePaths(ATermList paths, PathSet & out) +{ + while (!ATisEmpty(paths)) { + char * s; + ATerm t = ATgetFirst(paths); + if (!ATmatch(t, "", &s)) + throw badTerm("not a path", t); + out.insert(s); + paths = ATgetNext(paths); + } +} + + +static void checkClosure(const Closure & closure) +{ + if (closure.elems.size() == 0) + throw Error("empty closure"); + + PathSet decl; + for (ClosureElems::const_iterator i = closure.elems.begin(); + i != closure.elems.end(); i++) + decl.insert(i->first); + + for (PathSet::const_iterator i = closure.roots.begin(); + i != closure.roots.end(); i++) + if (decl.find(*i) == decl.end()) + throw Error(format("undefined root path `%1%'") % *i); + + for (ClosureElems::const_iterator i = closure.elems.begin(); + i != closure.elems.end(); i++) + for (PathSet::const_iterator j = i->second.refs.begin(); + j != i->second.refs.end(); j++) + if (decl.find(*j) == decl.end()) + throw Error( + format("undefined path `%1%' referenced by `%2%'") + % *j % i->first); +} + + +/* Parse a closure. */ +static bool parseClosure(ATerm t, Closure & closure) +{ + ATermList roots, elems; + + if (!ATmatch(t, "Closure([], [])", &roots, &elems)) + return false; + + parsePaths(roots, closure.roots); + + while (!ATisEmpty(elems)) { + char * s1; + ATermList refs; + ATerm t = ATgetFirst(elems); + if (!ATmatch(t, "(, [])", &s1, &refs)) + throw badTerm("not a closure element", t); + ClosureElem elem; + parsePaths(refs, elem.refs); + closure.elems[s1] = elem; + elems = ATgetNext(elems); + } + + checkClosure(closure); + return true; +} + + +static bool parseDerivation(ATerm t, Derivation & derivation) +{ + ATermList outs, ins, args, bnds; + char * builder; + char * platform; + + if (!ATmatch(t, "Derive([], [], , , [], [])", + &outs, &ins, &platform, &builder, &args, &bnds)) + { + /* !!! compatibility -> remove eventually */ + if (!ATmatch(t, "Derive([], [], , , [])", + &outs, &ins, &builder, &platform, &bnds)) + return false; + args = ATempty; + } + + parsePaths(outs, derivation.outputs); + parsePaths(ins, derivation.inputs); + + derivation.builder = builder; + derivation.platform = platform; + + while (!ATisEmpty(args)) { + char * s; + ATerm arg = ATgetFirst(args); + if (!ATmatch(arg, "", &s)) + throw badTerm("string expected", arg); + derivation.args.push_back(s); + args = ATgetNext(args); + } + + while (!ATisEmpty(bnds)) { + char * s1, * s2; + ATerm bnd = ATgetFirst(bnds); + if (!ATmatch(bnd, "(, )", &s1, &s2)) + throw badTerm("tuple of strings expected", bnd); + derivation.env[s1] = s2; + bnds = ATgetNext(bnds); + } + + return true; +} + + +NixExpr parseNixExpr(ATerm t) +{ + NixExpr ne; + if (parseClosure(t, ne.closure)) + ne.type = NixExpr::neClosure; + else if (parseDerivation(t, ne.derivation)) + ne.type = NixExpr::neDerivation; + else throw badTerm("not a Nix expression", t); + return ne; +} + + +static ATermList unparsePaths(const PathSet & paths) +{ + ATermList l = ATempty; + for (PathSet::const_iterator i = paths.begin(); + i != paths.end(); i++) + l = ATinsert(l, ATmake("", i->c_str())); + return ATreverse(l); +} + + +static ATerm unparseClosure(const Closure & closure) +{ + ATermList roots = unparsePaths(closure.roots); + + ATermList elems = ATempty; + for (ClosureElems::const_iterator i = closure.elems.begin(); + i != closure.elems.end(); i++) + elems = ATinsert(elems, + ATmake("(, )", + i->first.c_str(), + unparsePaths(i->second.refs))); + + return ATmake("Closure(, )", roots, elems); +} + + +static ATerm unparseDerivation(const Derivation & derivation) +{ + ATermList args = ATempty; + for (Strings::const_iterator i = derivation.args.begin(); + i != derivation.args.end(); i++) + args = ATinsert(args, ATmake("", i->c_str())); + + ATermList env = ATempty; + for (StringPairs::const_iterator i = derivation.env.begin(); + i != derivation.env.end(); i++) + env = ATinsert(env, + ATmake("(, )", + i->first.c_str(), i->second.c_str())); + + return ATmake("Derive(, , , , , )", + unparsePaths(derivation.outputs), + unparsePaths(derivation.inputs), + derivation.platform.c_str(), + derivation.builder.c_str(), + ATreverse(args), + ATreverse(env)); +} + + +ATerm unparseNixExpr(const NixExpr & ne) +{ + if (ne.type == NixExpr::neClosure) + return unparseClosure(ne.closure); + else if (ne.type == NixExpr::neDerivation) + return unparseDerivation(ne.derivation); + else abort(); +} diff --git a/src/libnix/expr.hh b/src/libnix/expr.hh new file mode 100644 index 000000000000..7d0420935f9d --- /dev/null +++ b/src/libnix/expr.hh @@ -0,0 +1,66 @@ +#ifndef __FSTATE_H +#define __FSTATE_H + +extern "C" { +#include +} + +#include "store.hh" + + +/* Abstract syntax of Nix expressions. */ + +struct ClosureElem +{ + PathSet refs; +}; + +typedef map ClosureElems; + +struct Closure +{ + PathSet roots; + ClosureElems elems; +}; + +typedef map StringPairs; + +struct Derivation +{ + PathSet outputs; + PathSet inputs; /* Nix expressions, not actual inputs */ + string platform; + Path builder; + Strings args; + StringPairs env; +}; + +struct NixExpr +{ + enum { neClosure, neDerivation } type; + Closure closure; + Derivation derivation; +}; + + +/* Return a canonical textual representation of an expression. */ +string printTerm(ATerm t); + +/* Throw an exception with an error message containing the given + aterm. */ +Error badTerm(const format & f, ATerm t); + +/* Hash an aterm. */ +Hash hashTerm(ATerm t); + +/* Write an aterm to the Nix store directory, and return its path. */ +Path writeTerm(ATerm t, const string & suffix); + +/* Parse a Nix expression. */ +NixExpr parseNixExpr(ATerm t); + +/* Parse a Nix expression. */ +ATerm unparseNixExpr(const NixExpr & ne); + + +#endif /* !__FSTATE_H */ diff --git a/src/libnix/globals.cc b/src/libnix/globals.cc new file mode 100644 index 000000000000..a292b49aeae0 --- /dev/null +++ b/src/libnix/globals.cc @@ -0,0 +1,8 @@ +#include "globals.hh" + +string nixStore = "/UNINIT"; +string nixDataDir = "/UNINIT"; +string nixLogDir = "/UNINIT"; +string nixDBPath = "/UNINIT"; + +bool keepFailed = false; diff --git a/src/libnix/globals.hh b/src/libnix/globals.hh new file mode 100644 index 000000000000..1b4d0bde3ffe --- /dev/null +++ b/src/libnix/globals.hh @@ -0,0 +1,29 @@ +#ifndef __GLOBALS_H +#define __GLOBALS_H + +#include + +using namespace std; + +/* Path names. */ + +/* nixStore is the directory where we generally store atomic and + derived files. */ +extern string nixStore; + +extern string nixDataDir; /* !!! fix */ + +/* nixLogDir is the directory where we log various operations. */ +extern string nixLogDir; + +/* nixDBPath is the path name of our Berkeley DB environment. */ +extern string nixDBPath; + + +/* Misc. global flags. */ + +/* Whether to keep temporary directories of failed builds. */ +extern bool keepFailed; + + +#endif /* !__GLOBALS_H */ diff --git a/src/libnix/hash.cc b/src/libnix/hash.cc new file mode 100644 index 000000000000..752b269125cb --- /dev/null +++ b/src/libnix/hash.cc @@ -0,0 +1,124 @@ +#include + +extern "C" { +#include "md5.h" +} + +#include "hash.hh" +#include "archive.hh" + + +Hash::Hash() +{ + memset(hash, 0, sizeof(hash)); +} + + +bool Hash::operator == (const Hash & h2) const +{ + for (unsigned int i = 0; i < hashSize; i++) + if (hash[i] != h2.hash[i]) return false; + return true; +} + + +bool Hash::operator != (const Hash & h2) const +{ + return !(*this == h2); +} + + +bool Hash::operator < (const Hash & h) const +{ + for (unsigned int i = 0; i < hashSize; i++) { + if (hash[i] < h.hash[i]) return true; + if (hash[i] > h.hash[i]) return false; + } + return false; +} + + +Hash::operator string() const +{ + ostringstream str; + for (unsigned int i = 0; i < hashSize; i++) { + str.fill('0'); + str.width(2); + str << hex << (int) hash[i]; + } + return str.str(); +} + + +Hash parseHash(const string & s) +{ + Hash hash; + if (s.length() != Hash::hashSize * 2) + throw Error(format("invalid hash `%1%'") % s); + for (unsigned int i = 0; i < Hash::hashSize; i++) { + string s2(s, i * 2, 2); + if (!isxdigit(s2[0]) || !isxdigit(s2[1])) + throw Error(format("invalid hash `%1%'") % s); + istringstream str(s2); + int n; + str >> hex >> n; + hash.hash[i] = n; + } + return hash; +} + + +bool isHash(const string & s) +{ + if (s.length() != 32) return false; + for (int i = 0; i < 32; i++) { + char c = s[i]; + if (!((c >= '0' && c <= '9') || + (c >= 'a' && c <= 'f'))) + return false; + } + return true; +} + + +Hash hashString(const string & s) +{ + Hash hash; + md5_buffer(s.c_str(), s.length(), hash.hash); + return hash; +} + + +Hash hashFile(const Path & path) +{ + Hash hash; + FILE * file = fopen(path.c_str(), "rb"); + if (!file) + throw SysError(format("file `%1%' does not exist") % path); + int err = md5_stream(file, hash.hash); + fclose(file); + if (err) throw SysError(format("cannot hash file `%1%'") % path); + return hash; +} + + +struct HashSink : DumpSink +{ + struct md5_ctx ctx; + virtual void operator () + (const unsigned char * data, unsigned int len) + { + md5_process_bytes(data, len, &ctx); + } +}; + + +Hash hashPath(const Path & path) +{ + Hash hash; + HashSink sink; + md5_init_ctx(&sink.ctx); + dumpPath(path, sink); + md5_finish_ctx(&sink.ctx, hash.hash); + return hash; +} diff --git a/src/libnix/hash.hh b/src/libnix/hash.hh new file mode 100644 index 000000000000..0062f987c021 --- /dev/null +++ b/src/libnix/hash.hh @@ -0,0 +1,51 @@ +#ifndef __HASH_H +#define __HASH_H + +#include + +#include "util.hh" + +using namespace std; + + +struct Hash +{ + static const unsigned int hashSize = 16; + unsigned char hash[hashSize]; + + /* Create a zeroed hash object. */ + Hash(); + + /* Check whether two hash are equal. */ + bool operator == (const Hash & h2) const; + + /* Check whether two hash are not equal. */ + bool operator != (const Hash & h2) const; + + /* For sorting. */ + bool operator < (const Hash & h) const; + + /* Convert a hash code into a hexadecimal representation. */ + operator string() const; +}; + + +/* Parse a hexadecimal representation of a hash code. */ +Hash parseHash(const string & s); + +/* Verify that the given string is a valid hash code. */ +bool isHash(const string & s); + +/* Compute the hash of the given string. */ +Hash hashString(const string & s); + +/* Compute the hash of the given file. */ +Hash hashFile(const Path & path); + +/* Compute the hash of the given path. The hash is defined as + md5(dump(path)). +*/ +Hash hashPath(const Path & path); + + +#endif /* !__HASH_H */ diff --git a/src/libnix/md5.c b/src/libnix/md5.c new file mode 100644 index 000000000000..fa67ebfcdbdf --- /dev/null +++ b/src/libnix/md5.c @@ -0,0 +1,448 @@ +/* Functions to compute MD5 message digest of files or memory blocks. + according to the definition of MD5 in RFC 1321 from April 1992. + Copyright (C) 1995,1996,1997,1999,2000,2001 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, write to the Free + Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + 02111-1307 USA. */ + +/* Written by Ulrich Drepper , 1995. */ + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include + +#include +#include + +#include "md5.h" + + +static md5_uint32 SWAP(md5_uint32 n) +{ + static int checked = 0; + static int bigendian = 0; + static md5_uint32 test; + + if (!checked) { + test = 1; + if (* (char *) &test == 0) + bigendian = 1; + checked = 1; + } + + if (bigendian) + return (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24)); + else + return n; +} + + +/* This array contains the bytes used to pad the buffer to the next + 64-byte boundary. (RFC 1321, 3.1: Step 1) */ +static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ }; + + +/* Initialize structure containing state of computation. + (RFC 1321, 3.3: Step 3) */ +void +md5_init_ctx (ctx) + struct md5_ctx *ctx; +{ + ctx->A = 0x67452301; + ctx->B = 0xefcdab89; + ctx->C = 0x98badcfe; + ctx->D = 0x10325476; + + ctx->total[0] = ctx->total[1] = 0; + ctx->buflen = 0; +} + +/* Put result from CTX in first 16 bytes following RESBUF. The result + must be in little endian byte order. + + IMPORTANT: On some systems it is required that RESBUF is correctly + aligned for a 32 bits value. */ +void * +md5_read_ctx (ctx, resbuf) + const struct md5_ctx *ctx; + void *resbuf; +{ + ((md5_uint32 *) resbuf)[0] = SWAP (ctx->A); + ((md5_uint32 *) resbuf)[1] = SWAP (ctx->B); + ((md5_uint32 *) resbuf)[2] = SWAP (ctx->C); + ((md5_uint32 *) resbuf)[3] = SWAP (ctx->D); + + return resbuf; +} + +/* Process the remaining bytes in the internal buffer and the usual + prolog according to the standard and write the result to RESBUF. + + IMPORTANT: On some systems it is required that RESBUF is correctly + aligned for a 32 bits value. */ +void * +md5_finish_ctx (ctx, resbuf) + struct md5_ctx *ctx; + void *resbuf; +{ + /* Take yet unprocessed bytes into account. */ + md5_uint32 bytes = ctx->buflen; + size_t pad; + + /* Now count remaining bytes. */ + ctx->total[0] += bytes; + if (ctx->total[0] < bytes) + ++ctx->total[1]; + + pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes; + memcpy (&ctx->buffer[bytes], fillbuf, pad); + + /* Put the 64-bit file length in *bits* at the end of the buffer. */ + *(md5_uint32 *) &ctx->buffer[bytes + pad] = SWAP (ctx->total[0] << 3); + *(md5_uint32 *) &ctx->buffer[bytes + pad + 4] = SWAP ((ctx->total[1] << 3) | + (ctx->total[0] >> 29)); + + /* Process last bytes. */ + md5_process_block (ctx->buffer, bytes + pad + 8, ctx); + + return md5_read_ctx (ctx, resbuf); +} + +/* Compute MD5 message digest for bytes read from STREAM. The + resulting message digest number will be written into the 16 bytes + beginning at RESBLOCK. */ +int +md5_stream (stream, resblock) + FILE *stream; + void *resblock; +{ + /* Important: BLOCKSIZE must be a multiple of 64. */ +#define BLOCKSIZE 4096 + struct md5_ctx ctx; + char buffer[BLOCKSIZE + 72]; + size_t sum; + + /* Initialize the computation context. */ + md5_init_ctx (&ctx); + + /* Iterate over full file contents. */ + while (1) + { + /* We read the file in blocks of BLOCKSIZE bytes. One call of the + computation function processes the whole buffer so that with the + next round of the loop another block can be read. */ + size_t n; + sum = 0; + + /* Read block. Take care for partial reads. */ + do + { + n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream); + + sum += n; + } + while (sum < BLOCKSIZE && n != 0); + if (n == 0 && ferror (stream)) + return 1; + + /* If end of file is reached, end the loop. */ + if (n == 0) + break; + + /* Process buffer with BLOCKSIZE bytes. Note that + BLOCKSIZE % 64 == 0 + */ + md5_process_block (buffer, BLOCKSIZE, &ctx); + } + + /* Add the last bytes if necessary. */ + if (sum > 0) + md5_process_bytes (buffer, sum, &ctx); + + /* Construct result in desired memory. */ + md5_finish_ctx (&ctx, resblock); + return 0; +} + +/* Compute MD5 message digest for LEN bytes beginning at BUFFER. The + result is always in little endian byte order, so that a byte-wise + output yields to the wanted ASCII representation of the message + digest. */ +void * +md5_buffer (buffer, len, resblock) + const char *buffer; + size_t len; + void *resblock; +{ + struct md5_ctx ctx; + + /* Initialize the computation context. */ + md5_init_ctx (&ctx); + + /* Process whole buffer but last len % 64 bytes. */ + md5_process_bytes (buffer, len, &ctx); + + /* Put result in desired memory area. */ + return md5_finish_ctx (&ctx, resblock); +} + + +void +md5_process_bytes (buffer, len, ctx) + const void *buffer; + size_t len; + struct md5_ctx *ctx; +{ + /* When we already have some bits in our internal buffer concatenate + both inputs first. */ + if (ctx->buflen != 0) + { + size_t left_over = ctx->buflen; + size_t add = 128 - left_over > len ? len : 128 - left_over; + + memcpy (&ctx->buffer[left_over], buffer, add); + ctx->buflen += add; + + if (ctx->buflen > 64) + { + md5_process_block (ctx->buffer, ctx->buflen & ~63, ctx); + + ctx->buflen &= 63; + /* The regions in the following copy operation cannot overlap. */ + memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~63], + ctx->buflen); + } + + buffer = (const char *) buffer + add; + len -= add; + } + + /* Process available complete blocks. */ + if (len >= 64) + { +#if !_STRING_ARCH_unaligned +/* To check alignment gcc has an appropriate operator. Other + compilers don't. */ +# if __GNUC__ >= 2 +# define UNALIGNED_P(p) (((md5_uintptr) p) % __alignof__ (md5_uint32) != 0) +# else +# define UNALIGNED_P(p) (((md5_uintptr) p) % sizeof (md5_uint32) != 0) +# endif + if (UNALIGNED_P (buffer)) + while (len > 64) + { + md5_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx); + buffer = (const char *) buffer + 64; + len -= 64; + } + else +#endif + { + md5_process_block (buffer, len & ~63, ctx); + buffer = (const char *) buffer + (len & ~63); + len &= 63; + } + } + + /* Move remaining bytes in internal buffer. */ + if (len > 0) + { + size_t left_over = ctx->buflen; + + memcpy (&ctx->buffer[left_over], buffer, len); + left_over += len; + if (left_over >= 64) + { + md5_process_block (ctx->buffer, 64, ctx); + left_over -= 64; + memcpy (ctx->buffer, &ctx->buffer[64], left_over); + } + ctx->buflen = left_over; + } +} + + +/* These are the four functions used in the four steps of the MD5 algorithm + and defined in the RFC 1321. The first function is a little bit optimized + (as found in Colin Plumbs public domain implementation). */ +/* #define FF(b, c, d) ((b & c) | (~b & d)) */ +#define FF(b, c, d) (d ^ (b & (c ^ d))) +#define FG(b, c, d) FF (d, b, c) +#define FH(b, c, d) (b ^ c ^ d) +#define FI(b, c, d) (c ^ (b | ~d)) + +/* Process LEN bytes of BUFFER, accumulating context into CTX. + It is assumed that LEN % 64 == 0. */ + +void +md5_process_block (buffer, len, ctx) + const void *buffer; + size_t len; + struct md5_ctx *ctx; +{ + md5_uint32 correct_words[16]; + const md5_uint32 *words = buffer; + size_t nwords = len / sizeof (md5_uint32); + const md5_uint32 *endp = words + nwords; + md5_uint32 A = ctx->A; + md5_uint32 B = ctx->B; + md5_uint32 C = ctx->C; + md5_uint32 D = ctx->D; + + /* First increment the byte count. RFC 1321 specifies the possible + length of the file up to 2^64 bits. Here we only compute the + number of bytes. Do a double word increment. */ + ctx->total[0] += len; + if (ctx->total[0] < len) + ++ctx->total[1]; + + /* Process all bytes in the buffer with 64 bytes in each round of + the loop. */ + while (words < endp) + { + md5_uint32 *cwp = correct_words; + md5_uint32 A_save = A; + md5_uint32 B_save = B; + md5_uint32 C_save = C; + md5_uint32 D_save = D; + + /* First round: using the given function, the context and a constant + the next context is computed. Because the algorithms processing + unit is a 32-bit word and it is determined to work on words in + little endian byte order we perhaps have to change the byte order + before the computation. To reduce the work for the next steps + we store the swapped words in the array CORRECT_WORDS. */ + +#define OP(a, b, c, d, s, T) \ + do \ + { \ + a += FF (b, c, d) + (*cwp++ = SWAP (*words)) + T; \ + ++words; \ + CYCLIC (a, s); \ + a += b; \ + } \ + while (0) + + /* It is unfortunate that C does not provide an operator for + cyclic rotation. Hope the C compiler is smart enough. */ +#define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s))) + + /* Before we start, one word to the strange constants. + They are defined in RFC 1321 as + + T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64 + */ + + /* Round 1. */ + OP (A, B, C, D, 7, 0xd76aa478); + OP (D, A, B, C, 12, 0xe8c7b756); + OP (C, D, A, B, 17, 0x242070db); + OP (B, C, D, A, 22, 0xc1bdceee); + OP (A, B, C, D, 7, 0xf57c0faf); + OP (D, A, B, C, 12, 0x4787c62a); + OP (C, D, A, B, 17, 0xa8304613); + OP (B, C, D, A, 22, 0xfd469501); + OP (A, B, C, D, 7, 0x698098d8); + OP (D, A, B, C, 12, 0x8b44f7af); + OP (C, D, A, B, 17, 0xffff5bb1); + OP (B, C, D, A, 22, 0x895cd7be); + OP (A, B, C, D, 7, 0x6b901122); + OP (D, A, B, C, 12, 0xfd987193); + OP (C, D, A, B, 17, 0xa679438e); + OP (B, C, D, A, 22, 0x49b40821); + + /* For the second to fourth round we have the possibly swapped words + in CORRECT_WORDS. Redefine the macro to take an additional first + argument specifying the function to use. */ +#undef OP +#define OP(f, a, b, c, d, k, s, T) \ + do \ + { \ + a += f (b, c, d) + correct_words[k] + T; \ + CYCLIC (a, s); \ + a += b; \ + } \ + while (0) + + /* Round 2. */ + OP (FG, A, B, C, D, 1, 5, 0xf61e2562); + OP (FG, D, A, B, C, 6, 9, 0xc040b340); + OP (FG, C, D, A, B, 11, 14, 0x265e5a51); + OP (FG, B, C, D, A, 0, 20, 0xe9b6c7aa); + OP (FG, A, B, C, D, 5, 5, 0xd62f105d); + OP (FG, D, A, B, C, 10, 9, 0x02441453); + OP (FG, C, D, A, B, 15, 14, 0xd8a1e681); + OP (FG, B, C, D, A, 4, 20, 0xe7d3fbc8); + OP (FG, A, B, C, D, 9, 5, 0x21e1cde6); + OP (FG, D, A, B, C, 14, 9, 0xc33707d6); + OP (FG, C, D, A, B, 3, 14, 0xf4d50d87); + OP (FG, B, C, D, A, 8, 20, 0x455a14ed); + OP (FG, A, B, C, D, 13, 5, 0xa9e3e905); + OP (FG, D, A, B, C, 2, 9, 0xfcefa3f8); + OP (FG, C, D, A, B, 7, 14, 0x676f02d9); + OP (FG, B, C, D, A, 12, 20, 0x8d2a4c8a); + + /* Round 3. */ + OP (FH, A, B, C, D, 5, 4, 0xfffa3942); + OP (FH, D, A, B, C, 8, 11, 0x8771f681); + OP (FH, C, D, A, B, 11, 16, 0x6d9d6122); + OP (FH, B, C, D, A, 14, 23, 0xfde5380c); + OP (FH, A, B, C, D, 1, 4, 0xa4beea44); + OP (FH, D, A, B, C, 4, 11, 0x4bdecfa9); + OP (FH, C, D, A, B, 7, 16, 0xf6bb4b60); + OP (FH, B, C, D, A, 10, 23, 0xbebfbc70); + OP (FH, A, B, C, D, 13, 4, 0x289b7ec6); + OP (FH, D, A, B, C, 0, 11, 0xeaa127fa); + OP (FH, C, D, A, B, 3, 16, 0xd4ef3085); + OP (FH, B, C, D, A, 6, 23, 0x04881d05); + OP (FH, A, B, C, D, 9, 4, 0xd9d4d039); + OP (FH, D, A, B, C, 12, 11, 0xe6db99e5); + OP (FH, C, D, A, B, 15, 16, 0x1fa27cf8); + OP (FH, B, C, D, A, 2, 23, 0xc4ac5665); + + /* Round 4. */ + OP (FI, A, B, C, D, 0, 6, 0xf4292244); + OP (FI, D, A, B, C, 7, 10, 0x432aff97); + OP (FI, C, D, A, B, 14, 15, 0xab9423a7); + OP (FI, B, C, D, A, 5, 21, 0xfc93a039); + OP (FI, A, B, C, D, 12, 6, 0x655b59c3); + OP (FI, D, A, B, C, 3, 10, 0x8f0ccc92); + OP (FI, C, D, A, B, 10, 15, 0xffeff47d); + OP (FI, B, C, D, A, 1, 21, 0x85845dd1); + OP (FI, A, B, C, D, 8, 6, 0x6fa87e4f); + OP (FI, D, A, B, C, 15, 10, 0xfe2ce6e0); + OP (FI, C, D, A, B, 6, 15, 0xa3014314); + OP (FI, B, C, D, A, 13, 21, 0x4e0811a1); + OP (FI, A, B, C, D, 4, 6, 0xf7537e82); + OP (FI, D, A, B, C, 11, 10, 0xbd3af235); + OP (FI, C, D, A, B, 2, 15, 0x2ad7d2bb); + OP (FI, B, C, D, A, 9, 21, 0xeb86d391); + + /* Add the starting values of the context. */ + A += A_save; + B += B_save; + C += C_save; + D += D_save; + } + + /* Put checksum in context given as argument. */ + ctx->A = A; + ctx->B = B; + ctx->C = C; + ctx->D = D; +} diff --git a/src/libnix/md5.h b/src/libnix/md5.h new file mode 100644 index 000000000000..6301e4558ca1 --- /dev/null +++ b/src/libnix/md5.h @@ -0,0 +1,151 @@ +/* Declaration of functions and data types used for MD5 sum computing + library functions. + Copyright (C) 1995,1996,1997,1999,2000,2001 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, write to the Free + Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + 02111-1307 USA. */ + +#ifndef _MD5_H +#define _MD5_H 1 + +#include + +#if defined HAVE_LIMITS_H || _LIBC +# include +#endif + +/* The following contortions are an attempt to use the C preprocessor + to determine an unsigned integral type that is 32 bits wide. An + alternative approach is to use autoconf's AC_CHECK_SIZEOF macro, but + doing that would require that the configure script compile and *run* + the resulting executable. Locally running cross-compiled executables + is usually not possible. */ + +#ifdef _LIBC +# include +typedef uint32_t md5_uint32; +typedef uintptr_t md5_uintptr; +#else +# if defined __STDC__ && __STDC__ +# define UINT_MAX_32_BITS 4294967295U +# else +# define UINT_MAX_32_BITS 0xFFFFFFFF +# endif + +/* If UINT_MAX isn't defined, assume it's a 32-bit type. + This should be valid for all systems GNU cares about because + that doesn't include 16-bit systems, and only modern systems + (that certainly have ) have 64+-bit integral types. */ + +# ifndef UINT_MAX +# define UINT_MAX UINT_MAX_32_BITS +# endif + +# if UINT_MAX == UINT_MAX_32_BITS + typedef unsigned int md5_uint32; +# else +# if USHRT_MAX == UINT_MAX_32_BITS + typedef unsigned short md5_uint32; +# else +# if ULONG_MAX == UINT_MAX_32_BITS + typedef unsigned long md5_uint32; +# else + /* The following line is intended to evoke an error. + Using #error is not portable enough. */ + "Cannot determine unsigned 32-bit data type." +# endif +# endif +# endif +/* We have to make a guess about the integer type equivalent in size + to pointers which should always be correct. */ +typedef unsigned long int md5_uintptr; +#endif + +#undef __P +#if defined (__STDC__) && __STDC__ +# define __P(x) x +#else +# define __P(x) () +#endif + +/* Structure to save state of computation between the single steps. */ +struct md5_ctx +{ + md5_uint32 A; + md5_uint32 B; + md5_uint32 C; + md5_uint32 D; + + md5_uint32 total[2]; + md5_uint32 buflen; + char buffer[128] __attribute__ ((__aligned__ (__alignof__ (md5_uint32)))); +}; + +/* + * The following three functions are build up the low level used in + * the functions `md5_stream' and `md5_buffer'. + */ + +/* Initialize structure containing state of computation. + (RFC 1321, 3.3: Step 3) */ +extern void md5_init_ctx __P ((struct md5_ctx *ctx)); + +/* Starting with the result of former calls of this function (or the + initialization function update the context for the next LEN bytes + starting at BUFFER. + It is necessary that LEN is a multiple of 64!!! */ +extern void md5_process_block __P ((const void *buffer, size_t len, + struct md5_ctx *ctx)); + +/* Starting with the result of former calls of this function (or the + initialization function update the context for the next LEN bytes + starting at BUFFER. + It is NOT required that LEN is a multiple of 64. */ +extern void md5_process_bytes __P ((const void *buffer, size_t len, + struct md5_ctx *ctx)); + +/* Process the remaining bytes in the buffer and put result from CTX + in first 16 bytes following RESBUF. The result is always in little + endian byte order, so that a byte-wise output yields to the wanted + ASCII representation of the message digest. + + IMPORTANT: On some systems it is required that RESBUF is correctly + aligned for a 32 bits value. */ +extern void *md5_finish_ctx __P ((struct md5_ctx *ctx, void *resbuf)); + + +/* Put result from CTX in first 16 bytes following RESBUF. The result is + always in little endian byte order, so that a byte-wise output yields + to the wanted ASCII representation of the message digest. + + IMPORTANT: On some systems it is required that RESBUF is correctly + aligned for a 32 bits value. */ +extern void *md5_read_ctx __P ((const struct md5_ctx *ctx, void *resbuf)); + + +/* Compute MD5 message digest for bytes read from STREAM. The + resulting message digest number will be written into the 16 bytes + beginning at RESBLOCK. */ +extern int md5_stream __P ((FILE *stream, void *resblock)); + +/* Compute MD5 message digest for LEN bytes beginning at BUFFER. The + result is always in little endian byte order, so that a byte-wise + output yields to the wanted ASCII representation of the message + digest. */ +extern void *md5_buffer __P ((const char *buffer, size_t len, + void *resblock)); + +#endif /* md5.h */ diff --git a/src/libnix/normalise.cc b/src/libnix/normalise.cc new file mode 100644 index 000000000000..be71081ffb25 --- /dev/null +++ b/src/libnix/normalise.cc @@ -0,0 +1,469 @@ +#include + +#include "normalise.hh" +#include "references.hh" +#include "exec.hh" +#include "pathlocks.hh" +#include "globals.hh" + + +static Path useSuccessor(const Path & path) +{ + string pathSucc; + if (querySuccessor(path, pathSucc)) { + debug(format("successor %1% -> %2%") % (string) path % pathSucc); + return pathSucc; + } else + return path; +} + + +#if 0 +/* Return a path whose contents have the given hash. If target is + not empty, ensure that such a path is realised in target (if + necessary by copying from another location). If prefix is not + empty, only return a path that is an descendent of prefix. */ + +string expandId(const FSId & id, const string & target = "", + const string & prefix = "/", FSIdSet pending = FSIdSet(), + bool ignoreSubstitutes = false) +{ + xxx +} + + +string expandId(const FSId & id, const string & target, + const string & prefix, FSIdSet pending, bool ignoreSubstitutes) +{ + Nest nest(lvlDebug, format("expanding %1%") % (string) id); + + Strings paths; + + if (!target.empty() && !isInPrefix(target, prefix)) + abort(); + + nixDB.queryStrings(noTxn, dbId2Paths, id, paths); + + /* Pick one equal to `target'. */ + if (!target.empty()) { + + for (Strings::iterator i = paths.begin(); + i != paths.end(); i++) + { + string path = *i; + if (path == target && pathExists(path)) + return path; + } + + } + + /* Arbitrarily pick the first one that exists and isn't stale. */ + for (Strings::iterator it = paths.begin(); + it != paths.end(); it++) + { + string path = *it; + if (isInPrefix(path, prefix) && pathExists(path)) { + if (target.empty()) + return path; + else { + /* Acquire a lock on the target path. */ + Strings lockPaths; + lockPaths.push_back(target); + PathLocks outputLock(lockPaths); + + /* Copy. */ + copyPath(path, target); + + /* Register the target path. */ + Transaction txn(nixDB); + registerPath(txn, target, id); + txn.commit(); + + return target; + } + } + } + + if (!ignoreSubstitutes) { + + if (pending.find(id) != pending.end()) + throw Error(format("id %1% already being expanded") % (string) id); + pending.insert(id); + + /* Try to realise the substitutes, but only if this id is not + already being realised by a substitute. */ + Strings subs; + nixDB.queryStrings(noTxn, dbSubstitutes, id, subs); /* non-existence = ok */ + + for (Strings::iterator it = subs.begin(); it != subs.end(); it++) { + FSId subId = parseHash(*it); + + debug(format("trying substitute %1%") % (string) subId); + + realiseClosure(normaliseNixExpr(subId, pending), pending); + + return expandId(id, target, prefix, pending); + } + + } + + throw Error(format("cannot expand id `%1%'") % (string) id); +} +#endif + + +Path normaliseNixExpr(const Path & _nePath, PathSet pending) +{ + Nest nest(lvlTalkative, + format("normalising expression in `%1%'") % (string) _nePath); + + /* Try to substitute the expression by any known successors in + order to speed up the rewrite process. */ + Path nePath = useSuccessor(_nePath); + + /* Get the Nix expression. */ + NixExpr ne = exprFromPath(nePath, pending); + + /* If this is a normal form (i.e., a closure) we are done. */ + if (ne.type == NixExpr::neClosure) return nePath; + if (ne.type != NixExpr::neDerivation) abort(); + + + /* Otherwise, it's a derivation expression, and we have to build it to + determine its normal form. */ + + + /* Some variables. */ + + /* Input paths, with their closure elements. */ + ClosureElems inClosures; + + /* Referenceable paths (i.e., input and output paths). */ + PathSet allPaths; + + /* The environment to be passed to the builder. */ + Environment env; + + /* The result. */ + NixExpr nf; + nf.type = NixExpr::neClosure; + + + /* The outputs are referenceable paths. */ + for (PathSet::iterator i = ne.derivation.outputs.begin(); + i != ne.derivation.outputs.end(); i++) + { + debug(format("building path `%1%'") % *i); + allPaths.insert(*i); + } + + /* Obtain locks on all output paths. The locks are automatically + released when we exit this function or Nix crashes. */ + PathLocks outputLocks(ne.derivation.outputs); + + /* Now check again whether there is a successor. This is because + another process may have started building in parallel. After + it has finished and released the locks, we can (and should) + reuse its results. (Strictly speaking the first successor + check above can be omitted, but that would be less efficient.) + Note that since we now hold the locks on the output paths, no + other process can build this expression, so no further checks + are necessary. */ + { + Path nePath2 = useSuccessor(nePath); + if (nePath != nePath2) { + NixExpr ne = exprFromPath(nePath2, pending); + debug(format("skipping build of expression `%1%', someone beat us to it") + % (string) nePath); + if (ne.type != NixExpr::neClosure) abort(); + return nePath2; + } + } + + /* Right platform? */ + if (ne.derivation.platform != thisSystem) + throw Error(format("a `%1%' is required, but I am a `%2%'") + % ne.derivation.platform % thisSystem); + + /* Realise inputs (and remember all input paths). */ + for (PathSet::iterator i = ne.derivation.inputs.begin(); + i != ne.derivation.inputs.end(); i++) + { + Path nfPath = normaliseNixExpr(*i, pending); + realiseClosure(nfPath, pending); + /* !!! nfPath should be a root of the garbage collector while + we are building */ + NixExpr ne = exprFromPath(nfPath, pending); + if (ne.type != NixExpr::neClosure) abort(); + for (ClosureElems::iterator j = ne.closure.elems.begin(); + j != ne.closure.elems.end(); j++) + { + inClosures[j->first] = j->second; + allPaths.insert(j->first); + } + } + + /* Most shells initialise PATH to some default (/bin:/usr/bin:...) when + PATH is not set. We don't want this, so we fill it in with some dummy + value. */ + env["PATH"] = "/path-not-set"; + + /* Set HOME to a non-existing path to prevent certain programs from using + /etc/passwd (or NIS, or whatever) to locate the home directory (for + example, wget looks for ~/.wgetrc). I.e., these tools use /etc/passwd + if HOME is not set, but they will just assume that the settings file + they are looking for does not exist if HOME is set but points to some + non-existing path. */ + env["HOME"] = "/homeless-shelter"; + + /* Build the environment. */ + for (StringPairs::iterator i = ne.derivation.env.begin(); + i != ne.derivation.env.end(); i++) + env[i->first] = i->second; + + /* We can skip running the builder if we can expand all output + paths from their ids. */ + bool fastBuild = false; +#if 0 + bool fastBuild = true; + for (PathSet::iterator i = ne.derivation.outputs.begin(); + i != ne.derivation.outputs.end(); i++) + { + try { + expandId(i->second, i->first, "/", pending); + } catch (Error & e) { + debug(format("fast build failed for `%1%': %2%") + % i->first % e.what()); + fastBuild = false; + break; + } + } +#endif + + if (!fastBuild) { + + /* If any of the outputs already exist but are not registered, + delete them. */ + for (PathSet::iterator i = ne.derivation.outputs.begin(); + i != ne.derivation.outputs.end(); i++) + { + Path path = *i; + if (isValidPath(path)) + throw Error(format("obstructed build: path `%1%' exists") % path); + if (pathExists(path)) { + debug(format("removing unregistered path `%1%'") % path); + deletePath(path); + } + } + + /* Run the builder. */ + msg(lvlChatty, format("building...")); + runProgram(ne.derivation.builder, ne.derivation.args, env); + msg(lvlChatty, format("build completed")); + + } else + msg(lvlChatty, format("fast build succesful")); + + /* Check whether the output paths were created, and grep each + output path to determine what other paths it references. Also make all + output paths read-only. */ + PathSet usedPaths; + for (PathSet::iterator i = ne.derivation.outputs.begin(); + i != ne.derivation.outputs.end(); i++) + { + Path path = *i; + if (!pathExists(path)) + throw Error(format("path `%1%' does not exist") % path); + nf.closure.roots.insert(path); + + makePathReadOnly(path); + + /* For this output path, find the references to other paths contained + in it. */ + Strings refPaths = filterReferences(path, + Strings(allPaths.begin(), allPaths.end())); + + /* Construct a closure element for this output path. */ + ClosureElem elem; + + /* For each path referenced by this output path, add its id to the + closure element and add the id to the `usedPaths' set (so that the + elements referenced by *its* closure are added below). */ + for (Paths::iterator j = refPaths.begin(); + j != refPaths.end(); j++) + { + Path path = *j; + elem.refs.insert(path); + if (inClosures.find(path) != inClosures.end()) + usedPaths.insert(path); + else if (ne.derivation.outputs.find(path) == ne.derivation.outputs.end()) + abort(); + } + + nf.closure.elems[path] = elem; + } + + /* Close the closure. That is, for any referenced path, add the paths + referenced by it. */ + PathSet donePaths; + + while (!usedPaths.empty()) { + PathSet::iterator i = usedPaths.begin(); + Path path = *i; + usedPaths.erase(i); + + if (donePaths.find(path) != donePaths.end()) continue; + donePaths.insert(path); + + ClosureElems::iterator j = inClosures.find(path); + if (j == inClosures.end()) abort(); + + nf.closure.elems[path] = j->second; + + for (PathSet::iterator k = j->second.refs.begin(); + k != j->second.refs.end(); k++) + usedPaths.insert(*k); + } + + /* For debugging, print out the referenced and unreferenced paths. */ + for (ClosureElems::iterator i = inClosures.begin(); + i != inClosures.end(); i++) + { + PathSet::iterator j = donePaths.find(i->first); + if (j == donePaths.end()) + debug(format("NOT referenced: `%1%'") % i->first); + else + debug(format("referenced: `%1%'") % i->first); + } + + /* Write the normal form. This does not have to occur in the + transaction below because writing terms is idem-potent. */ + ATerm nfTerm = unparseNixExpr(nf); + msg(lvlVomit, format("normal form: %1%") % printTerm(nfTerm)); + Path nfPath = writeTerm(nfTerm, "-s"); + + /* Register each outpat path, and register the normal form. This + is wrapped in one database transaction to ensure that if we + crash, either everything is registered or nothing is. This is + for recoverability: unregistered paths in the store can be + deleted arbitrarily, while registered paths can only be deleted + by running the garbage collector. */ + Transaction txn; + createStoreTransaction(txn); + for (PathSet::iterator i = ne.derivation.outputs.begin(); + i != ne.derivation.outputs.end(); i++) + registerValidPath(txn, *i); + registerSuccessor(txn, nePath, nfPath); + txn.commit(); + + return nfPath; +} + + +void realiseClosure(const Path & nePath, PathSet pending) +{ + Nest nest(lvlDebug, format("realising closure `%1%'") % nePath); + + NixExpr ne = exprFromPath(nePath, pending); + if (ne.type != NixExpr::neClosure) + throw Error(format("expected closure in `%1%'") % nePath); + + for (ClosureElems::const_iterator i = ne.closure.elems.begin(); + i != ne.closure.elems.end(); i++) + ensurePath(i->first, pending); +} + + +void ensurePath(const Path & path, PathSet pending) +{ + /* If the path is already valid, we're done. */ + if (isValidPath(path)) return; + + /* Otherwise, try the substitutes. */ + Paths subPaths = querySubstitutes(path); + + for (Paths::iterator i = subPaths.begin(); + i != subPaths.end(); i++) + { + try { + normaliseNixExpr(*i, pending); + if (isValidPath(path)) return; + throw Error(format("substitute failed to produce expected output path")); + } catch (Error & e) { + msg(lvlTalkative, + format("building of substitute `%1%' for `%2%' failed: %3%") + % *i % path % e.what()); + } + } + + throw Error(format("path `%1%' is required, " + "but there are no (successful) substitutes") % path); +} + + +NixExpr exprFromPath(const Path & path, PathSet pending) +{ + ensurePath(path, pending); + ATerm t = ATreadFromNamedFile(path.c_str()); + if (!t) throw Error(format("cannot read aterm from `%1%'") % path); + return parseNixExpr(t); +} + + +PathSet nixExprRoots(const Path & nePath) +{ + PathSet paths; + + NixExpr ne = exprFromPath(nePath); + + if (ne.type == NixExpr::neClosure) + paths.insert(ne.closure.roots.begin(), ne.closure.roots.end()); + else if (ne.type == NixExpr::neDerivation) + paths.insert(ne.derivation.outputs.begin(), + ne.derivation.outputs.end()); + else abort(); + + return paths; +} + + +static void requisitesWorker(const Path & nePath, + bool includeExprs, bool includeSuccessors, + PathSet & paths, PathSet & doneSet) +{ + if (doneSet.find(nePath) != doneSet.end()) return; + doneSet.insert(nePath); + + NixExpr ne = exprFromPath(nePath); + + if (ne.type == NixExpr::neClosure) + for (ClosureElems::iterator i = ne.closure.elems.begin(); + i != ne.closure.elems.end(); i++) + paths.insert(i->first); + + else if (ne.type == NixExpr::neDerivation) + for (PathSet::iterator i = ne.derivation.inputs.begin(); + i != ne.derivation.inputs.end(); i++) + requisitesWorker(*i, + includeExprs, includeSuccessors, paths, doneSet); + + else abort(); + + if (includeExprs) paths.insert(nePath); + + string nfPath; + if (includeSuccessors && (nfPath = useSuccessor(nePath)) != nePath) + requisitesWorker(nfPath, includeExprs, includeSuccessors, + paths, doneSet); +} + + +PathSet nixExprRequisites(const Path & nePath, + bool includeExprs, bool includeSuccessors) +{ + PathSet paths; + PathSet doneSet; + requisitesWorker(nePath, includeExprs, includeSuccessors, + paths, doneSet); + return paths; +} diff --git a/src/libnix/normalise.hh b/src/libnix/normalise.hh new file mode 100644 index 000000000000..bbe846404cc0 --- /dev/null +++ b/src/libnix/normalise.hh @@ -0,0 +1,46 @@ +#ifndef __NORMALISE_H +#define __NORMALISE_H + +#include "expr.hh" + + +/* Normalise a Nix expression. That is, if the expression is a + derivation, a path containing an equivalent closure expression is + returned. This requires that the derivation is performed, unless a + successor is known. */ +Path normaliseNixExpr(const Path & nePath, PathSet pending = PathSet()); + +/* Realise a closure expression in the file system. + + The pending paths are those that are already being realised. This + prevents infinite recursion for paths realised through a substitute + (since when we build the substitute, we would first try to realise + its output paths through substitutes... kaboom!). */ +void realiseClosure(const Path & nePath, PathSet pending = PathSet()); + +/* Ensure that a path exists, possibly by instantiating it by + realising a substitute. */ +void ensurePath(const Path & path, PathSet pending = PathSet()); + +/* Read a Nix expression, after ensuring its existence through + ensurePath(). */ +NixExpr exprFromPath(const Path & path, PathSet pending = PathSet()); + +/* Get the list of root (output) paths of the given Nix expression. */ +PathSet nixExprRoots(const Path & nePath); + +/* Get the list of paths that are required to realise the given + expression. For a derive expression, this is the union of + requisites of the inputs; for a closure expression, it is the path of + each element in the closure. If `includeExprs' is true, include the + paths of the Nix expressions themselves. If `includeSuccessors' is + true, include the requisites of successors. */ +PathSet nixExprRequisites(const Path & nePath, + bool includeExprs, bool includeSuccessors); + +/* Return the list of the paths of all known Nix expressions whose + output paths are completely contained in the set `outputs'. */ +PathSet findGenerators(const PathSet & outputs); + + +#endif /* !__NORMALISE_H */ diff --git a/src/libnix/pathlocks.cc b/src/libnix/pathlocks.cc new file mode 100644 index 000000000000..3ecbbbcbafd5 --- /dev/null +++ b/src/libnix/pathlocks.cc @@ -0,0 +1,90 @@ +#include + +#include +#include +#include + +#include "pathlocks.hh" + + +bool lockFile(int fd, LockType lockType, bool wait) +{ + struct flock lock; + if (lockType == ltRead) lock.l_type = F_RDLCK; + else if (lockType == ltWrite) lock.l_type = F_WRLCK; + else if (lockType == ltNone) lock.l_type = F_UNLCK; + else abort(); + lock.l_whence = SEEK_SET; + lock.l_start = 0; + lock.l_len = 0; /* entire file */ + + if (wait) { + while (fcntl(fd, F_SETLKW, &lock) != 0) + if (errno != EINTR) + throw SysError(format("acquiring/releasing lock")); + } else { + while (fcntl(fd, F_SETLK, &lock) != 0) { + if (errno == EACCES || errno == EAGAIN) return false; + if (errno != EINTR) + throw SysError(format("acquiring/releasing lock")); + } + } + + return true; +} + + +/* This enables us to check whether are not already holding a lock on + a file ourselves. POSIX locks (fcntl) suck in this respect: if we + close a descriptor, the previous lock will be closed as well. And + there is no way to query whether we already have a lock (F_GETLK + only works on locks held by other processes). */ +static StringSet lockedPaths; /* !!! not thread-safe */ + + +PathLocks::PathLocks(const PathSet & _paths) +{ + /* Note that `fds' is built incrementally so that the destructor + will only release those locks that we have already acquired. */ + + /* Sort the paths. This assures that locks are always acquired in + the same order, thus preventing deadlocks. */ + Paths paths(_paths.begin(), _paths.end()); + paths.sort(); + + /* Acquire the lock for each path. */ + for (Paths::iterator i = paths.begin(); i != paths.end(); i++) { + Path path = *i; + Path lockPath = path + ".lock"; + + debug(format("locking path `%1%'") % path); + + if (lockedPaths.find(lockPath) != lockedPaths.end()) { + debug(format("already holding lock on `%1%'") % lockPath); + continue; + } + + /* Open/create the lock file. */ + int fd = open(lockPath.c_str(), O_WRONLY | O_CREAT, 0666); + if (fd == -1) + throw SysError(format("opening lock file `%1%'") % lockPath); + + fds.push_back(fd); + this->paths.push_back(lockPath); + + /* Acquire an exclusive lock. */ + lockFile(fd, ltWrite, true); + + lockedPaths.insert(lockPath); + } +} + + +PathLocks::~PathLocks() +{ + for (list::iterator i = fds.begin(); i != fds.end(); i++) + close(*i); + + for (Paths::iterator i = paths.begin(); i != paths.end(); i++) + lockedPaths.erase(*i); +} diff --git a/src/libnix/pathlocks.hh b/src/libnix/pathlocks.hh new file mode 100644 index 000000000000..ce61386d6df0 --- /dev/null +++ b/src/libnix/pathlocks.hh @@ -0,0 +1,24 @@ +#ifndef __PATHLOCKS_H +#define __PATHLOCKS_H + +#include "util.hh" + + +typedef enum LockType { ltRead, ltWrite, ltNone }; + +bool lockFile(int fd, LockType lockType, bool wait); + + +class PathLocks +{ +private: + list fds; + Paths paths; + +public: + PathLocks(const PathSet & _paths); + ~PathLocks(); +}; + + +#endif /* !__PATHLOCKS_H */ diff --git a/src/libnix/references.cc b/src/libnix/references.cc new file mode 100644 index 000000000000..be432665b884 --- /dev/null +++ b/src/libnix/references.cc @@ -0,0 +1,110 @@ +#include +#include + +#include +#include +#include +#include +#include + +#include "references.hh" +#include "hash.hh" + + +static void search(const string & s, + Strings & ids, Strings & seen) +{ + for (Strings::iterator i = ids.begin(); + i != ids.end(); ) + { + if (s.find(*i) == string::npos) + i++; + else { + debug(format("found reference to `%1%'") % *i); + seen.push_back(*i); + i = ids.erase(i); + } + } +} + + +void checkPath(const string & path, + Strings & ids, Strings & seen) +{ + struct stat st; + if (lstat(path.c_str(), &st)) + throw SysError(format("getting attributes of path `%1%'") % path); + + if (S_ISDIR(st.st_mode)) { + DIR * dir = opendir(path.c_str()); + + struct dirent * dirent; + while (errno = 0, dirent = readdir(dir)) { + string name = dirent->d_name; + if (name == "." || name == "..") continue; + search(name, ids, seen); + checkPath(path + "/" + name, ids, seen); + } + + closedir(dir); /* !!! close on exception */ + } + + else if (S_ISREG(st.st_mode)) { + + debug(format("checking `%1%'") % path); + + int fd = open(path.c_str(), O_RDONLY); + if (fd == -1) throw SysError(format("opening file `%1%'") % path); + + unsigned char * buf = new unsigned char[st.st_size]; + + readFull(fd, buf, st.st_size); + + search(string((char *) buf, st.st_size), ids, seen); + + delete buf; /* !!! autodelete */ + + close(fd); /* !!! close on exception */ + } + + else if (S_ISLNK(st.st_mode)) { + char buf[st.st_size]; + if (readlink(path.c_str(), buf, st.st_size) != st.st_size) + throw SysError(format("reading symbolic link `%1%'") % path); + search(string(buf, st.st_size), ids, seen); + } + + else throw Error(format("unknown file type: %1%") % path); +} + + +Strings filterReferences(const string & path, const Strings & paths) +{ + map backMap; + Strings ids; + Strings seen; + + /* For efficiency (and a higher hit rate), just search for the + hash part of the file name. (This assumes that all references + have the form `HASH-bla'). */ + for (Strings::const_iterator i = paths.begin(); + i != paths.end(); i++) + { + string s = string(baseNameOf(*i), 0, 32); + parseHash(s); + ids.push_back(s); + backMap[s] = *i; + } + + checkPath(path, ids, seen); + + Strings found; + for (Strings::iterator i = seen.begin(); i != seen.end(); i++) + { + map::iterator j; + if ((j = backMap.find(*i)) == backMap.end()) abort(); + found.push_back(j->second); + } + + return found; +} diff --git a/src/libnix/references.hh b/src/libnix/references.hh new file mode 100644 index 000000000000..d009453d6a00 --- /dev/null +++ b/src/libnix/references.hh @@ -0,0 +1,10 @@ +#ifndef __VALUES_H +#define __VALUES_H + +#include "util.hh" + + +Strings filterReferences(const Path & path, const Strings & refs); + + +#endif /* !__VALUES_H */ diff --git a/src/libnix/store.cc b/src/libnix/store.cc new file mode 100644 index 000000000000..2d223313b612 --- /dev/null +++ b/src/libnix/store.cc @@ -0,0 +1,405 @@ +#include + +#include +#include +#include +#include +#include + +#include "store.hh" +#include "globals.hh" +#include "db.hh" +#include "archive.hh" +#include "pathlocks.hh" + + +/* Nix database. */ +static Database nixDB; + + +/* Database tables. */ + +/* dbValidPaths :: Path -> () + + The existence of a key $p$ indicates that path $p$ is valid (that + is, produced by a succesful build). */ +static TableId dbValidPaths; + +/* dbSuccessors :: Path -> Path + + Each pair $(p_1, p_2)$ in this mapping records the fact that the + Nix expression stored at path $p_1$ has a successor expression + stored at path $p_2$. + + Note that a term $y$ is a successor of $x$ iff there exists a + sequence of rewrite steps that rewrites $x$ into $y$. +*/ +static TableId dbSuccessors; + +/* dbSuccessorsRev :: Path -> [Path] + + The reverse mapping of dbSuccessors (i.e., it stores the + predecessors of a Nix expression). +*/ +static TableId dbSuccessorsRev; + +/* dbSubstitutes :: Path -> [Path] + + Each pair $(p, [ps])$ tells Nix that it can realise any of the + Nix expressions stored at paths $ps$ to produce a path $p$. + + The main purpose of this is for distributed caching of derivates. + One system can compute a derivate and put it on a website (as a Nix + archive), for instance, and then another system can register a + substitute for that derivate. The substitute in this case might be + a Nix expression that fetches the Nix archive. +*/ +static TableId dbSubstitutes; + +/* dbSubstitutesRev :: Path -> [Path] + + The reverse mapping of dbSubstitutes. +*/ +static TableId dbSubstitutesRev; + + +void openDB() +{ + nixDB.open(nixDBPath); + dbValidPaths = nixDB.openTable("validpaths"); + dbSuccessors = nixDB.openTable("successors"); + dbSuccessorsRev = nixDB.openTable("successors-rev"); + dbSubstitutes = nixDB.openTable("substitutes"); + dbSubstitutesRev = nixDB.openTable("substitutes-rev"); +} + + +void initDB() +{ +} + + +void createStoreTransaction(Transaction & txn) +{ + Transaction txn2(nixDB); + txn2.moveTo(txn); +} + + +/* Path copying. */ + +struct CopySink : DumpSink +{ + int fd; + virtual void operator () (const unsigned char * data, unsigned int len) + { + writeFull(fd, data, len); + } +}; + + +struct CopySource : RestoreSource +{ + int fd; + virtual void operator () (unsigned char * data, unsigned int len) + { + readFull(fd, data, len); + } +}; + + +void copyPath(const Path & src, const Path & dst) +{ + debug(format("copying `%1%' to `%2%'") % src % dst); + + /* Unfortunately C++ doesn't support coprocedures, so we have no + nice way to chain CopySink and CopySource together. Instead we + fork off a child to run the sink. (Fork-less platforms should + use a thread). */ + + /* Create a pipe. */ + int fds[2]; + if (pipe(fds) == -1) throw SysError("creating pipe"); + + /* Fork. */ + pid_t pid; + switch (pid = fork()) { + + case -1: + throw SysError("unable to fork"); + + case 0: /* child */ + try { + close(fds[1]); + CopySource source; + source.fd = fds[0]; + restorePath(dst, source); + _exit(0); + } catch (exception & e) { + cerr << "error: " << e.what() << endl; + } + _exit(1); + } + + close(fds[0]); + + /* Parent. */ + + CopySink sink; + sink.fd = fds[1]; + dumpPath(src, sink); + + /* Wait for the child to finish. */ + int status; + if (waitpid(pid, &status, 0) != pid) + throw SysError("waiting for child"); + + if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) + throw Error("cannot copy file: child died"); +} + + +void registerSuccessor(const Transaction & txn, + const Path & srcPath, const Path & sucPath) +{ + Path known; + if (nixDB.queryString(txn, dbSuccessors, srcPath, known) && + known != sucPath) + { + throw Error(format( + "the `impossible' happened: expression in path " + "`%1%' appears to have multiple successors " + "(known `%2%', new `%3%'") + % srcPath % known % sucPath); + } + + Paths revs; + nixDB.queryStrings(txn, dbSuccessorsRev, sucPath, revs); + if (find(revs.begin(), revs.end(), srcPath) == revs.end()) + revs.push_back(srcPath); + + nixDB.setString(txn, dbSuccessors, srcPath, sucPath); + nixDB.setStrings(txn, dbSuccessorsRev, sucPath, revs); +} + + +bool querySuccessor(const Path & srcPath, Path & sucPath) +{ + return nixDB.queryString(noTxn, dbSuccessors, srcPath, sucPath); +} + + +Paths queryPredecessors(const Path & sucPath) +{ + Paths revs; + nixDB.queryStrings(noTxn, dbSuccessorsRev, sucPath, revs); + return revs; +} + + +void registerSubstitute(const Path & srcPath, const Path & subPath) +{ + Transaction txn(nixDB); + + Paths subs; + nixDB.queryStrings(txn, dbSubstitutes, srcPath, subs); + + if (find(subs.begin(), subs.end(), subPath) != subs.end()) { + /* Nothing to do if the substitute is already known. */ + txn.abort(); + return; + } + subs.push_front(subPath); /* new substitutes take precedence */ + + Paths revs; + nixDB.queryStrings(txn, dbSubstitutesRev, subPath, revs); + if (find(revs.begin(), revs.end(), srcPath) == revs.end()) + revs.push_back(srcPath); + + nixDB.setStrings(txn, dbSubstitutes, srcPath, subs); + nixDB.setStrings(txn, dbSubstitutesRev, subPath, revs); + + txn.commit(); +} + + +Paths querySubstitutes(const Path & srcPath) +{ + Paths subPaths; + nixDB.queryStrings(noTxn, dbSubstitutes, srcPath, subPaths); + return subPaths; +} + + +void registerValidPath(const Transaction & txn, const Path & _path) +{ + Path path(canonPath(_path)); + debug(format("registering path `%1%'") % path); + nixDB.setString(txn, dbValidPaths, path, ""); +} + + +bool isValidPath(const Path & path) +{ + string s; + return nixDB.queryString(noTxn, dbValidPaths, path, s); +} + + +void unregisterValidPath(const Path & _path) +{ + Path path(canonPath(_path)); + Transaction txn(nixDB); + + debug(format("unregistering path `%1%'") % path); + + nixDB.delPair(txn, dbValidPaths, path); + + txn.commit(); +} + + +static bool isInPrefix(const string & path, const string & _prefix) +{ + string prefix = canonPath(_prefix + "/"); + return string(path, 0, prefix.size()) == prefix; +} + + +Path addToStore(const Path & _srcPath) +{ + Path srcPath(absPath(_srcPath)); + debug(format("adding `%1%' to the store") % srcPath); + + Hash h = hashPath(srcPath); + + string baseName = baseNameOf(srcPath); + Path dstPath = canonPath(nixStore + "/" + (string) h + "-" + baseName); + + if (!isValidPath(dstPath)) { + + /* The first check above is an optimisation to prevent + unnecessary lock acquisition. */ + + PathSet lockPaths; + lockPaths.insert(dstPath); + PathLocks outputLock(lockPaths); + + if (!isValidPath(dstPath)) { + copyPath(srcPath, dstPath); + + Transaction txn(nixDB); + registerValidPath(txn, dstPath); + txn.commit(); + } + } + + return dstPath; +} + + +void addTextToStore(const Path & dstPath, const string & s) +{ + if (!isValidPath(dstPath)) { + + /* !!! locking? -> parallel writes are probably idempotent */ + + int fd = open(dstPath.c_str(), O_CREAT | O_EXCL | O_WRONLY, 0666); + if (fd == -1) throw SysError(format("creating store file `%1%'") % dstPath); + + if (write(fd, s.c_str(), s.size()) != (ssize_t) s.size()) + throw SysError(format("writing store file `%1%'") % dstPath); + + close(fd); /* !!! close on exception */ + + Transaction txn(nixDB); + registerValidPath(txn, dstPath); + txn.commit(); + } +} + + +void deleteFromStore(const Path & _path) +{ + Path path(canonPath(_path)); + + if (!isInPrefix(path, nixStore)) + throw Error(format("path `%1%' is not in the store") % path); + + unregisterValidPath(path); + + deletePath(path); +} + + +void verifyStore() +{ + Transaction txn(nixDB); + + Paths paths; + nixDB.enumTable(txn, dbValidPaths, paths); + + for (Paths::iterator i = paths.begin(); + i != paths.end(); i++) + { + Path path = *i; + if (!pathExists(path)) { + debug(format("path `%1%' disappeared") % path); + nixDB.delPair(txn, dbValidPaths, path); + nixDB.delPair(txn, dbSuccessorsRev, path); + nixDB.delPair(txn, dbSubstitutesRev, path); + } + } + +#if 0 + Strings subs; + nixDB.enumTable(txn, dbSubstitutes, subs); + + for (Strings::iterator i = subs.begin(); + i != subs.end(); i++) + { + FSId srcId = parseHash(*i); + + Strings subIds; + nixDB.queryStrings(txn, dbSubstitutes, srcId, subIds); + + for (Strings::iterator j = subIds.begin(); + j != subIds.end(); ) + { + FSId subId = parseHash(*j); + + Strings subPaths; + nixDB.queryStrings(txn, dbId2Paths, subId, subPaths); + if (subPaths.size() == 0) { + debug(format("erasing substitute %1% for %2%") + % (string) subId % (string) srcId); + j = subIds.erase(j); + } else j++; + } + + nixDB.setStrings(txn, dbSubstitutes, srcId, subIds); + } +#endif + + Paths sucs; + nixDB.enumTable(txn, dbSuccessors, sucs); + + for (Paths::iterator i = sucs.begin(); i != sucs.end(); i++) { + Path srcPath = *i; + + Path sucPath; + if (!nixDB.queryString(txn, dbSuccessors, srcPath, sucPath)) abort(); + + Paths revs; + nixDB.queryStrings(txn, dbSuccessorsRev, sucPath, revs); + + if (find(revs.begin(), revs.end(), srcPath) == revs.end()) { + debug(format("reverse successor mapping from `%1%' to `%2%' missing") + % srcPath % sucPath); + revs.push_back(srcPath); + nixDB.setStrings(txn, dbSuccessorsRev, sucPath, revs); + } + } + + txn.commit(); +} diff --git a/src/libnix/store.hh b/src/libnix/store.hh new file mode 100644 index 000000000000..dab3d603f802 --- /dev/null +++ b/src/libnix/store.hh @@ -0,0 +1,72 @@ +#ifndef __STORE_H +#define __STORE_H + +#include + +#include "hash.hh" +#include "db.hh" + +using namespace std; + + +/* Open the database environment. */ +void openDB(); + +/* Create the required database tables. */ +void initDB(); + +/* Get a transaction object. */ +void createStoreTransaction(Transaction & txn); + +/* Copy a path recursively. */ +void copyPath(const Path & src, const Path & dst); + +/* Register a successor. This function accepts a transaction handle + so that it can be enclosed in an atomic operation with calls to + registerValidPath(). This must be atomic, since if we register a + successor for a derivation without registering the paths built in + the derivation, we have a successor with dangling pointers, and if + we do it in reverse order, we can get an obstructed build (since to + rebuild the successor, the outputs paths must not exist). */ +void registerSuccessor(const Transaction & txn, + const Path & srcPath, const Path & sucPath); + +/* Return the predecessors of the Nix expression stored at the given + path. */ +bool querySuccessor(const Path & srcPath, Path & sucPath); + +/* Return the predecessors of the Nix expression stored at the given + path. */ +Paths queryPredecessors(const Path & sucPath); + +/* Register a substitute. */ +void registerSubstitute(const Path & srcPath, const Path & subPath); + +/* Return the substitutes expression for the given path. */ +Paths querySubstitutes(const Path & srcPath); + +/* Register the validity of a path. */ +void registerValidPath(const Transaction & txn, const Path & path); + +/* Unregister the validity of a path. */ +void unregisterValidPath(const Path & path); + +/* Checks whether a path is valid. */ +bool isValidPath(const Path & path); + +/* Copy the contents of a path to the store and register the validity + the resulting path. The resulting path is returned. */ +Path addToStore(const Path & srcPath); + +/* Like addToStore, but the path of the output is given, and the + contents written to the output path is a regular file containing + the given string. */ +void addTextToStore(const Path & dstPath, const string & s); + +/* Delete a value from the nixStore directory. */ +void deleteFromStore(const Path & path); + +void verifyStore(); + + +#endif /* !__STORE_H */ diff --git a/src/libnix/test-builder-1.sh b/src/libnix/test-builder-1.sh new file mode 100755 index 000000000000..80e23354c3b9 --- /dev/null +++ b/src/libnix/test-builder-1.sh @@ -0,0 +1,3 @@ +#! /bin/sh + +echo "Hello World" > $out diff --git a/src/libnix/test-builder-2.sh b/src/libnix/test-builder-2.sh new file mode 100755 index 000000000000..0794fa96a401 --- /dev/null +++ b/src/libnix/test-builder-2.sh @@ -0,0 +1,9 @@ +#! /bin/sh + +echo "builder 2" + +/bin/mkdir $out || exit 1 +cd $out || exit 1 +echo "Hallo Wereld" > bla +echo $builder >> bla +echo $out >> bla diff --git a/src/libnix/test.cc b/src/libnix/test.cc new file mode 100644 index 000000000000..457fecf245f0 --- /dev/null +++ b/src/libnix/test.cc @@ -0,0 +1,162 @@ +#include + +#include +#include + +#include "hash.hh" +#include "archive.hh" +#include "util.hh" +#include "normalise.hh" +#include "globals.hh" + + +void realise(Path nePath) +{ + Nest nest(lvlDebug, format("TEST: realising `%1%'") % nePath); + realiseClosure(normaliseNixExpr(nePath)); +} + + +struct MySink : DumpSink +{ + virtual void operator () (const unsigned char * data, unsigned int len) + { + /* Don't use cout, it's slow as hell! */ + writeFull(STDOUT_FILENO, data, len); + } +}; + + +struct MySource : RestoreSource +{ + virtual void operator () (unsigned char * data, unsigned int len) + { + readFull(STDIN_FILENO, data, len); + } +}; + + +void runTests() +{ + verbosity = (Verbosity) 100; + + /* Hashing. */ + string s = "0b0ffd0538622bfe20b92c4aa57254d9"; + Hash h = parseHash(s); + if ((string) h != s) abort(); + + try { + h = parseHash("blah blah"); + abort(); + } catch (Error err) { }; + + try { + h = parseHash("0b0ffd0538622bfe20b92c4aa57254d99"); + abort(); + } catch (Error err) { }; + + /* Path canonicalisation. */ + cout << canonPath("/./../././//") << endl; + cout << canonPath("/foo/bar") << endl; + cout << canonPath("///foo/////bar//") << endl; + cout << canonPath("/././/foo/////bar//.") << endl; + cout << canonPath("/foo////bar//..///x/") << endl; + cout << canonPath("/foo////bar//..//..//x/y/../z/") << endl; + cout << canonPath("/foo/bar/../../../..///") << endl; + + /* Dumping. */ + +#if 0 + MySink sink; + dumpPath("scratch", sink); + cout << (string) hashPath("scratch") << endl; +#endif + + /* Restoring. */ +#if 0 + MySource source; + restorePath("outdir", source); + cout << (string) hashPath("outdir") << endl; + return; +#endif + + /* Set up the test environment. */ + + mkdir("scratch", 0777); + mkdir("scratch/db", 0777); + + string testDir = absPath("scratch"); + cout << testDir << endl; + + nixStore = testDir; + nixLogDir = testDir; + nixDBPath = testDir + "/db"; + + openDB(); + initDB(); + + /* Expression evaluation. */ + + Path builder1fn; + builder1fn = addToStore("./test-builder-1.sh"); + + ATerm fs1 = ATmake( + "Closure([], [(, [])])", + builder1fn.c_str(), + builder1fn.c_str()); + Path fs1ne = writeTerm(fs1, "-c"); + + realise(fs1ne); + realise(fs1ne); + + string out1h = hashString("foo"); /* !!! bad */ + Path out1fn = nixStore + "/" + (string) out1h + "-hello.txt"; + ATerm fs3 = ATmake( + "Derive([], [], , , [], [(\"out\", )])", + out1fn.c_str(), + fs1ne.c_str(), + thisSystem.c_str(), + builder1fn.c_str(), + out1fn.c_str()); + debug(printTerm(fs3)); + Path fs3ne = writeTerm(fs3, "-d"); + + realise(fs3ne); + realise(fs3ne); + + + Path builder4fn = addToStore("./test-builder-2.sh"); + + ATerm fs4 = ATmake( + "Closure([], [(, [])])", + builder4fn.c_str(), + builder4fn.c_str()); + Path fs4ne = writeTerm(fs4, "-c"); + + realise(fs4ne); + + string out5h = hashString("bar"); /* !!! bad */ + Path out5fn = nixStore + "/" + (string) out5h + "-hello2"; + ATerm fs5 = ATmake( + "Derive([], [], , , [], [(\"out\", ), (\"builder\", )])", + out5fn.c_str(), + fs4ne.c_str(), + thisSystem.c_str(), + builder4fn.c_str(), + out5fn.c_str(), + builder4fn.c_str()); + debug(printTerm(fs5)); + Path fs5ne = writeTerm(fs5, "-d"); + + realise(fs5ne); + realise(fs5ne); +} + + +void run(Strings args) +{ + runTests(); +} + + +string programId = "test"; diff --git a/src/libnix/util.cc b/src/libnix/util.cc new file mode 100644 index 000000000000..c1d0fedea8aa --- /dev/null +++ b/src/libnix/util.cc @@ -0,0 +1,253 @@ +#include +#include +#include + +#include +#include +#include +#include + +#include "util.hh" + + +string thisSystem = SYSTEM; + + +Error::Error(const format & f) +{ + err = f.str(); +} + + +SysError::SysError(const format & f) + : Error(format("%1%: %2%") % f.str() % strerror(errno)) +{ +} + + +Path absPath(Path path, Path dir) +{ + if (path[0] != '/') { + if (dir == "") { + char buf[PATH_MAX]; + if (!getcwd(buf, sizeof(buf))) + throw SysError("cannot get cwd"); + dir = buf; + } + path = dir + "/" + path; + } + return canonPath(path); +} + + +Path canonPath(const Path & path) +{ + string s; + + if (path[0] != '/') + throw Error(format("not an absolute path: `%1%'") % path); + + string::const_iterator i = path.begin(), end = path.end(); + + while (1) { + + /* Skip slashes. */ + while (i != end && *i == '/') i++; + if (i == end) break; + + /* Ignore `.'. */ + if (*i == '.' && (i + 1 == end || i[1] == '/')) + i++; + + /* If `..', delete the last component. */ + else if (*i == '.' && i + 1 < end && i[1] == '.' && + (i + 2 == end || i[2] == '/')) + { + if (!s.empty()) s.erase(s.rfind('/')); + i += 2; + } + + /* Normal component; copy it. */ + else { + s += '/'; + while (i != end && *i != '/') s += *i++; + } + } + + return s.empty() ? "/" : s; +} + + +Path dirOf(const Path & path) +{ + unsigned int pos = path.rfind('/'); + if (pos == string::npos) + throw Error(format("invalid file name: %1%") % path); + return Path(path, 0, pos); +} + + +string baseNameOf(const Path & path) +{ + unsigned int pos = path.rfind('/'); + if (pos == string::npos) + throw Error(format("invalid file name %1% ") % path); + return string(path, pos + 1); +} + + +bool pathExists(const Path & path) +{ + int res; + struct stat st; + res = stat(path.c_str(), &st); + if (!res) return true; + if (errno != ENOENT) + throw SysError(format("getting status of %1%") % path); + return false; +} + + +void deletePath(const Path & path) +{ + msg(lvlVomit, format("deleting path `%1%'") % path); + + struct stat st; + if (lstat(path.c_str(), &st)) + throw SysError(format("getting attributes of path `%1%'") % path); + + if (S_ISDIR(st.st_mode)) { + Strings names; + + DIR * dir = opendir(path.c_str()); + + struct dirent * dirent; + while (errno = 0, dirent = readdir(dir)) { + string name = dirent->d_name; + if (name == "." || name == "..") continue; + names.push_back(name); + } + + closedir(dir); /* !!! close on exception */ + + /* Make the directory writable. */ + if (!(st.st_mode & S_IWUSR)) { + if (chmod(path.c_str(), st.st_mode | S_IWUSR) == -1) + throw SysError(format("making `%1%' writable")); + } + + for (Strings::iterator i = names.begin(); i != names.end(); i++) + deletePath(path + "/" + *i); + } + + if (remove(path.c_str()) == -1) + throw SysError(format("cannot unlink `%1%'") % path); +} + + +void makePathReadOnly(const Path & path) +{ + struct stat st; + if (lstat(path.c_str(), &st)) + throw SysError(format("getting attributes of path `%1%'") % path); + + if (!S_ISLNK(st.st_mode) && (st.st_mode & S_IWUSR)) { + if (chmod(path.c_str(), st.st_mode & ~S_IWUSR) == -1) + throw SysError(format("making `%1%' read-only") % path); + } + + if (S_ISDIR(st.st_mode)) { + DIR * dir = opendir(path.c_str()); + + struct dirent * dirent; + while (errno = 0, dirent = readdir(dir)) { + string name = dirent->d_name; + if (name == "." || name == "..") continue; + makePathReadOnly(path + "/" + name); + } + + closedir(dir); /* !!! close on exception */ + } +} + + +static Path tempName() +{ + static int counter = 0; + char * s = getenv("TMPDIR"); + Path tmpRoot = s ? canonPath(Path(s)) : "/tmp"; + return (format("%1%/nix-%2%-%3%") % tmpRoot % getpid() % counter++).str(); +} + + +Path createTempDir() +{ + while (1) { + Path tmpDir = tempName(); + if (mkdir(tmpDir.c_str(), 0777) == 0) return tmpDir; + if (errno != EEXIST) + throw SysError(format("creating directory `%1%'") % tmpDir); + } +} + + +Verbosity verbosity = lvlError; + +static int nestingLevel = 0; + + +Nest::Nest(Verbosity level, const format & f) +{ + if (level > verbosity) + nest = false; + else { + msg(level, f); + nest = true; + nestingLevel++; + } +} + + +Nest::~Nest() +{ + if (nest) nestingLevel--; +} + + +void msg(Verbosity level, const format & f) +{ + if (level > verbosity) return; + string spaces; + for (int i = 0; i < nestingLevel; i++) + spaces += "| "; + cerr << format("%1%%2%\n") % spaces % f.str(); +} + + +void debug(const format & f) +{ + msg(lvlDebug, f); +} + + +void readFull(int fd, unsigned char * buf, size_t count) +{ + while (count) { + ssize_t res = read(fd, (char *) buf, count); + if (res == -1) throw SysError("reading from file"); + if (res == 0) throw Error("unexpected end-of-file"); + count -= res; + buf += res; + } +} + + +void writeFull(int fd, const unsigned char * buf, size_t count) +{ + while (count) { + ssize_t res = write(fd, (char *) buf, count); + if (res == -1) throw SysError("writing to file"); + count -= res; + buf += res; + } +} diff --git a/src/libnix/util.hh b/src/libnix/util.hh new file mode 100644 index 000000000000..016289176be8 --- /dev/null +++ b/src/libnix/util.hh @@ -0,0 +1,116 @@ +#ifndef __UTIL_H +#define __UTIL_H + +#include +#include +#include +#include + +#include + +#include + +using namespace std; +using namespace boost; + + +class Error : public exception +{ +protected: + string err; +public: + Error(const format & f); + ~Error() throw () { }; + const char * what() const throw () { return err.c_str(); } + const string & msg() const throw () { return err; } +}; + +class SysError : public Error +{ +public: + SysError(const format & f); +}; + +class UsageError : public Error +{ +public: + UsageError(const format & f) : Error(f) { }; +}; + + +typedef list Strings; +typedef set StringSet; + + +/* Paths are just strings. */ +typedef string Path; +typedef list Paths; +typedef set PathSet; + + +/* The canonical system name, as returned by config.guess. */ +extern string thisSystem; + + +/* Return an absolutized path, resolving paths relative to the + specified directory, or the current directory otherwise. The path + is also canonicalised. */ +Path absPath(Path path, Path dir = ""); + +/* Canonicalise a path (as in realpath(3)). */ +Path canonPath(const Path & path); + +/* Return the directory part of the given path, i.e., everything + before the final `/'. */ +Path dirOf(const Path & path); + +/* Return the base name of the given path, i.e., everything following + the final `/'. */ +string baseNameOf(const Path & path); + +/* Return true iff the given path exists. */ +bool pathExists(const Path & path); + +/* Delete a path; i.e., in the case of a directory, it is deleted + recursively. Don't use this at home, kids. */ +void deletePath(const Path & path); + +/* Make a path read-only recursively. */ +void makePathReadOnly(const Path & path); + +/* Create a temporary directory. */ +Path createTempDir(); + + +/* Messages. */ + +typedef enum { + lvlError, + lvlTalkative, + lvlChatty, + lvlDebug, + lvlVomit +} Verbosity; + +extern Verbosity verbosity; /* supress msgs > this */ + +class Nest +{ +private: + bool nest; +public: + Nest(Verbosity level, const format & f); + ~Nest(); +}; + +void msg(Verbosity level, const format & f); +void debug(const format & f); /* short-hand for msg(lvlDebug, ...) */ + + +/* Wrappers arount read()/write() that read/write exactly the + requested number of bytes. */ +void readFull(int fd, unsigned char * buf, size_t count); +void writeFull(int fd, const unsigned char * buf, size_t count); + + +#endif /* !__UTIL_H */ diff --git a/src/md5.c b/src/md5.c deleted file mode 100644 index fa67ebfcdbdf..000000000000 --- a/src/md5.c +++ /dev/null @@ -1,448 +0,0 @@ -/* Functions to compute MD5 message digest of files or memory blocks. - according to the definition of MD5 in RFC 1321 from April 1992. - Copyright (C) 1995,1996,1997,1999,2000,2001 Free Software Foundation, Inc. - This file is part of the GNU C Library. - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - The GNU C Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library; if not, write to the Free - Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA - 02111-1307 USA. */ - -/* Written by Ulrich Drepper , 1995. */ - -#ifdef HAVE_CONFIG_H -# include -#endif - -#include - -#include -#include - -#include "md5.h" - - -static md5_uint32 SWAP(md5_uint32 n) -{ - static int checked = 0; - static int bigendian = 0; - static md5_uint32 test; - - if (!checked) { - test = 1; - if (* (char *) &test == 0) - bigendian = 1; - checked = 1; - } - - if (bigendian) - return (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24)); - else - return n; -} - - -/* This array contains the bytes used to pad the buffer to the next - 64-byte boundary. (RFC 1321, 3.1: Step 1) */ -static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ }; - - -/* Initialize structure containing state of computation. - (RFC 1321, 3.3: Step 3) */ -void -md5_init_ctx (ctx) - struct md5_ctx *ctx; -{ - ctx->A = 0x67452301; - ctx->B = 0xefcdab89; - ctx->C = 0x98badcfe; - ctx->D = 0x10325476; - - ctx->total[0] = ctx->total[1] = 0; - ctx->buflen = 0; -} - -/* Put result from CTX in first 16 bytes following RESBUF. The result - must be in little endian byte order. - - IMPORTANT: On some systems it is required that RESBUF is correctly - aligned for a 32 bits value. */ -void * -md5_read_ctx (ctx, resbuf) - const struct md5_ctx *ctx; - void *resbuf; -{ - ((md5_uint32 *) resbuf)[0] = SWAP (ctx->A); - ((md5_uint32 *) resbuf)[1] = SWAP (ctx->B); - ((md5_uint32 *) resbuf)[2] = SWAP (ctx->C); - ((md5_uint32 *) resbuf)[3] = SWAP (ctx->D); - - return resbuf; -} - -/* Process the remaining bytes in the internal buffer and the usual - prolog according to the standard and write the result to RESBUF. - - IMPORTANT: On some systems it is required that RESBUF is correctly - aligned for a 32 bits value. */ -void * -md5_finish_ctx (ctx, resbuf) - struct md5_ctx *ctx; - void *resbuf; -{ - /* Take yet unprocessed bytes into account. */ - md5_uint32 bytes = ctx->buflen; - size_t pad; - - /* Now count remaining bytes. */ - ctx->total[0] += bytes; - if (ctx->total[0] < bytes) - ++ctx->total[1]; - - pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes; - memcpy (&ctx->buffer[bytes], fillbuf, pad); - - /* Put the 64-bit file length in *bits* at the end of the buffer. */ - *(md5_uint32 *) &ctx->buffer[bytes + pad] = SWAP (ctx->total[0] << 3); - *(md5_uint32 *) &ctx->buffer[bytes + pad + 4] = SWAP ((ctx->total[1] << 3) | - (ctx->total[0] >> 29)); - - /* Process last bytes. */ - md5_process_block (ctx->buffer, bytes + pad + 8, ctx); - - return md5_read_ctx (ctx, resbuf); -} - -/* Compute MD5 message digest for bytes read from STREAM. The - resulting message digest number will be written into the 16 bytes - beginning at RESBLOCK. */ -int -md5_stream (stream, resblock) - FILE *stream; - void *resblock; -{ - /* Important: BLOCKSIZE must be a multiple of 64. */ -#define BLOCKSIZE 4096 - struct md5_ctx ctx; - char buffer[BLOCKSIZE + 72]; - size_t sum; - - /* Initialize the computation context. */ - md5_init_ctx (&ctx); - - /* Iterate over full file contents. */ - while (1) - { - /* We read the file in blocks of BLOCKSIZE bytes. One call of the - computation function processes the whole buffer so that with the - next round of the loop another block can be read. */ - size_t n; - sum = 0; - - /* Read block. Take care for partial reads. */ - do - { - n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream); - - sum += n; - } - while (sum < BLOCKSIZE && n != 0); - if (n == 0 && ferror (stream)) - return 1; - - /* If end of file is reached, end the loop. */ - if (n == 0) - break; - - /* Process buffer with BLOCKSIZE bytes. Note that - BLOCKSIZE % 64 == 0 - */ - md5_process_block (buffer, BLOCKSIZE, &ctx); - } - - /* Add the last bytes if necessary. */ - if (sum > 0) - md5_process_bytes (buffer, sum, &ctx); - - /* Construct result in desired memory. */ - md5_finish_ctx (&ctx, resblock); - return 0; -} - -/* Compute MD5 message digest for LEN bytes beginning at BUFFER. The - result is always in little endian byte order, so that a byte-wise - output yields to the wanted ASCII representation of the message - digest. */ -void * -md5_buffer (buffer, len, resblock) - const char *buffer; - size_t len; - void *resblock; -{ - struct md5_ctx ctx; - - /* Initialize the computation context. */ - md5_init_ctx (&ctx); - - /* Process whole buffer but last len % 64 bytes. */ - md5_process_bytes (buffer, len, &ctx); - - /* Put result in desired memory area. */ - return md5_finish_ctx (&ctx, resblock); -} - - -void -md5_process_bytes (buffer, len, ctx) - const void *buffer; - size_t len; - struct md5_ctx *ctx; -{ - /* When we already have some bits in our internal buffer concatenate - both inputs first. */ - if (ctx->buflen != 0) - { - size_t left_over = ctx->buflen; - size_t add = 128 - left_over > len ? len : 128 - left_over; - - memcpy (&ctx->buffer[left_over], buffer, add); - ctx->buflen += add; - - if (ctx->buflen > 64) - { - md5_process_block (ctx->buffer, ctx->buflen & ~63, ctx); - - ctx->buflen &= 63; - /* The regions in the following copy operation cannot overlap. */ - memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~63], - ctx->buflen); - } - - buffer = (const char *) buffer + add; - len -= add; - } - - /* Process available complete blocks. */ - if (len >= 64) - { -#if !_STRING_ARCH_unaligned -/* To check alignment gcc has an appropriate operator. Other - compilers don't. */ -# if __GNUC__ >= 2 -# define UNALIGNED_P(p) (((md5_uintptr) p) % __alignof__ (md5_uint32) != 0) -# else -# define UNALIGNED_P(p) (((md5_uintptr) p) % sizeof (md5_uint32) != 0) -# endif - if (UNALIGNED_P (buffer)) - while (len > 64) - { - md5_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx); - buffer = (const char *) buffer + 64; - len -= 64; - } - else -#endif - { - md5_process_block (buffer, len & ~63, ctx); - buffer = (const char *) buffer + (len & ~63); - len &= 63; - } - } - - /* Move remaining bytes in internal buffer. */ - if (len > 0) - { - size_t left_over = ctx->buflen; - - memcpy (&ctx->buffer[left_over], buffer, len); - left_over += len; - if (left_over >= 64) - { - md5_process_block (ctx->buffer, 64, ctx); - left_over -= 64; - memcpy (ctx->buffer, &ctx->buffer[64], left_over); - } - ctx->buflen = left_over; - } -} - - -/* These are the four functions used in the four steps of the MD5 algorithm - and defined in the RFC 1321. The first function is a little bit optimized - (as found in Colin Plumbs public domain implementation). */ -/* #define FF(b, c, d) ((b & c) | (~b & d)) */ -#define FF(b, c, d) (d ^ (b & (c ^ d))) -#define FG(b, c, d) FF (d, b, c) -#define FH(b, c, d) (b ^ c ^ d) -#define FI(b, c, d) (c ^ (b | ~d)) - -/* Process LEN bytes of BUFFER, accumulating context into CTX. - It is assumed that LEN % 64 == 0. */ - -void -md5_process_block (buffer, len, ctx) - const void *buffer; - size_t len; - struct md5_ctx *ctx; -{ - md5_uint32 correct_words[16]; - const md5_uint32 *words = buffer; - size_t nwords = len / sizeof (md5_uint32); - const md5_uint32 *endp = words + nwords; - md5_uint32 A = ctx->A; - md5_uint32 B = ctx->B; - md5_uint32 C = ctx->C; - md5_uint32 D = ctx->D; - - /* First increment the byte count. RFC 1321 specifies the possible - length of the file up to 2^64 bits. Here we only compute the - number of bytes. Do a double word increment. */ - ctx->total[0] += len; - if (ctx->total[0] < len) - ++ctx->total[1]; - - /* Process all bytes in the buffer with 64 bytes in each round of - the loop. */ - while (words < endp) - { - md5_uint32 *cwp = correct_words; - md5_uint32 A_save = A; - md5_uint32 B_save = B; - md5_uint32 C_save = C; - md5_uint32 D_save = D; - - /* First round: using the given function, the context and a constant - the next context is computed. Because the algorithms processing - unit is a 32-bit word and it is determined to work on words in - little endian byte order we perhaps have to change the byte order - before the computation. To reduce the work for the next steps - we store the swapped words in the array CORRECT_WORDS. */ - -#define OP(a, b, c, d, s, T) \ - do \ - { \ - a += FF (b, c, d) + (*cwp++ = SWAP (*words)) + T; \ - ++words; \ - CYCLIC (a, s); \ - a += b; \ - } \ - while (0) - - /* It is unfortunate that C does not provide an operator for - cyclic rotation. Hope the C compiler is smart enough. */ -#define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s))) - - /* Before we start, one word to the strange constants. - They are defined in RFC 1321 as - - T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64 - */ - - /* Round 1. */ - OP (A, B, C, D, 7, 0xd76aa478); - OP (D, A, B, C, 12, 0xe8c7b756); - OP (C, D, A, B, 17, 0x242070db); - OP (B, C, D, A, 22, 0xc1bdceee); - OP (A, B, C, D, 7, 0xf57c0faf); - OP (D, A, B, C, 12, 0x4787c62a); - OP (C, D, A, B, 17, 0xa8304613); - OP (B, C, D, A, 22, 0xfd469501); - OP (A, B, C, D, 7, 0x698098d8); - OP (D, A, B, C, 12, 0x8b44f7af); - OP (C, D, A, B, 17, 0xffff5bb1); - OP (B, C, D, A, 22, 0x895cd7be); - OP (A, B, C, D, 7, 0x6b901122); - OP (D, A, B, C, 12, 0xfd987193); - OP (C, D, A, B, 17, 0xa679438e); - OP (B, C, D, A, 22, 0x49b40821); - - /* For the second to fourth round we have the possibly swapped words - in CORRECT_WORDS. Redefine the macro to take an additional first - argument specifying the function to use. */ -#undef OP -#define OP(f, a, b, c, d, k, s, T) \ - do \ - { \ - a += f (b, c, d) + correct_words[k] + T; \ - CYCLIC (a, s); \ - a += b; \ - } \ - while (0) - - /* Round 2. */ - OP (FG, A, B, C, D, 1, 5, 0xf61e2562); - OP (FG, D, A, B, C, 6, 9, 0xc040b340); - OP (FG, C, D, A, B, 11, 14, 0x265e5a51); - OP (FG, B, C, D, A, 0, 20, 0xe9b6c7aa); - OP (FG, A, B, C, D, 5, 5, 0xd62f105d); - OP (FG, D, A, B, C, 10, 9, 0x02441453); - OP (FG, C, D, A, B, 15, 14, 0xd8a1e681); - OP (FG, B, C, D, A, 4, 20, 0xe7d3fbc8); - OP (FG, A, B, C, D, 9, 5, 0x21e1cde6); - OP (FG, D, A, B, C, 14, 9, 0xc33707d6); - OP (FG, C, D, A, B, 3, 14, 0xf4d50d87); - OP (FG, B, C, D, A, 8, 20, 0x455a14ed); - OP (FG, A, B, C, D, 13, 5, 0xa9e3e905); - OP (FG, D, A, B, C, 2, 9, 0xfcefa3f8); - OP (FG, C, D, A, B, 7, 14, 0x676f02d9); - OP (FG, B, C, D, A, 12, 20, 0x8d2a4c8a); - - /* Round 3. */ - OP (FH, A, B, C, D, 5, 4, 0xfffa3942); - OP (FH, D, A, B, C, 8, 11, 0x8771f681); - OP (FH, C, D, A, B, 11, 16, 0x6d9d6122); - OP (FH, B, C, D, A, 14, 23, 0xfde5380c); - OP (FH, A, B, C, D, 1, 4, 0xa4beea44); - OP (FH, D, A, B, C, 4, 11, 0x4bdecfa9); - OP (FH, C, D, A, B, 7, 16, 0xf6bb4b60); - OP (FH, B, C, D, A, 10, 23, 0xbebfbc70); - OP (FH, A, B, C, D, 13, 4, 0x289b7ec6); - OP (FH, D, A, B, C, 0, 11, 0xeaa127fa); - OP (FH, C, D, A, B, 3, 16, 0xd4ef3085); - OP (FH, B, C, D, A, 6, 23, 0x04881d05); - OP (FH, A, B, C, D, 9, 4, 0xd9d4d039); - OP (FH, D, A, B, C, 12, 11, 0xe6db99e5); - OP (FH, C, D, A, B, 15, 16, 0x1fa27cf8); - OP (FH, B, C, D, A, 2, 23, 0xc4ac5665); - - /* Round 4. */ - OP (FI, A, B, C, D, 0, 6, 0xf4292244); - OP (FI, D, A, B, C, 7, 10, 0x432aff97); - OP (FI, C, D, A, B, 14, 15, 0xab9423a7); - OP (FI, B, C, D, A, 5, 21, 0xfc93a039); - OP (FI, A, B, C, D, 12, 6, 0x655b59c3); - OP (FI, D, A, B, C, 3, 10, 0x8f0ccc92); - OP (FI, C, D, A, B, 10, 15, 0xffeff47d); - OP (FI, B, C, D, A, 1, 21, 0x85845dd1); - OP (FI, A, B, C, D, 8, 6, 0x6fa87e4f); - OP (FI, D, A, B, C, 15, 10, 0xfe2ce6e0); - OP (FI, C, D, A, B, 6, 15, 0xa3014314); - OP (FI, B, C, D, A, 13, 21, 0x4e0811a1); - OP (FI, A, B, C, D, 4, 6, 0xf7537e82); - OP (FI, D, A, B, C, 11, 10, 0xbd3af235); - OP (FI, C, D, A, B, 2, 15, 0x2ad7d2bb); - OP (FI, B, C, D, A, 9, 21, 0xeb86d391); - - /* Add the starting values of the context. */ - A += A_save; - B += B_save; - C += C_save; - D += D_save; - } - - /* Put checksum in context given as argument. */ - ctx->A = A; - ctx->B = B; - ctx->C = C; - ctx->D = D; -} diff --git a/src/md5.h b/src/md5.h deleted file mode 100644 index 6301e4558ca1..000000000000 --- a/src/md5.h +++ /dev/null @@ -1,151 +0,0 @@ -/* Declaration of functions and data types used for MD5 sum computing - library functions. - Copyright (C) 1995,1996,1997,1999,2000,2001 Free Software Foundation, Inc. - This file is part of the GNU C Library. - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - The GNU C Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library; if not, write to the Free - Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA - 02111-1307 USA. */ - -#ifndef _MD5_H -#define _MD5_H 1 - -#include - -#if defined HAVE_LIMITS_H || _LIBC -# include -#endif - -/* The following contortions are an attempt to use the C preprocessor - to determine an unsigned integral type that is 32 bits wide. An - alternative approach is to use autoconf's AC_CHECK_SIZEOF macro, but - doing that would require that the configure script compile and *run* - the resulting executable. Locally running cross-compiled executables - is usually not possible. */ - -#ifdef _LIBC -# include -typedef uint32_t md5_uint32; -typedef uintptr_t md5_uintptr; -#else -# if defined __STDC__ && __STDC__ -# define UINT_MAX_32_BITS 4294967295U -# else -# define UINT_MAX_32_BITS 0xFFFFFFFF -# endif - -/* If UINT_MAX isn't defined, assume it's a 32-bit type. - This should be valid for all systems GNU cares about because - that doesn't include 16-bit systems, and only modern systems - (that certainly have ) have 64+-bit integral types. */ - -# ifndef UINT_MAX -# define UINT_MAX UINT_MAX_32_BITS -# endif - -# if UINT_MAX == UINT_MAX_32_BITS - typedef unsigned int md5_uint32; -# else -# if USHRT_MAX == UINT_MAX_32_BITS - typedef unsigned short md5_uint32; -# else -# if ULONG_MAX == UINT_MAX_32_BITS - typedef unsigned long md5_uint32; -# else - /* The following line is intended to evoke an error. - Using #error is not portable enough. */ - "Cannot determine unsigned 32-bit data type." -# endif -# endif -# endif -/* We have to make a guess about the integer type equivalent in size - to pointers which should always be correct. */ -typedef unsigned long int md5_uintptr; -#endif - -#undef __P -#if defined (__STDC__) && __STDC__ -# define __P(x) x -#else -# define __P(x) () -#endif - -/* Structure to save state of computation between the single steps. */ -struct md5_ctx -{ - md5_uint32 A; - md5_uint32 B; - md5_uint32 C; - md5_uint32 D; - - md5_uint32 total[2]; - md5_uint32 buflen; - char buffer[128] __attribute__ ((__aligned__ (__alignof__ (md5_uint32)))); -}; - -/* - * The following three functions are build up the low level used in - * the functions `md5_stream' and `md5_buffer'. - */ - -/* Initialize structure containing state of computation. - (RFC 1321, 3.3: Step 3) */ -extern void md5_init_ctx __P ((struct md5_ctx *ctx)); - -/* Starting with the result of former calls of this function (or the - initialization function update the context for the next LEN bytes - starting at BUFFER. - It is necessary that LEN is a multiple of 64!!! */ -extern void md5_process_block __P ((const void *buffer, size_t len, - struct md5_ctx *ctx)); - -/* Starting with the result of former calls of this function (or the - initialization function update the context for the next LEN bytes - starting at BUFFER. - It is NOT required that LEN is a multiple of 64. */ -extern void md5_process_bytes __P ((const void *buffer, size_t len, - struct md5_ctx *ctx)); - -/* Process the remaining bytes in the buffer and put result from CTX - in first 16 bytes following RESBUF. The result is always in little - endian byte order, so that a byte-wise output yields to the wanted - ASCII representation of the message digest. - - IMPORTANT: On some systems it is required that RESBUF is correctly - aligned for a 32 bits value. */ -extern void *md5_finish_ctx __P ((struct md5_ctx *ctx, void *resbuf)); - - -/* Put result from CTX in first 16 bytes following RESBUF. The result is - always in little endian byte order, so that a byte-wise output yields - to the wanted ASCII representation of the message digest. - - IMPORTANT: On some systems it is required that RESBUF is correctly - aligned for a 32 bits value. */ -extern void *md5_read_ctx __P ((const struct md5_ctx *ctx, void *resbuf)); - - -/* Compute MD5 message digest for bytes read from STREAM. The - resulting message digest number will be written into the 16 bytes - beginning at RESBLOCK. */ -extern int md5_stream __P ((FILE *stream, void *resblock)); - -/* Compute MD5 message digest for LEN bytes beginning at BUFFER. The - result is always in little endian byte order, so that a byte-wise - output yields to the wanted ASCII representation of the message - digest. */ -extern void *md5_buffer __P ((const char *buffer, size_t len, - void *resblock)); - -#endif /* md5.h */ diff --git a/src/nix-hash.cc b/src/nix-hash.cc deleted file mode 100644 index 77c169b9a952..000000000000 --- a/src/nix-hash.cc +++ /dev/null @@ -1,20 +0,0 @@ -#include - -#include "hash.hh" -#include "shared.hh" - - -void run(Strings args) -{ - bool flat = false; - for (Strings::iterator i = args.begin(); - i != args.end(); i++) - if (*i == "--flat") flat = true; - else - cout << format("%1%\n") % (string) - (flat ? hashFile(*i) : hashPath(*i)); -} - - -string programId = "nix-hash"; - diff --git a/src/nix-hash/nix-hash.cc b/src/nix-hash/nix-hash.cc new file mode 100644 index 000000000000..77c169b9a952 --- /dev/null +++ b/src/nix-hash/nix-hash.cc @@ -0,0 +1,20 @@ +#include + +#include "hash.hh" +#include "shared.hh" + + +void run(Strings args) +{ + bool flat = false; + for (Strings::iterator i = args.begin(); + i != args.end(); i++) + if (*i == "--flat") flat = true; + else + cout << format("%1%\n") % (string) + (flat ? hashFile(*i) : hashPath(*i)); +} + + +string programId = "nix-hash"; + diff --git a/src/nix-help.txt b/src/nix-help.txt deleted file mode 100644 index bf2afd061205..000000000000 --- a/src/nix-help.txt +++ /dev/null @@ -1,32 +0,0 @@ -nix [OPTIONS...] [ARGUMENTS...] - -Operations: - - --install / -i: realise a Nix expression - --delete / -d: delete paths from the Nix store - --add / -A: copy a path to the Nix store - --query / -q: query information - - --successor: register a successor expression - --substitute: register a substitute expression - - --dump: dump a path as a Nix archive - --restore: restore a path from a Nix archive - - --init: initialise the Nix database - --verify: verify Nix structures - - --version: output version information - --help: display help - -Query flags: - - --list / -l: query the output paths (roots) of a Nix expression (default) - --requisites / -r: print all paths necessary to realise expression - --predecessors: print predecessors of a Nix expression - --graph: print a dot graph rooted at given ids - -Options: - - --verbose / -v: verbose operation (may be repeated) - --keep-failed / -K: keep temporary directories of failed builds diff --git a/src/nix.cc b/src/nix.cc deleted file mode 100644 index 1875689990a4..000000000000 --- a/src/nix.cc +++ /dev/null @@ -1,304 +0,0 @@ -#include -#include - -#include "globals.hh" -#include "normalise.hh" -#include "archive.hh" -#include "shared.hh" -#include "dotgraph.hh" - - -typedef void (* Operation) (Strings opFlags, Strings opArgs); - - -static void printHelp() -{ - cout << -#include "nix-help.txt.hh" - ; - exit(0); -} - - - -static Path checkPath(const Path & arg) -{ - return arg; /* !!! check that arg is in the store */ -} - - -/* Realise (or install) paths from the given Nix expressions. */ -static void opInstall(Strings opFlags, Strings opArgs) -{ - if (!opFlags.empty()) throw UsageError("unknown flag"); - - for (Strings::iterator i = opArgs.begin(); - i != opArgs.end(); i++) - { - Path nfPath = normaliseNixExpr(checkPath(*i)); - realiseClosure(nfPath); - cout << format("%1%\n") % (string) nfPath; - } -} - - -/* Delete a path in the Nix store directory. */ -static void opDelete(Strings opFlags, Strings opArgs) -{ - if (!opFlags.empty()) throw UsageError("unknown flag"); - - for (Strings::iterator it = opArgs.begin(); - it != opArgs.end(); it++) - deleteFromStore(checkPath(*it)); -} - - -/* Add paths to the Nix values directory and print the hashes of those - paths. */ -static void opAdd(Strings opFlags, Strings opArgs) -{ - if (!opFlags.empty()) throw UsageError("unknown flag"); - - for (Strings::iterator i = opArgs.begin(); i != opArgs.end(); i++) - cout << format("%1%\n") % addToStore(*i); -} - - -Path maybeNormalise(const Path & ne, bool normalise) -{ - return normalise ? normaliseNixExpr(ne) : ne; -} - - -/* Perform various sorts of queries. */ -static void opQuery(Strings opFlags, Strings opArgs) -{ - enum { qList, qRequisites, qPredecessors, qGraph - } query = qList; - bool normalise = false; - bool includeExprs = true; - bool includeSuccessors = false; - - for (Strings::iterator i = opFlags.begin(); - i != opFlags.end(); i++) - if (*i == "--list" || *i == "-l") query = qList; - else if (*i == "--requisites" || *i == "-r") query = qRequisites; - else if (*i == "--predecessors") query = qPredecessors; - else if (*i == "--graph") query = qGraph; - else if (*i == "--normalise" || *i == "-n") normalise = true; - else if (*i == "--exclude-exprs") includeExprs = false; - else if (*i == "--include-successors") includeSuccessors = true; - else throw UsageError(format("unknown flag `%1%'") % *i); - - switch (query) { - - case qList: { - for (Strings::iterator i = opArgs.begin(); - i != opArgs.end(); i++) - { - StringSet paths = nixExprRoots( - maybeNormalise(checkPath(*i), normalise)); - for (StringSet::iterator j = paths.begin(); - j != paths.end(); j++) - cout << format("%s\n") % *j; - } - break; - } - - case qRequisites: { - StringSet paths; - for (Strings::iterator i = opArgs.begin(); - i != opArgs.end(); i++) - { - StringSet paths2 = nixExprRequisites( - maybeNormalise(checkPath(*i), normalise), - includeExprs, includeSuccessors); - paths.insert(paths2.begin(), paths2.end()); - } - for (StringSet::iterator i = paths.begin(); - i != paths.end(); i++) - cout << format("%s\n") % *i; - break; - } - - case qPredecessors: { - for (Strings::iterator i = opArgs.begin(); - i != opArgs.end(); i++) - { - Paths preds = queryPredecessors(checkPath(*i)); - for (Paths::iterator j = preds.begin(); - j != preds.end(); j++) - cout << format("%s\n") % *j; - } - break; - } - - case qGraph: { - PathSet roots; - for (Strings::iterator i = opArgs.begin(); - i != opArgs.end(); i++) - roots.insert(maybeNormalise(checkPath(*i), normalise)); - printDotGraph(roots); - break; - } - - default: - abort(); - } -} - - -static void opSuccessor(Strings opFlags, Strings opArgs) -{ - if (!opFlags.empty()) throw UsageError("unknown flag"); - if (opArgs.size() % 2) throw UsageError("expecting even number of arguments"); - - Transaction txn; - createStoreTransaction(txn); - for (Strings::iterator i = opArgs.begin(); - i != opArgs.end(); ) - { - Path path1 = checkPath(*i++); - Path path2 = checkPath(*i++); - registerSuccessor(txn, path1, path2); - } - txn.commit(); -} - - -static void opSubstitute(Strings opFlags, Strings opArgs) -{ - if (!opFlags.empty()) throw UsageError("unknown flag"); - if (opArgs.size() % 2) throw UsageError("expecting even number of arguments"); - - for (Strings::iterator i = opArgs.begin(); - i != opArgs.end(); ) - { - Path src = checkPath(*i++); - Path sub = checkPath(*i++); - registerSubstitute(src, sub); - } -} - - -/* A sink that writes dump output to stdout. */ -struct StdoutSink : DumpSink -{ - virtual void operator () - (const unsigned char * data, unsigned int len) - { - writeFull(STDOUT_FILENO, data, len); - } -}; - - -/* Dump a path as a Nix archive. The archive is written to standard - output. */ -static void opDump(Strings opFlags, Strings opArgs) -{ - if (!opFlags.empty()) throw UsageError("unknown flag"); - if (opArgs.size() != 1) throw UsageError("only one argument allowed"); - - StdoutSink sink; - string path = *opArgs.begin(); - dumpPath(path, sink); -} - - -/* A source that read restore intput to stdin. */ -struct StdinSource : RestoreSource -{ - virtual void operator () (unsigned char * data, unsigned int len) - { - readFull(STDIN_FILENO, data, len); - } -}; - - -/* Restore a value from a Nix archive. The archive is written to - standard input. */ -static void opRestore(Strings opFlags, Strings opArgs) -{ - if (!opFlags.empty()) throw UsageError("unknown flag"); - if (opArgs.size() != 1) throw UsageError("only one argument allowed"); - - StdinSource source; - restorePath(*opArgs.begin(), source); -} - - -/* Initialise the Nix databases. */ -static void opInit(Strings opFlags, Strings opArgs) -{ - if (!opFlags.empty()) throw UsageError("unknown flag"); - if (!opArgs.empty()) - throw UsageError("--init does not have arguments"); - initDB(); -} - - -/* Verify the consistency of the Nix environment. */ -static void opVerify(Strings opFlags, Strings opArgs) -{ - verifyStore(); -} - - -/* Scan the arguments; find the operation, set global flags, put all - other flags in a list, and put all other arguments in another - list. */ -void run(Strings args) -{ - Strings opFlags, opArgs; - Operation op = 0; - - for (Strings::iterator it = args.begin(); it != args.end(); ) - { - string arg = *it++; - - Operation oldOp = op; - - if (arg == "--install" || arg == "-i") - op = opInstall; - else if (arg == "--delete" || arg == "-d") - op = opDelete; - else if (arg == "--add" || arg == "-A") - op = opAdd; - else if (arg == "--query" || arg == "-q") - op = opQuery; - else if (arg == "--successor") - op = opSuccessor; - else if (arg == "--substitute") - op = opSubstitute; - else if (arg == "--dump") - op = opDump; - else if (arg == "--restore") - op = opRestore; - else if (arg == "--init") - op = opInit; - else if (arg == "--verify") - op = opVerify; - else if (arg == "--verbose" || arg == "-v") - verbosity = (Verbosity) ((int) verbosity + 1); - else if (arg == "--keep-failed" || arg == "-K") - keepFailed = true; - else if (arg == "--help") - printHelp(); - else if (arg[0] == '-') - opFlags.push_back(arg); - else - opArgs.push_back(arg); - - if (oldOp && oldOp != op) - throw UsageError("only one operation may be specified"); - } - - if (!op) throw UsageError("no operation specified"); - - openDB(); - - op(opFlags, opArgs); -} - - -string programId = "nix"; diff --git a/src/nix/dotgraph.cc b/src/nix/dotgraph.cc new file mode 100644 index 000000000000..36daf7f9966d --- /dev/null +++ b/src/nix/dotgraph.cc @@ -0,0 +1,135 @@ +#include "dotgraph.hh" +#include "normalise.hh" + + +static string dotQuote(const string & s) +{ + return "\"" + s + "\""; +} + + +static string nextColour() +{ + static int n = 0; + static string colours[] = + { "black", "red", "green", "blue" + , "magenta", "burlywood" }; + return colours[n++ % (sizeof(colours) / sizeof(string))]; +} + + +static string makeEdge(const string & src, const string & dst) +{ + format f = format("%1% -> %2% [color = %3%];\n") + % dotQuote(src) % dotQuote(dst) % dotQuote(nextColour()); + return f.str(); +} + + +static string makeNode(const string & id, const string & label, + const string & colour) +{ + format f = format("%1% [label = %2%, shape = box, " + "style = filled, fillcolor = %3%];\n") + % dotQuote(id) % dotQuote(label) % dotQuote(colour); + return f.str(); +} + + +static string symbolicName(const string & path) +{ + string p = baseNameOf(path); + if (isHash(string(p, 0, Hash::hashSize * 2)) && + p[Hash::hashSize * 2] == '-') + p = string(p, Hash::hashSize * 2 + 1); + return p; +} + + +string pathLabel(const Path & nePath, const string & elemPath) +{ + return (string) nePath + "-" + elemPath; +} + + +void printClosure(const Path & nePath, const NixExpr & fs) +{ + PathSet workList(fs.closure.roots); + PathSet doneSet; + + for (PathSet::iterator i = workList.begin(); i != workList.end(); i++) { + cout << makeEdge(pathLabel(nePath, *i), nePath); + } + + while (!workList.empty()) { + Path path = *(workList.begin()); + workList.erase(path); + + if (doneSet.find(path) == doneSet.end()) { + doneSet.insert(path); + + ClosureElems::const_iterator elem = fs.closure.elems.find(path); + if (elem == fs.closure.elems.end()) + throw Error(format("bad closure, missing path `%1%'") % path); + + for (StringSet::const_iterator i = elem->second.refs.begin(); + i != elem->second.refs.end(); i++) + { + workList.insert(*i); + cout << makeEdge(pathLabel(nePath, *i), pathLabel(nePath, path)); + } + + cout << makeNode(pathLabel(nePath, path), + symbolicName(path), "#ff0000"); + } + } +} + + +void printDotGraph(const PathSet & roots) +{ + PathSet workList(roots); + PathSet doneSet; + + cout << "digraph G {\n"; + + while (!workList.empty()) { + Path nePath = *(workList.begin()); + workList.erase(nePath); + + if (doneSet.find(nePath) == doneSet.end()) { + doneSet.insert(nePath); + + NixExpr ne = exprFromPath(nePath); + + string label, colour; + + if (ne.type == NixExpr::neDerivation) { + for (PathSet::iterator i = ne.derivation.inputs.begin(); + i != ne.derivation.inputs.end(); i++) + { + workList.insert(*i); + cout << makeEdge(*i, nePath); + } + + label = "derivation"; + colour = "#00ff00"; + for (StringPairs::iterator i = ne.derivation.env.begin(); + i != ne.derivation.env.end(); i++) + if (i->first == "name") label = i->second; + } + + else if (ne.type == NixExpr::neClosure) { + label = ""; + colour = "#00ffff"; + printClosure(nePath, ne); + } + + else abort(); + + cout << makeNode(nePath, label, colour); + } + } + + cout << "}\n"; +} diff --git a/src/nix/dotgraph.hh b/src/nix/dotgraph.hh new file mode 100644 index 000000000000..a5c5248f144e --- /dev/null +++ b/src/nix/dotgraph.hh @@ -0,0 +1,8 @@ +#ifndef __DOTGRAPH_H +#define __DOTGRAPH_H + +#include "expr.hh" + +void printDotGraph(const PathSet & roots); + +#endif /* !__DOTGRAPH_H */ diff --git a/src/nix/nix-help.txt b/src/nix/nix-help.txt new file mode 100644 index 000000000000..bf2afd061205 --- /dev/null +++ b/src/nix/nix-help.txt @@ -0,0 +1,32 @@ +nix [OPTIONS...] [ARGUMENTS...] + +Operations: + + --install / -i: realise a Nix expression + --delete / -d: delete paths from the Nix store + --add / -A: copy a path to the Nix store + --query / -q: query information + + --successor: register a successor expression + --substitute: register a substitute expression + + --dump: dump a path as a Nix archive + --restore: restore a path from a Nix archive + + --init: initialise the Nix database + --verify: verify Nix structures + + --version: output version information + --help: display help + +Query flags: + + --list / -l: query the output paths (roots) of a Nix expression (default) + --requisites / -r: print all paths necessary to realise expression + --predecessors: print predecessors of a Nix expression + --graph: print a dot graph rooted at given ids + +Options: + + --verbose / -v: verbose operation (may be repeated) + --keep-failed / -K: keep temporary directories of failed builds diff --git a/src/nix/nix.cc b/src/nix/nix.cc new file mode 100644 index 000000000000..1875689990a4 --- /dev/null +++ b/src/nix/nix.cc @@ -0,0 +1,304 @@ +#include +#include + +#include "globals.hh" +#include "normalise.hh" +#include "archive.hh" +#include "shared.hh" +#include "dotgraph.hh" + + +typedef void (* Operation) (Strings opFlags, Strings opArgs); + + +static void printHelp() +{ + cout << +#include "nix-help.txt.hh" + ; + exit(0); +} + + + +static Path checkPath(const Path & arg) +{ + return arg; /* !!! check that arg is in the store */ +} + + +/* Realise (or install) paths from the given Nix expressions. */ +static void opInstall(Strings opFlags, Strings opArgs) +{ + if (!opFlags.empty()) throw UsageError("unknown flag"); + + for (Strings::iterator i = opArgs.begin(); + i != opArgs.end(); i++) + { + Path nfPath = normaliseNixExpr(checkPath(*i)); + realiseClosure(nfPath); + cout << format("%1%\n") % (string) nfPath; + } +} + + +/* Delete a path in the Nix store directory. */ +static void opDelete(Strings opFlags, Strings opArgs) +{ + if (!opFlags.empty()) throw UsageError("unknown flag"); + + for (Strings::iterator it = opArgs.begin(); + it != opArgs.end(); it++) + deleteFromStore(checkPath(*it)); +} + + +/* Add paths to the Nix values directory and print the hashes of those + paths. */ +static void opAdd(Strings opFlags, Strings opArgs) +{ + if (!opFlags.empty()) throw UsageError("unknown flag"); + + for (Strings::iterator i = opArgs.begin(); i != opArgs.end(); i++) + cout << format("%1%\n") % addToStore(*i); +} + + +Path maybeNormalise(const Path & ne, bool normalise) +{ + return normalise ? normaliseNixExpr(ne) : ne; +} + + +/* Perform various sorts of queries. */ +static void opQuery(Strings opFlags, Strings opArgs) +{ + enum { qList, qRequisites, qPredecessors, qGraph + } query = qList; + bool normalise = false; + bool includeExprs = true; + bool includeSuccessors = false; + + for (Strings::iterator i = opFlags.begin(); + i != opFlags.end(); i++) + if (*i == "--list" || *i == "-l") query = qList; + else if (*i == "--requisites" || *i == "-r") query = qRequisites; + else if (*i == "--predecessors") query = qPredecessors; + else if (*i == "--graph") query = qGraph; + else if (*i == "--normalise" || *i == "-n") normalise = true; + else if (*i == "--exclude-exprs") includeExprs = false; + else if (*i == "--include-successors") includeSuccessors = true; + else throw UsageError(format("unknown flag `%1%'") % *i); + + switch (query) { + + case qList: { + for (Strings::iterator i = opArgs.begin(); + i != opArgs.end(); i++) + { + StringSet paths = nixExprRoots( + maybeNormalise(checkPath(*i), normalise)); + for (StringSet::iterator j = paths.begin(); + j != paths.end(); j++) + cout << format("%s\n") % *j; + } + break; + } + + case qRequisites: { + StringSet paths; + for (Strings::iterator i = opArgs.begin(); + i != opArgs.end(); i++) + { + StringSet paths2 = nixExprRequisites( + maybeNormalise(checkPath(*i), normalise), + includeExprs, includeSuccessors); + paths.insert(paths2.begin(), paths2.end()); + } + for (StringSet::iterator i = paths.begin(); + i != paths.end(); i++) + cout << format("%s\n") % *i; + break; + } + + case qPredecessors: { + for (Strings::iterator i = opArgs.begin(); + i != opArgs.end(); i++) + { + Paths preds = queryPredecessors(checkPath(*i)); + for (Paths::iterator j = preds.begin(); + j != preds.end(); j++) + cout << format("%s\n") % *j; + } + break; + } + + case qGraph: { + PathSet roots; + for (Strings::iterator i = opArgs.begin(); + i != opArgs.end(); i++) + roots.insert(maybeNormalise(checkPath(*i), normalise)); + printDotGraph(roots); + break; + } + + default: + abort(); + } +} + + +static void opSuccessor(Strings opFlags, Strings opArgs) +{ + if (!opFlags.empty()) throw UsageError("unknown flag"); + if (opArgs.size() % 2) throw UsageError("expecting even number of arguments"); + + Transaction txn; + createStoreTransaction(txn); + for (Strings::iterator i = opArgs.begin(); + i != opArgs.end(); ) + { + Path path1 = checkPath(*i++); + Path path2 = checkPath(*i++); + registerSuccessor(txn, path1, path2); + } + txn.commit(); +} + + +static void opSubstitute(Strings opFlags, Strings opArgs) +{ + if (!opFlags.empty()) throw UsageError("unknown flag"); + if (opArgs.size() % 2) throw UsageError("expecting even number of arguments"); + + for (Strings::iterator i = opArgs.begin(); + i != opArgs.end(); ) + { + Path src = checkPath(*i++); + Path sub = checkPath(*i++); + registerSubstitute(src, sub); + } +} + + +/* A sink that writes dump output to stdout. */ +struct StdoutSink : DumpSink +{ + virtual void operator () + (const unsigned char * data, unsigned int len) + { + writeFull(STDOUT_FILENO, data, len); + } +}; + + +/* Dump a path as a Nix archive. The archive is written to standard + output. */ +static void opDump(Strings opFlags, Strings opArgs) +{ + if (!opFlags.empty()) throw UsageError("unknown flag"); + if (opArgs.size() != 1) throw UsageError("only one argument allowed"); + + StdoutSink sink; + string path = *opArgs.begin(); + dumpPath(path, sink); +} + + +/* A source that read restore intput to stdin. */ +struct StdinSource : RestoreSource +{ + virtual void operator () (unsigned char * data, unsigned int len) + { + readFull(STDIN_FILENO, data, len); + } +}; + + +/* Restore a value from a Nix archive. The archive is written to + standard input. */ +static void opRestore(Strings opFlags, Strings opArgs) +{ + if (!opFlags.empty()) throw UsageError("unknown flag"); + if (opArgs.size() != 1) throw UsageError("only one argument allowed"); + + StdinSource source; + restorePath(*opArgs.begin(), source); +} + + +/* Initialise the Nix databases. */ +static void opInit(Strings opFlags, Strings opArgs) +{ + if (!opFlags.empty()) throw UsageError("unknown flag"); + if (!opArgs.empty()) + throw UsageError("--init does not have arguments"); + initDB(); +} + + +/* Verify the consistency of the Nix environment. */ +static void opVerify(Strings opFlags, Strings opArgs) +{ + verifyStore(); +} + + +/* Scan the arguments; find the operation, set global flags, put all + other flags in a list, and put all other arguments in another + list. */ +void run(Strings args) +{ + Strings opFlags, opArgs; + Operation op = 0; + + for (Strings::iterator it = args.begin(); it != args.end(); ) + { + string arg = *it++; + + Operation oldOp = op; + + if (arg == "--install" || arg == "-i") + op = opInstall; + else if (arg == "--delete" || arg == "-d") + op = opDelete; + else if (arg == "--add" || arg == "-A") + op = opAdd; + else if (arg == "--query" || arg == "-q") + op = opQuery; + else if (arg == "--successor") + op = opSuccessor; + else if (arg == "--substitute") + op = opSubstitute; + else if (arg == "--dump") + op = opDump; + else if (arg == "--restore") + op = opRestore; + else if (arg == "--init") + op = opInit; + else if (arg == "--verify") + op = opVerify; + else if (arg == "--verbose" || arg == "-v") + verbosity = (Verbosity) ((int) verbosity + 1); + else if (arg == "--keep-failed" || arg == "-K") + keepFailed = true; + else if (arg == "--help") + printHelp(); + else if (arg[0] == '-') + opFlags.push_back(arg); + else + opArgs.push_back(arg); + + if (oldOp && oldOp != op) + throw UsageError("only one operation may be specified"); + } + + if (!op) throw UsageError("no operation specified"); + + openDB(); + + op(opFlags, opArgs); +} + + +string programId = "nix"; diff --git a/src/normalise.cc b/src/normalise.cc deleted file mode 100644 index be71081ffb25..000000000000 --- a/src/normalise.cc +++ /dev/null @@ -1,469 +0,0 @@ -#include - -#include "normalise.hh" -#include "references.hh" -#include "exec.hh" -#include "pathlocks.hh" -#include "globals.hh" - - -static Path useSuccessor(const Path & path) -{ - string pathSucc; - if (querySuccessor(path, pathSucc)) { - debug(format("successor %1% -> %2%") % (string) path % pathSucc); - return pathSucc; - } else - return path; -} - - -#if 0 -/* Return a path whose contents have the given hash. If target is - not empty, ensure that such a path is realised in target (if - necessary by copying from another location). If prefix is not - empty, only return a path that is an descendent of prefix. */ - -string expandId(const FSId & id, const string & target = "", - const string & prefix = "/", FSIdSet pending = FSIdSet(), - bool ignoreSubstitutes = false) -{ - xxx -} - - -string expandId(const FSId & id, const string & target, - const string & prefix, FSIdSet pending, bool ignoreSubstitutes) -{ - Nest nest(lvlDebug, format("expanding %1%") % (string) id); - - Strings paths; - - if (!target.empty() && !isInPrefix(target, prefix)) - abort(); - - nixDB.queryStrings(noTxn, dbId2Paths, id, paths); - - /* Pick one equal to `target'. */ - if (!target.empty()) { - - for (Strings::iterator i = paths.begin(); - i != paths.end(); i++) - { - string path = *i; - if (path == target && pathExists(path)) - return path; - } - - } - - /* Arbitrarily pick the first one that exists and isn't stale. */ - for (Strings::iterator it = paths.begin(); - it != paths.end(); it++) - { - string path = *it; - if (isInPrefix(path, prefix) && pathExists(path)) { - if (target.empty()) - return path; - else { - /* Acquire a lock on the target path. */ - Strings lockPaths; - lockPaths.push_back(target); - PathLocks outputLock(lockPaths); - - /* Copy. */ - copyPath(path, target); - - /* Register the target path. */ - Transaction txn(nixDB); - registerPath(txn, target, id); - txn.commit(); - - return target; - } - } - } - - if (!ignoreSubstitutes) { - - if (pending.find(id) != pending.end()) - throw Error(format("id %1% already being expanded") % (string) id); - pending.insert(id); - - /* Try to realise the substitutes, but only if this id is not - already being realised by a substitute. */ - Strings subs; - nixDB.queryStrings(noTxn, dbSubstitutes, id, subs); /* non-existence = ok */ - - for (Strings::iterator it = subs.begin(); it != subs.end(); it++) { - FSId subId = parseHash(*it); - - debug(format("trying substitute %1%") % (string) subId); - - realiseClosure(normaliseNixExpr(subId, pending), pending); - - return expandId(id, target, prefix, pending); - } - - } - - throw Error(format("cannot expand id `%1%'") % (string) id); -} -#endif - - -Path normaliseNixExpr(const Path & _nePath, PathSet pending) -{ - Nest nest(lvlTalkative, - format("normalising expression in `%1%'") % (string) _nePath); - - /* Try to substitute the expression by any known successors in - order to speed up the rewrite process. */ - Path nePath = useSuccessor(_nePath); - - /* Get the Nix expression. */ - NixExpr ne = exprFromPath(nePath, pending); - - /* If this is a normal form (i.e., a closure) we are done. */ - if (ne.type == NixExpr::neClosure) return nePath; - if (ne.type != NixExpr::neDerivation) abort(); - - - /* Otherwise, it's a derivation expression, and we have to build it to - determine its normal form. */ - - - /* Some variables. */ - - /* Input paths, with their closure elements. */ - ClosureElems inClosures; - - /* Referenceable paths (i.e., input and output paths). */ - PathSet allPaths; - - /* The environment to be passed to the builder. */ - Environment env; - - /* The result. */ - NixExpr nf; - nf.type = NixExpr::neClosure; - - - /* The outputs are referenceable paths. */ - for (PathSet::iterator i = ne.derivation.outputs.begin(); - i != ne.derivation.outputs.end(); i++) - { - debug(format("building path `%1%'") % *i); - allPaths.insert(*i); - } - - /* Obtain locks on all output paths. The locks are automatically - released when we exit this function or Nix crashes. */ - PathLocks outputLocks(ne.derivation.outputs); - - /* Now check again whether there is a successor. This is because - another process may have started building in parallel. After - it has finished and released the locks, we can (and should) - reuse its results. (Strictly speaking the first successor - check above can be omitted, but that would be less efficient.) - Note that since we now hold the locks on the output paths, no - other process can build this expression, so no further checks - are necessary. */ - { - Path nePath2 = useSuccessor(nePath); - if (nePath != nePath2) { - NixExpr ne = exprFromPath(nePath2, pending); - debug(format("skipping build of expression `%1%', someone beat us to it") - % (string) nePath); - if (ne.type != NixExpr::neClosure) abort(); - return nePath2; - } - } - - /* Right platform? */ - if (ne.derivation.platform != thisSystem) - throw Error(format("a `%1%' is required, but I am a `%2%'") - % ne.derivation.platform % thisSystem); - - /* Realise inputs (and remember all input paths). */ - for (PathSet::iterator i = ne.derivation.inputs.begin(); - i != ne.derivation.inputs.end(); i++) - { - Path nfPath = normaliseNixExpr(*i, pending); - realiseClosure(nfPath, pending); - /* !!! nfPath should be a root of the garbage collector while - we are building */ - NixExpr ne = exprFromPath(nfPath, pending); - if (ne.type != NixExpr::neClosure) abort(); - for (ClosureElems::iterator j = ne.closure.elems.begin(); - j != ne.closure.elems.end(); j++) - { - inClosures[j->first] = j->second; - allPaths.insert(j->first); - } - } - - /* Most shells initialise PATH to some default (/bin:/usr/bin:...) when - PATH is not set. We don't want this, so we fill it in with some dummy - value. */ - env["PATH"] = "/path-not-set"; - - /* Set HOME to a non-existing path to prevent certain programs from using - /etc/passwd (or NIS, or whatever) to locate the home directory (for - example, wget looks for ~/.wgetrc). I.e., these tools use /etc/passwd - if HOME is not set, but they will just assume that the settings file - they are looking for does not exist if HOME is set but points to some - non-existing path. */ - env["HOME"] = "/homeless-shelter"; - - /* Build the environment. */ - for (StringPairs::iterator i = ne.derivation.env.begin(); - i != ne.derivation.env.end(); i++) - env[i->first] = i->second; - - /* We can skip running the builder if we can expand all output - paths from their ids. */ - bool fastBuild = false; -#if 0 - bool fastBuild = true; - for (PathSet::iterator i = ne.derivation.outputs.begin(); - i != ne.derivation.outputs.end(); i++) - { - try { - expandId(i->second, i->first, "/", pending); - } catch (Error & e) { - debug(format("fast build failed for `%1%': %2%") - % i->first % e.what()); - fastBuild = false; - break; - } - } -#endif - - if (!fastBuild) { - - /* If any of the outputs already exist but are not registered, - delete them. */ - for (PathSet::iterator i = ne.derivation.outputs.begin(); - i != ne.derivation.outputs.end(); i++) - { - Path path = *i; - if (isValidPath(path)) - throw Error(format("obstructed build: path `%1%' exists") % path); - if (pathExists(path)) { - debug(format("removing unregistered path `%1%'") % path); - deletePath(path); - } - } - - /* Run the builder. */ - msg(lvlChatty, format("building...")); - runProgram(ne.derivation.builder, ne.derivation.args, env); - msg(lvlChatty, format("build completed")); - - } else - msg(lvlChatty, format("fast build succesful")); - - /* Check whether the output paths were created, and grep each - output path to determine what other paths it references. Also make all - output paths read-only. */ - PathSet usedPaths; - for (PathSet::iterator i = ne.derivation.outputs.begin(); - i != ne.derivation.outputs.end(); i++) - { - Path path = *i; - if (!pathExists(path)) - throw Error(format("path `%1%' does not exist") % path); - nf.closure.roots.insert(path); - - makePathReadOnly(path); - - /* For this output path, find the references to other paths contained - in it. */ - Strings refPaths = filterReferences(path, - Strings(allPaths.begin(), allPaths.end())); - - /* Construct a closure element for this output path. */ - ClosureElem elem; - - /* For each path referenced by this output path, add its id to the - closure element and add the id to the `usedPaths' set (so that the - elements referenced by *its* closure are added below). */ - for (Paths::iterator j = refPaths.begin(); - j != refPaths.end(); j++) - { - Path path = *j; - elem.refs.insert(path); - if (inClosures.find(path) != inClosures.end()) - usedPaths.insert(path); - else if (ne.derivation.outputs.find(path) == ne.derivation.outputs.end()) - abort(); - } - - nf.closure.elems[path] = elem; - } - - /* Close the closure. That is, for any referenced path, add the paths - referenced by it. */ - PathSet donePaths; - - while (!usedPaths.empty()) { - PathSet::iterator i = usedPaths.begin(); - Path path = *i; - usedPaths.erase(i); - - if (donePaths.find(path) != donePaths.end()) continue; - donePaths.insert(path); - - ClosureElems::iterator j = inClosures.find(path); - if (j == inClosures.end()) abort(); - - nf.closure.elems[path] = j->second; - - for (PathSet::iterator k = j->second.refs.begin(); - k != j->second.refs.end(); k++) - usedPaths.insert(*k); - } - - /* For debugging, print out the referenced and unreferenced paths. */ - for (ClosureElems::iterator i = inClosures.begin(); - i != inClosures.end(); i++) - { - PathSet::iterator j = donePaths.find(i->first); - if (j == donePaths.end()) - debug(format("NOT referenced: `%1%'") % i->first); - else - debug(format("referenced: `%1%'") % i->first); - } - - /* Write the normal form. This does not have to occur in the - transaction below because writing terms is idem-potent. */ - ATerm nfTerm = unparseNixExpr(nf); - msg(lvlVomit, format("normal form: %1%") % printTerm(nfTerm)); - Path nfPath = writeTerm(nfTerm, "-s"); - - /* Register each outpat path, and register the normal form. This - is wrapped in one database transaction to ensure that if we - crash, either everything is registered or nothing is. This is - for recoverability: unregistered paths in the store can be - deleted arbitrarily, while registered paths can only be deleted - by running the garbage collector. */ - Transaction txn; - createStoreTransaction(txn); - for (PathSet::iterator i = ne.derivation.outputs.begin(); - i != ne.derivation.outputs.end(); i++) - registerValidPath(txn, *i); - registerSuccessor(txn, nePath, nfPath); - txn.commit(); - - return nfPath; -} - - -void realiseClosure(const Path & nePath, PathSet pending) -{ - Nest nest(lvlDebug, format("realising closure `%1%'") % nePath); - - NixExpr ne = exprFromPath(nePath, pending); - if (ne.type != NixExpr::neClosure) - throw Error(format("expected closure in `%1%'") % nePath); - - for (ClosureElems::const_iterator i = ne.closure.elems.begin(); - i != ne.closure.elems.end(); i++) - ensurePath(i->first, pending); -} - - -void ensurePath(const Path & path, PathSet pending) -{ - /* If the path is already valid, we're done. */ - if (isValidPath(path)) return; - - /* Otherwise, try the substitutes. */ - Paths subPaths = querySubstitutes(path); - - for (Paths::iterator i = subPaths.begin(); - i != subPaths.end(); i++) - { - try { - normaliseNixExpr(*i, pending); - if (isValidPath(path)) return; - throw Error(format("substitute failed to produce expected output path")); - } catch (Error & e) { - msg(lvlTalkative, - format("building of substitute `%1%' for `%2%' failed: %3%") - % *i % path % e.what()); - } - } - - throw Error(format("path `%1%' is required, " - "but there are no (successful) substitutes") % path); -} - - -NixExpr exprFromPath(const Path & path, PathSet pending) -{ - ensurePath(path, pending); - ATerm t = ATreadFromNamedFile(path.c_str()); - if (!t) throw Error(format("cannot read aterm from `%1%'") % path); - return parseNixExpr(t); -} - - -PathSet nixExprRoots(const Path & nePath) -{ - PathSet paths; - - NixExpr ne = exprFromPath(nePath); - - if (ne.type == NixExpr::neClosure) - paths.insert(ne.closure.roots.begin(), ne.closure.roots.end()); - else if (ne.type == NixExpr::neDerivation) - paths.insert(ne.derivation.outputs.begin(), - ne.derivation.outputs.end()); - else abort(); - - return paths; -} - - -static void requisitesWorker(const Path & nePath, - bool includeExprs, bool includeSuccessors, - PathSet & paths, PathSet & doneSet) -{ - if (doneSet.find(nePath) != doneSet.end()) return; - doneSet.insert(nePath); - - NixExpr ne = exprFromPath(nePath); - - if (ne.type == NixExpr::neClosure) - for (ClosureElems::iterator i = ne.closure.elems.begin(); - i != ne.closure.elems.end(); i++) - paths.insert(i->first); - - else if (ne.type == NixExpr::neDerivation) - for (PathSet::iterator i = ne.derivation.inputs.begin(); - i != ne.derivation.inputs.end(); i++) - requisitesWorker(*i, - includeExprs, includeSuccessors, paths, doneSet); - - else abort(); - - if (includeExprs) paths.insert(nePath); - - string nfPath; - if (includeSuccessors && (nfPath = useSuccessor(nePath)) != nePath) - requisitesWorker(nfPath, includeExprs, includeSuccessors, - paths, doneSet); -} - - -PathSet nixExprRequisites(const Path & nePath, - bool includeExprs, bool includeSuccessors) -{ - PathSet paths; - PathSet doneSet; - requisitesWorker(nePath, includeExprs, includeSuccessors, - paths, doneSet); - return paths; -} diff --git a/src/normalise.hh b/src/normalise.hh deleted file mode 100644 index bbe846404cc0..000000000000 --- a/src/normalise.hh +++ /dev/null @@ -1,46 +0,0 @@ -#ifndef __NORMALISE_H -#define __NORMALISE_H - -#include "expr.hh" - - -/* Normalise a Nix expression. That is, if the expression is a - derivation, a path containing an equivalent closure expression is - returned. This requires that the derivation is performed, unless a - successor is known. */ -Path normaliseNixExpr(const Path & nePath, PathSet pending = PathSet()); - -/* Realise a closure expression in the file system. - - The pending paths are those that are already being realised. This - prevents infinite recursion for paths realised through a substitute - (since when we build the substitute, we would first try to realise - its output paths through substitutes... kaboom!). */ -void realiseClosure(const Path & nePath, PathSet pending = PathSet()); - -/* Ensure that a path exists, possibly by instantiating it by - realising a substitute. */ -void ensurePath(const Path & path, PathSet pending = PathSet()); - -/* Read a Nix expression, after ensuring its existence through - ensurePath(). */ -NixExpr exprFromPath(const Path & path, PathSet pending = PathSet()); - -/* Get the list of root (output) paths of the given Nix expression. */ -PathSet nixExprRoots(const Path & nePath); - -/* Get the list of paths that are required to realise the given - expression. For a derive expression, this is the union of - requisites of the inputs; for a closure expression, it is the path of - each element in the closure. If `includeExprs' is true, include the - paths of the Nix expressions themselves. If `includeSuccessors' is - true, include the requisites of successors. */ -PathSet nixExprRequisites(const Path & nePath, - bool includeExprs, bool includeSuccessors); - -/* Return the list of the paths of all known Nix expressions whose - output paths are completely contained in the set `outputs'. */ -PathSet findGenerators(const PathSet & outputs); - - -#endif /* !__NORMALISE_H */ diff --git a/src/pathlocks.cc b/src/pathlocks.cc deleted file mode 100644 index 3ecbbbcbafd5..000000000000 --- a/src/pathlocks.cc +++ /dev/null @@ -1,90 +0,0 @@ -#include - -#include -#include -#include - -#include "pathlocks.hh" - - -bool lockFile(int fd, LockType lockType, bool wait) -{ - struct flock lock; - if (lockType == ltRead) lock.l_type = F_RDLCK; - else if (lockType == ltWrite) lock.l_type = F_WRLCK; - else if (lockType == ltNone) lock.l_type = F_UNLCK; - else abort(); - lock.l_whence = SEEK_SET; - lock.l_start = 0; - lock.l_len = 0; /* entire file */ - - if (wait) { - while (fcntl(fd, F_SETLKW, &lock) != 0) - if (errno != EINTR) - throw SysError(format("acquiring/releasing lock")); - } else { - while (fcntl(fd, F_SETLK, &lock) != 0) { - if (errno == EACCES || errno == EAGAIN) return false; - if (errno != EINTR) - throw SysError(format("acquiring/releasing lock")); - } - } - - return true; -} - - -/* This enables us to check whether are not already holding a lock on - a file ourselves. POSIX locks (fcntl) suck in this respect: if we - close a descriptor, the previous lock will be closed as well. And - there is no way to query whether we already have a lock (F_GETLK - only works on locks held by other processes). */ -static StringSet lockedPaths; /* !!! not thread-safe */ - - -PathLocks::PathLocks(const PathSet & _paths) -{ - /* Note that `fds' is built incrementally so that the destructor - will only release those locks that we have already acquired. */ - - /* Sort the paths. This assures that locks are always acquired in - the same order, thus preventing deadlocks. */ - Paths paths(_paths.begin(), _paths.end()); - paths.sort(); - - /* Acquire the lock for each path. */ - for (Paths::iterator i = paths.begin(); i != paths.end(); i++) { - Path path = *i; - Path lockPath = path + ".lock"; - - debug(format("locking path `%1%'") % path); - - if (lockedPaths.find(lockPath) != lockedPaths.end()) { - debug(format("already holding lock on `%1%'") % lockPath); - continue; - } - - /* Open/create the lock file. */ - int fd = open(lockPath.c_str(), O_WRONLY | O_CREAT, 0666); - if (fd == -1) - throw SysError(format("opening lock file `%1%'") % lockPath); - - fds.push_back(fd); - this->paths.push_back(lockPath); - - /* Acquire an exclusive lock. */ - lockFile(fd, ltWrite, true); - - lockedPaths.insert(lockPath); - } -} - - -PathLocks::~PathLocks() -{ - for (list::iterator i = fds.begin(); i != fds.end(); i++) - close(*i); - - for (Paths::iterator i = paths.begin(); i != paths.end(); i++) - lockedPaths.erase(*i); -} diff --git a/src/pathlocks.hh b/src/pathlocks.hh deleted file mode 100644 index ce61386d6df0..000000000000 --- a/src/pathlocks.hh +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef __PATHLOCKS_H -#define __PATHLOCKS_H - -#include "util.hh" - - -typedef enum LockType { ltRead, ltWrite, ltNone }; - -bool lockFile(int fd, LockType lockType, bool wait); - - -class PathLocks -{ -private: - list fds; - Paths paths; - -public: - PathLocks(const PathSet & _paths); - ~PathLocks(); -}; - - -#endif /* !__PATHLOCKS_H */ diff --git a/src/references.cc b/src/references.cc deleted file mode 100644 index be432665b884..000000000000 --- a/src/references.cc +++ /dev/null @@ -1,110 +0,0 @@ -#include -#include - -#include -#include -#include -#include -#include - -#include "references.hh" -#include "hash.hh" - - -static void search(const string & s, - Strings & ids, Strings & seen) -{ - for (Strings::iterator i = ids.begin(); - i != ids.end(); ) - { - if (s.find(*i) == string::npos) - i++; - else { - debug(format("found reference to `%1%'") % *i); - seen.push_back(*i); - i = ids.erase(i); - } - } -} - - -void checkPath(const string & path, - Strings & ids, Strings & seen) -{ - struct stat st; - if (lstat(path.c_str(), &st)) - throw SysError(format("getting attributes of path `%1%'") % path); - - if (S_ISDIR(st.st_mode)) { - DIR * dir = opendir(path.c_str()); - - struct dirent * dirent; - while (errno = 0, dirent = readdir(dir)) { - string name = dirent->d_name; - if (name == "." || name == "..") continue; - search(name, ids, seen); - checkPath(path + "/" + name, ids, seen); - } - - closedir(dir); /* !!! close on exception */ - } - - else if (S_ISREG(st.st_mode)) { - - debug(format("checking `%1%'") % path); - - int fd = open(path.c_str(), O_RDONLY); - if (fd == -1) throw SysError(format("opening file `%1%'") % path); - - unsigned char * buf = new unsigned char[st.st_size]; - - readFull(fd, buf, st.st_size); - - search(string((char *) buf, st.st_size), ids, seen); - - delete buf; /* !!! autodelete */ - - close(fd); /* !!! close on exception */ - } - - else if (S_ISLNK(st.st_mode)) { - char buf[st.st_size]; - if (readlink(path.c_str(), buf, st.st_size) != st.st_size) - throw SysError(format("reading symbolic link `%1%'") % path); - search(string(buf, st.st_size), ids, seen); - } - - else throw Error(format("unknown file type: %1%") % path); -} - - -Strings filterReferences(const string & path, const Strings & paths) -{ - map backMap; - Strings ids; - Strings seen; - - /* For efficiency (and a higher hit rate), just search for the - hash part of the file name. (This assumes that all references - have the form `HASH-bla'). */ - for (Strings::const_iterator i = paths.begin(); - i != paths.end(); i++) - { - string s = string(baseNameOf(*i), 0, 32); - parseHash(s); - ids.push_back(s); - backMap[s] = *i; - } - - checkPath(path, ids, seen); - - Strings found; - for (Strings::iterator i = seen.begin(); i != seen.end(); i++) - { - map::iterator j; - if ((j = backMap.find(*i)) == backMap.end()) abort(); - found.push_back(j->second); - } - - return found; -} diff --git a/src/references.hh b/src/references.hh deleted file mode 100644 index d009453d6a00..000000000000 --- a/src/references.hh +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef __VALUES_H -#define __VALUES_H - -#include "util.hh" - - -Strings filterReferences(const Path & path, const Strings & refs); - - -#endif /* !__VALUES_H */ diff --git a/src/shared.cc b/src/shared.cc deleted file mode 100644 index 80463308a3f0..000000000000 --- a/src/shared.cc +++ /dev/null @@ -1,79 +0,0 @@ -#include -#include - -extern "C" { -#include -} - -#include "globals.hh" -#include "shared.hh" - -#include "config.h" - - -/* Initialize and reorder arguments, then call the actual argument - processor. */ -static void initAndRun(int argc, char * * argv) -{ - /* Setup Nix paths. */ - nixStore = NIX_STORE_DIR; - nixDataDir = NIX_DATA_DIR; - nixLogDir = NIX_LOG_DIR; - nixDBPath = (string) NIX_STATE_DIR + "/db"; - - /* Put the arguments in a vector. */ - Strings args; - while (argc--) args.push_back(*argv++); - args.erase(args.begin()); - - /* Expand compound dash options (i.e., `-qlf' -> `-q -l -f'). */ - for (Strings::iterator it = args.begin(); - it != args.end(); ) - { - string arg = *it; - if (arg.length() > 2 && arg[0] == '-' && arg[1] != '-') { - for (unsigned int i = 1; i < arg.length(); i++) - if (isalpha(arg[i])) - args.insert(it, (string) "-" + arg[i]); - else { - args.insert(it, string(arg, i)); - break; - } - it = args.erase(it); - } else it++; - } - - run(args); -} - - -static char buf[1024]; - -int main(int argc, char * * argv) -{ - /* ATerm setup. */ - ATerm bottomOfStack; - ATinit(argc, argv, &bottomOfStack); - - /* Turn on buffering for cerr. */ - cerr.rdbuf()->pubsetbuf(buf, sizeof(buf)); - - try { - initAndRun(argc, argv); - } catch (UsageError & e) { - msg(lvlError, - format( - "error: %1%\n" - "Try `%2% --help' for more information.") - % e.what() % programId); - return 1; - } catch (Error & e) { - msg(lvlError, format("error: %1%") % e.msg()); - return 1; - } catch (exception & e) { - msg(lvlError, format("error: %1%") % e.what()); - return 1; - } - - return 0; -} diff --git a/src/shared.hh b/src/shared.hh deleted file mode 100644 index 8ea637fd21ac..000000000000 --- a/src/shared.hh +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef __SHARED_H -#define __SHARED_H - -#include - -#include "util.hh" - - -void run(Strings args); - - -extern string programId; - - -#endif /* !__SHARED_H */ diff --git a/src/store.cc b/src/store.cc deleted file mode 100644 index 2d223313b612..000000000000 --- a/src/store.cc +++ /dev/null @@ -1,405 +0,0 @@ -#include - -#include -#include -#include -#include -#include - -#include "store.hh" -#include "globals.hh" -#include "db.hh" -#include "archive.hh" -#include "pathlocks.hh" - - -/* Nix database. */ -static Database nixDB; - - -/* Database tables. */ - -/* dbValidPaths :: Path -> () - - The existence of a key $p$ indicates that path $p$ is valid (that - is, produced by a succesful build). */ -static TableId dbValidPaths; - -/* dbSuccessors :: Path -> Path - - Each pair $(p_1, p_2)$ in this mapping records the fact that the - Nix expression stored at path $p_1$ has a successor expression - stored at path $p_2$. - - Note that a term $y$ is a successor of $x$ iff there exists a - sequence of rewrite steps that rewrites $x$ into $y$. -*/ -static TableId dbSuccessors; - -/* dbSuccessorsRev :: Path -> [Path] - - The reverse mapping of dbSuccessors (i.e., it stores the - predecessors of a Nix expression). -*/ -static TableId dbSuccessorsRev; - -/* dbSubstitutes :: Path -> [Path] - - Each pair $(p, [ps])$ tells Nix that it can realise any of the - Nix expressions stored at paths $ps$ to produce a path $p$. - - The main purpose of this is for distributed caching of derivates. - One system can compute a derivate and put it on a website (as a Nix - archive), for instance, and then another system can register a - substitute for that derivate. The substitute in this case might be - a Nix expression that fetches the Nix archive. -*/ -static TableId dbSubstitutes; - -/* dbSubstitutesRev :: Path -> [Path] - - The reverse mapping of dbSubstitutes. -*/ -static TableId dbSubstitutesRev; - - -void openDB() -{ - nixDB.open(nixDBPath); - dbValidPaths = nixDB.openTable("validpaths"); - dbSuccessors = nixDB.openTable("successors"); - dbSuccessorsRev = nixDB.openTable("successors-rev"); - dbSubstitutes = nixDB.openTable("substitutes"); - dbSubstitutesRev = nixDB.openTable("substitutes-rev"); -} - - -void initDB() -{ -} - - -void createStoreTransaction(Transaction & txn) -{ - Transaction txn2(nixDB); - txn2.moveTo(txn); -} - - -/* Path copying. */ - -struct CopySink : DumpSink -{ - int fd; - virtual void operator () (const unsigned char * data, unsigned int len) - { - writeFull(fd, data, len); - } -}; - - -struct CopySource : RestoreSource -{ - int fd; - virtual void operator () (unsigned char * data, unsigned int len) - { - readFull(fd, data, len); - } -}; - - -void copyPath(const Path & src, const Path & dst) -{ - debug(format("copying `%1%' to `%2%'") % src % dst); - - /* Unfortunately C++ doesn't support coprocedures, so we have no - nice way to chain CopySink and CopySource together. Instead we - fork off a child to run the sink. (Fork-less platforms should - use a thread). */ - - /* Create a pipe. */ - int fds[2]; - if (pipe(fds) == -1) throw SysError("creating pipe"); - - /* Fork. */ - pid_t pid; - switch (pid = fork()) { - - case -1: - throw SysError("unable to fork"); - - case 0: /* child */ - try { - close(fds[1]); - CopySource source; - source.fd = fds[0]; - restorePath(dst, source); - _exit(0); - } catch (exception & e) { - cerr << "error: " << e.what() << endl; - } - _exit(1); - } - - close(fds[0]); - - /* Parent. */ - - CopySink sink; - sink.fd = fds[1]; - dumpPath(src, sink); - - /* Wait for the child to finish. */ - int status; - if (waitpid(pid, &status, 0) != pid) - throw SysError("waiting for child"); - - if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) - throw Error("cannot copy file: child died"); -} - - -void registerSuccessor(const Transaction & txn, - const Path & srcPath, const Path & sucPath) -{ - Path known; - if (nixDB.queryString(txn, dbSuccessors, srcPath, known) && - known != sucPath) - { - throw Error(format( - "the `impossible' happened: expression in path " - "`%1%' appears to have multiple successors " - "(known `%2%', new `%3%'") - % srcPath % known % sucPath); - } - - Paths revs; - nixDB.queryStrings(txn, dbSuccessorsRev, sucPath, revs); - if (find(revs.begin(), revs.end(), srcPath) == revs.end()) - revs.push_back(srcPath); - - nixDB.setString(txn, dbSuccessors, srcPath, sucPath); - nixDB.setStrings(txn, dbSuccessorsRev, sucPath, revs); -} - - -bool querySuccessor(const Path & srcPath, Path & sucPath) -{ - return nixDB.queryString(noTxn, dbSuccessors, srcPath, sucPath); -} - - -Paths queryPredecessors(const Path & sucPath) -{ - Paths revs; - nixDB.queryStrings(noTxn, dbSuccessorsRev, sucPath, revs); - return revs; -} - - -void registerSubstitute(const Path & srcPath, const Path & subPath) -{ - Transaction txn(nixDB); - - Paths subs; - nixDB.queryStrings(txn, dbSubstitutes, srcPath, subs); - - if (find(subs.begin(), subs.end(), subPath) != subs.end()) { - /* Nothing to do if the substitute is already known. */ - txn.abort(); - return; - } - subs.push_front(subPath); /* new substitutes take precedence */ - - Paths revs; - nixDB.queryStrings(txn, dbSubstitutesRev, subPath, revs); - if (find(revs.begin(), revs.end(), srcPath) == revs.end()) - revs.push_back(srcPath); - - nixDB.setStrings(txn, dbSubstitutes, srcPath, subs); - nixDB.setStrings(txn, dbSubstitutesRev, subPath, revs); - - txn.commit(); -} - - -Paths querySubstitutes(const Path & srcPath) -{ - Paths subPaths; - nixDB.queryStrings(noTxn, dbSubstitutes, srcPath, subPaths); - return subPaths; -} - - -void registerValidPath(const Transaction & txn, const Path & _path) -{ - Path path(canonPath(_path)); - debug(format("registering path `%1%'") % path); - nixDB.setString(txn, dbValidPaths, path, ""); -} - - -bool isValidPath(const Path & path) -{ - string s; - return nixDB.queryString(noTxn, dbValidPaths, path, s); -} - - -void unregisterValidPath(const Path & _path) -{ - Path path(canonPath(_path)); - Transaction txn(nixDB); - - debug(format("unregistering path `%1%'") % path); - - nixDB.delPair(txn, dbValidPaths, path); - - txn.commit(); -} - - -static bool isInPrefix(const string & path, const string & _prefix) -{ - string prefix = canonPath(_prefix + "/"); - return string(path, 0, prefix.size()) == prefix; -} - - -Path addToStore(const Path & _srcPath) -{ - Path srcPath(absPath(_srcPath)); - debug(format("adding `%1%' to the store") % srcPath); - - Hash h = hashPath(srcPath); - - string baseName = baseNameOf(srcPath); - Path dstPath = canonPath(nixStore + "/" + (string) h + "-" + baseName); - - if (!isValidPath(dstPath)) { - - /* The first check above is an optimisation to prevent - unnecessary lock acquisition. */ - - PathSet lockPaths; - lockPaths.insert(dstPath); - PathLocks outputLock(lockPaths); - - if (!isValidPath(dstPath)) { - copyPath(srcPath, dstPath); - - Transaction txn(nixDB); - registerValidPath(txn, dstPath); - txn.commit(); - } - } - - return dstPath; -} - - -void addTextToStore(const Path & dstPath, const string & s) -{ - if (!isValidPath(dstPath)) { - - /* !!! locking? -> parallel writes are probably idempotent */ - - int fd = open(dstPath.c_str(), O_CREAT | O_EXCL | O_WRONLY, 0666); - if (fd == -1) throw SysError(format("creating store file `%1%'") % dstPath); - - if (write(fd, s.c_str(), s.size()) != (ssize_t) s.size()) - throw SysError(format("writing store file `%1%'") % dstPath); - - close(fd); /* !!! close on exception */ - - Transaction txn(nixDB); - registerValidPath(txn, dstPath); - txn.commit(); - } -} - - -void deleteFromStore(const Path & _path) -{ - Path path(canonPath(_path)); - - if (!isInPrefix(path, nixStore)) - throw Error(format("path `%1%' is not in the store") % path); - - unregisterValidPath(path); - - deletePath(path); -} - - -void verifyStore() -{ - Transaction txn(nixDB); - - Paths paths; - nixDB.enumTable(txn, dbValidPaths, paths); - - for (Paths::iterator i = paths.begin(); - i != paths.end(); i++) - { - Path path = *i; - if (!pathExists(path)) { - debug(format("path `%1%' disappeared") % path); - nixDB.delPair(txn, dbValidPaths, path); - nixDB.delPair(txn, dbSuccessorsRev, path); - nixDB.delPair(txn, dbSubstitutesRev, path); - } - } - -#if 0 - Strings subs; - nixDB.enumTable(txn, dbSubstitutes, subs); - - for (Strings::iterator i = subs.begin(); - i != subs.end(); i++) - { - FSId srcId = parseHash(*i); - - Strings subIds; - nixDB.queryStrings(txn, dbSubstitutes, srcId, subIds); - - for (Strings::iterator j = subIds.begin(); - j != subIds.end(); ) - { - FSId subId = parseHash(*j); - - Strings subPaths; - nixDB.queryStrings(txn, dbId2Paths, subId, subPaths); - if (subPaths.size() == 0) { - debug(format("erasing substitute %1% for %2%") - % (string) subId % (string) srcId); - j = subIds.erase(j); - } else j++; - } - - nixDB.setStrings(txn, dbSubstitutes, srcId, subIds); - } -#endif - - Paths sucs; - nixDB.enumTable(txn, dbSuccessors, sucs); - - for (Paths::iterator i = sucs.begin(); i != sucs.end(); i++) { - Path srcPath = *i; - - Path sucPath; - if (!nixDB.queryString(txn, dbSuccessors, srcPath, sucPath)) abort(); - - Paths revs; - nixDB.queryStrings(txn, dbSuccessorsRev, sucPath, revs); - - if (find(revs.begin(), revs.end(), srcPath) == revs.end()) { - debug(format("reverse successor mapping from `%1%' to `%2%' missing") - % srcPath % sucPath); - revs.push_back(srcPath); - nixDB.setStrings(txn, dbSuccessorsRev, sucPath, revs); - } - } - - txn.commit(); -} diff --git a/src/store.hh b/src/store.hh deleted file mode 100644 index dab3d603f802..000000000000 --- a/src/store.hh +++ /dev/null @@ -1,72 +0,0 @@ -#ifndef __STORE_H -#define __STORE_H - -#include - -#include "hash.hh" -#include "db.hh" - -using namespace std; - - -/* Open the database environment. */ -void openDB(); - -/* Create the required database tables. */ -void initDB(); - -/* Get a transaction object. */ -void createStoreTransaction(Transaction & txn); - -/* Copy a path recursively. */ -void copyPath(const Path & src, const Path & dst); - -/* Register a successor. This function accepts a transaction handle - so that it can be enclosed in an atomic operation with calls to - registerValidPath(). This must be atomic, since if we register a - successor for a derivation without registering the paths built in - the derivation, we have a successor with dangling pointers, and if - we do it in reverse order, we can get an obstructed build (since to - rebuild the successor, the outputs paths must not exist). */ -void registerSuccessor(const Transaction & txn, - const Path & srcPath, const Path & sucPath); - -/* Return the predecessors of the Nix expression stored at the given - path. */ -bool querySuccessor(const Path & srcPath, Path & sucPath); - -/* Return the predecessors of the Nix expression stored at the given - path. */ -Paths queryPredecessors(const Path & sucPath); - -/* Register a substitute. */ -void registerSubstitute(const Path & srcPath, const Path & subPath); - -/* Return the substitutes expression for the given path. */ -Paths querySubstitutes(const Path & srcPath); - -/* Register the validity of a path. */ -void registerValidPath(const Transaction & txn, const Path & path); - -/* Unregister the validity of a path. */ -void unregisterValidPath(const Path & path); - -/* Checks whether a path is valid. */ -bool isValidPath(const Path & path); - -/* Copy the contents of a path to the store and register the validity - the resulting path. The resulting path is returned. */ -Path addToStore(const Path & srcPath); - -/* Like addToStore, but the path of the output is given, and the - contents written to the output path is a regular file containing - the given string. */ -void addTextToStore(const Path & dstPath, const string & s); - -/* Delete a value from the nixStore directory. */ -void deleteFromStore(const Path & path); - -void verifyStore(); - - -#endif /* !__STORE_H */ diff --git a/src/test-builder-1.sh b/src/test-builder-1.sh deleted file mode 100755 index 80e23354c3b9..000000000000 --- a/src/test-builder-1.sh +++ /dev/null @@ -1,3 +0,0 @@ -#! /bin/sh - -echo "Hello World" > $out diff --git a/src/test-builder-2.sh b/src/test-builder-2.sh deleted file mode 100755 index 0794fa96a401..000000000000 --- a/src/test-builder-2.sh +++ /dev/null @@ -1,9 +0,0 @@ -#! /bin/sh - -echo "builder 2" - -/bin/mkdir $out || exit 1 -cd $out || exit 1 -echo "Hallo Wereld" > bla -echo $builder >> bla -echo $out >> bla diff --git a/src/test-expr-1.nix b/src/test-expr-1.nix deleted file mode 100644 index a94ee73828d9..000000000000 --- a/src/test-expr-1.nix +++ /dev/null @@ -1 +0,0 @@ -Str("Hello World") diff --git a/src/test.cc b/src/test.cc deleted file mode 100644 index 457fecf245f0..000000000000 --- a/src/test.cc +++ /dev/null @@ -1,162 +0,0 @@ -#include - -#include -#include - -#include "hash.hh" -#include "archive.hh" -#include "util.hh" -#include "normalise.hh" -#include "globals.hh" - - -void realise(Path nePath) -{ - Nest nest(lvlDebug, format("TEST: realising `%1%'") % nePath); - realiseClosure(normaliseNixExpr(nePath)); -} - - -struct MySink : DumpSink -{ - virtual void operator () (const unsigned char * data, unsigned int len) - { - /* Don't use cout, it's slow as hell! */ - writeFull(STDOUT_FILENO, data, len); - } -}; - - -struct MySource : RestoreSource -{ - virtual void operator () (unsigned char * data, unsigned int len) - { - readFull(STDIN_FILENO, data, len); - } -}; - - -void runTests() -{ - verbosity = (Verbosity) 100; - - /* Hashing. */ - string s = "0b0ffd0538622bfe20b92c4aa57254d9"; - Hash h = parseHash(s); - if ((string) h != s) abort(); - - try { - h = parseHash("blah blah"); - abort(); - } catch (Error err) { }; - - try { - h = parseHash("0b0ffd0538622bfe20b92c4aa57254d99"); - abort(); - } catch (Error err) { }; - - /* Path canonicalisation. */ - cout << canonPath("/./../././//") << endl; - cout << canonPath("/foo/bar") << endl; - cout << canonPath("///foo/////bar//") << endl; - cout << canonPath("/././/foo/////bar//.") << endl; - cout << canonPath("/foo////bar//..///x/") << endl; - cout << canonPath("/foo////bar//..//..//x/y/../z/") << endl; - cout << canonPath("/foo/bar/../../../..///") << endl; - - /* Dumping. */ - -#if 0 - MySink sink; - dumpPath("scratch", sink); - cout << (string) hashPath("scratch") << endl; -#endif - - /* Restoring. */ -#if 0 - MySource source; - restorePath("outdir", source); - cout << (string) hashPath("outdir") << endl; - return; -#endif - - /* Set up the test environment. */ - - mkdir("scratch", 0777); - mkdir("scratch/db", 0777); - - string testDir = absPath("scratch"); - cout << testDir << endl; - - nixStore = testDir; - nixLogDir = testDir; - nixDBPath = testDir + "/db"; - - openDB(); - initDB(); - - /* Expression evaluation. */ - - Path builder1fn; - builder1fn = addToStore("./test-builder-1.sh"); - - ATerm fs1 = ATmake( - "Closure([], [(, [])])", - builder1fn.c_str(), - builder1fn.c_str()); - Path fs1ne = writeTerm(fs1, "-c"); - - realise(fs1ne); - realise(fs1ne); - - string out1h = hashString("foo"); /* !!! bad */ - Path out1fn = nixStore + "/" + (string) out1h + "-hello.txt"; - ATerm fs3 = ATmake( - "Derive([], [], , , [], [(\"out\", )])", - out1fn.c_str(), - fs1ne.c_str(), - thisSystem.c_str(), - builder1fn.c_str(), - out1fn.c_str()); - debug(printTerm(fs3)); - Path fs3ne = writeTerm(fs3, "-d"); - - realise(fs3ne); - realise(fs3ne); - - - Path builder4fn = addToStore("./test-builder-2.sh"); - - ATerm fs4 = ATmake( - "Closure([], [(, [])])", - builder4fn.c_str(), - builder4fn.c_str()); - Path fs4ne = writeTerm(fs4, "-c"); - - realise(fs4ne); - - string out5h = hashString("bar"); /* !!! bad */ - Path out5fn = nixStore + "/" + (string) out5h + "-hello2"; - ATerm fs5 = ATmake( - "Derive([], [], , , [], [(\"out\", ), (\"builder\", )])", - out5fn.c_str(), - fs4ne.c_str(), - thisSystem.c_str(), - builder4fn.c_str(), - out5fn.c_str(), - builder4fn.c_str()); - debug(printTerm(fs5)); - Path fs5ne = writeTerm(fs5, "-d"); - - realise(fs5ne); - realise(fs5ne); -} - - -void run(Strings args) -{ - runTests(); -} - - -string programId = "test"; diff --git a/src/util.cc b/src/util.cc deleted file mode 100644 index c1d0fedea8aa..000000000000 --- a/src/util.cc +++ /dev/null @@ -1,253 +0,0 @@ -#include -#include -#include - -#include -#include -#include -#include - -#include "util.hh" - - -string thisSystem = SYSTEM; - - -Error::Error(const format & f) -{ - err = f.str(); -} - - -SysError::SysError(const format & f) - : Error(format("%1%: %2%") % f.str() % strerror(errno)) -{ -} - - -Path absPath(Path path, Path dir) -{ - if (path[0] != '/') { - if (dir == "") { - char buf[PATH_MAX]; - if (!getcwd(buf, sizeof(buf))) - throw SysError("cannot get cwd"); - dir = buf; - } - path = dir + "/" + path; - } - return canonPath(path); -} - - -Path canonPath(const Path & path) -{ - string s; - - if (path[0] != '/') - throw Error(format("not an absolute path: `%1%'") % path); - - string::const_iterator i = path.begin(), end = path.end(); - - while (1) { - - /* Skip slashes. */ - while (i != end && *i == '/') i++; - if (i == end) break; - - /* Ignore `.'. */ - if (*i == '.' && (i + 1 == end || i[1] == '/')) - i++; - - /* If `..', delete the last component. */ - else if (*i == '.' && i + 1 < end && i[1] == '.' && - (i + 2 == end || i[2] == '/')) - { - if (!s.empty()) s.erase(s.rfind('/')); - i += 2; - } - - /* Normal component; copy it. */ - else { - s += '/'; - while (i != end && *i != '/') s += *i++; - } - } - - return s.empty() ? "/" : s; -} - - -Path dirOf(const Path & path) -{ - unsigned int pos = path.rfind('/'); - if (pos == string::npos) - throw Error(format("invalid file name: %1%") % path); - return Path(path, 0, pos); -} - - -string baseNameOf(const Path & path) -{ - unsigned int pos = path.rfind('/'); - if (pos == string::npos) - throw Error(format("invalid file name %1% ") % path); - return string(path, pos + 1); -} - - -bool pathExists(const Path & path) -{ - int res; - struct stat st; - res = stat(path.c_str(), &st); - if (!res) return true; - if (errno != ENOENT) - throw SysError(format("getting status of %1%") % path); - return false; -} - - -void deletePath(const Path & path) -{ - msg(lvlVomit, format("deleting path `%1%'") % path); - - struct stat st; - if (lstat(path.c_str(), &st)) - throw SysError(format("getting attributes of path `%1%'") % path); - - if (S_ISDIR(st.st_mode)) { - Strings names; - - DIR * dir = opendir(path.c_str()); - - struct dirent * dirent; - while (errno = 0, dirent = readdir(dir)) { - string name = dirent->d_name; - if (name == "." || name == "..") continue; - names.push_back(name); - } - - closedir(dir); /* !!! close on exception */ - - /* Make the directory writable. */ - if (!(st.st_mode & S_IWUSR)) { - if (chmod(path.c_str(), st.st_mode | S_IWUSR) == -1) - throw SysError(format("making `%1%' writable")); - } - - for (Strings::iterator i = names.begin(); i != names.end(); i++) - deletePath(path + "/" + *i); - } - - if (remove(path.c_str()) == -1) - throw SysError(format("cannot unlink `%1%'") % path); -} - - -void makePathReadOnly(const Path & path) -{ - struct stat st; - if (lstat(path.c_str(), &st)) - throw SysError(format("getting attributes of path `%1%'") % path); - - if (!S_ISLNK(st.st_mode) && (st.st_mode & S_IWUSR)) { - if (chmod(path.c_str(), st.st_mode & ~S_IWUSR) == -1) - throw SysError(format("making `%1%' read-only") % path); - } - - if (S_ISDIR(st.st_mode)) { - DIR * dir = opendir(path.c_str()); - - struct dirent * dirent; - while (errno = 0, dirent = readdir(dir)) { - string name = dirent->d_name; - if (name == "." || name == "..") continue; - makePathReadOnly(path + "/" + name); - } - - closedir(dir); /* !!! close on exception */ - } -} - - -static Path tempName() -{ - static int counter = 0; - char * s = getenv("TMPDIR"); - Path tmpRoot = s ? canonPath(Path(s)) : "/tmp"; - return (format("%1%/nix-%2%-%3%") % tmpRoot % getpid() % counter++).str(); -} - - -Path createTempDir() -{ - while (1) { - Path tmpDir = tempName(); - if (mkdir(tmpDir.c_str(), 0777) == 0) return tmpDir; - if (errno != EEXIST) - throw SysError(format("creating directory `%1%'") % tmpDir); - } -} - - -Verbosity verbosity = lvlError; - -static int nestingLevel = 0; - - -Nest::Nest(Verbosity level, const format & f) -{ - if (level > verbosity) - nest = false; - else { - msg(level, f); - nest = true; - nestingLevel++; - } -} - - -Nest::~Nest() -{ - if (nest) nestingLevel--; -} - - -void msg(Verbosity level, const format & f) -{ - if (level > verbosity) return; - string spaces; - for (int i = 0; i < nestingLevel; i++) - spaces += "| "; - cerr << format("%1%%2%\n") % spaces % f.str(); -} - - -void debug(const format & f) -{ - msg(lvlDebug, f); -} - - -void readFull(int fd, unsigned char * buf, size_t count) -{ - while (count) { - ssize_t res = read(fd, (char *) buf, count); - if (res == -1) throw SysError("reading from file"); - if (res == 0) throw Error("unexpected end-of-file"); - count -= res; - buf += res; - } -} - - -void writeFull(int fd, const unsigned char * buf, size_t count) -{ - while (count) { - ssize_t res = write(fd, (char *) buf, count); - if (res == -1) throw SysError("writing to file"); - count -= res; - buf += res; - } -} diff --git a/src/util.hh b/src/util.hh deleted file mode 100644 index 016289176be8..000000000000 --- a/src/util.hh +++ /dev/null @@ -1,116 +0,0 @@ -#ifndef __UTIL_H -#define __UTIL_H - -#include -#include -#include -#include - -#include - -#include - -using namespace std; -using namespace boost; - - -class Error : public exception -{ -protected: - string err; -public: - Error(const format & f); - ~Error() throw () { }; - const char * what() const throw () { return err.c_str(); } - const string & msg() const throw () { return err; } -}; - -class SysError : public Error -{ -public: - SysError(const format & f); -}; - -class UsageError : public Error -{ -public: - UsageError(const format & f) : Error(f) { }; -}; - - -typedef list Strings; -typedef set StringSet; - - -/* Paths are just strings. */ -typedef string Path; -typedef list Paths; -typedef set PathSet; - - -/* The canonical system name, as returned by config.guess. */ -extern string thisSystem; - - -/* Return an absolutized path, resolving paths relative to the - specified directory, or the current directory otherwise. The path - is also canonicalised. */ -Path absPath(Path path, Path dir = ""); - -/* Canonicalise a path (as in realpath(3)). */ -Path canonPath(const Path & path); - -/* Return the directory part of the given path, i.e., everything - before the final `/'. */ -Path dirOf(const Path & path); - -/* Return the base name of the given path, i.e., everything following - the final `/'. */ -string baseNameOf(const Path & path); - -/* Return true iff the given path exists. */ -bool pathExists(const Path & path); - -/* Delete a path; i.e., in the case of a directory, it is deleted - recursively. Don't use this at home, kids. */ -void deletePath(const Path & path); - -/* Make a path read-only recursively. */ -void makePathReadOnly(const Path & path); - -/* Create a temporary directory. */ -Path createTempDir(); - - -/* Messages. */ - -typedef enum { - lvlError, - lvlTalkative, - lvlChatty, - lvlDebug, - lvlVomit -} Verbosity; - -extern Verbosity verbosity; /* supress msgs > this */ - -class Nest -{ -private: - bool nest; -public: - Nest(Verbosity level, const format & f); - ~Nest(); -}; - -void msg(Verbosity level, const format & f); -void debug(const format & f); /* short-hand for msg(lvlDebug, ...) */ - - -/* Wrappers arount read()/write() that read/write exactly the - requested number of bytes. */ -void readFull(int fd, unsigned char * buf, size_t count); -void writeFull(int fd, const unsigned char * buf, size_t count); - - -#endif /* !__UTIL_H */ -- cgit 1.4.1