diff options
author | Eelco Dolstra <e.dolstra@tudelft.nl> | 2003-11-19T17·27+0000 |
---|---|---|
committer | Eelco Dolstra <e.dolstra@tudelft.nl> | 2003-11-19T17·27+0000 |
commit | 9898746ef3732979bf30e9048021b6232ddf15ac (patch) | |
tree | 77f1391387f0e28f5495104fed3e4227bbeb4af3 /src/libutil | |
parent | fd7ac09f1073179d9ac439c3e9fb12a1bf00a7d5 (diff) |
* nix-env: a tool to manage user environments.
* Replace all directory reading code by a generic readDirectory() function.
Diffstat (limited to 'src/libutil')
-rw-r--r-- | src/libutil/archive.cc | 21 | ||||
-rw-r--r-- | src/libutil/util.cc | 45 | ||||
-rw-r--r-- | src/libutil/util.hh | 4 |
3 files changed, 33 insertions, 37 deletions
diff --git a/src/libutil/archive.cc b/src/libutil/archive.cc index ed57df4c9f7b..f605e8b61954 100644 --- a/src/libutil/archive.cc +++ b/src/libutil/archive.cc @@ -51,23 +51,12 @@ static void dump(const string & path, DumpSink & sink); static void dumpEntries(const Path & path, DumpSink & sink) { - AutoCloseDir dir = opendir(path.c_str()); - if (!dir) throw SysError("opening directory " + path); + Strings names = readDirectory(path); + vector<string> names2(names.begin(), names.end()); + sort(names2.begin(), names2.end()); - vector<string> 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<string>::iterator it = names.begin(); - it != names.end(); it++) + for (vector<string>::iterator it = names2.begin(); + it != names2.end(); it++) { writeString("entry", sink); writeString("(", sink); diff --git a/src/libutil/util.cc b/src/libutil/util.cc index 299a37f6c877..6a032a3f10f3 100644 --- a/src/libutil/util.cc +++ b/src/libutil/util.cc @@ -108,6 +108,25 @@ bool pathExists(const Path & path) } +Strings readDirectory(const Path & path) +{ + Strings names; + + AutoCloseDir dir = opendir(path.c_str()); + if (!dir) throw SysError(format("opening directory `%1%'") % path); + + struct dirent * dirent; + while (errno = 0, dirent = readdir(dir)) { /* sic */ + string name = dirent->d_name; + if (name == "." || name == "..") continue; + names.push_back(name); + } + if (errno) throw SysError(format("reading directory `%1%'") % path); + + return names; +} + + void deletePath(const Path & path) { printMsg(lvlVomit, format("deleting path `%1%'") % path); @@ -117,18 +136,7 @@ void deletePath(const Path & path) throw SysError(format("getting attributes of path `%1%'") % path); if (S_ISDIR(st.st_mode)) { - Strings names; - - { - AutoCloseDir 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); - } - } /* scoped to ensure that dir is closed at this point */ + Strings names = readDirectory(path); /* Make the directory writable. */ if (!(st.st_mode & S_IWUSR)) { @@ -136,7 +144,7 @@ void deletePath(const Path & path) throw SysError(format("making `%1%' writable")); } - for (Strings::iterator i = names.begin(); i != names.end(); i++) + for (Strings::iterator i = names.begin(); i != names.end(); ++i) deletePath(path + "/" + *i); } @@ -157,14 +165,9 @@ void makePathReadOnly(const Path & path) } if (S_ISDIR(st.st_mode)) { - AutoCloseDir 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); - } + Strings names = readDirectory(path); + for (Strings::iterator i = names.begin(); i != names.end(); ++i) + makePathReadOnly(path + "/" + *i); } } diff --git a/src/libutil/util.hh b/src/libutil/util.hh index e6b600eff8ce..d0e7b3ada873 100644 --- a/src/libutil/util.hh +++ b/src/libutil/util.hh @@ -73,6 +73,10 @@ string baseNameOf(const Path & path); /* Return true iff the given path exists. */ bool pathExists(const Path & path); +/* Read the contents of a directory. The entries `.' and `..' are + removed. */ +Strings readDirectory(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); |