aboutsummaryrefslogtreecommitdiff
#pragma once

#include "types.hh"

#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <signal.h>
#include <map>
#include <functional>

#include <cstdio>


namespace nix {


#define foreach(it_type, it, collection)                                \
    for (it_type it = (collection).begin(); it != (collection).end(); ++it)

#define foreach_reverse(it_type, it, collection)                                \
    for (it_type it = (collection).rbegin(); it != (collection).rend(); ++it)


/* Return an environment variable. */
string getEnv(const string & key, const string & def = "");

/* 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 by removing all `.' or `..' components and
   double or trailing slashes.  Optionally resolves all symlink
   components such that each component of the resulting path is *not*
   a symbolic link. */
Path canonPath(const Path & path, bool resolveSymlinks = false);

/* Return the directory part of the given canonical path, i.e.,
   everything before the final `/'.  If the path is the root or an
   immediate child thereof (e.g., `/foo'), this means an empty string
   is returned. */
Path dirOf(const Path & path);

/* Return the base name of the given canonical path, i.e., everything
   following the final `/'. */
string baseNameOf(const Path & path);

/* Check whether a given path is a descendant of the given
   directory. */
bool isInDir(const Path & path, const Path & dir);

/* Get status of `path'. */
struct stat lstat(const Path & path);

/* Return true iff the given path exists. */
bool pathExists(const Path & path);

/* Read the contents (target) of a symbolic link.  The result is not
   in any way canonicalised. */
Path readLink(const Path & path);

bool isLink(const Path & path);

/* Read the contents of a directory.  The entries `.' and `..' are
   removed. */
struct DirEntry
{
    string name;
    ino_t ino;
    unsigned char type; // one of DT_*
    DirEntry(const string & name, ino_t ino, unsigned char type)
        : name(name), ino(ino), type(type) { }
};

typedef vector<DirEntry> DirEntries;

DirEntries readDirectory(const Path & path);

unsigned char getFileType(const Path & path);

/* Read the contents of a file into a string. */
string readFile(int fd);
string readFile(const Path & path, bool drain = false);

/* Write a string to a file. */
void writeFile(const Path & path, const string & s);

/* Read a line from a file descriptor. */
string readLine(int fd);

/* Write a line to a file descriptor. */
void writeLine(int fd, string s);

/* Delete a path; i.e., in the case of a directory, it is deleted
   recursively.  Don't use this at home, kids.  The second variant
   returns the number of bytes and blocks freed, and 'linkThreshold' denotes
   the number of links under which a file is accounted for in 'bytesFreed'.  */
void deletePath(const Path & path);

void deletePath(const Path & path, unsigned long long & bytesFreed,
    size_t linkThreshold = 1);

/* Copy SOURCE to DESTINATION, recursively, preserving ownership.  Throw if
   SOURCE contains a file that is not a regular file, symlink, or directory.
   When DELETESOURCE is true, delete source files once they have been
   copied.  */
void copyFileRecursively(const Path &source, const Path &destination,
    bool deleteSource = false);

/* Create a temporary directory. */
Path createTempDir(const Path & tmpRoot = "", const Path & prefix = "nix",
    bool includePid = true, bool useGlobalCounter = true, mode_t mode = 0755);

/* Create a directory and all its parents, if necessary.  Returns the
   list of created directories, in order of creation. */
Paths createDirs(const Path & path);

/* Create a symlink. */
void createSymlink(const Path & target, const Path & link);


template<class T, class A>
T singleton(const A & a)
{
    T t;
    t.insert(a);
    return t;
}


/* Messages. */


typedef enum {
    ltPretty,   /* nice, nested output */
    ltEscapes,  /* nesting indicated using escape codes (for log2xml) */
    ltFlat      /* no nesting */
} LogType;

extern LogType logType;
extern Verbosity verbosity; /* suppress msgs > this */

class Nest
{
private:
    bool nest;
public:
    Nest();
    ~Nest();
    void open(Verbosity level, const FormatOrString & fs);
    void close();
};

void printMsg_(Verbosity level, const FormatOrString & fs);

#define startNest(varName, level, f) \
    Nest varName; \
    if (level <= verbosity) { \
      varName.open(level, (f)); \
    }

#define printMsg(level, f) \
    do { \
        if (level <= nix::verbosity) { \
            nix::printMsg_(level, (f)); \
        } \
    } while (0)

#define debug(f) printMsg(lvlDebug, f)

void warnOnce(bool & haveWarned, const FormatOrString & fs);

void writeToStderr(const string & s);

extern void (*_writeToStderr) (const unsigned char * buf, size_t count);


/* 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);
void writeFull(int fd, const string & s);

MakeError(EndOfFile, Error)


/* Read a file descriptor until EOF occurs. */
string drainFD(int fd);



/* Automatic cleanup of resources. */


template <class T>
struct AutoDeleteArray
{
    T * p;
    AutoDeleteArray(T * p) : p(p) { }
    ~AutoDeleteArray()
    {
        delete [] p;
    }
};


class AutoDelete
{
    Path path;
    bool del;
    bool recursive;
public:
    AutoDelete(const Path & p, bool recursive = true);
    ~AutoDelete();
    void cancel();
};


class AutoCloseFD
{
    int fd;
public:
    AutoCloseFD();
    AutoCloseFD(int fd);
    AutoCloseFD(const AutoCloseFD & fd);
    ~AutoCloseFD();
    void operator =(int fd);
    operator int() const;
    void close();
    bool isOpen();
    int borrow();
};


class Pipe
{
public:
    AutoCloseFD readSide, writeSide;
    void create();
};


class AutoCloseDir
{
    DIR * dir;
public:
    AutoCloseDir();
    AutoCloseDir(DIR * dir);
    ~AutoCloseDir();
    void operator =(DIR * dir);
    operator DIR *();
    void close();
};


class Pid
{
    pid_t pid;
    bool separatePG;
    int killSignal;
public:
    Pid();
    Pid(pid_t pid);
    ~Pid();
    void operator =(pid_t pid);
    operator pid_t();
    void kill(bool quiet = false);
    int wait(bool block);
    void setSeparatePG(bool separatePG);
    void setKillSignal(int signal);
};

/* An "agent" is a helper program that runs in the background and that we talk
   to over pipes, such as the "guix offload" program.  */
struct Agent
{
    /* Pipes for talking to the agent. */
    Pipe toAgent;

