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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
|
#include "StarFile.hpp"
#include "StarFormat.hpp"
#include "StarRandom.hpp"
#include "StarEncode.hpp"
#include <errno.h>
#include <string.h>
#include <limits.h>
#include <stdlib.h>
#include <dirent.h>
#include <unistd.h>
#include <libgen.h>
#include <fcntl.h>
#include <sys/stat.h>
#ifdef STAR_SYSTEM_MACOSX
#include <mach-o/dyld.h>
#elif defined STAR_SYSTEM_FREEBSD
#include <sys/types.h>
#include <sys/sysctl.h>
#endif
namespace Star {
namespace {
int fdFromHandle(void* ptr) {
return (int)(intptr_t)ptr;
}
void* handleFromFd(int handle) {
return (void*)(intptr_t)handle;
}
}
String File::convertDirSeparators(String const& path) {
return path.replace("\\", "/");
}
String File::currentDirectory() {
char buffer[PATH_MAX];
if (::getcwd(buffer, PATH_MAX) == NULL)
throw IOException("getcwd failed");
return String(buffer);
}
void File::changeDirectory(const String& dirName) {
if (::chdir(dirName.utf8Ptr()) != 0)
throw IOException(strf("could not change directory to {}", dirName));
}
void File::makeDirectory(String const& dirName) {
if (::mkdir(dirName.utf8Ptr(), 0777) != 0)
throw IOException(strf("could not create directory '{}', {}", dirName, strerror(errno)));
}
List<pair<String, bool>> File::dirList(const String& dirName, bool skipDots) {
List<std::pair<String, bool>> fileList;
DIR* directory = ::opendir(dirName.utf8Ptr());
if (directory == NULL)
throw IOException::format("dirList failed on dir: '{}'", dirName);
for (dirent* entry = ::readdir(directory); entry != NULL; entry = ::readdir(directory)) {
String entryString = entry->d_name;
if (!skipDots || (entryString != "." && entryString != "..")) {
bool isDirectory = false;
if (entry->d_type == DT_DIR) {
isDirectory = true;
} else if (entry->d_type == DT_LNK || entry->d_type == DT_UNKNOWN) {
isDirectory = File::isDirectory(File::relativeTo(dirName, entryString));
}
fileList.append({entryString, isDirectory});
}
}
::closedir(directory);
return fileList;
}
String File::baseName(const String& fileName) {
String ret;
std::string file = fileName.utf8();
char* fn = new char[file.size() + 1];
std::copy(file.begin(), file.end(), fn);
fn[file.size()] = 0;
ret = String(::basename(fn));
delete[] fn;
return ret;
}
String File::dirName(const String& fileName) {
String ret;
std::string file = fileName.utf8();
char* fn = new char[file.size() + 1];
std::copy(file.begin(), file.end(), fn);
fn[file.size()] = 0;
ret = String(::dirname(fn));
delete[] fn;
return ret;
}
String File::relativeTo(String const& relativeTo, String const& path) {
if (path.beginsWith("/"))
return path;
return relativeTo.trimEnd("/") + '/' + path;
}
String File::fullPath(const String& fileName) {
char buffer[PATH_MAX];
if (::realpath(fileName.utf8Ptr(), buffer) == NULL)
throw IOException::format("realpath failed on file: '{}' problem path was: '{}'", fileName, buffer);
return String(buffer);
}
String File::temporaryFileName() {
return relativeTo(P_tmpdir, strf("starbound.tmpfile.{}", hexEncode(Random::randBytes(16))));
}
FilePtr File::temporaryFile() {
return open(temporaryFileName(), IOMode::ReadWrite);
}
FilePtr File::ephemeralFile() {
auto file = make_shared<File>();
ByteArray path = ByteArray::fromCStringWithNull(relativeTo(P_tmpdir, "starbound.tmpfile.XXXXXXXX").utf8Ptr());
auto res = mkstemp(path.ptr());
if (res < 0)
throw IOException::format("tmpfile error: {}", strerror(errno));
if (::unlink(path.ptr()) < 0)
throw IOException::format("Could not remove mkstemp file when creating ephemeralFile: {}", strerror(errno));
file->m_file = handleFromFd(res);
file->setMode(IOMode::ReadWrite);
return file;
}
String File::temporaryDirectory() {
String dirname = relativeTo(P_tmpdir, strf("starbound.tmpdir.{}", hexEncode(Random::randBytes(16))));
makeDirectory(dirname);
return dirname;
}
bool File::exists(String const& path) {
struct stat st_buf;
int status = stat(path.utf8Ptr(), &st_buf);
return status == 0;
}
bool File::isFile(String const& path) {
struct stat st_buf;
int status = stat(path.utf8Ptr(), &st_buf);
if (status != 0)
return false;
return S_ISREG(st_buf.st_mode);
}
bool File::isDirectory(String const& path) {
struct stat st_buf;
int status = stat(path.utf8Ptr(), &st_buf);
if (status != 0)
return false;
return S_ISDIR(st_buf.st_mode);
}
void File::remove(String const& filename) {
if (::remove(filename.utf8Ptr()) < 0)
throw IOException::format("remove error: {}", strerror(errno));
}
void File::rename(String const& source, String const& target) {
if (::rename(source.utf8Ptr(), target.utf8Ptr()) < 0)
throw IOException::format("rename error: {}", strerror(errno));
}
void File::overwriteFileWithRename(char const* data, size_t len, String const& filename, String const& newSuffix) {
String newFile = filename + newSuffix;
writeFile(data, len, newFile);
File::rename(newFile, filename);
}
void* File::fopen(char const* filename, IOMode mode) {
int oflag = 0;
if (mode & IOMode::Read && mode & IOMode::Write)
oflag |= O_RDWR | O_CREAT;
else if (mode & IOMode::Read)
oflag |= O_RDONLY;
else if (mode & IOMode::Write)
oflag |= O_WRONLY | O_CREAT;
if (mode & IOMode::Truncate)
oflag |= O_TRUNC;
int fd = ::open(filename, oflag, 0666);
if (fd < 0)
throw IOException::format("Error opening file '{}', error: {}", filename, strerror(errno));
if (mode & IOMode::Append) {
if (lseek(fd, 0, SEEK_END) < 0)
throw IOException::format("Error opening file '{}', cannot seek: {}", filename, strerror(errno));
}
return handleFromFd(fd);
}
void File::fseek(void* f, StreamOffset offset, IOSeek seekMode) {
auto fd = fdFromHandle(f);
int retCode;
if (seekMode == IOSeek::Relative)
retCode = lseek(fd, offset, SEEK_CUR);
else if (seekMode == IOSeek::Absolute)
retCode = lseek(fd, offset, SEEK_SET);
else
retCode = lseek(fd, offset, SEEK_END);
if (retCode < 0)
throw IOException::format("Seek error: {}", strerror(errno));
}
StreamOffset File::ftell(void* f) {
return lseek(fdFromHandle(f), 0, SEEK_CUR);
}
size_t File::fread(void* file, char* data, size_t len) {
if (len == 0)
return 0;
auto fd = fdFromHandle(file);
auto ret = ::read(fd, data, len);
if (ret < 0) {
if (errno == EAGAIN || errno == EINTR)
return 0;
throw IOException::format("Read error: {}", strerror(errno));
} else {
return ret;
}
}
size_t File::fwrite(void* file, char const* data, size_t len) {
if (len == 0)
return 0;
auto fd = fdFromHandle(file);
auto ret = ::write(fd, data, len);
if (ret < 0) {
if (errno == EAGAIN || errno == EINTR)
return 0;
throw IOException::format("Write error: {}", strerror(errno));
} else {
return ret;
}
}
void File::fsync(void* file) {
auto fd = fdFromHandle(file);
#ifdef STAR_SYSTEM_LINUX
::fdatasync(fd);
#else
::fsync(fd);
#endif
}
void File::fclose(void* file) {
if (::close(fdFromHandle(file)) < 0)
throw IOException::format("Close error: {}", strerror(errno));
}
StreamOffset File::fsize(void* file) {
StreamOffset pos = ftell(file);
StreamOffset size = lseek(fdFromHandle(file), 0, SEEK_END);
lseek(fdFromHandle(file), pos, SEEK_SET);
return size;
}
size_t File::pread(void* file, char* data, size_t len, StreamOffset position) {
return ::pread(fdFromHandle(file), data, len, position);
}
size_t File::pwrite(void* file, char const* data, size_t len, StreamOffset position) {
return ::pwrite(fdFromHandle(file), data, len, position);
}
void File::resize(void* f, StreamOffset size) {
if (::ftruncate(fdFromHandle(f), size) < 0)
throw IOException::format("resize error: {}", strerror(errno));
}
}
|