Nix 2.26.3
Nix, the purely functional package manager; unstable internal interfaces
 
Loading...
Searching...
No Matches
file-system.hh
Go to the documentation of this file.
1#pragma once
7
8#include "types.hh"
9#include "error.hh"
10#include "logging.hh"
11#include "file-descriptor.hh"
12#include "file-path.hh"
13
14#include <sys/types.h>
15#include <sys/stat.h>
16#include <dirent.h>
17#include <unistd.h>
18#ifdef _WIN32
19# include <windef.h>
20#endif
21#include <signal.h>
22
23#include <atomic>
24#include <functional>
25#include <map>
26#include <sstream>
27#include <optional>
28
36#ifndef S_ISLNK
37# define S_ISLNK(m) false
38#endif
39
40namespace nix {
41
42struct Sink;
43struct Source;
44
48bool isAbsolute(PathView path);
49
57Path absPath(PathView path,
58 std::optional<PathView> dir = {},
59 bool resolveSymlinks = false);
60
61inline Path absPath(const Path & path,
62 std::optional<PathView> dir = {},
63 bool resolveSymlinks = false)
64{
65 return absPath(PathView{path}, dir, resolveSymlinks);
66}
67
68std::filesystem::path absPath(const std::filesystem::path & path,
69 bool resolveSymlinks = false);
70
82Path canonPath(PathView path, bool resolveSymlinks = false);
83
93Path dirOf(const PathView path);
94
102std::string_view baseNameOf(std::string_view path);
103
108bool isInDir(std::string_view path, std::string_view dir);
109
114bool isDirOrInDir(std::string_view path, std::string_view dir);
115
119struct stat stat(const Path & path);
120struct stat lstat(const Path & path);
125std::optional<struct stat> maybeLstat(const Path & path);
126
132bool pathExists(const Path & path);
133
134namespace fs {
135
145inline bool symlink_exists(const std::filesystem::path & path) {
146 return std::filesystem::exists(std::filesystem::symlink_status(path));
147}
148
149} // namespace fs
150
166std::filesystem::path makeParentCanonical(const std::filesystem::path & path);
167
174bool pathAccessible(const std::filesystem::path & path);
175
183Path readLink(const Path & path);
184
188Descriptor openDirectory(const std::filesystem::path & path);
189
193std::string readFile(const Path & path);
194std::string readFile(const std::filesystem::path & path);
195void readFile(const Path & path, Sink & sink);
196
200void writeFile(const Path & path, std::string_view s, mode_t mode = 0666, bool sync = false);
201static inline void writeFile(const std::filesystem::path & path, std::string_view s, mode_t mode = 0666, bool sync = false)
202{
203 return writeFile(path.string(), s, mode, sync);
204}
205
206void writeFile(const Path & path, Source & source, mode_t mode = 0666, bool sync = false);
207static inline void writeFile(const std::filesystem::path & path, Source & source, mode_t mode = 0666, bool sync = false)
208{
209 return writeFile(path.string(), source, mode, sync);
210}
211
215void syncParent(const Path & path);
216
220void recursiveSync(const Path & path);
221
227void deletePath(const std::filesystem::path & path);
228
229void deletePath(const std::filesystem::path & path, uint64_t & bytesFreed);
230
237void createDirs(const Path & path);
238inline void createDirs(PathView path)
239{
240 return createDirs(Path(path));
241}
242
246void createDir(const Path & path, mode_t mode = 0755);
247
261void setWriteTime(
262 const std::filesystem::path & path,
263 time_t accessedTime,
264 time_t modificationTime,
265 std::optional<bool> isSymlink = std::nullopt);
266
270void setWriteTime(const std::filesystem::path & path, const struct stat & st);
271
276void createSymlink(const Path & target, const Path & link);
277
281void replaceSymlink(const std::filesystem::path & target, const std::filesystem::path & link);
282
283inline void replaceSymlink(const Path & target, const Path & link)
284{
285 return replaceSymlink(std::filesystem::path{target}, std::filesystem::path{link});
286}
287
295void moveFile(const Path & src, const Path & dst);
296
303void copyFile(const std::filesystem::path & from, const std::filesystem::path & to, bool andDelete);
304
308class AutoDelete
309{
310 std::filesystem::path _path;
311 bool del;
312 bool recursive;
313public:
314 AutoDelete();
315 AutoDelete(const std::filesystem::path & p, bool recursive = true);
316 ~AutoDelete();
317
318 void cancel();
319
320 void reset(const std::filesystem::path & p, bool recursive = true);
321
322 const std::filesystem::path & path() const { return _path; }
323 PathViewNG view() const { return _path; }
324
325 operator const std::filesystem::path & () const { return _path; }
326 operator PathViewNG () const { return _path; }
327};
328
329
331{
332 void operator()(DIR * dir) const {
333 closedir(dir);
334 }
335};
336
337typedef std::unique_ptr<DIR, DIRDeleter> AutoCloseDir;
338
339
343Path createTempDir(const Path & tmpRoot = "", const Path & prefix = "nix",
344 bool includePid = true, bool useGlobalCounter = true, mode_t mode = 0755);
345
349std::pair<AutoCloseFD, Path> createTempFile(const Path & prefix = "nix");
350
354Path defaultTempDir();
355
360bool isExecutableFileAmbient(const std::filesystem::path & exe);
361
365typedef std::function<bool(const Path & path)> PathFilter;
366
367extern PathFilter defaultPathFilter;
368
369}
This file defines two main structs/classes used in nix error handling.
int Descriptor
Definition file-descriptor.hh:20
std::function< bool(const Path &path)> PathFilter
Definition file-system.hh:365
Descriptor openDirectory(const std::filesystem::path &path)
bool symlink_exists(const std::filesystem::path &path)
Definition file-system.hh:145
std::optional< PathView > dir
Definition lexer.l:3856
std::optional< SourceAccessor::Stat > maybeLstat() const
return s
Definition lexer.l:459
bool includePid
Definition lexer.l:4142
return writeFile(path.string(), s, mode, sync)
std::shared_ptr< T > p
Definition lexer.l:1269
std::string std::string_view from
Definition lexer.l:2591
std::string readFile() const
std::string path
Definition lexer.l:1399
std::string std::string_view std::string_view to
Definition lexer.l:2592
std::string readLink() const
bool pathExists() const
Definition file-system.hh:331
Definition file-path.hh:26
Definition serialise.hh:20
std::string Path
Definition types.hh:22