1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
#pragma once
#include <util/generic/flags.h>
#include <util/generic/string.h>
#include <util/generic/yexception.h>
namespace NFs {
enum EFilePermission {
FP_ALL_EXEC = 01,
FP_ALL_WRITE = 02,
FP_ALL_READ = 04,
FP_GROUP_READ = 040,
FP_GROUP_WRITE = 020,
FP_GROUP_EXEC = 010,
FP_OWNER_READ = 0400,
FP_OWNER_WRITE = 0200,
FP_OWNER_EXEC = 0100,
FP_COMMON_FILE = 0777,
FP_SECRET_FILE = 0700,
FP_NONSECRET_FILE = 0744,
};
Y_DECLARE_FLAGS(EFilePermissions, EFilePermission);
/// Add executable bit
///
/// @param[in] path Path to mark as executable
/// @param[in] exec New value of execution bit
/// @returns true if bit has changed or false otherwise
bool SetExecutable(const TString& path, bool exec);
/// Remove a file or empty directory
///
/// @param[in] path Path to file or directory
/// @returns true on success or false otherwise
/// LastSystemError() is set in case of failure
bool Remove(const TString& path);
/// Remove a file or directory with contents
/// Does nothing if path does not exist
///
/// @param[in] path Path to file or directory
/// @throws
void RemoveRecursive(const TString& path);
/// Creates directory
///
/// @param[in] path Path to the directory
/// @param[in] mode Access permissions field; NOTE: ignored on win
/// @returns true on success or false otherwise
bool MakeDirectory(const TString& path, EFilePermissions mode);
/// Creates directory
///
/// @param[in] path Path to the directory
/// @returns true on success or false otherwise
/// NOTE: access permissions is set to 0777
inline bool MakeDirectory(const TString& path) {
return MakeDirectory(path, FP_COMMON_FILE);
}
/// Creates directory and all non-existings parents
///
/// @param[in] path Path to be created
/// @param[in] mode Access permissions field; NOTE: ignored on win
/// @param[in] alwaysCreate Throw if path already exists or failed to create
/// @returns true if target path created or exists (and directory)
bool MakeDirectoryRecursive(const TString& path, EFilePermissions mode, bool alwaysCreate);
/// Creates directory and all non-existings parents
///
/// @param[in] path Path to be created
/// @param[in] mode Access permissions field; NOTE: ignored on win
/// @returns true if target path created or exists (and directory)
inline bool MakeDirectoryRecursive(const TString& path, EFilePermissions mode) {
return MakeDirectoryRecursive(path, mode, false);
}
/// Creates directory and all non-existings parents
///
/// @param[in] path Path to be created
/// @returns true if target path created or exists (and directory)
inline bool MakeDirectoryRecursive(const TString& path) {
return MakeDirectoryRecursive(path, FP_COMMON_FILE, false);
}
/// Rename a file or directory.
/// Removes newPath if it exists
///
/// @param[in] oldPath Path to file or directory to rename
/// @param[in] newPath New path of file or directory
/// @returns true on success or false otherwise
/// LastSystemError() is set in case of failure
bool Rename(const TString& oldPath, const TString& newPath);
/// Creates a new directory entry for a file
/// or creates a new one with the same content
///
/// @param[in] existingPath Path to an existing file
/// @param[in] newPath New path of file
void HardLinkOrCopy(const TString& existingPath, const TString& newPath);
/// Creates a new directory entry for a file
///
/// @param[in] existingPath Path to an existing file
/// @param[in] newPath New path of file
/// @returns true if new link was created or false otherwise
/// LastSystemError() is set in case of failure
bool HardLink(const TString& existingPath, const TString& newPath);
/// Creates a symlink to a file
///
/// @param[in] targetPath Path to a target file
/// @param[in] linkPath Path of symlink
/// @returns true if new link was created or false otherwise
/// LastSystemError() is set in case of failure
bool SymLink(const TString& targetPath, const TString& linkPath);
/// Reads value of a symbolic link
///
/// @param[in] path Path to a symlink
/// @returns File path that a symlink points to
TString ReadLink(const TString& path);
/// Append contents of a file to a new file
///
/// @param[in] dstPath Path to a destination file
/// @param[in] srcPath Path to a source file
void Cat(const TString& dstPath, const TString& srcPath);
/// Copy contents of a file to a new file
///
/// @param[in] existingPath Path to an existing file
/// @param[in] newPath New path of file
void Copy(const TString& existingPath, const TString& newPath);
/// Returns path to the current working directory
///
/// Note: is not threadsafe
TString CurrentWorkingDirectory();
/// Changes current working directory
///
/// @param[in] path Path for new cwd
/// Note: is not threadsafe
void SetCurrentWorkingDirectory(const TString& path);
/// Checks if file exists
///
/// @param[in] path Path to check
bool Exists(const TString& path);
/// Ensures that file exists
///
/// @param[in] path Path to check
/// @returns input argument
inline const TString& EnsureExists(const TString& path) {
Y_ENSURE_EX(Exists(path), TFileError{} << "Path " << path << " does not exists (checked from cwd:" << NFs::CurrentWorkingDirectory() << ")");
return path;
}
} // namespace NFs
Y_DECLARE_OPERATORS_FOR_FLAGS(NFs::EFilePermissions);
|