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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
|
/*
* The ManaPlus Client
* Copyright (C) 2013-2017 The ManaPlus Developers
*
* This file is part of The ManaPlus Client.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef USE_PHYSFS
#include "fs/virtfs/virtfsdir.h"
#include "fs/files.h"
#include "fs/mkdir.h"
#include "fs/paths.h"
#include "fs/virtfs.h"
#include "fs/virtfile.h"
#include "fs/virtfsfuncs.h"
#include "fs/virtlist.h"
#include "fs/virtfs/virtdirentry.h"
#include "fs/virtfs/virtfileprivate.h"
#include "utils/checkutils.h"
#include "utils/dtor.h"
#include "utils/stringutils.h"
#include <dirent.h>
#include <fcntl.h>
#include <iostream>
#include <unistd.h>
#ifdef USE_FILE_FOPEN
#include <stdio.h>
#endif // USE_FILE_FOPEN
#include <sys/types.h>
#include <sys/stat.h>
#include "debug.h"
extern const char *dirSeparator;
namespace
{
std::string mWriteDir;
std::string mBaseDir;
std::string mUserDir;
bool mPermitLinks = false;
VirtFsFuncs funcs;
} // namespace
namespace VirtFsDir
{
VirtFile *openInternal(VirtFsEntry *restrict const entry,
const std::string &filename,
const FILEMTYPE mode)
{
const std::string path = entry->root + filename;
if (Files::existsLocal(path) == false)
return nullptr;
FILEHTYPE fd = FILEOPEN(path.c_str(),
mode);
if (fd == FILEHDEFAULT)
{
reportAlways("VirtFs::open file open error: %s",
filename.c_str());
return nullptr;
}
VirtFile *restrict const file = new VirtFile(&funcs);
file->mPrivate = new VirtFilePrivate(fd);
return file;
}
VirtFile *openRead(VirtFsEntry *restrict const entry,
const std::string &filename)
{
return openInternal(entry, filename, FILEOPEN_FLAG_READ);
}
VirtFile *openWrite(VirtFsEntry *restrict const entry,
const std::string &filename)
{
return openInternal(entry, filename, FILEOPEN_FLAG_WRITE);
}
VirtFile *openAppend(VirtFsEntry *restrict const entry,
const std::string &filename)
{
return openInternal(entry, filename, FILEOPEN_FLAG_APPEND);
}
void deinit()
{
}
#if defined(__native_client__)
void init(const std::string &restrict name A_UNUSED)
{
mBaseDir = "/";
#elif defined(ANDROID)
void init(const std::string &restrict name A_UNUSED)
{
mBaseDir = getRealPath(".");
#else // defined(__native_client__)
void init(const std::string &restrict name)
{
mBaseDir = getRealPath(getFileDir(name));
#endif // defined(__native_client__)
prepareFsPath(mBaseDir);
mUserDir = getHomePath();
prepareFsPath(mUserDir);
initFuncs(&funcs);
}
void initFuncs(VirtFsFuncs *restrict const ptr)
{
ptr->close = &VirtFsDir::close;
ptr->read = &VirtFsDir::read;
ptr->write = &VirtFsDir::write;
ptr->fileLength = &VirtFsDir::fileLength;
ptr->tell = &VirtFsDir::tell;
ptr->seek = &VirtFsDir::seek;
ptr->eof = &VirtFsDir::eof;
ptr->exists = &VirtFsDir::exists;
ptr->getRealDir = &VirtFsDir::getRealDir;
ptr->enumerate = &VirtFsDir::enumerate;
ptr->isDirectory = &VirtFsDir::isDirectory;
ptr->openRead = &VirtFsDir::openRead;
ptr->openWrite = &VirtFsDir::openWrite;
ptr->openAppend = &VirtFsDir::openAppend;
}
VirtFsFuncs *getFuncs()
{
return &funcs;
}
const char *getBaseDir()
{
return mBaseDir.c_str();
}
const char *getUserDir()
{
return mUserDir.c_str();
}
bool getRealDir(VirtFsEntry *restrict const entry,
const std::string &filename,
const std::string &dirName A_UNUSED,
std::string &realDir)
{
VirtDirEntry *const dirEntry = static_cast<VirtDirEntry*>(entry);
const std::string path = dirEntry->root + filename;
if (Files::existsLocal(dirEntry->root + filename))
{
realDir = dirEntry->userDir;
return true;
}
return false;
}
bool exists(VirtFsEntry *restrict const entry,
const std::string &fileName,
const std::string &dirName A_UNUSED)
{
return Files::existsLocal(entry->root + fileName);
}
void enumerate(VirtFsEntry *restrict const entry,
const std::string &dirName,
StringVect &names)
{
const std::string path = entry->root + dirName;
const struct dirent *next_file = nullptr;
DIR *const dir = opendir(path.c_str());
if (dir)
{
while ((next_file = readdir(dir)))
{
const std::string file = next_file->d_name;
if (file == "." || file == "..")
continue;
#ifndef WIN32
if (mPermitLinks == false)
{
struct stat statbuf;
if (lstat(path.c_str(), &statbuf) == 0 &&
S_ISLNK(statbuf.st_mode) != 0)
{
continue;
}
}
#endif // WIN32
bool found(false);
FOR_EACH (StringVectCIter, itn, names)
{
if (*itn == file)
{
found = true;
break;
}
}
if (found == false)
names.push_back(file);
}
closedir(dir);
}
}
bool isDirectory(VirtFsEntry *restrict const entry,
const std::string &dirName,
bool &isDirFlag)
{
std::string path = entry->root + dirName;
struct stat statbuf;
if (stat(path.c_str(), &statbuf) == 0)
{
isDirFlag = (S_ISDIR(statbuf.st_mode) != 0);
return true;
}
return false;
}
bool isSymbolicLink(std::string name)
{
prepareFsPath(name);
if (checkPath(name) == false)
{
reportAlways("VirtFsDir::isSymbolicLink invalid path: %s",
name.c_str());
return false;
}
#ifndef WIN32
if (mPermitLinks == false)
return false;
struct stat statbuf;
return lstat(name.c_str(), &statbuf) == 0 &&
S_ISLNK(statbuf.st_mode) != 0;
#else
return false;
#endif // WIN32
}
void freeList(VirtList *restrict const handle)
{
delete handle;
}
bool setWriteDir(std::string newDir)
{
prepareFsPath(newDir);
mWriteDir = newDir;
if (findLast(mWriteDir, std::string(dirSeparator)) == false)
mWriteDir += dirSeparator;
return true;
}
bool mkdir(std::string dirname)
{
prepareFsPath(dirname);
if (mWriteDir.empty())
{
reportAlways("VirtFsDir::mkdir write dir is empty");
return false;
}
return mkdir_r((mWriteDir + dirname).c_str()) != -1;
}
bool remove(std::string filename)
{
prepareFsPath(filename);
if (mWriteDir.empty())
{
reportAlways("VirtFsDir::remove write dir is empty");
return false;
}
return ::remove((mWriteDir + filename).c_str()) != 0;
}
void permitLinks(const bool val)
{
mPermitLinks = val;
}
int close(VirtFile *restrict const file)
{
if (file == nullptr)
return 0;
delete file;
return 1;
}
int64_t read(VirtFile *restrict const file,
void *restrict const buffer,
const uint32_t objSize,
const uint32_t objCount)
{
if (file == nullptr)
return 0;
FILEHTYPE fd = file->mPrivate->mFd;
if (fd == FILEHDEFAULT)
{
reportAlways("VirtFsDir::read file not opened.");
return 0;
}
#ifdef USE_FILE_FOPEN
return fread(buffer, objSize, objCount, fd);
#else // USE_FILE_FOPEN
int max = objSize * objCount;
int cnt = ::read(fd, buffer, max);
if (cnt <= 0)
return cnt;
return cnt / objSize;
#endif // USE_FILE_FOPEN
}
int64_t write(VirtFile *restrict const file,
const void *restrict const buffer,
const uint32_t objSize,
const uint32_t objCount)
{
if (file == nullptr)
return 0;
FILEHTYPE fd = file->mPrivate->mFd;
if (fd == FILEHDEFAULT)
{
reportAlways("VirtFsDir::write file not opened.");
return 0;
}
#ifdef USE_FILE_FOPEN
return fwrite(buffer, objSize, objCount, fd);
#else // USE_FILE_FOPEN
int max = objSize * objCount;
int cnt = ::write(fd, buffer, max);
if (cnt <= 0)
return cnt;
return cnt / objSize;
#endif // USE_FILE_FOPEN
}
int64_t fileLength(VirtFile *restrict const file)
{
if (file == nullptr)
return -1;
FILEHTYPE fd = file->mPrivate->mFd;
if (fd == FILEHDEFAULT)
{
reportAlways("VirtFsDir::fileLength file not opened.");
return 0;
}
#ifdef USE_FILE_FOPEN
const long pos = ftell(fd);
fseek(fd, 0, SEEK_END);
const long sz = ftell(fd);
fseek(fd, pos, SEEK_SET);
return sz;
#else // USE_FILE_FOPEN
struct stat statbuf;
if (fstat(fd, &statbuf) == -1)
{
reportAlways("VirtFsDir::fileLength error.");
return -1;
}
return static_cast<int64_t>(statbuf.st_size);
#endif // USE_FILE_FOPEN
}
int64_t tell(VirtFile *restrict const file)
{
if (file == nullptr)
return -1;
FILEHTYPE fd = file->mPrivate->mFd;
if (fd == FILEHDEFAULT)
{
reportAlways("VirtFsDir::tell file not opened.");
return 0;
}
#ifdef USE_FILE_FOPEN
const int64_t pos = ftell(fd);
#else // USE_FILE_FOPEN
const int64_t pos = lseek(fd, 0, SEEK_CUR);
#endif // USE_FILE_FOPEN
return pos;
}
int seek(VirtFile *restrict const file,
const uint64_t pos)
{
if (file == nullptr)
return 0;
FILEHTYPE fd = file->mPrivate->mFd;
if (fd == FILEHDEFAULT)
{
reportAlways("VirtFsDir::seek file not opened.");
return 0;
}
const int64_t res = FILESEEK(fd, pos, SEEK_SET);
if (res == -1)
return 0;
return 1;
}
int eof(VirtFile *restrict const file)
{
if (file == nullptr)
return -1;
FILEHTYPE fd = file->mPrivate->mFd;
if (fd == FILEHDEFAULT)
{
reportAlways("VirtFsDir::eof file not opened.");
return 0;
}
#ifdef USE_FILE_FOPEN
const int flag = feof(fd);
if (flag != 0)
return 1;
const int64_t pos = ftell(fd);
const int64_t len = fileLength(file);
#else // USE_FILE_FOPEN
const int64_t pos = lseek(fd, 0, SEEK_CUR);
struct stat statbuf;
if (fstat(fd, &statbuf) == -1)
{
reportAlways("VirtFsDir::fileLength error.");
return -1;
}
const int64_t len = static_cast<int64_t>(statbuf.st_size);
#endif // USE_FILE_FOPEN
return pos < 0 || len < 0 || pos >= len;
}
} // namespace VirtFs
#endif // USE_PHYSFS
|