* string -> Path.

This commit is contained in:
Eelco Dolstra 2003-10-07 14:37:41 +00:00
parent 5d4171f7fb
commit b9f4942bd2
2 changed files with 26 additions and 20 deletions

View File

@ -25,7 +25,7 @@ SysError::SysError(const format & f)
} }
string absPath(string path, string dir) Path absPath(Path path, Path dir)
{ {
if (path[0] != '/') { if (path[0] != '/') {
if (dir == "") { if (dir == "") {
@ -40,7 +40,7 @@ string absPath(string path, string dir)
} }
string canonPath(const string & path) Path canonPath(const Path & path)
{ {
string s; string s;
@ -78,16 +78,16 @@ string canonPath(const string & path)
} }
string dirOf(string path) Path dirOf(const Path & path)
{ {
unsigned int pos = path.rfind('/'); unsigned int pos = path.rfind('/');
if (pos == string::npos) if (pos == string::npos)
throw Error(format("invalid file name: %1%") % path); throw Error(format("invalid file name: %1%") % path);
return string(path, 0, pos); return Path(path, 0, pos);
} }
string baseNameOf(string path) string baseNameOf(const Path & path)
{ {
unsigned int pos = path.rfind('/'); unsigned int pos = path.rfind('/');
if (pos == string::npos) if (pos == string::npos)
@ -96,7 +96,7 @@ string baseNameOf(string path)
} }
bool pathExists(const string & path) bool pathExists(const Path & path)
{ {
int res; int res;
struct stat st; struct stat st;
@ -108,7 +108,7 @@ bool pathExists(const string & path)
} }
void deletePath(const string & path) void deletePath(const Path & path)
{ {
msg(lvlVomit, format("deleting path `%1%'") % path); msg(lvlVomit, format("deleting path `%1%'") % path);
@ -145,7 +145,7 @@ void deletePath(const string & path)
} }
void makePathReadOnly(const string & path) void makePathReadOnly(const Path & path)
{ {
struct stat st; struct stat st;
if (lstat(path.c_str(), &st)) if (lstat(path.c_str(), &st))
@ -171,19 +171,19 @@ void makePathReadOnly(const string & path)
} }
static string tempName() static Path tempName()
{ {
static int counter = 0; static int counter = 0;
char * s = getenv("TMPDIR"); char * s = getenv("TMPDIR");
string tmpRoot = s ? canonPath(string(s)) : "/tmp"; Path tmpRoot = s ? canonPath(Path(s)) : "/tmp";
return (format("%1%/nix-%2%-%3%") % tmpRoot % getpid() % counter++).str(); return (format("%1%/nix-%2%-%3%") % tmpRoot % getpid() % counter++).str();
} }
string createTempDir() Path createTempDir()
{ {
while (1) { while (1) {
string tmpDir = tempName(); Path tmpDir = tempName();
if (mkdir(tmpDir.c_str(), 0777) == 0) return tmpDir; if (mkdir(tmpDir.c_str(), 0777) == 0) return tmpDir;
if (errno != EEXIST) if (errno != EEXIST)
throw SysError(format("creating directory `%1%'") % tmpDir); throw SysError(format("creating directory `%1%'") % tmpDir);

View File

@ -42,6 +42,12 @@ typedef list<string> Strings;
typedef set<string> StringSet; typedef set<string> StringSet;
/* Paths are just strings. */
typedef string Path;
typedef list<Path> Paths;
typedef set<Path> PathSet;
/* The canonical system name, as returned by config.guess. */ /* The canonical system name, as returned by config.guess. */
extern string thisSystem; extern string thisSystem;
@ -49,31 +55,31 @@ extern string thisSystem;
/* Return an absolutized path, resolving paths relative to the /* Return an absolutized path, resolving paths relative to the
specified directory, or the current directory otherwise. The path specified directory, or the current directory otherwise. The path
is also canonicalised. */ is also canonicalised. */
string absPath(string path, string dir = ""); Path absPath(Path path, Path dir = "");
/* Canonicalise a path (as in realpath(3)). */ /* Canonicalise a path (as in realpath(3)). */
string canonPath(const string & path); Path canonPath(const Path & path);
/* Return the directory part of the given path, i.e., everything /* Return the directory part of the given path, i.e., everything
before the final `/'. */ before the final `/'. */
string dirOf(string path); Path dirOf(const Path & path);
/* Return the base name of the given path, i.e., everything following /* Return the base name of the given path, i.e., everything following
the final `/'. */ the final `/'. */
string baseNameOf(string path); string baseNameOf(const Path & path);
/* Return true iff the given path exists. */ /* Return true iff the given path exists. */
bool pathExists(const string & path); bool pathExists(const Path & path);
/* Delete a path; i.e., in the case of a directory, it is deleted /* Delete a path; i.e., in the case of a directory, it is deleted
recursively. Don't use this at home, kids. */ recursively. Don't use this at home, kids. */
void deletePath(const string & path); void deletePath(const Path & path);
/* Make a path read-only recursively. */ /* Make a path read-only recursively. */
void makePathReadOnly(const string & path); void makePathReadOnly(const Path & path);
/* Create a temporary directory. */ /* Create a temporary directory. */
string createTempDir(); Path createTempDir();
/* Messages. */ /* Messages. */