| 1 | #pragma once
|
| 2 |
|
| 3 | #include "types.hh"
|
| 4 |
|
| 5 | #include <sstream>
|
| 6 | #include <string_view>
|
| 7 |
|
| 8 | #include <sys/types.h>
|
| 9 | #include <sys/stat.h>
|
| 10 | #include <dirent.h>
|
| 11 | #include <unistd.h>
|
| 12 | #include <signal.h>
|
| 13 | #include <map>
|
| 14 | #include <functional>
|
| 15 |
|
| 16 | #include <cstdio>
|
| 17 |
|
| 18 |
|
| 19 | namespace nix {
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 | string getEnv(const string & key, const string & def = "");
|
| 24 |
|
| 25 | |
| 26 | PROGRAM is a relative filename. If PROGRAM is an absolute filename for a
|
| 27 | file that doesn't exist, or it can't be found in PATH, then return the
|
| 28 | empty string.
|
| 29 | string findProgram(const string & program);
|
| 30 |
|
| 31 | |
| 32 | specified directory, or the current directory otherwise. The path
|
| 33 | is also canonicalised.
|
| 34 | Path absPath(Path path, Path dir = "");
|
| 35 |
|
| 36 | |
| 37 | double or trailing slashes. Optionally resolves all symlink
|
| 38 | components such that each component of the resulting path is *not*
|
| 39 | a symbolic link.
|
| 40 | Path canonPath(const Path & path, bool resolveSymlinks = false);
|
| 41 |
|
| 42 | |
| 43 | everything before the final `/'. If the path is the root or an
|
| 44 | immediate child thereof (e.g., `/foo'), this means an empty string
|
| 45 | is returned.
|
| 46 | Path dirOf(const Path & path);
|
| 47 |
|
| 48 | |
| 49 | following the final `/'.
|
| 50 | string baseNameOf(const Path & path);
|
| 51 |
|
| 52 | |
| 53 | directory.
|
| 54 | bool isInDir(const Path & path, const Path & dir);
|
| 55 |
|
| 56 |
|
| 57 | struct stat lstat(const Path & path);
|
| 58 |
|
| 59 |
|
| 60 | bool pathExists(const Path & path);
|
| 61 |
|
| 62 | |
| 63 | in any way canonicalised.
|
| 64 | Path readLink(const Path & path);
|
| 65 |
|
| 66 | bool isLink(const Path & path);
|
| 67 |
|
| 68 |
|
| 69 | bool isPlainFile(const Path & path);
|
| 70 |
|
| 71 | |
| 72 | removed.
|
| 73 | struct DirEntry
|
| 74 | {
|
| 75 | string name;
|
| 76 | ino_t ino;
|
| 77 | unsigned char type; |
| 78 | DirEntry(const string & name, ino_t ino, unsigned char type)
|
| 79 | : name(name), ino(ino), type(type) { }
|
| 80 | };
|
| 81 |
|
| 82 | typedef vector<DirEntry> DirEntries;
|
| 83 |
|
| 84 | DirEntries readDirectory(const Path & path);
|
| 85 |
|
| 86 | unsigned char getFileType(const Path & path);
|
| 87 |
|
| 88 |
|
| 89 | string readFile(int fd);
|
| 90 | string readFile(const Path & path, bool drain = false);
|
| 91 |
|
| 92 |
|
| 93 | void writeFile(const Path & path, const string & s);
|
| 94 |
|
| 95 |
|
| 96 | string readLine(int fd);
|
| 97 |
|
| 98 |
|
| 99 | void writeLine(int fd, string s);
|
| 100 |
|
| 101 | |
| 102 | recursively. Don't use this at home, kids. The second variant
|
| 103 | returns the number of bytes and blocks freed, and 'linkThreshold' denotes
|
| 104 | the number of links under which a file is accounted for in 'bytesFreed'.
|
| 105 |
|
| 106 | Note that if a directory is unreadable, chmod will be invoked on it to make
|
| 107 | it u+rwx. On non-linux systems with no equivalent to
|
| 108 | /proc/sys/fs/protected_hardlinks, a TOCTTOU race may allow the directory to
|
| 109 | be replaced with a hardlink to an important file, making that file's
|
| 110 | permissions overly-strict.
|
| 111 |
|
| 112 | void deletePath(const Path & path);
|
| 113 |
|
| 114 | void deletePath(const Path & path, unsigned long long & bytesFreed,
|
| 115 | size_t linkThreshold = 1);
|
| 116 |
|
| 117 | |
| 118 | SOURCE contains a file that is not a regular file, symlink, or directory.
|
| 119 | When DELETESOURCE is true, delete source files once they have been
|
| 120 | copied.
|
| 121 | void copyFileRecursively(const Path &source, const Path &destination,
|
| 122 | bool deleteSource = false);
|
| 123 |
|
| 124 |
|
| 125 | Path createTempDir(const Path & tmpRoot = "", const Path & prefix = "nix",
|
| 126 | bool includePid = true, bool useGlobalCounter = true, mode_t mode = 0755);
|
| 127 |
|
| 128 | |
| 129 | list of created directories, in order of creation.
|
| 130 | Paths createDirs(const Path & path);
|
| 131 |
|
| 132 |
|
| 133 | void createSymlink(const Path & target, const Path & link);
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 | typedef enum {
|
| 140 | ltPretty,
|
| 141 | ltEscapes,
|
| 142 | ltFlat
|
| 143 | } LogType;
|
| 144 |
|
| 145 | inline LogType logType {ltPretty};
|
| 146 | inline Verbosity verbosity {lvlInfo};
|
| 147 |
|
| 148 | class Nest
|
| 149 | {
|
| 150 | private:
|
| 151 | bool nest;
|
| 152 | public:
|
| 153 | Nest();
|
| 154 | ~Nest();
|
| 155 | void open(Verbosity level, std::string_view fs);
|
| 156 | void close();
|
| 157 | };
|
| 158 |
|
| 159 | void printMsg_(Verbosity level, std::string_view fs);
|
| 160 |
|
| 161 | #define startNest(varName, level, f) \
|
| 162 | Nest varName; \
|
| 163 | if (level <= verbosity) { \
|
| 164 | varName.open(level, (f)); \
|
| 165 | }
|
| 166 |
|
| 167 | #define printMsg(level, f) \
|
| 168 | do { \
|
| 169 | if (level <= nix::verbosity) { \
|
| 170 | nix::printMsg_(level, (f)); \
|
| 171 | } \
|
| 172 | } while (0)
|
| 173 |
|
| 174 | #define debug(f) printMsg(lvlDebug, f)
|
| 175 |
|
| 176 | void warnOnce(bool & haveWarned, std::string_view fs);
|
| 177 |
|
| 178 | void writeToStderr(const string & s);
|
| 179 |
|
| 180 | inline void (*_writeToStderr) (const unsigned char * buf, size_t count) = 0;
|
| 181 |
|
| 182 |
|
| 183 | |
| 184 | requested number of bytes.
|
| 185 | void readFull(int fd, unsigned char * buf, size_t count);
|
| 186 | void writeFull(int fd, const unsigned char * buf, size_t count);
|
| 187 | void writeFull(int fd, const string & s);
|
| 188 |
|
| 189 | MakeError(EndOfFile, Error)
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 | string drainFD(int fd);
|
| 194 |
|
| 195 | void waitForMessage(int fd, const string & message);
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 | class AutoDelete
|
| 203 | {
|
| 204 | Path path;
|
| 205 | bool del;
|
| 206 | bool recursive;
|
| 207 | public:
|
| 208 | AutoDelete(const Path & p, bool recursive = true);
|
| 209 | ~AutoDelete();
|
| 210 | void cancel();
|
| 211 | };
|
| 212 |
|
| 213 |
|
| 214 | class AutoCloseFD
|
| 215 | {
|
| 216 | int fd;
|
| 217 | public:
|
| 218 | AutoCloseFD();
|
| 219 | AutoCloseFD(int fd);
|
| 220 | AutoCloseFD(const AutoCloseFD & fd);
|
| 221 | ~AutoCloseFD();
|
| 222 | void operator =(int fd);
|
| 223 | operator int() const;
|
| 224 | void close();
|
| 225 | bool isOpen();
|
| 226 | int borrow();
|
| 227 | };
|
| 228 |
|
| 229 | |
| 230 | byte of regular data.
|
| 231 | void sendFD(int sock, int fd);
|
| 232 | int receiveFD(int sock);
|
| 233 |
|
| 234 | class Pipe
|
| 235 | {
|
| 236 | public:
|
| 237 | AutoCloseFD readSide, writeSide;
|
| 238 | void create();
|
| 239 | };
|
| 240 |
|
| 241 |
|
| 242 | class AutoCloseDir
|
| 243 | {
|
| 244 | DIR * dir;
|
| 245 | public:
|
| 246 | AutoCloseDir();
|
| 247 | AutoCloseDir(DIR * dir);
|
| 248 | ~AutoCloseDir();
|
| 249 | void operator =(DIR * dir);
|
| 250 | operator DIR *();
|
| 251 | void close();
|
| 252 | };
|
| 253 |
|
| 254 |
|
| 255 | class Pid
|
| 256 | {
|
| 257 | pid_t pid;
|
| 258 | bool separatePG;
|
| 259 | int killSignal;
|
| 260 | public:
|
| 261 | Pid();
|
| 262 | Pid(pid_t pid);
|
| 263 | ~Pid();
|
| 264 | void operator =(pid_t pid);
|
| 265 | operator pid_t();
|
| 266 | void kill(bool quiet = false);
|
| 267 | int wait(bool block);
|
| 268 | void setSeparatePG(bool separatePG);
|
| 269 | void setKillSignal(int signal);
|
| 270 | };
|
| 271 |
|
| 272 | |
| 273 | to over pipes, such as the "guix offload" program.
|
| 274 | struct Agent
|
| 275 | {
|
| 276 |
|
| 277 | Pipe toAgent;
|
| 278 |
|
| 279 |
|
| 280 | Pipe fromAgent;
|
| 281 |
|
| 282 | |
| 283 | by "guix offload".
|
| 284 | Pipe builderOut;
|
| 285 |
|
| 286 |
|
| 287 | Pid pid;
|
| 288 |
|
| 289 | |
| 290 | environment variable name/value pairs.
|
| 291 | Agent(const string &command, const Strings &args,
|
| 292 | const std::map<string, string> &env = std::map<string, string>());
|
| 293 |
|
| 294 | ~Agent();
|
| 295 | };
|
| 296 |
|
| 297 |
|
| 298 | |
| 299 | a SIGKILL.
|
| 300 | void killUser(uid_t uid);
|
| 301 |
|
| 302 |
|
| 303 | |
| 304 | pid to the caller.
|
| 305 | pid_t startProcess(std::function<void()> fun, bool dieWithParent = true,
|
| 306 | const string & errorPrefix = "error: ", bool runExitHandlers = false);
|
| 307 |
|
| 308 |
|
| 309 | |
| 310 | shell backtick operator).
|
| 311 | string runProgram(Path program, bool searchPath = false,
|
| 312 | const Strings & args = Strings());
|
| 313 |
|
| 314 | MakeError(ExecError, Error)
|
| 315 |
|
| 316 | |
| 317 | *'s. The result must not be accessed beyond the lifetime of the
|
| 318 | list of strings.
|
| 319 | std::vector<char *> stringsToCharPtrs(const Strings & ss);
|
| 320 |
|
| 321 | |
| 322 | listed in the given set. Good practice in child processes.
|
| 323 | void closeMostFDs(const set<int> & exceptions);
|
| 324 |
|
| 325 |
|
| 326 | void closeOnExec(int fd);
|
| 327 |
|
| 328 |
|
| 329 | void keepOnExec(int fd);
|
| 330 |
|
| 331 |
|
| 332 | void commonChildInit(Pipe & logPipe);
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 | inline volatile sig_atomic_t _isInterrupted = 0;
|
| 337 |
|
| 338 | void _interrupted();
|
| 339 |
|
| 340 | void inline checkInterrupt()
|
| 341 | {
|
| 342 | if (_isInterrupted) _interrupted();
|
| 343 | }
|
| 344 |
|
| 345 | MakeError(Interrupted, BaseError)
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 | template<class C> C tokenizeString(const string & s, const string & separators = " \t\n\r");
|
| 350 |
|
| 351 |
|
| 352 | |
| 353 | elements.
|
| 354 | string concatStringsSep(const string & sep, const Strings & ss);
|
| 355 | string concatStringsSep(const string & sep, const StringSet & ss);
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 | string chomp(const string & s);
|
| 360 |
|
| 361 |
|
| 362 | |
| 363 | error string.
|
| 364 | string statusToString(int status);
|
| 365 |
|
| 366 | bool statusOk(int status);
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 | template<class N> bool string2Int(const string & s, N & n)
|
| 371 | {
|
| 372 | std::istringstream str(s);
|
| 373 | str >> n;
|
| 374 | return str && str.get() == EOF;
|
| 375 | }
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 | void expect(std::istream & str, std::string_view s);
|
| 380 |
|
| 381 | MakeError(FormatError, Error)
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 | string parseString(std::istream & str);
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 | bool endOfList(std::istream & str);
|
| 390 |
|
| 391 |
|
| 392 | |
| 393 | used in /etc/fstab and /proc/mounts (e.g. "foo\040bar" decodes to
|
| 394 | "foo bar").
|
| 395 | string decodeOctalEscaped(const string & s);
|
| 396 |
|
| 397 |
|
| 398 | |
| 399 | ignore the exception.
|
| 400 | void ignoreException();
|
| 401 |
|
| 402 |
|
| 403 | }
|