-
Notifications
You must be signed in to change notification settings - Fork 4
/
file-posix.cpp
288 lines (245 loc) · 6.38 KB
/
file-posix.cpp
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
#include "file.h"
#include "os.h"
#define MMAP_THRESHOLD 128*1024
#ifdef __unix__
#include <unistd.h>
//#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/mman.h>
const char * window_get_proc_path()
{
//we could lstat it, but apparently that just returns zero on /proc on Linux.
ssize_t bufsize=64;
static char * linkname=NULL;
if (linkname) return linkname;
while (true)
{
linkname=malloc(bufsize);
ssize_t r=readlink("/proc/self/exe", linkname, bufsize);
if (r<0 || r>=bufsize)
{
free(linkname);
if (r<0) return NULL;
bufsize*=2;
continue;
}
linkname[r]='\0';
char * end=strrchr(linkname, '/');
if (end) *end='\0';
return linkname;
}
}
static void window_cwd_enter(const char * dir);
static void window_cwd_leave();
char * _window_native_get_absolute_path(const char * basepath, const char * path, bool allow_up)
{
if (!basepath || !path) return NULL;
const char * filepart=strrchr(basepath, '/');
if (!filepart) return NULL;
char * basedir=strndup(basepath, filepart+1-basepath);
window_cwd_enter(basedir);
char * ret=realpath(path, NULL);
window_cwd_leave();
if (!allow_up && ret && strncasecmp(basedir, ret, filepart+1-basepath)!=0)
{
free(ret);
ret=NULL;
}
free(basedir);
return ret;
}
static const char * cwd_init;
static const char * cwd_bogus;
static mutex cwd_mutex;
static void window_cwd_enter(const char * dir)
{
cwd_mutex.lock();
char * cwd_bogus_check=getcwd(NULL, 0);
if (strcmp(cwd_bogus, cwd_bogus_check)!=0) abort();//if this fires, someone changed the directory without us knowing - not allowed. cwd belongs to the frontend.
free(cwd_bogus_check);
ignore(chdir(dir));
}
static void window_cwd_leave()
{
ignore(chdir(cwd_bogus));
cwd_mutex.unlock();
}
const char * window_get_cwd()
{
return cwd_init;
}
void _window_init_file()
{
char * cwd_init_tmp=getcwd(NULL, 0);
char * cwdend=strrchr(cwd_init_tmp, '/');
if (!cwdend) cwd_init="/";
else if (cwdend[1]=='/') cwd_init=cwd_init_tmp;
else
{
size_t cwdlen=strlen(cwd_init_tmp);
char * cwd_init_fixed=malloc(cwdlen+1+1);
memcpy(cwd_init_fixed, cwd_init_tmp, cwdlen);
cwd_init_fixed[cwdlen+0]='/';
cwd_init_fixed[cwdlen+1]='\0';
cwd_init=cwd_init_fixed;
free(cwd_init_tmp);
}
//try a couple of useless directories and hope one of them works
//this seems to be the best one:
//- even root can't create files here
//- it contains no files with a plausible name on a standard Ubuntu box (I have an ath9k-phy0, nothing will ever want that filename)
//- a wild write will not do anything dangerous except turn on some lamps
!chdir("/sys/class/leds/") ||
//the rest are in case it's not accessible (weird chroot? not linux?), so try some random things
!chdir("/sys/") ||
!chdir("/dev/") ||
!chdir("/home/") ||
!chdir("/tmp/") ||
!chdir("/");
cwd_bogus=getcwd(NULL, 0);//POSIX does not specify getcwd(NULL), it's Linux-specific
}
/*
static void* file_alloc(int fd, size_t len, bool writable)
{
if (len <= MMAP_THRESHOLD)
{
uint8_t* data=malloc(len+1);
pread(fd, data, len, 0);
data[len]='\0';
return data;
}
else
{
void* data=mmap(NULL, len+1, writable ? (PROT_READ|PROT_WRITE) : PROT_READ, MAP_SHARED, fd, 0);
if (data==MAP_FAILED) return NULL;
if (len % sysconf(_SC_PAGESIZE) == 0)
{
mmap((char*)data + len, 1, PROT_READ, MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED, -1, 0);
}
return data;
}
}
*/
static long pagesize() { return sysconf(_SC_PAGESIZE); }
namespace {
class file_fs : public file {
int fd;
public:
//do not use the file pointer, dup() doesn't clone that one
file_fs(const char * filename, int fd, size_t len) : file(filename) { this->fd=fd; this->len=len; }
file* clone() { return new file_fs(this->filename, dup(this->fd), this->len); }
void read(size_t start, void* target, size_t len) { ignore(pread(fd, target, len, start)); }
void* mmap(size_t start, size_t len)
{
size_t offset = start % pagesize();
void* data=::mmap(NULL, len+offset, PROT_READ, MAP_SHARED, this->fd, start-offset);
if (data==MAP_FAILED) return NULL;
return (char*)data+offset;
}
void unmap(const void* data, size_t len)
{
size_t offset = (uintptr_t)data % pagesize();
munmap((char*)data-offset, len+offset);
}
~file_fs() { close(fd); }
};
}
file* file::create_fs(const char * filename)
{
int fd=open(filename, O_RDONLY);
if (fd<0) return NULL;
struct stat st;
if (fstat(fd, &st)<0) goto fail;
return new file_fs(filename, fd, st.st_size);
fail:
close(fd);
return NULL;
}
#if 0
namespace {
class file_fs_wr : public filewrite {
public:
int fd;
bool truncate;
file_fs_wr(int fd) : fd(fd) {}
/*private*/ void alloc(size_t size)
{
this->data=file_alloc(this->fd, size, true);
this->len=size;
if (this->data==NULL) abort();
}
/*private*/ void dealloc()
{
//no msync - munmap is guaranteed to do that already (and linux tracks dirty pages anyways)
if (this->len <= MMAP_THRESHOLD)
{
pwrite(this->fd, this->data, this->len, 0);
free(this->data);
}
else
{
munmap(this->data, this->len+1);
}
}
bool resize(size_t newsize)
{
if (ftruncate(this->fd, newsize) < 0) return false;
if (this->len < MMAP_THRESHOLD && newsize < MMAP_THRESHOLD)
{
this->len=newsize;
uint8_t* data=realloc(this->data, newsize+1);
data[newsize]='\0';
this->data=data;
return true;
}
dealloc();
alloc(newsize);
return true;
}
void sync()
{
if (this->truncate)
{
ftruncate(this->fd, this->len);
this->truncate=false;
}
msync(this->data, this->len, MS_SYNC);//no MS_INVALIDATE because I can't figure out what it's supposed to do
//on linux, it does nothing whatsoever, except in some EINVAL handlers
}
~file_fs_wr()
{
sync();
dealloc();
close(this->fd);
}
};
};
filewrite* filewrite::create_fs(const char * filename, bool truncate)
{
static const int oflags[]={ O_RDWR|O_CREAT, O_WRONLY|O_CREAT };
int fd=open(filename, oflags[truncate], 0666);//umask defaults to turning this to 644
if (fd<0) return NULL;
if (truncate)
{
file_fs_wr* f=new file_fs_wr(fd);
f->truncate=true;
return f;
}
else
{
struct stat st;
if (fstat(fd, &st)<0) goto fail;
file_fs_wr* f; f=new file_fs_wr(fd);
f->alloc(st.st_size);
return f;
}
fail:
close(fd);
return NULL;
}
#endif
#endif