From 75068e7d753cf6cbe45a4bf294000dca9bd41d8b Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Mon, 4 Sep 2006 21:06:23 +0000 Subject: * Use a proper namespace. * Optimise header file usage a bit. * Compile the parser as C++. --- src/libstore/build.cc | 53 +++++++++++++++++++++++++++------------------ src/libstore/build.hh | 9 +++++++- src/libstore/db.cc | 18 ++++++++++----- src/libstore/db.hh | 16 ++++++++------ src/libstore/derivations.cc | 7 +++++- src/libstore/derivations.hh | 16 ++++++++++---- src/libstore/gc.cc | 13 +++++++++-- src/libstore/gc.hh | 8 ++++++- src/libstore/globals.cc | 11 ++++++++-- src/libstore/globals.hh | 11 ++++++---- src/libstore/misc.cc | 9 ++++++++ src/libstore/misc.hh | 6 +++++ src/libstore/pathlocks.cc | 11 ++++++++-- src/libstore/pathlocks.hh | 10 +++++++-- src/libstore/references.cc | 15 +++++++++---- src/libstore/references.hh | 6 +++-- src/libstore/store.cc | 46 +++++++++++++++++++++------------------ src/libstore/store.hh | 10 +++++++-- 18 files changed, 193 insertions(+), 82 deletions(-) (limited to 'src/libstore') diff --git a/src/libstore/build.cc b/src/libstore/build.cc index c953707a51..647671036d 100644 --- a/src/libstore/build.cc +++ b/src/libstore/build.cc @@ -1,3 +1,13 @@ +#include "build.hh" +#include "references.hh" +#include "pathlocks.hh" +#include "misc.hh" +#include "globals.hh" +#include "gc.hh" +#include "store.hh" +#include "db.hh" +#include "util.hh" + #include #include #include @@ -15,13 +25,11 @@ #include #include -#include "build.hh" -#include "references.hh" -#include "pathlocks.hh" -#include "misc.hh" -#include "globals.hh" -#include "gc.hh" +namespace nix { + +using std::map; + /* !!! TODO derivationFromPath shouldn't be used here */ @@ -38,8 +46,8 @@ class Worker; /* A pointer to a goal. */ class Goal; -typedef shared_ptr GoalPtr; -typedef weak_ptr WeakGoalPtr; +typedef boost::shared_ptr GoalPtr; +typedef boost::weak_ptr WeakGoalPtr; /* Set of goals. */ typedef set Goals; @@ -50,7 +58,7 @@ typedef map WeakGoalMap; -class Goal : public enable_shared_from_this +class Goal : public boost::enable_shared_from_this { public: typedef enum {ecBusy, ecSuccess, ecFailed} ExitCode; @@ -447,8 +455,8 @@ static void killUser(uid_t uid) if (kill(-1, SIGKILL) == -1) throw SysError(format("cannot kill processes for UID `%1%'") % uid); - } catch (exception & e) { - cerr << format("build error: %1%\n") % e.what(); + } catch (std::exception & e) { + std::cerr << format("build error: %1%\n") % e.what(); quickExit(1); } quickExit(0); @@ -930,8 +938,8 @@ DerivationGoal::HookReply DerivationGoal::tryBuildHook() throw SysError(format("executing `%1%'") % buildHook); - } catch (exception & e) { - cerr << format("build error: %1%\n") % e.what(); + } catch (std::exception & e) { + std::cerr << format("build error: %1%\n") % e.what(); } quickExit(1); } @@ -1326,8 +1334,8 @@ void DerivationGoal::startBuilder() throw SysError(format("executing `%1%'") % drv.builder); - } catch (exception & e) { - cerr << format("build error: %1%\n") % e.what(); + } catch (std::exception & e) { + std::cerr << format("build error: %1%\n") % e.what(); } quickExit(1); } @@ -1593,7 +1601,7 @@ private: Pid pid; /* Lock on the store path. */ - shared_ptr outputLock; + boost::shared_ptr outputLock; typedef void (SubstitutionGoal::*GoalState)(); GoalState state; @@ -1719,7 +1727,7 @@ void SubstitutionGoal::tryToRun() } /* Acquire a lock on the output path. */ - outputLock = shared_ptr(new PathLocks); + outputLock = boost::shared_ptr(new PathLocks); outputLock->lockPaths(singleton(storePath), (format("waiting for lock on `%1%'") % storePath).str()); @@ -1767,8 +1775,8 @@ void SubstitutionGoal::tryToRun() throw SysError(format("executing `%1%'") % sub.program); - } catch (exception & e) { - cerr << format("substitute error: %1%\n") % e.what(); + } catch (std::exception & e) { + std::cerr << format("substitute error: %1%\n") % e.what(); } quickExit(1); } @@ -1930,8 +1938,8 @@ static void removeGoal(GoalPtr goal, WeakGoalMap & goalMap) void Worker::removeGoal(GoalPtr goal) { - ::removeGoal(goal, derivationGoals); - ::removeGoal(goal, substitutionGoals); + nix::removeGoal(goal, derivationGoals); + nix::removeGoal(goal, substitutionGoals); if (topGoals.find(goal) != topGoals.end()) { topGoals.erase(goal); /* If a top-level goal failed, then kill all other goals @@ -2160,3 +2168,6 @@ void ensurePath(const Path & path) if (goal->getExitCode() != Goal::ecSuccess) throw Error(format("path `%1%' does not exist and cannot be created") % path); } + + +} diff --git a/src/libstore/build.hh b/src/libstore/build.hh index 489a6cabc8..c90c126769 100644 --- a/src/libstore/build.hh +++ b/src/libstore/build.hh @@ -1,8 +1,13 @@ #ifndef __BUILD_H #define __BUILD_H -#include "derivations.hh" +#include "types.hh" + + +namespace nix { + + /* Ensure that the output paths of the derivation are valid. If they are already valid, this is a no-op. Otherwise, validity can be reached in two ways. First, if the output paths have @@ -16,5 +21,7 @@ void buildDerivations(const PathSet & drvPaths); void ensurePath(const Path & storePath); +} + #endif /* !__BUILD_H */ diff --git a/src/libstore/db.cc b/src/libstore/db.cc index 7f428dc488..ca5d0582a0 100644 --- a/src/libstore/db.cc +++ b/src/libstore/db.cc @@ -1,3 +1,7 @@ +#include "db.hh" +#include "util.hh" +#include "pathlocks.hh" + #include #include #include @@ -7,9 +11,8 @@ #include -#include "db.hh" -#include "util.hh" -#include "pathlocks.hh" + +namespace nix { /* Wrapper class to ensure proper destruction. */ @@ -112,7 +115,7 @@ Db * Database::getDb(TableId table) if (table == 0) throw Error("database table is not open " "(maybe you don't have sufficient permission?)"); - map::iterator i = tables.find(table); + std::map::iterator i = tables.find(table); if (i == tables.end()) throw Error("unknown table id"); return i->second; @@ -263,10 +266,10 @@ void Database::close() try { - for (map::iterator i = tables.begin(); + for (std::map::iterator i = tables.begin(); i != tables.end(); ) { - map::iterator j = i; + std::map::iterator j = i; ++j; closeTable(i->first); i = j; @@ -433,3 +436,6 @@ void Database::enumTable(const Transaction & txn, TableId table, } catch (DbException e) { rethrow(e); } } + + +} diff --git a/src/libstore/db.hh b/src/libstore/db.hh index 0315728de5..54f490f885 100644 --- a/src/libstore/db.hh +++ b/src/libstore/db.hh @@ -1,13 +1,9 @@ #ifndef __DB_H #define __DB_H -#include -#include -#include - -#include "util.hh" +#include "types.hh" -using namespace std; +#include /* Defined externally. */ @@ -16,6 +12,9 @@ class DbEnv; class Db; +namespace nix { + + class Database; @@ -53,7 +52,7 @@ private: DbEnv * env; TableId nextId; - map tables; + std::map tables; void requireEnv(); @@ -99,5 +98,8 @@ public: DbNoPermission(const format & f) : Error(f) { }; }; + +} + #endif /* !__DB_H */ diff --git a/src/libstore/derivations.cc b/src/libstore/derivations.cc index e2d81bda83..d6291cd45a 100644 --- a/src/libstore/derivations.cc +++ b/src/libstore/derivations.cc @@ -1,11 +1,13 @@ #include "derivations.hh" -#include "globals.hh" #include "store.hh" #include "derivations-ast.hh" #include "derivations-ast.cc" +namespace nix { + + Hash hashTerm(ATerm t) { return hashString(htSHA256, atPrint(t)); @@ -170,3 +172,6 @@ bool isDerivation(const string & fileName) fileName.size() >= drvExtension.size() && string(fileName, fileName.size() - drvExtension.size()) == drvExtension; } + + +} diff --git a/src/libstore/derivations.hh b/src/libstore/derivations.hh index 9358db2eda..d1c9db5084 100644 --- a/src/libstore/derivations.hh +++ b/src/libstore/derivations.hh @@ -2,7 +2,12 @@ #define __DERIVATIONS_H #include "aterm.hh" -#include "store.hh" +#include "hash.hh" + +#include + + +namespace nix { /* Extension of derivations in the Nix store. */ @@ -27,13 +32,13 @@ struct DerivationOutput } }; -typedef map DerivationOutputs; +typedef std::map DerivationOutputs; /* For inputs that are sub-derivations, we specify exactly which output IDs we are interested in. */ -typedef map DerivationInputs; +typedef std::map DerivationInputs; -typedef map StringPairs; +typedef std::map StringPairs; struct Derivation { @@ -63,5 +68,8 @@ ATerm unparseDerivation(const Derivation & drv); derivations. */ bool isDerivation(const string & fileName); + +} + #endif /* !__DERIVATIONS_H */ diff --git a/src/libstore/gc.cc b/src/libstore/gc.cc index 37fde29ca0..59e71daa0b 100644 --- a/src/libstore/gc.cc +++ b/src/libstore/gc.cc @@ -1,7 +1,10 @@ -#include "globals.hh" #include "gc.hh" +#include "globals.hh" #include "misc.hh" #include "pathlocks.hh" +#include "store.hh" +#include "db.hh" +#include "util.hh" #include @@ -17,6 +20,9 @@ #endif +namespace nix { + + static string gcLockName = "gc.lock"; static string tempRootsDir = "temproots"; static string gcRootsDir = "gcroots"; @@ -192,7 +198,7 @@ void removeTempRoots() } -typedef shared_ptr FDPtr; +typedef boost::shared_ptr FDPtr; typedef list FDs; @@ -558,3 +564,6 @@ void collectGarbage(GCAction action, const PathSet & pathsToDelete, } } } + + +} diff --git a/src/libstore/gc.hh b/src/libstore/gc.hh index c6b13bc471..54fc4dec4b 100644 --- a/src/libstore/gc.hh +++ b/src/libstore/gc.hh @@ -1,7 +1,10 @@ #ifndef __GC_H #define __GC_H -#include "util.hh" +#include "types.hh" + + +namespace nix { /* Garbage collector operation. */ @@ -39,4 +42,7 @@ Path addPermRoot(const Path & storePath, const Path & gcRoot, bool indirect); +} + + #endif /* !__GC_H */ diff --git a/src/libstore/globals.cc b/src/libstore/globals.cc index f61fe167d8..466d0e0b26 100644 --- a/src/libstore/globals.cc +++ b/src/libstore/globals.cc @@ -1,9 +1,13 @@ #include "globals.hh" +#include "util.hh" #include #include +namespace nix { + + string nixStore = "/UNINIT"; string nixDataDir = "/UNINIT"; string nixLogDir = "/UNINIT"; @@ -23,7 +27,7 @@ string thisSystem = "unset"; static bool settingsRead = false; -static map settings; +static std::map settings; string & at(Strings & ss, unsigned int n) @@ -72,7 +76,7 @@ static void readSettings() Strings querySetting(const string & name, const Strings & def) { if (!settingsRead) readSettings(); - map::iterator i = settings.find(name); + std::map::iterator i = settings.find(name); return i == settings.end() ? def : i->second; } @@ -98,3 +102,6 @@ bool queryBoolSetting(const string & name, bool def) else throw Error(format("configuration option `%1%' should be either `true' or `false', not `%2%'") % name % v); } + + +} diff --git a/src/libstore/globals.hh b/src/libstore/globals.hh index 03aaa13d89..9441dc344a 100644 --- a/src/libstore/globals.hh +++ b/src/libstore/globals.hh @@ -1,11 +1,11 @@ #ifndef __GLOBALS_H #define __GLOBALS_H -#include -#include -#include "util.hh" +#include "types.hh" + + +namespace nix { -using namespace std; /* Path names. */ @@ -67,5 +67,8 @@ string querySetting(const string & name, const string & def); bool queryBoolSetting(const string & name, bool def); + +} + #endif /* !__GLOBALS_H */ diff --git a/src/libstore/misc.cc b/src/libstore/misc.cc index 91cf25f271..156d059b6f 100644 --- a/src/libstore/misc.cc +++ b/src/libstore/misc.cc @@ -1,4 +1,10 @@ +#include "misc.hh" +#include "store.hh" #include "build.hh" +#include "db.hh" + + +namespace nix { Derivation derivationFromPath(const Path & drvPath) @@ -81,3 +87,6 @@ void queryMissing(const PathSet & targets, } } } + + +} diff --git a/src/libstore/misc.hh b/src/libstore/misc.hh index f758f5bfd5..ad1ad80eb1 100644 --- a/src/libstore/misc.hh +++ b/src/libstore/misc.hh @@ -4,6 +4,9 @@ #include "derivations.hh" +namespace nix { + + /* Read a derivation, after ensuring its existence through ensurePath(). */ Derivation derivationFromPath(const Path & drvPath); @@ -29,4 +32,7 @@ void queryMissing(const PathSet & targets, PathSet & willBuild, PathSet & willSubstitute); +} + + #endif /* !__MISC_H */ diff --git a/src/libstore/pathlocks.cc b/src/libstore/pathlocks.cc index 8d0e6c3299..03ba4f2124 100644 --- a/src/libstore/pathlocks.cc +++ b/src/libstore/pathlocks.cc @@ -1,17 +1,21 @@ +#include "pathlocks.hh" +#include "util.hh" + #include #include #include #include -#include "pathlocks.hh" - #ifdef __CYGWIN__ #include #include #endif +namespace nix { + + int openLockFile(const Path & path, bool create) { AutoCloseFD fd; @@ -220,3 +224,6 @@ void PathLocks::setDeletion(bool deletePaths) { this->deletePaths = deletePaths; } + + +} diff --git a/src/libstore/pathlocks.hh b/src/libstore/pathlocks.hh index 911fe4579f..87bb7bf3ef 100644 --- a/src/libstore/pathlocks.hh +++ b/src/libstore/pathlocks.hh @@ -1,7 +1,10 @@ #ifndef __PATHLOCKS_H #define __PATHLOCKS_H -#include "util.hh" +#include "types.hh" + + +namespace nix { /* Open (possibly create) a lock file and return the file descriptor. @@ -22,7 +25,7 @@ bool lockFile(int fd, LockType lockType, bool wait); class PathLocks { private: - typedef pair FDPair; + typedef std::pair FDPair; list fds; bool deletePaths; @@ -37,4 +40,7 @@ public: }; +} + + #endif /* !__PATHLOCKS_H */ diff --git a/src/libstore/references.cc b/src/libstore/references.cc index d67e5b9dcc..aba4ef0107 100644 --- a/src/libstore/references.cc +++ b/src/libstore/references.cc @@ -1,3 +1,7 @@ +#include "references.hh" +#include "hash.hh" +#include "util.hh" + #include #include @@ -7,8 +11,8 @@ #include #include -#include "references.hh" -#include "hash.hh" + +namespace nix { static unsigned int refLength = 32; /* characters */ @@ -90,7 +94,7 @@ void checkPath(const string & path, PathSet scanForReferences(const string & path, const PathSet & paths) { - map backMap; + std::map backMap; StringSet ids; StringSet seen; @@ -114,10 +118,13 @@ PathSet scanForReferences(const string & path, const PathSet & paths) PathSet found; for (StringSet::iterator i = seen.begin(); i != seen.end(); i++) { - map::iterator j; + std::map::iterator j; if ((j = backMap.find(*i)) == backMap.end()) abort(); found.insert(j->second); } return found; } + + +} diff --git a/src/libstore/references.hh b/src/libstore/references.hh index 4b1299e7a3..76a7ee166d 100644 --- a/src/libstore/references.hh +++ b/src/libstore/references.hh @@ -1,10 +1,12 @@ #ifndef __REFERENCES_H #define __REFERENCES_H -#include "util.hh" +#include "types.hh" +namespace nix { PathSet scanForReferences(const Path & path, const PathSet & refs); - + +} #endif /* !__REFERENCES_H */ diff --git a/src/libstore/store.cc b/src/libstore/store.cc index 2f91937493..f8441af9cb 100644 --- a/src/libstore/store.cc +++ b/src/libstore/store.cc @@ -1,3 +1,13 @@ +#include "store.hh" +#include "util.hh" +#include "globals.hh" +#include "db.hh" +#include "archive.hh" +#include "pathlocks.hh" +#include "gc.hh" +#include "aterm.hh" +#include "derivations-ast.hh" + #include #include @@ -6,14 +16,9 @@ #include #include -#include "store.hh" -#include "globals.hh" -#include "db.hh" -#include "archive.hh" -#include "pathlocks.hh" -#include "gc.hh" - +namespace nix { + /* Nix database. */ static Database nixDB; @@ -956,10 +961,6 @@ void verifyStore(bool checkContents) } -#include "aterm.hh" -#include "derivations-ast.hh" - - /* Upgrade from schema 1 (Nix <= 0.7) to schema 2 (Nix >= 0.8). */ static void upgradeStore07() { @@ -971,7 +972,7 @@ static void upgradeStore07() nixDB.enumTable(txn, dbValidPaths, validPaths2); PathSet validPaths(validPaths2.begin(), validPaths2.end()); - cerr << "hashing paths..."; + std::cerr << "hashing paths..."; int n = 0; for (PathSet::iterator i = validPaths.begin(); i != validPaths.end(); ++i) { checkInterrupt(); @@ -980,20 +981,20 @@ static void upgradeStore07() if (s == "") { Hash hash = hashPath(htSHA256, *i); setHash(txn, *i, hash); - cerr << "."; + std::cerr << "."; if (++n % 1000 == 0) { txn.commit(); txn.begin(nixDB); } } } - cerr << "\n"; + std::cerr << std::endl; txn.commit(); txn.begin(nixDB); - cerr << "processing closures..."; + std::cerr << "processing closures..."; for (PathSet::iterator i = validPaths.begin(); i != validPaths.end(); ++i) { checkInterrupt(); if (i->size() > 6 && string(*i, i->size() - 6) == ".store") { @@ -1037,10 +1038,10 @@ static void upgradeStore07() setReferences(txn, path, references); } - cerr << "."; + std::cerr << "."; } } - cerr << "\n"; + std::cerr << std::endl; /* !!! maybe this transaction is way too big */ txn.commit(); @@ -1061,7 +1062,7 @@ static void upgradeStore09() Transaction txn(nixDB); - cerr << "converting referers to referrers..."; + std::cerr << "converting referers to referrers..."; TableId dbReferers = nixDB.openTable("referers"); /* sic! */ @@ -1080,16 +1081,19 @@ static void upgradeStore09() if (++n % 1000 == 0) { txn.commit(); txn.begin(nixDB); - cerr << "|"; + std::cerr << "|"; } - cerr << "."; + std::cerr << "."; } txn.commit(); - cerr << "\n"; + std::cerr << std::endl; nixDB.closeTable(dbReferers); nixDB.deleteTable("referers"); } + + +} diff --git a/src/libstore/store.hh b/src/libstore/store.hh index a170c69e74..7b18871e4f 100644 --- a/src/libstore/store.hh +++ b/src/libstore/store.hh @@ -4,9 +4,12 @@ #include #include "hash.hh" -#include "db.hh" -using namespace std; + +namespace nix { + + +class Transaction; /* Nix store and database schema version. Version 1 (or 0) was Nix <= @@ -168,5 +171,8 @@ void deleteFromStore(const Path & path, unsigned long long & bytesFreed); void verifyStore(bool checkContents); + +} + #endif /* !__STORE_H */ -- cgit 1.4.1