diff options
author | Eelco Dolstra <e.dolstra@tudelft.nl> | 2003-04-08T12·00+0000 |
---|---|---|
committer | Eelco Dolstra <e.dolstra@tudelft.nl> | 2003-04-08T12·00+0000 |
commit | 0d2b24cdd103f21861ad42fd6d98e5d1cb252646 (patch) | |
tree | 3956ec0b4b024acbf4c3a4f0ecc979fd9ccfa52c /src/util.hh | |
parent | 814b256da43ebdad79fe9544a59f6ae680da7cb9 (diff) |
* `Fix' is a high-level descriptor instantiator for Nix. It replaces
nix-instantiate.
Diffstat (limited to 'src/util.hh')
-rw-r--r-- | src/util.hh | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/src/util.hh b/src/util.hh index 8d82c80c14b4..fb405b0f1200 100644 --- a/src/util.hh +++ b/src/util.hh @@ -1,7 +1,15 @@ #ifndef __UTIL_H #define __UTIL_H +#include <string> #include <vector> +#include <sstream> + +#include <unistd.h> + +extern "C" { +#include "md5.h" +} using namespace std; @@ -31,4 +39,78 @@ public: typedef vector<string> Strings; +/* !!! the following shouldn't be here; abuse of the preprocessor */ + + +/* The canonical system name, as returned by config.guess. */ +static string thisSystem = SYSTEM; + + +/* The prefix of the Nix installation, and the environment variable + that can be used to override the default. */ +static string nixHomeDir = "/nix"; +static string nixHomeDirEnvVar = "NIX"; + + +string absPath(string filename, string dir = "") +{ + if (filename[0] != '/') { + if (dir == "") { + char buf[PATH_MAX]; + if (!getcwd(buf, sizeof(buf))) + throw Error("cannot get cwd"); + dir = buf; + } + filename = dir + "/" + filename; + /* !!! canonicalise */ + char resolved[PATH_MAX]; + if (!realpath(filename.c_str(), resolved)) + throw Error("cannot canonicalise path " + filename); + filename = resolved; + } + return filename; +} + + +string printHash(unsigned char * buf) +{ + ostringstream str; + for (int i = 0; i < 16; i++) { + str.fill('0'); + str.width(2); + str << hex << (int) buf[i]; + } + return str.str(); +} + + +/* Verify that a reference is valid (that is, is a MD5 hash code). */ +void checkHash(const string & s) +{ + string err = "invalid reference: " + s; + if (s.length() != 32) + throw BadRefError(err); + for (int i = 0; i < 32; i++) { + char c = s[i]; + if (!((c >= '0' && c <= '9') || + (c >= 'a' && c <= 'f'))) + throw BadRefError(err); + } +} + + +/* Compute the MD5 hash of a file. */ +string hashFile(string filename) +{ + unsigned char hash[16]; + FILE * file = fopen(filename.c_str(), "rb"); + if (!file) + throw BadRefError("file `" + filename + "' does not exist"); + int err = md5_stream(file, hash); + fclose(file); + if (err) throw BadRefError("cannot hash file"); + return printHash(hash); +} + + #endif /* !__UTIL_H */ |