Nix 2.93.3
Lix: A modern, delicious implementation of the Nix package manager; unstable internal interfaces
Loading...
Searching...
No Matches
file-system.hh
Go to the documentation of this file.
1#pragma once
7
10#include "lix/libutil/types.hh"
12
13#include <sys/types.h>
14#include <sys/stat.h>
15#include <dirent.h>
16#include <unistd.h>
17
18#include <functional>
19#include <optional>
20
21#ifndef HAVE_STRUCT_DIRENT_D_TYPE
22#define DT_UNKNOWN 0
23#define DT_REG 1
24#define DT_LNK 2
25#define DT_DIR 3
26#endif
27
28namespace nix {
29
30struct Sink;
31struct Source;
32
38Path getCwd();
39
45Path absPath(Path path,
46 std::optional<PathView> dir = {},
47 bool resolveSymlinks = false);
48
55Path canonPath(PathView path, bool resolveSymlinks = false);
56
71Path realPath(Path const & path);
72
81Path tildePath(Path const & path, const std::optional<Path> & home = std::nullopt);
82
88void chmodPath(const Path & path, mode_t mode);
89
96Path dirOf(const PathView path);
97
102std::string_view baseNameOf(std::string_view path);
103
107std::string expandTilde(std::string_view path);
108
113bool isInDir(std::string_view path, std::string_view dir);
114
119bool isDirOrInDir(std::string_view path, std::string_view dir);
120
124struct stat stat(const Path & path);
125struct stat lstat(const Path & path);
126
131std::optional<struct stat> maybeStat(const Path & path);
132
137std::optional<struct stat> maybeLstat(const Path & path);
138
142bool pathExists(const Path & path);
143
151bool pathAccessible(const Path & path, bool resolveSymlinks = false);
152
157Path readLink(const Path & path);
158
159bool isLink(const Path & path);
160
165struct DirEntry
166{
167 std::string name;
168 ino_t ino;
172 unsigned char type;
173 DirEntry(std::string name, ino_t ino, unsigned char type)
174 : name(std::move(name)), ino(ino), type(type) { }
175};
176
177typedef std::vector<DirEntry> DirEntries;
178
179DirEntries readDirectory(const Path & path);
180
181unsigned char getFileType(const Path & path);
182
186std::string readFile(const Path & path);
187Generator<Bytes> readFileSource(const Path & path);
188
192void writeFile(const Path & path, std::string_view s, mode_t mode = 0666);
193
194void writeFile(const Path & path, Source & source, mode_t mode = 0666);
195
196void writeFile(AutoCloseFD & fd, std::string_view s, mode_t mode = 0666);
197kj::Promise<Result<void>>
198writeFile(const Path & path, AsyncInputStream & source, mode_t mode = 0666);
199
203void writeFileAndSync(const Path & path, std::string_view s, mode_t mode = 0666);
204
208void syncParent(const Path & path);
209
215void deletePath(const Path & path);
216void deletePathUninterruptible(const Path & path);
217
218void deletePath(const Path & path, uint64_t & bytesFreed);
219
224Paths createDirs(const Path & path);
225inline Paths createDirs(PathView path)
226{
227 return createDirs(Path(path));
228}
229
233void createSymlink(const Path & target, const Path & link);
234
238void replaceSymlink(const Path & target, const Path & link);
239
240void renameFile(const Path & src, const Path & dst);
241
249void moveFile(const Path & src, const Path & dst);
250
252{
256 bool deleteAfter = false;
257
261 bool followSymlinks = false;
262};
263
270void copyFile(const Path & oldPath, const Path & newPath, CopyFileFlags flags);
271
275
276
277class AutoDelete
278{
279 Path path;
280 bool del;
281 bool recursive;
282public:
283 AutoDelete();
284 AutoDelete(const Path & p, bool recursive = true);
285 ~AutoDelete();
286 void cancel();
287 void reset(const Path & p, bool recursive = true);
288 operator Path() const { return path; }
289 operator PathView() const { return path; }
290};
291
293{
294 void operator()(DIR * dir) const {
295 closedir(dir);
296 }
297};
298
299typedef std::unique_ptr<DIR, DIRDeleter> AutoCloseDir;
300
304Path createTempSubdir(const Path & parent, const Path & prefix = "nix",
305 bool includePid = true, bool useGlobalCounter = true, mode_t mode = 0755);
306
313Path makeTempPath(const Path & root, const Path & suffix = ".tmp");
314
318typedef std::function<bool(const Path & path)> PathFilter;
319
320extern PathFilter defaultPathFilter;
321
322}
std::function< bool(const Path &path)> PathFilter
Definition file-system.hh:318
Definition file-system.hh:252
bool followSymlinks
Definition file-system.hh:261
bool deleteAfter
Definition file-system.hh:256
Definition file-system.hh:293
unsigned char type
Definition file-system.hh:172
Definition serialise.hh:18
Definition serialise.hh:66
std::string Path
Definition types.hh:28