    /* Pipe for the agent's standard output/error. */
    Pipe fromAgent;

    /* Pipe for build standard output/error--e.g., for build processes started
       by "guix offload".  */
    Pipe builderOut;

    /* The process ID of the agent. */
    Pid pid;

    /* The command and arguments passed to the agent along with a list of
       environment variable name/value pairs.  */
    Agent(const string &command, const Strings &args,
	  const std::map<string, string> &env = std::map<string, string>());

    ~Agent();
};


/* Kill all processes running under the specified uid by sending them
   a SIGKILL. */
void killUser(uid_t uid);


/* Fork a process that runs the given function, and return the child
   pid to the caller. */
pid_t startProcess(std::function<void()> fun, bool dieWithParent = true,
    const string & errorPrefix = "error: ", bool runExitHandlers = false);


/* Run a program and return its stdout in a string (i.e., like the
   shell backtick operator). */
string runProgram(Path program, bool searchPath = false,
    const Strings & args = Strings());

MakeError(ExecError, Error)

/* Convert a list of strings to a null-terminated vector of char
   *'s. The result must not be accessed beyond the lifetime of the
   list of strings. */
std::vector<char *> stringsToCharPtrs(const Strings & ss);

/* Close all file descriptors except stdin, stdout, stderr, and those
   listed in the given set.  Good practice in child processes. */
void closeMostFDs(const set<int> & exceptions);

/* Set the close-on-exec flag for the given file descriptor. */
void closeOnExec(int fd);

/* Common initialisation performed in child processes. */
void commonChildInit(Pipe & logPipe);

/* User interruption. */

extern volatile sig_atomic_t _isInterrupted;

void _interrupted();

void inline checkInterrupt()
{
    if (_isInterrupted) _interrupted();
}

MakeError(Interrupted, BaseError)


/* String tokenizer. */
template<class C> C tokenizeString(const string & s, const string & separators = " \t\n\r");


/* Concatenate the given strings with a separator between the
   elements. */
string concatStringsSep(const string & sep, const Strings & ss);
string concatStringsSep(const string & sep, const StringSet & ss);


/* Remove trailing whitespace from a string. */
string chomp(const string & s);


/* Convert the exit status of a child as returned by wait() into an
   error string. */
string statusToString(int status);

bool statusOk(int status);


/* Parse a string into an integer. */
template<class N> bool string2Int(const string & s, N & n)
{
    std::istringstream str(s);
    str >> n;
    return str && str.get() == EOF;
}


/* Return true iff `s' ends in `suffix'. */
bool hasSuffix(const string & s, const string & suffix);


/* Read string `s' from stream `str'. */
void expect(std::istream & str, const string & s);

MakeError(FormatError, Error)


/* Read a C-style string from stream `str'. */
string parseString(std::istream & str);


/* Utility function used to parse legacy ATerms. */
bool endOfList(std::istream & str);


/* Exception handling in destructors: print an error message, then
   ignore the exception. */
void ignoreException();


}
(elm->package-name elm)) (test-equal "infer-elm-package-name" elm (infer-elm-package-name guix)))) (test-round-trip "elm/core" "elm-core") (test-round-trip "elm/html" "elm-html") (test-round-trip "elm-explorations/markdown" "elm-explorations-markdown") (test-round-trip "elm-explorations/test" "elm-explorations-test") (test-round-trip "elm-explorations/foo-bar" "elm-explorations-foo-bar") (test-round-trip "elm/explorations" "elm-explorations") (test-round-trip "terezka/intervals" "elm-terezka-intervals") (test-round-trip "justinmimbs/time-extra" "elm-justinmimbs-time-extra") (test-round-trip "danhandrea/elm-date-format" "elm-danhandrea-elm-date-format")) (test-group "upstream-name needed" ;; Upstream names that our heuristic can't infer. We still check that the ;; round-trip behavior of 'infer-elm-package-name' works as promised for ;; the hypothetical Elm name it doesn't infer. (define-syntax-rule (test-upstream-needed elm guix inferred) (test-group elm (test-equal "elm->package-name" guix (elm->package-name elm)) (test-group "infer-elm-package-name" (test-equal "infers other name" inferred (infer-elm-package-name guix)) (test-equal "infered name round-trips" guix (elm->package-name inferred))))) (test-upstream-needed "elm/virtual-dom" "elm-virtual-dom" "virtual/dom") (test-upstream-needed "elm/project-metadata-utils" "elm-project-metadata-utils" "project/metadata-utils") (test-upstream-needed "explorations/foo" "elm-explorations-foo" "elm-explorations/foo") (test-upstream-needed "explorations/foo-bar" "elm-explorations-foo-bar" "elm-explorations/foo-bar") (test-upstream-needed "explorations-central/foo" "elm-explorations-central-foo" "elm-explorations/central-foo") (test-upstream-needed "explorations-central/foo-bar" "elm-explorations-central-foo-bar" "elm-explorations/central-foo-bar") (test-upstream-needed "elm-xyz/foo" "elm-xyz-foo" "xyz/foo") (test-upstream-needed "elm-xyz/foo-bar" "elm-xyz-foo-bar" "xyz/foo-bar") (test-upstream-needed "elm-explorations-xyz/foo" "elm-explorations-xyz-foo" "elm-explorations/xyz-foo") (test-upstream-needed "elm-explorations-xyz/foo-bar" "elm-explorations-xyz-foo-bar" "elm-explorations/xyz-foo-bar")) (test-group "no inferred Elm name" ;; Cases that 'infer-elm-package-name' should not attempt to handle, ;; because 'elm->package-name' would never produce such names. (define-syntax-rule (test-not-inferred guix) (test-assert guix (not (infer-elm-package-name guix)))) (test-not-inferred "elm") (test-not-inferred "guile") (test-not-inferred "gcc-toolchain") (test-not-inferred "font-adobe-source-sans-pro"))) (define test-package-registry-json ;; we intentionally list versions in different orders here "{ \"elm/core\": [\"1.0.0\", \"1.0.1\", \"1.0.2\", \"1.0.3\", \"1.0.4\"], \"elm-guix/demo\": [\"2.0.0\", \"3.0.0\", \"1.0.0\"] }") (define test-elm-core-json "{ \"type\": \"package\", \"name\": \"elm/core\", \"summary\": \"Elm's standard libraries\", \"license\": \"BSD-3-Clause\", \"version\": \"1.0.4\", \"exposed-modules\": { \"Primitives\": [ \"Basics\", \"String\", \"Char\", \"Bitwise\", \"Tuple\" ], \"Collections\": [ \"List\", \"Dict\", \"Set\", \"Array\" ], \"Error Handling\": [ \"Maybe\", \"Result\" ], \"Debug\": [ \"Debug\" ], \"Effects\": [ \"Platform.Cmd\", \"Platform.Sub\", \"Platform\", \"Process\", \"Task\" ] }, \"elm-version\": \"0.19.0 <= v < 0.20.0\", \"dependencies\": {}, \"test-dependencies\": {} }") (define test-elm-core-readme "# Core Libraries Every Elm project needs this package! It provides **basic functionality** like addition and subtraction as well as **data structures** like lists, dictionaries, and sets.") (define test-elm-guix-demo-json "{ \"type\": \"package\", \"name\": \"elm-guix/demo\", \"summary\": \"A test for `(guix import elm)`\", \"license\": \"GPL-3.0-or-later\", \"version\": \"3.0.0\", \"exposed-modules\": [ \"Guix.Demo\" ], \"elm-version\": \"0.19.0 <= v < 0.20.0\", \"dependencies\": { \"elm/core\": \"1.0.0 <= v < 2.0.0\" }, \"test-dependencies\": { \"elm/json\": \"1.0.0 <= v < 2.0.0\" } }") (define test-elm-guix-demo-readme ;; intentionally left blank "") (define (directory-sha256 directory) "Returns the string representing the hash of DIRECTORY as would be used in a package definition." (bytevector->nix-base32-string (file-hash* directory #:algorithm (hash-algorithm sha256) #:recursive? #t))) (test-group "(guix import elm)" (call-with-temporary-directory (lambda (dir) ;; Initialize our fake git checkouts. (define elm-core-dir (string-append dir "/test-elm-core-1.0.4")) (define elm-guix-demo-dir (string-append dir "/test-elm-guix-demo-3.0.0")) (for-each (match-lambda ((dir json readme) (mkdir dir) (with-output-to-file (string-append dir "/elm.json") (lambda () (display json))) (with-output-to-file (string-append dir "/README.md") (lambda () (display readme))))) `((,elm-core-dir ,test-elm-core-json ,test-elm-core-readme) (,elm-guix-demo-dir ,test-elm-guix-demo-json ,test-elm-guix-demo-readme))) ;; Replace network resources with sample data. (parameterize ((%elm-package-registry (lambda () (json-string->scm test-package-registry-json))) (%current-elm-checkout (lambda (name version) (match (list name version) (("elm/core" "1.0.4") elm-core-dir) (("elm-guix/demo" "3.0.0") elm-guix-demo-dir))))) (test-assert "(elm->guix-package \"elm/core\")" (match (elm->guix-package "elm/core") (`(package (name "elm-core") (version "1.0.4") (source (elm-package-origin "elm/core" version (base32 ,(? string? hash)))) (build-system elm-build-system) (home-page "https://package.elm-lang.org/packages/elm/core/1.0.4") (synopsis "Elm's standard libraries") (description "Every Elm project needs this package!") (license license:bsd-3)) (equal? (directory-sha256 elm-core-dir) hash)) (x (raise-exception x)))) (test-assert "(elm-recursive-import \"elm-guix/demo\")" (match (elm-recursive-import "elm-guix/demo") (`((package (name "elm-guix-demo") (version "3.0.0") (source (elm-package-origin "elm-guix/demo" version (base32 ,(? string? hash)))) (build-system elm-build-system) (propagated-inputs ,'`(("elm-core" ,elm-core))) (inputs ,'`(("elm-json" ,elm-json))) (home-page "https://package.elm-lang.org/packages/elm-guix/demo/3.0.0") (synopsis "A test for `(guix import elm)`") (description "This package provides a test for `(guix import elm)`") (properties '((upstream-name . "elm-guix/demo"))) (license license:gpl3+))) (equal? (directory-sha256 elm-guix-demo-dir) hash)) (x (raise-exception x)))))))) (test-end "elm")