Repository navigation
Expand file tree
/
Copy pathFile.cpp
More file actions
137 lines (116 loc) · 3.76 KB
/
Copy pathFile.cpp
File metadata and controls
137 lines (116 loc) · 3.76 KB
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
/*
* File.cpp
*
* Created on: Jun 24, 2015
* Author: gatanasov
*/
#include "File.h"
#include "NativeScriptAssert.h"
#include <cerrno>
#include <climits>
#include <cstdlib>
#include <sstream>
#include <fstream>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
using namespace std;
namespace tns {
string File::ReadText(const string& filePath) {
bool ok;
return ReadText(filePath, ok);
}
string File::ReadText(const string& filePath, bool& ok) {
ok = false;
FILE* file = fopen(filePath.c_str(), READ_BINARY);
if (file == nullptr) {
// A path that never existed, or one deleted between a caller's stat and
// this open. Callers surface their own error; reading on regardless
// aborts the process on a null FILE*.
DEBUG_WRITE_FORCE("File::ReadText: cannot open %s", filePath.c_str());
return string();
}
fseek(file, 0, SEEK_END);
auto size = ftell(file);
if (size < 0) {
DEBUG_WRITE_FORCE("File::ReadText: cannot size %s", filePath.c_str());
fclose(file);
return string();
}
rewind(file);
// Read straight into the string that is returned: main and worker runtimes
// read modules concurrently from their own threads, so there is no scratch
// buffer to share, and the caller needs a string either way.
string content(static_cast<size_t>(size), '\0');
auto bytesRead = fread(content.data(), 1, static_cast<size_t>(size), file);
fclose(file);
// A short read is a truncated file, not a failure to open; `size` is only
// what the directory entry claimed.
content.resize(bytesRead);
ok = true;
return content;
}
void* File::ReadBinary(const string& filePath, int& length) {
length = 0;
auto file = fopen(filePath.c_str(), READ_BINARY);
if (!file) {
return nullptr;
}
fseek(file, 0, SEEK_END);
auto size = ftell(file);
if (size < 0 || size > INT_MAX) {
fclose(file);
return nullptr;
}
rewind(file);
// A file truncated after the size was taken reads short. Callers hand the
// buffer to consumers that trust its length (V8 does not checksum code
// caches in release builds), so it is all of the file or nothing.
uint8_t* data = new uint8_t[size];
auto bytesRead = fread(data, sizeof(uint8_t), size, file);
fclose(file);
if (bytesRead != static_cast<size_t>(size)) {
delete[] data;
return nullptr;
}
length = static_cast<int>(size);
return data;
}
bool File::WriteBinary(const string& filePath, const void* data, int length, time_t modificationTime) {
// Exclusive creation in the target's directory, so concurrent writers of
// the same path never share a temporary and rename stays on one file system.
string tempPath(filePath);
tempPath.append(".ns-XXXXXX");
int fd = mkstemp(tempPath.data());
if (fd < 0) {
return false;
}
bool ok = length >= 0;
auto cursor = static_cast<const uint8_t*>(data);
size_t remaining = ok ? static_cast<size_t>(length) : 0;
while (ok && remaining > 0) {
ssize_t written = write(fd, cursor, remaining);
if (written < 0) {
ok = errno == EINTR;
continue;
}
cursor += written;
remaining -= static_cast<size_t>(written);
}
if (ok && modificationTime >= 0) {
struct timespec times[2];
times[0].tv_sec = 0;
times[0].tv_nsec = UTIME_NOW;
times[1].tv_sec = modificationTime;
times[1].tv_nsec = 0;
ok = futimens(fd, times) == 0;
}
ok = (close(fd) == 0) && ok;
ok = ok && rename(tempPath.c_str(), filePath.c_str()) == 0;
if (!ok) {
unlink(tempPath.c_str());
}
return ok;
}
const char* File::READ_BINARY = "rb";
}