Nix 2.36.0
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
10
11#include "nix/util/fun.hh"
12#include "nix/util/types.hh"
14#include "nix/util/file-path.hh"
15#include "nix/util/deleter.hh"
16
17#include <filesystem>
18#include <sys/types.h>
19#include <sys/stat.h>
20#include <dirent.h>
21#include <unistd.h>
22#ifdef _WIN32
23# include <windef.h>
24# include <wchar.h>
25#endif
26
27#include <functional>
28#include <optional>
29
37#ifndef S_IFLNK
38# ifndef _WIN32
39# error "S_IFLNK should be defined on non-Windows platforms"
40# endif
41# define S_IFLNK 0120000
42#endif
43#ifndef S_ISLNK
44# ifndef _WIN32
45# error "S_ISLNK should be defined on non-Windows platforms"
46# endif
47# define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
48#endif
49
50namespace nix {
51
52struct Sink;
53struct Source;
54
62std::filesystem::path
63absPath(const std::filesystem::path & path, const std::filesystem::path * dir = nullptr, bool resolveSymlinks = false);
64
76std::filesystem::path canonPath(const std::filesystem::path & path, bool resolveSymlinks = false);
77
85std::string_view baseNameOf(std::string_view path);
86
91bool isInDir(const std::filesystem::path & path, const std::filesystem::path & dir);
92
97bool isDirOrInDir(const std::filesystem::path & path, const std::filesystem::path & dir);
98
103#ifdef _WIN32
104 struct ::__stat64
105#else
106 struct ::stat
107#endif
108 ;
109
110#ifdef _WIN32
111namespace windows {
112
116time_t fileTimeToUnixTime(const FILETIME & ft);
117
129void statFromFileInfo(
130 PosixStat & st,
131 DWORD dwFileAttributes,
132 const FILETIME & ftCreationTime,
133 const FILETIME & ftLastAccessTime,
134 const FILETIME & ftLastWriteTime,
135 DWORD nFileSizeHigh,
136 DWORD nFileSizeLow,
137 DWORD nNumberOfLinks = 1);
138
139} // namespace windows
140#endif
141
145PosixStat lstat(const std::filesystem::path & path);
149PosixStat stat(const std::filesystem::path & path);
154std::optional<PosixStat> maybeLstat(const std::filesystem::path & path);
155std::optional<PosixStat> maybeStat(const std::filesystem::path & path);
156
160bool pathExists(const std::filesystem::path & path);
161
168bool pathAccessible(const std::filesystem::path & path);
169
174std::filesystem::path readLink(const std::filesystem::path & path);
175
188std::filesystem::path descriptorToPath(Descriptor fd);
189
193enum struct FinalSymlink : bool { DontFollow = false, Follow = true };
194
202Result<AutoCloseFD, NativeError>
203openDirectory(const std::filesystem::path & path, FinalSymlink finalSymlink = FinalSymlink::Follow);
204
218Result<AutoCloseFD, NativeError>
219openDirectoryTraversable(const std::filesystem::path & path, FinalSymlink finalSymlink = FinalSymlink::Follow);
220
228Result<AutoCloseFD, NativeError>
229openFileReadonly(const std::filesystem::path & path, FinalSymlink finalSymlink = FinalSymlink::Follow);
230
232{
236 bool truncateExisting:1 = false;
244 bool writeOnly:1 = true;
245};
246
257openNewFileForWrite(const std::filesystem::path & path, mode_t mode, OpenNewFileForWriteParams params);
258
262std::string readFile(const std::filesystem::path & path);
263void readFile(const std::filesystem::path & path, Sink & sink, bool memory_map = true);
264
265enum struct FsSync { Yes, No };
266
270void writeFile(
271 const std::filesystem::path & path,
272 std::string_view s,
273 mode_t mode = 0666,
274 FsSync sync = FsSync::No,
275 FinalSymlink finalSymlink = FinalSymlink::Follow);
276
277void writeFile(
278 const std::filesystem::path & path,
279 Source & source,
280 mode_t mode = 0666,
281 FsSync sync = FsSync::No,
282 FinalSymlink finalSymlink = FinalSymlink::Follow);
283
284void writeFile(Descriptor fd, Source & source, FsSync sync, const std::filesystem::path * origPath = nullptr);
285
286void writeFile(
287 Descriptor fd, std::string_view s, FsSync sync = FsSync::No, const std::filesystem::path * origPath = nullptr);
288
292void syncParent(const std::filesystem::path & path);
293
297void recursiveSync(const std::filesystem::path & path);
298
304void deletePath(const std::filesystem::path & path);
305
311void deletePath(const std::filesystem::path & path, uint64_t & bytesFreed);
312
318void createDirs(const std::filesystem::path & path);
319
323void createDir(const std::filesystem::path & path, mode_t mode = 0755);
324
338void setWriteTime(
339 const std::filesystem::path & path,
340 time_t accessedTime,
341 time_t modificationTime,
342 std::optional<bool> isSymlink = std::nullopt);
343
347void setWriteTime(const std::filesystem::path & path, const PosixStat & st);
348
353void createSymlink(const std::filesystem::path & target, const std::filesystem::path & link);
354
358void replaceSymlink(const std::filesystem::path & target, const std::filesystem::path & link);
359
369void copyFile(const std::filesystem::path & from, const std::filesystem::path & to, bool contents);
370
374class AutoDelete
375{
376 std::filesystem::path _path;
377 bool del;
378 bool recursive;
379public:
380 AutoDelete();
381
382 AutoDelete(AutoDelete && x) noexcept
383 {
384 _path = std::move(x._path);
385 del = x.del;
386 recursive = x.recursive;
387 x.del = false;
388 }
389
390 AutoDelete & operator=(AutoDelete && x) noexcept
391 {
392 swap(*this, x);
393 return *this;
394 }
395
396 friend void swap(AutoDelete & lhs, AutoDelete & rhs) noexcept
397 {
398 using std::swap;
399 swap(lhs._path, rhs._path);
400 swap(lhs.del, rhs.del);
401 swap(lhs.recursive, rhs.recursive);
402 }
403
404 AutoDelete(const std::filesystem::path & p, bool recursive = true);
405 AutoDelete(const AutoDelete &) = delete;
406 AutoDelete & operator=(const AutoDelete &) = delete;
407 ~AutoDelete();
408
415 void deletePath();
416
420 void cancel() noexcept;
421
422 const std::filesystem::path & path() const
423 {
424 return _path;
425 }
426
427 PathView view() const
428 {
429 return _path;
430 }
431
432 operator const std::filesystem::path &() const
433 {
434 return _path;
435 }
436
437 operator PathView() const
438 {
439 return _path;
440 }
441};
442
443typedef std::unique_ptr<DIR, Deleter<closedir>> AutoCloseDir;
444
448std::filesystem::path
449createTempDir(const std::filesystem::path & tmpRoot = "", const std::string & prefix = "nix", mode_t mode = 0755);
450
455AutoCloseFD createAnonymousTempFile();
456
460std::pair<AutoCloseFD, std::filesystem::path>
461createTempFile(const std::filesystem::path & root, const std::filesystem::path & prefix);
462
466std::pair<AutoCloseFD, std::filesystem::path> createTempFile(const std::filesystem::path & prefix = "nix");
467
473std::filesystem::path defaultTempDir();
474
479bool isExecutableFileAmbient(const std::filesystem::path & exe);
480
487std::filesystem::path makeTempPath(const std::filesystem::path & root, const std::string & suffix = ".tmp");
488
494typedef fun<bool(const std::string & path)> PathFilter;
495
496extern PathFilter defaultPathFilter;
497
512bool chmodIfNeeded(const std::filesystem::path & path, mode_t mode, mode_t mask = S_IRWXU | S_IRWXG | S_IRWXO);
513
523void chmod(const std::filesystem::path & path, mode_t mode);
524
525#ifndef _WIN32
533void chown(const std::filesystem::path & path, uid_t owner, gid_t group);
534#endif
535
541void unlink(const std::filesystem::path & path);
542
548void unlinkIfExists(const std::filesystem::path & path);
549
555void tryUnlink(const std::filesystem::path & path);
556
566void movePath(const std::filesystem::path & src, const std::filesystem::path & dst);
567
574void renameFile(const std::filesystem::path & src, const std::filesystem::path & dst);
575
581class DirectoryIterator
582{
583public:
584 // --- Iterator Traits ---
585 using iterator_category = std::input_iterator_tag;
586 using value_type = std::filesystem::directory_entry;
587 using difference_type = std::ptrdiff_t;
588 using pointer = const std::filesystem::directory_entry *;
589 using reference = const std::filesystem::directory_entry &;
590
591 // Default constructor (represents end iterator)
592 DirectoryIterator() noexcept = default;
593
594 // Constructor taking a path
595 explicit DirectoryIterator(const std::filesystem::path & p);
596
597 reference operator*() const
598 {
599 // Accessing the value itself doesn't typically throw filesystem_error
600 // after successful construction/increment, but underlying operations might.
601 // If directory_entry methods called via -> could throw, add try-catch there.
602 return *it_;
603 }
604
605 pointer operator->() const
606 {
607 return &(*it_);
608 }
609
610 DirectoryIterator & operator++();
611
612 // Postfix increment operator
613 DirectoryIterator operator++(int)
614 {
615 DirectoryIterator temp = *this;
616 ++(*this); // Uses the prefix increment's try-catch logic
617 return temp;
618 }
619
620 // Equality comparison
621 friend bool operator==(const DirectoryIterator & a, const DirectoryIterator & b) noexcept
622 {
623 return a.it_ == b.it_;
624 }
625
626 // Inequality comparison
627 friend bool operator!=(const DirectoryIterator & a, const DirectoryIterator & b) noexcept
628 {
629 return !(a == b);
630 }
631
632 // Allow direct use in range-based for loops if iterating over an instance
633 DirectoryIterator begin() const
634 {
635 return *this;
636 }
637
638 DirectoryIterator end() const
639 {
640 return DirectoryIterator{};
641 }
642
643
644private:
645 std::filesystem::directory_iterator it_;
646};
647
648} // namespace nix
Definition file-descriptor.hh:265
void deletePath()
Definition file-system.cc:419
void cancel() noexcept
Definition file-system.cc:439
Definition fun.hh:19
File descriptor operations for almost arbitrary file descriptors.
void movePath(const std::filesystem::path &src, const std::filesystem::path &dst)
Move/rename path 'src' to 'dst'.
Definition file-system.cc:343
std::filesystem::path defaultTempDir()
Definition file-system.cc:96
FinalSymlink
Definition file-system.hh:193
void chown(const std::filesystem::path &path, uid_t owner, gid_t group)
Definition file-system.cc:337
fun< bool(const std::string &path)> PathFilter
Definition file-system.hh:494
void renameFile(const std::filesystem::path &src, const std::filesystem::path &dst)
Thin wrapper around rename that throws SystemError on errors.
Definition file-system.cc:409
Result< AutoCloseFD, NativeError > openNewFileForWrite(const std::filesystem::path &path, mode_t mode, OpenNewFileForWriteParams params)
Definition file-system.cc:58
struct ::stat PosixStat
Definition file-system.hh:102
outcome::unchecked< T, E > Result
Definition result.hh:24
Definition file-system.hh:232
bool truncateExisting
Definition file-system.hh:236
bool followSymlinksOnTruncate
Definition file-system.hh:240
bool writeOnly
Definition file-system.hh:244
Definition file-path.hh:23
Definition serialise.hh:18