-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathgfile.cc
435 lines (371 loc) · 11.1 KB
/
gfile.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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
//========================================================================
//
// gfile.cc
//
// Miscellaneous file and directory name manipulation.
//
// Copyright 1996-2003 Glyph & Cog, LLC
//
//========================================================================
//========================================================================
//
// Modified under the Poppler project - http://poppler.freedesktop.org
//
// All changes made under the Poppler project to this file are licensed
// under GPL version 2 or later
//
// Copyright (C) 2006 Takashi Iwai <[email protected]>
// Copyright (C) 2006 Kristian Høgsberg <[email protected]>
// Copyright (C) 2008 Adam Batkin <[email protected]>
// Copyright (C) 2008, 2010, 2012, 2013 Hib Eris <[email protected]>
// Copyright (C) 2009, 2012, 2014, 2017, 2018, 2021, 2022, 2024 Albert Astals Cid <[email protected]>
// Copyright (C) 2009 Kovid Goyal <[email protected]>
// Copyright (C) 2013, 2018 Adam Reichold <[email protected]>
// Copyright (C) 2013, 2017 Adrian Johnson <[email protected]>
// Copyright (C) 2013 Peter Breitenlohner <[email protected]>
// Copyright (C) 2013, 2017 Thomas Freitag <[email protected]>
// Copyright (C) 2017 Christoph Cullmann <[email protected]>
// Copyright (C) 2018 Mojca Miklavec <[email protected]>
// Copyright (C) 2019, 2021 Christian Persch <[email protected]>
//
// To see a description of the changes please see the Changelog file that
// came with your tarball or type make ChangeLog if you are building from git
//
//========================================================================
#include <config.h>
#ifndef _WIN32
# include <sys/types.h>
# include <sys/stat.h>
# include <fcntl.h>
# include <climits>
# include <cstring>
# include <pwd.h>
#endif // _WIN32
#include <cstdio>
#include <limits>
#include "GooString.h"
#include "gfile.h"
// Some systems don't define this, so just make it something reasonably
// large.
#ifndef PATH_MAX
# define PATH_MAX 1024
#endif
#ifndef _WIN32
using namespace std::string_literals;
namespace {
template<typename...>
struct void_type
{
using type = void;
};
template<typename... Args>
using void_t = typename void_type<Args...>::type;
template<typename Stat, typename = void_t<>>
struct StatMtim
{
static const struct timespec &value(const Stat &stbuf) { return stbuf.st_mtim; }
};
// Mac OS X uses a different field name than POSIX and this detects it.
template<typename Stat>
struct StatMtim<Stat, void_t<decltype(Stat::st_mtimespec)>>
{
static const struct timespec &value(const Stat &stbuf) { return stbuf.st_mtimespec; }
};
inline const struct timespec &mtim(const struct stat &stbuf)
{
return StatMtim<struct stat>::value(stbuf);
}
}
#endif
//------------------------------------------------------------------------
GooString *appendToPath(GooString *path, const char *fileName)
{
#ifdef _WIN32
//---------- Win32 ----------
GooString *tmp;
char buf[256];
char *fp;
tmp = new GooString(path);
tmp->append('/');
tmp->append(fileName);
GetFullPathNameA(tmp->c_str(), sizeof(buf), buf, &fp);
delete tmp;
path->clear();
path->append(buf);
return path;
#else
//---------- Unix ----------
int i;
// appending "." does nothing
if (!strcmp(fileName, ".")) {
return path;
}
// appending ".." goes up one directory
if (!strcmp(fileName, "..")) {
for (i = path->getLength() - 2; i >= 0; --i) {
if (path->getChar(i) == '/') {
break;
}
}
if (i <= 0) {
if (path->getChar(0) == '/') {
path->del(1, path->getLength() - 1);
} else {
path->clear();
path->append("..");
}
} else {
path->del(i, path->getLength() - i);
}
return path;
}
// otherwise, append "/" and new path component
if (path->getLength() > 0 && path->getChar(path->getLength() - 1) != '/') {
path->append('/');
}
path->append(fileName);
return path;
#endif
}
#ifndef _WIN32
static bool makeFileDescriptorCloexec(int fd)
{
# ifdef FD_CLOEXEC
int flags = fcntl(fd, F_GETFD);
if (flags >= 0 && !(flags & FD_CLOEXEC)) {
flags = fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
}
return flags >= 0;
# else
return true;
# endif
}
int openFileDescriptor(const char *path, int flags)
{
# ifdef O_CLOEXEC
return open(path, flags | O_CLOEXEC);
# else
int fd = open(path, flags);
if (fd == -1)
return fd;
if (!makeFileDescriptorCloexec(fd)) {
close(fd);
return -1;
}
return fd;
# endif
}
#endif
FILE *openFile(const char *path, const char *mode)
{
#ifdef _WIN32
OSVERSIONINFO version;
wchar_t wPath[_MAX_PATH + 1];
char nPath[_MAX_PATH + 1];
wchar_t wMode[8];
const char *p;
size_t i;
// NB: _wfopen is only available in NT
version.dwOSVersionInfoSize = sizeof(version);
GetVersionEx(&version);
if (version.dwPlatformId == VER_PLATFORM_WIN32_NT) {
for (p = path, i = 0; *p && i < _MAX_PATH; ++i) {
if ((p[0] & 0xe0) == 0xc0 && p[1] && (p[1] & 0xc0) == 0x80) {
wPath[i] = (wchar_t)(((p[0] & 0x1f) << 6) | (p[1] & 0x3f));
p += 2;
} else if ((p[0] & 0xf0) == 0xe0 && p[1] && (p[1] & 0xc0) == 0x80 && p[2] && (p[2] & 0xc0) == 0x80) {
wPath[i] = (wchar_t)(((p[0] & 0x0f) << 12) | ((p[1] & 0x3f) << 6) | (p[2] & 0x3f));
p += 3;
} else {
wPath[i] = (wchar_t)(p[0] & 0xff);
p += 1;
}
}
wPath[i] = (wchar_t)0;
for (i = 0; (i < sizeof(mode) - 1) && mode[i]; ++i) {
wMode[i] = (wchar_t)(mode[i] & 0xff);
}
wMode[i] = (wchar_t)0;
return _wfopen(wPath, wMode);
} else {
for (p = path, i = 0; *p && i < _MAX_PATH; ++i) {
if ((p[0] & 0xe0) == 0xc0 && p[1] && (p[1] & 0xc0) == 0x80) {
nPath[i] = (char)(((p[0] & 0x1f) << 6) | (p[1] & 0x3f));
p += 2;
} else if ((p[0] & 0xf0) == 0xe0 && p[1] && (p[1] & 0xc0) == 0x80 && p[2] && (p[2] & 0xc0) == 0x80) {
nPath[i] = (char)(((p[1] & 0x3f) << 6) | (p[2] & 0x3f));
p += 3;
} else {
nPath[i] = p[0];
p += 1;
}
}
nPath[i] = '\0';
return fopen(nPath, mode);
}
#else
// First try to atomically open the file with CLOEXEC
const std::string modeStr = mode + "e"s;
FILE *file = fopen(path, modeStr.c_str());
if (file != nullptr) {
return file;
}
// Fall back to the provided mode and apply CLOEXEC afterwards
file = fopen(path, mode);
if (file == nullptr) {
return nullptr;
}
if (!makeFileDescriptorCloexec(fileno(file))) {
fclose(file);
return nullptr;
}
return file;
#endif
}
char *getLine(char *buf, int size, FILE *f)
{
int c, i;
i = 0;
while (i < size - 1) {
if ((c = fgetc(f)) == EOF) {
break;
}
buf[i++] = (char)c;
if (c == '\x0a') {
break;
}
if (c == '\x0d') {
c = fgetc(f);
if (c == '\x0a' && i < size - 1) {
buf[i++] = (char)c;
} else if (c != EOF) {
ungetc(c, f);
}
break;
}
}
buf[i] = '\0';
if (i == 0) {
return nullptr;
}
return buf;
}
int Gfseek(FILE *f, Goffset offset, int whence)
{
#if defined(HAVE_FSEEKO)
return fseeko(f, offset, whence);
#elif defined(HAVE_FSEEK64)
return fseek64(f, offset, whence);
#elif defined(__MINGW32__)
return fseeko64(f, offset, whence);
#elif defined(_WIN32)
return _fseeki64(f, offset, whence);
#else
return fseek(f, offset, whence);
#endif
}
Goffset Gftell(FILE *f)
{
#if defined(HAVE_FSEEKO)
return ftello(f);
#elif defined(HAVE_FSEEK64)
return ftell64(f);
#elif defined(__MINGW32__)
return ftello64(f);
#elif defined(_WIN32)
return _ftelli64(f);
#else
return ftell(f);
#endif
}
Goffset GoffsetMax()
{
#if defined(HAVE_FSEEKO)
return (std::numeric_limits<off_t>::max)();
#elif defined(HAVE_FSEEK64) || defined(__MINGW32__)
return (std::numeric_limits<off64_t>::max)();
#elif defined(_WIN32)
return (std::numeric_limits<__int64>::max)();
#else
return (std::numeric_limits<long>::max)();
#endif
}
//------------------------------------------------------------------------
// GooFile
//------------------------------------------------------------------------
#ifdef _WIN32
GooFile::GooFile(HANDLE handleA) : handle(handleA)
{
GetFileTime(handleA, nullptr, nullptr, &modifiedTimeOnOpen);
}
int GooFile::read(char *buf, int n, Goffset offset) const
{
DWORD m;
LARGE_INTEGER largeInteger = {};
largeInteger.QuadPart = offset;
OVERLAPPED overlapped = {};
overlapped.Offset = largeInteger.LowPart;
overlapped.OffsetHigh = largeInteger.HighPart;
return FALSE == ReadFile(handle, buf, n, &m, &overlapped) ? -1 : m;
}
Goffset GooFile::size() const
{
LARGE_INTEGER size = { (DWORD)-1, -1 };
GetFileSizeEx(handle, &size);
return size.QuadPart;
}
std::unique_ptr<GooFile> GooFile::open(const std::string &fileName)
{
HANDLE handle = CreateFileA(fileName.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
return handle == INVALID_HANDLE_VALUE ? std::unique_ptr<GooFile>() : std::unique_ptr<GooFile>(new GooFile(handle));
}
std::unique_ptr<GooFile> GooFile::open(const wchar_t *fileName)
{
HANDLE handle = CreateFileW(fileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
return handle == INVALID_HANDLE_VALUE ? std::unique_ptr<GooFile>() : std::unique_ptr<GooFile>(new GooFile(handle));
}
bool GooFile::modificationTimeChangedSinceOpen() const
{
struct _FILETIME lastModified;
GetFileTime(handle, nullptr, nullptr, &lastModified);
return modifiedTimeOnOpen.dwHighDateTime != lastModified.dwHighDateTime || modifiedTimeOnOpen.dwLowDateTime != lastModified.dwLowDateTime;
}
#else
int GooFile::read(char *buf, int n, Goffset offset) const
{
# ifdef HAVE_PREAD64
return pread64(fd, buf, n, offset);
# else
return pread(fd, buf, n, offset);
# endif
}
Goffset GooFile::size() const
{
# ifdef HAVE_LSEEK64
return lseek64(fd, 0, SEEK_END);
# else
return lseek(fd, 0, SEEK_END);
# endif
}
std::unique_ptr<GooFile> GooFile::open(const std::string &fileName)
{
int fd = openFileDescriptor(fileName.c_str(), O_RDONLY);
return GooFile::open(fd);
}
std::unique_ptr<GooFile> GooFile::open(int fdA)
{
return fdA < 0 ? std::unique_ptr<GooFile>() : std::unique_ptr<GooFile>(new GooFile(fdA));
}
GooFile::GooFile(int fdA) : fd(fdA)
{
struct stat statbuf;
fstat(fd, &statbuf);
modifiedTimeOnOpen = mtim(statbuf);
}
bool GooFile::modificationTimeChangedSinceOpen() const
{
struct stat statbuf;
fstat(fd, &statbuf);
return modifiedTimeOnOpen.tv_sec != mtim(statbuf).tv_sec || modifiedTimeOnOpen.tv_nsec != mtim(statbuf).tv_nsec;
}
#endif // _WIN32