forked from viewfinderco/viewfinder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileUtils.cc
239 lines (219 loc) · 5.29 KB
/
FileUtils.cc
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Copyright 2012 Viewfinder. All rights reserved.
// Author: Peter Mattis.
#import <dirent.h>
#import <errno.h>
#import <fcntl.h>
#import <sys/stat.h>
#import <unistd.h>
#import <google/protobuf/message_lite.h>
#import "FileUtils.h"
#import "Logging.h"
int FileCreate(const string& path, bool exclude_from_backup) {
const int new_fd = open(path.c_str(), O_CREAT|O_WRONLY, 0644);
if (new_fd < 0) {
VLOG("open failed: %s: %d (%s)", path, errno, strerror(errno));
return -1;
}
if (exclude_from_backup) {
FileExcludeFromBackup(path);
}
return new_fd;
}
bool FileExists(const string& path) {
struct stat s;
if (stat(path.c_str(), &s) < 0) {
return false;
}
return s.st_mode & S_IFREG;
}
int64_t FileSize(const string& path) {
struct stat s;
if (stat(path.c_str(), &s) < 0) {
if (errno != ENOENT) {
VLOG("stat failed: %s: %d (%s)", path, errno, strerror(errno));
}
return -1;
}
return s.st_size;
}
int64_t FileSize(int fd) {
struct stat s;
if (fstat(fd, &s) < 0) {
VLOG("fstat failed: fd %d: %d (%s)", fd, errno, strerror(errno));
return -1;
}
return s.st_size;
}
bool FileRename(const string& old_path, const string& new_path) {
if (rename(old_path.c_str(), new_path.c_str()) < 0) {
VLOG("rename failed: %s -> %s: %d (%s)",
old_path, new_path, errno, strerror(errno));
return false;
}
return true;
}
bool FileRemove(const string& path) {
if (unlink(path.c_str()) < 0) {
if (errno != ENOENT) {
VLOG("remove failed: %s: %d (%s)", path, errno, strerror(errno));
}
return false;
}
return true;
}
bool DirCreate(const string& path, int mode, bool exclude_from_backup) {
if (mkdir(path.c_str(), mode) < 0) {
if (errno != EEXIST) {
VLOG("mkdir failed: %s: %d (%s)", path, errno, strerror(errno));
return false;
}
return true;
}
if (exclude_from_backup) {
FileExcludeFromBackup(path, true);
}
return true;
}
bool DirExists(const string& path) {
struct stat s;
if (stat(path.c_str(), &s) < 0) {
return false;
}
return s.st_mode & S_IFDIR;
}
bool DirRemove(const string& path, bool recursive, int* files, int* dirs) {
if (recursive) {
DIR* dir = opendir(path.c_str());
if (!dir) {
return false;
}
struct dirent* r = NULL;
while ((r = readdir(dir)) != 0) {
const string name(r->d_name);
if (name == "." || name == "..") {
continue;
}
const string subpath(path + "/" + name);
struct stat s;
if (lstat(subpath.c_str(), &s) < 0) {
continue;
}
if (s.st_mode & S_IFDIR) {
if (!DirRemove(subpath, true, files, dirs)) {
break;
}
} else {
if (!FileRemove(subpath)) {
break;
}
if (files) {
*files += 1;
}
}
}
closedir(dir);
}
if (rmdir(path.c_str()) < 0) {
LOG("rmdir failed: %s: %d (%s)", path, errno, strerror(errno));
return false;
}
if (dirs) {
*dirs += 1;
}
return true;
}
bool DirList(const string& path, vector<string>* files) {
DIR* dir = opendir(path.c_str());
if (!dir) {
return false;
}
struct dirent* r = NULL;
while ((r = readdir(dir)) != 0) {
const string name(r->d_name);
if (name == "." || name == "..") {
continue;
}
files->push_back(name);
}
closedir(dir);
return true;
}
bool WriteStringToFD(int fd, const Slice& str, bool silent) {
const char* p = str.data();
int n = str.size();
while (n > 0) {
ssize_t res = write(fd, p, n);
if (res < 0) {
if (!silent) {
LOG("write failed: %d (%s)", errno, strerror(errno));
}
break;
}
p += res;
n -= res;
}
return (n == 0);
}
bool WriteStringToFile(const string& path, const Slice& str,
bool exclude_from_backup) {
const string tmp_path(path + ".tmp");
int fd = FileCreate(tmp_path, exclude_from_backup);
if (fd < 0) {
LOG("open failed: %s: %d (%s)", tmp_path, errno, strerror(errno));
return false;
}
const bool res = WriteStringToFD(fd, str);
close(fd);
if (!res) {
FileRemove(tmp_path);
return false;
}
return FileRename(tmp_path, path);
}
bool WriteProtoToFile(
const string& path, const google::protobuf::MessageLite& message,
bool exclude_from_backup) {
return WriteStringToFile(
path, message.SerializeAsString(), exclude_from_backup);
}
bool ReadFileToString(const string& path, string* str) {
int fd = open(path.c_str(), O_RDONLY);
if (fd < 0) {
// LOG("open failed: %s: %d (%s)", path, errno, strerror(errno));
return false;
}
struct stat s;
if (fstat(fd, &s) < 0) {
LOG("stat failed: %s: %d (%s)", path, errno, strerror(errno));
return false;
}
int n = s.st_size;
str->resize(n);
char* p = &(*str)[0];
while (n > 0) {
ssize_t res = read(fd, p, n);
if (res < 0) {
LOG("read failed: %s: %d (%s)", path, errno, strerror(errno));
break;
}
p += res;
n -= res;
}
close(fd);
return n == 0;
}
string ReadFileToString(const string& path) {
string s;
if (!ReadFileToString(path, &s)) {
return string();
}
return s;
}
bool ReadFileToProto(
const string& path, google::protobuf::MessageLite* message) {
string s;
if (!ReadFileToString(path, &s)) {
return false;
}
return message->ParseFromString(s);
}