Nix  2.93.0-dev
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
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
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, bool sync = false);
193
194void writeFile(const Path & path, Source & source, mode_t mode = 0666, bool sync = false);
195kj::Promise<Result<void>>
196writeFile(const Path & path, AsyncInputStream & source, mode_t mode = 0666, bool sync = false);
197
201void syncParent(const Path & path);
202
208void deletePath(const Path & path);
209void deletePathUninterruptible(const Path & path);
210
211void deletePath(const Path & path, uint64_t & bytesFreed);
212
217Paths createDirs(const Path & path);
218inline Paths createDirs(PathView path)
219{
220 return createDirs(Path(path));
221}
222
226void createSymlink(const Path & target, const Path & link);
227
231void replaceSymlink(const Path & target, const Path & link);
232
233void renameFile(const Path & src, const Path & dst);
234
242void moveFile(const Path & src, const Path & dst);
243
245{
249 bool deleteAfter = false;
250
254 bool followSymlinks = false;
255};
256
263void copyFile(const Path & oldPath, const Path & newPath, CopyFileFlags flags);
264
271{
272 Path path;
273 bool del;
274 bool recursive;
275public:
276 AutoDelete();
277 AutoDelete(const Path & p, bool recursive = true);
278 ~AutoDelete();
279 void cancel();
280 void reset(const Path & p, bool recursive = true);
281 operator Path() const { return path; }
282 operator PathView() const { return path; }
283};
284
286{
287 void operator()(DIR * dir) const {
288 closedir(dir);
289 }
290};
291
292typedef std::unique_ptr<DIR, DIRDeleter> AutoCloseDir;
293
297Path createTempSubdir(const Path & parent, const Path & prefix = "nix",
298 bool includePid = true, bool useGlobalCounter = true, mode_t mode = 0755);
299
306Path makeTempPath(const Path & root, const Path & suffix = ".tmp");
307
311typedef std::function<bool(const Path & path)> PathFilter;
312
313extern PathFilter defaultPathFilter;
314
315}
Definition file-system.hh:271
std::function< bool(const Path &path) PathFilter)
Definition file-system.hh:311
Definition file-system.hh:245
bool followSymlinks
Definition file-system.hh:254
bool deleteAfter
Definition file-system.hh:249
Definition file-system.hh:286
Definition file-system.hh:166
unsigned char type
Definition file-system.hh:172
std::string Path
Definition types.hh:28