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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
#include "fs.h"
#include "defaults.h"
#if defined(_win_)
#include "fs_win.h"
#else
#include <unistd.h>
#include <errno.h>
#endif
#include <util/generic/yexception.h>
#include <util/memory/tempbuf.h>
#include <util/stream/file.h>
#include <util/folder/iterator.h>
#include <util/system/fstat.h>
#include <util/folder/path.h>
bool NFs::Remove(const TString& path) {
#if defined(_win_)
return NFsPrivate::WinRemove(path);
#else
return ::remove(path.data()) == 0;
#endif
}
void NFs::RemoveRecursive(const TString& path) {
static const TStringBuf errStr = "error while removing ";
if (!NFs::Exists(path)) {
return;
}
if (!TFileStat(path).IsDir()) {
if (!NFs::Remove(path)) {
ythrow TSystemError() << errStr << path << " with cwd (" << NFs::CurrentWorkingDirectory() << ")";
}
}
TDirIterator dir(path);
for (auto it = dir.begin(); it != dir.end(); ++it) {
switch (it->fts_info) {
case FTS_DOT:
case FTS_D:
break;
default:
if (!NFs::Remove(it->fts_path)) {
ythrow TSystemError() << errStr << it->fts_path << " with cwd (" << NFs::CurrentWorkingDirectory() << ")";
}
break;
}
}
}
bool NFs::MakeDirectory(const TString& path, EFilePermissions mode) {
#if defined(_win_)
Y_UNUSED(mode);
return NFsPrivate::WinMakeDirectory(path);
#else
return mkdir(path.data(), mode) == 0;
#endif
}
bool NFs::MakeDirectoryRecursive(const TString& path, EFilePermissions mode, bool alwaysCreate) {
if (NFs::Exists(path) && TFileStat(path).IsDir()) {
if (alwaysCreate) {
ythrow TIoException() << "path " << path << " already exists"
<< " with cwd (" << NFs::CurrentWorkingDirectory() << ")";
}
return true;
} else {
//NOTE: recursion is finite due to existence of "." and "/"
if (!NFs::MakeDirectoryRecursive(TFsPath(path).Parent(), mode, false)) {
return false;
}
bool isDirMade = NFs::MakeDirectory(path, mode);
if (!isDirMade && alwaysCreate) {
ythrow TIoException() << "failed to create " << path << " with cwd (" << NFs::CurrentWorkingDirectory() << ")";
}
return TFileStat(path).IsDir();
}
}
bool NFs::Rename(const TString& oldPath, const TString& newPath) {
#if defined(_win_)
return NFsPrivate::WinRename(oldPath, newPath);
#else
return ::rename(oldPath.data(), newPath.data()) == 0;
#endif
}
void NFs::HardLinkOrCopy(const TString& existingPath, const TString& newPath) {
if (!NFs::HardLink(existingPath, newPath)) {
Copy(existingPath, newPath);
}
}
bool NFs::HardLink(const TString& existingPath, const TString& newPath) {
#if defined(_win_)
return NFsPrivate::WinHardLink(existingPath, newPath);
#elif defined(_unix_)
return (0 == link(existingPath.data(), newPath.data()));
#endif
}
bool NFs::SymLink(const TString& targetPath, const TString& linkPath) {
#if defined(_win_)
return NFsPrivate::WinSymLink(targetPath, linkPath);
#elif defined(_unix_)
return 0 == symlink(targetPath.data(), linkPath.data());
#endif
}
TString NFs::ReadLink(const TString& path) {
#if defined(_win_)
return NFsPrivate::WinReadLink(path);
#elif defined(_unix_)
TTempBuf buf;
while (true) {
ssize_t r = readlink(path.data(), buf.Data(), buf.Size());
if (r < 0) {
ythrow yexception() << "can't read link " << path << ", errno = " << errno;
}
if (r < (ssize_t)buf.Size()) {
return TString(buf.Data(), r);
}
buf = TTempBuf(buf.Size() * 2);
}
#endif
}
void NFs::Cat(const TString& dstPath, const TString& srcPath) {
TUnbufferedFileInput src(srcPath);
TUnbufferedFileOutput dst(TFile(dstPath, ForAppend | WrOnly | Seq));
TransferData(&src, &dst);
}
void NFs::Copy(const TString& existingPath, const TString& newPath) {
TUnbufferedFileInput src(existingPath);
TUnbufferedFileOutput dst(TFile(newPath, CreateAlways | WrOnly | Seq));
TransferData(&src, &dst);
}
bool NFs::Exists(const TString& path) {
#if defined(_win_)
return NFsPrivate::WinExists(path);
#elif defined(_unix_)
return access(path.data(), F_OK) == 0;
#endif
}
TString NFs::CurrentWorkingDirectory() {
#if defined(_win_)
return NFsPrivate::WinCurrentWorkingDirectory();
#elif defined(_unix_)
TTempBuf result;
char* r = getcwd(result.Data(), result.Size());
if (r == nullptr) {
throw TIoSystemError() << "failed to getcwd";
}
return result.Data();
#endif
}
void NFs::SetCurrentWorkingDirectory(const TString& path) {
#ifdef _win_
bool ok = NFsPrivate::WinSetCurrentWorkingDirectory(path);
#else
bool ok = !chdir(path.data());
#endif
if (!ok) {
ythrow TSystemError() << "failed to change directory to " << path.Quote();
}
}
|