-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstorage.cpp
More file actions
640 lines (571 loc) · 16.5 KB
/
storage.cpp
File metadata and controls
640 lines (571 loc) · 16.5 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
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
/* If you are missing that file, acquire a complete release at teeworlds.com. */
#include <base/hash_ctxt.h>
#include <base/system.h>
#include <engine/storage.h>
#include "linereader.h"
#include <zlib.h>
// compiled-in data-dir path
#define DATA_DIR "data"
class CStorage : public IStorage
{
public:
enum
{
MAX_PATHS = 16
};
char m_aaStoragePaths[MAX_PATHS][IO_MAX_PATH_LENGTH];
int m_NumPaths;
char m_aDataDir[IO_MAX_PATH_LENGTH];
char m_aUserDir[IO_MAX_PATH_LENGTH];
char m_aCurrentDir[IO_MAX_PATH_LENGTH];
char m_aAppDir[IO_MAX_PATH_LENGTH];
CStorage()
{
mem_zero(m_aaStoragePaths, sizeof(m_aaStoragePaths));
m_NumPaths = 0;
m_aDataDir[0] = 0;
m_aUserDir[0] = 0;
m_aCurrentDir[0] = 0;
m_aAppDir[0] = 0;
}
int Init(const char *pApplicationName, int StorageType, int NumArgs, const char **ppArguments)
{
// get userdir
fs_storage_path(pApplicationName, m_aUserDir, sizeof(m_aUserDir));
// get appdir
FindAppDir(ppArguments[0]);
// get datadir
FindDataDir();
// get currentdir
fs_getcwd(m_aCurrentDir, sizeof(m_aCurrentDir));
// load paths from storage.cfg
LoadPaths();
if(!m_NumPaths)
{
dbg_msg("storage", "using standard paths");
AddDefaultPaths();
}
// add save directories
if(StorageType != STORAGETYPE_BASIC)
{
if(m_NumPaths && (!m_aaStoragePaths[TYPE_SAVE][0] || !fs_makedir_recursive(m_aaStoragePaths[TYPE_SAVE])))
{
char aPath[IO_MAX_PATH_LENGTH];
if(StorageType == STORAGETYPE_CLIENT)
{
fs_makedir(GetPath(TYPE_SAVE, "screenshots", aPath, sizeof(aPath)));
fs_makedir(GetPath(TYPE_SAVE, "screenshots/auto", aPath, sizeof(aPath)));
fs_makedir(GetPath(TYPE_SAVE, "maps", aPath, sizeof(aPath)));
fs_makedir(GetPath(TYPE_SAVE, "downloadedmaps", aPath, sizeof(aPath)));
fs_makedir(GetPath(TYPE_SAVE, "downloadedres", aPath, sizeof(aPath)));
fs_makedir(GetPath(TYPE_SAVE, "skins", aPath, sizeof(aPath)));
fs_makedir(GetPath(TYPE_SAVE, "editor", aPath, sizeof(aPath)));
}
fs_makedir(GetPath(TYPE_SAVE, "dumps", aPath, sizeof(aPath)));
fs_makedir(GetPath(TYPE_SAVE, "demos", aPath, sizeof(aPath)));
fs_makedir(GetPath(TYPE_SAVE, "demos/auto", aPath, sizeof(aPath)));
fs_makedir(GetPath(TYPE_SAVE, "configs", aPath, sizeof(aPath)));
}
else
{
dbg_msg("storage", "unable to create save directory");
return 1;
}
}
return m_NumPaths ? 0 : 1;
}
void LoadPaths()
{
// check current directory
IOHANDLE File = io_open("storage.cfg", IOFLAG_READ | IOFLAG_SKIP_BOM);
if(!File)
{
// check usable path in argv[0]
char aBuffer[IO_MAX_PATH_LENGTH];
str_copy(aBuffer, m_aAppDir, sizeof(aBuffer));
str_append(aBuffer, "/storage.cfg", sizeof(aBuffer));
File = io_open(aBuffer, IOFLAG_READ | IOFLAG_SKIP_BOM);
if(!File)
{
dbg_msg("storage", "couldn't open storage.cfg");
return;
}
}
CLineReader LineReader;
LineReader.Init(File);
const char *pLine;
while((pLine = LineReader.Get()))
{
const char *pLineWithoutPrefix = str_startswith(pLine, "add_path ");
if(pLineWithoutPrefix)
{
AddPath(pLineWithoutPrefix);
}
}
io_close(File);
if(!m_NumPaths)
dbg_msg("storage", "no paths found in storage.cfg");
}
void AddDefaultPaths()
{
AddPath("$USERDIR");
AddPath("$DATADIR");
AddPath("$CURRENTDIR");
AddPath("$APPDIR");
}
bool IsDuplicatePath(const char *pPath)
{
for(int i = 0; i < m_NumPaths; ++i)
{
if(!str_comp(m_aaStoragePaths[i], pPath))
return true;
}
return false;
}
void AddPath(const char *pPath)
{
if(m_NumPaths >= MAX_PATHS || !pPath[0])
return;
if(!str_comp(pPath, "$USERDIR"))
{
if(m_aUserDir[0])
{
if(!IsDuplicatePath(m_aUserDir))
{
str_copy(m_aaStoragePaths[m_NumPaths++], m_aUserDir, IO_MAX_PATH_LENGTH);
dbg_msg("storage", "added path '$USERDIR' ('%s')", m_aUserDir);
}
else
dbg_msg("storage", "skipping duplicate path '$USERDIR' ('%s')", m_aUserDir);
}
}
else if(!str_comp(pPath, "$DATADIR"))
{
if(m_aDataDir[0])
{
if(!IsDuplicatePath(m_aDataDir))
{
str_copy(m_aaStoragePaths[m_NumPaths++], m_aDataDir, IO_MAX_PATH_LENGTH);
dbg_msg("storage", "added path '$DATADIR' ('%s')", m_aDataDir);
}
else
dbg_msg("storage", "skipping duplicate path '$DATADIR' ('%s')", m_aDataDir);
}
}
else if(!str_comp(pPath, "$CURRENTDIR"))
{
if(m_aCurrentDir[0])
{
if(!IsDuplicatePath(m_aCurrentDir))
{
str_copy(m_aaStoragePaths[m_NumPaths++], m_aCurrentDir, IO_MAX_PATH_LENGTH);
dbg_msg("storage", "added path '$CURRENTDIR' ('%s')", m_aCurrentDir);
}
else
dbg_msg("storage", "skipping duplicate path '$CURRENTDIR' ('%s')", m_aCurrentDir);
}
}
else if(!str_comp(pPath, "$APPDIR"))
{
if(m_aAppDir[0])
{
if(!IsDuplicatePath(m_aAppDir))
{
str_copy(m_aaStoragePaths[m_NumPaths++], m_aAppDir, IO_MAX_PATH_LENGTH);
dbg_msg("storage", "added path '$APPDIR' ('%s')", m_aAppDir);
}
else
dbg_msg("storage", "skipping duplicate path '$APPDIR' ('%s')", m_aAppDir);
}
}
else
{
if(fs_is_dir(pPath))
{
if(!IsDuplicatePath(pPath))
{
str_copy(m_aaStoragePaths[m_NumPaths++], pPath, IO_MAX_PATH_LENGTH);
dbg_msg("storage", "added path '%s'", pPath);
}
else
dbg_msg("storage", "skipping duplicate path '%s'", pPath);
}
}
}
void FindAppDir(const char *pArgv0)
{
// check for usable path in argv[0]
unsigned int Pos = ~0U;
for(unsigned i = 0; pArgv0[i]; ++i)
if(pArgv0[i] == '/' || pArgv0[i] == '\\')
Pos = i;
if(Pos < IO_MAX_PATH_LENGTH)
{
str_copy(m_aAppDir, pArgv0, Pos + 1);
if(!fs_is_dir(m_aAppDir))
m_aAppDir[0] = 0;
}
}
void FindDataDir()
{
// 1) use data-dir in PWD if present
if(fs_is_dir("data/mapres"))
{
str_copy(m_aDataDir, "data", sizeof(m_aDataDir));
return;
}
// 2) use compiled-in data-dir if present
if(fs_is_dir(DATA_DIR "/mapres"))
{
str_copy(m_aDataDir, DATA_DIR, sizeof(m_aDataDir));
return;
}
// 3) check for usable path in argv[0]
{
char aBaseDir[IO_MAX_PATH_LENGTH];
str_copy(aBaseDir, m_aAppDir, sizeof(aBaseDir));
str_format(m_aDataDir, sizeof(m_aDataDir), "%s/data", aBaseDir);
str_append(aBaseDir, "/data/mapres", sizeof(aBaseDir));
if(fs_is_dir(aBaseDir))
return;
else
m_aDataDir[0] = 0;
}
#if defined(CONF_FAMILY_UNIX)
// 4) check for all default locations
{
const char *aDirs[] = {
"/usr/share/teeworlds/data",
"/usr/share/games/teeworlds/data",
"/usr/local/share/teeworlds/data",
"/usr/local/share/games/teeworlds/data",
"/usr/pkg/share/teeworlds/data",
"/usr/pkg/share/games/teeworlds/data",
"/opt/teeworlds/data"};
const int DirsCount = sizeof(aDirs) / sizeof(aDirs[0]);
int i;
for(i = 0; i < DirsCount; i++)
{
char aBuf[128];
str_format(aBuf, sizeof(aBuf), "%s/mapres", aDirs[i]);
if(fs_is_dir(aBuf))
{
str_copy(m_aDataDir, aDirs[i], sizeof(m_aDataDir));
return;
}
}
}
#endif
// no data-dir found
dbg_msg("storage", "warning no data directory found");
}
virtual void ListDirectory(int Type, const char *pPath, FS_LISTDIR_CALLBACK pfnCallback, void *pUser)
{
char aBuffer[IO_MAX_PATH_LENGTH];
if(Type == TYPE_ALL)
{
// list all available directories
for(int i = 0; i < m_NumPaths; ++i)
fs_listdir(GetPath(i, pPath, aBuffer, sizeof(aBuffer)), pfnCallback, i, pUser);
}
else if((Type >= 0 && Type < m_NumPaths) || Type == TYPE_APP)
{
// list wanted directory
fs_listdir(GetPath(Type, pPath, aBuffer, sizeof(aBuffer)), pfnCallback, Type, pUser);
}
}
virtual void ListDirectoryFileInfo(int Type, const char *pPath, FS_LISTDIR_CALLBACK_FILEINFO pfnCallback, void *pUser)
{
char aBuffer[IO_MAX_PATH_LENGTH];
if(Type == TYPE_ALL)
{
// list all available directories
for(int i = 0; i < m_NumPaths; ++i)
fs_listdir_fileinfo(GetPath(i, pPath, aBuffer, sizeof(aBuffer)), pfnCallback, i, pUser);
}
else if((Type >= 0 && Type < m_NumPaths) || Type == TYPE_APP)
{
// list wanted directory
fs_listdir_fileinfo(GetPath(Type, pPath, aBuffer, sizeof(aBuffer)), pfnCallback, Type, pUser);
}
}
const char *GetPath(int Type, const char *pDir, char *pBuffer, unsigned BufferSize)
{
if(Type == TYPE_APP)
{
str_format(pBuffer, BufferSize, "%s%s%s", m_aAppDir, !m_aAppDir[0] ? "" : "/", pDir);
return pBuffer;
}
str_format(pBuffer, BufferSize, "%s%s%s", m_aaStoragePaths[Type], !m_aaStoragePaths[Type][0] ? "" : "/", pDir);
return pBuffer;
}
// Open a file. This checks that the path appears to be a subdirectory
// of one of the storage paths.
virtual IOHANDLE OpenFile(const char *pFilename, int Flags, int Type, char *pBuffer = 0, int BufferSize = 0, FCheckCallback pfnCheckCB = 0, const void *pCheckCBData = 0)
{
char aBuffer[IO_MAX_PATH_LENGTH];
if(!pBuffer)
{
pBuffer = aBuffer;
BufferSize = sizeof(aBuffer);
}
// Check whether the path contains '..' (parent directory) paths. We'd
// normally still have to check whether it is an absolute path
// (starts with a path separator (or a drive name on windows)),
// but since we concatenate this path with another path later
// on, this can't become absolute.
//
// E. g. "/etc/passwd" => "/path/to/storage//etc/passwd", which
// is safe.
if(str_path_unsafe(pFilename) != 0)
{
dbg_msg("storage", "refusing to open path which looks like it could escape those specified in 'storage.cfg': %s", pFilename);
return 0;
}
// open file
if(Flags & IOFLAG_WRITE)
{
return io_open(GetPath(TYPE_SAVE, pFilename, pBuffer, BufferSize), Flags);
}
else
{
IOHANDLE Handle = 0;
int LB = 0, UB = m_NumPaths; // check all available directories
if((Type >= 0 && Type < m_NumPaths) || Type == TYPE_APP) // check wanted directory
{
LB = Type;
UB = Type + 1;
}
else
dbg_assert(Type == TYPE_ALL, "invalid storage type");
for(int i = LB; i < UB; ++i)
{
Handle = io_open(GetPath(i, pFilename, pBuffer, BufferSize), Flags);
if(Handle)
{
// do an additional check on the file
if(pfnCheckCB && !pfnCheckCB(Handle, pCheckCBData))
{
io_close(Handle);
Handle = 0;
}
else
return Handle;
}
}
}
pBuffer[0] = 0;
return 0;
}
bool ReadFile(const char *pFilename, int Type, void **ppResult, unsigned *pResultLen)
{
IOHANDLE File = OpenFile(pFilename, IOFLAG_READ, Type);
if(!File)
{
*ppResult = 0x0;
*pResultLen = 0;
return false;
}
io_read_all(File, ppResult, pResultLen);
io_close(File);
return true;
}
char *ReadFileStr(const char *pFilename, int Type)
{
IOHANDLE File = OpenFile(pFilename, IOFLAG_READ | IOFLAG_SKIP_BOM, Type);
if(!File)
{
return 0;
}
char *pResult = io_read_all_str(File);
io_close(File);
return pResult;
}
struct CFindCBData
{
CStorage *m_pStorage;
const char *m_pFilename;
const char *m_pPath;
char *m_pBuffer;
int m_BufferSize;
const SHA256_DIGEST *m_pWantedSha256;
unsigned m_WantedCrc;
unsigned m_WantedSize;
bool m_CheckHashAndSize;
};
static int FindFileCallback(const char *pName, int IsDir, int Type, void *pUser)
{
CFindCBData Data = *static_cast<CFindCBData *>(pUser);
if(IsDir)
{
if(pName[0] == '.')
return 0;
// search within the folder
char aBuf[IO_MAX_PATH_LENGTH];
char aPath[IO_MAX_PATH_LENGTH];
str_format(aPath, sizeof(aPath), "%s/%s", Data.m_pPath, pName);
Data.m_pPath = aPath;
fs_listdir(Data.m_pStorage->GetPath(Type, aPath, aBuf, sizeof(aBuf)), FindFileCallback, Type, &Data);
if(Data.m_pBuffer[0])
return 1;
}
else if(!str_comp(pName, Data.m_pFilename))
{
// found the file
str_format(Data.m_pBuffer, Data.m_BufferSize, "%s/%s", Data.m_pPath, Data.m_pFilename);
if(Data.m_CheckHashAndSize)
{
// check crc and size
SHA256_DIGEST Sha256;
unsigned Crc = 0;
unsigned Size = 0;
if(!Data.m_pStorage->GetHashAndSize(Data.m_pBuffer, Type, &Sha256, &Crc, &Size) || (Data.m_pWantedSha256 && Sha256 != *Data.m_pWantedSha256) || Crc != Data.m_WantedCrc || Size != Data.m_WantedSize)
{
Data.m_pBuffer[0] = 0;
return 0;
}
}
return 1;
}
return 0;
}
bool FindFileImpl(int Type, CFindCBData *pCBData)
{
if(pCBData->m_BufferSize < 1)
return false;
pCBData->m_pBuffer[0] = 0;
char aBuf[IO_MAX_PATH_LENGTH];
if(Type == TYPE_ALL)
{
// search within all available directories
for(int i = 0; i < m_NumPaths; ++i)
{
fs_listdir(GetPath(i, pCBData->m_pPath, aBuf, sizeof(aBuf)), FindFileCallback, i, pCBData);
if(pCBData->m_pBuffer[0])
return true;
}
}
else if((Type >= 0 && Type < m_NumPaths) || Type == TYPE_APP)
{
// search within wanted directory
fs_listdir(GetPath(Type, pCBData->m_pPath, aBuf, sizeof(aBuf)), FindFileCallback, Type, pCBData);
}
return pCBData->m_pBuffer[0] != 0;
}
virtual bool FindFile(const char *pFilename, const char *pPath, int Type, char *pBuffer, int BufferSize)
{
CFindCBData Data;
Data.m_pStorage = this;
Data.m_pFilename = pFilename;
Data.m_pPath = pPath;
Data.m_pBuffer = pBuffer;
Data.m_BufferSize = BufferSize;
Data.m_pWantedSha256 = 0;
Data.m_WantedCrc = 0;
Data.m_WantedSize = 0;
Data.m_CheckHashAndSize = false;
return FindFileImpl(Type, &Data);
}
virtual bool FindFile(const char *pFilename, const char *pPath, int Type, char *pBuffer, int BufferSize, const SHA256_DIGEST *pWantedSha256, unsigned WantedCrc, unsigned WantedSize)
{
CFindCBData Data;
Data.m_pStorage = this;
Data.m_pFilename = pFilename;
Data.m_pPath = pPath;
Data.m_pBuffer = pBuffer;
Data.m_BufferSize = BufferSize;
Data.m_pWantedSha256 = pWantedSha256;
Data.m_WantedCrc = WantedCrc;
Data.m_WantedSize = WantedSize;
Data.m_CheckHashAndSize = true;
return FindFileImpl(Type, &Data);
}
virtual bool RemoveFile(const char *pFilename, int Type)
{
if((Type < 0 || Type >= m_NumPaths) && Type != TYPE_APP)
return false;
char aBuffer[IO_MAX_PATH_LENGTH];
return !fs_remove(GetPath(Type, pFilename, aBuffer, sizeof(aBuffer)));
}
virtual bool RenameFile(const char *pOldFilename, const char *pNewFilename, int Type)
{
if((Type < 0 || Type >= m_NumPaths) && Type != TYPE_APP)
return false;
char aOldBuffer[IO_MAX_PATH_LENGTH];
char aNewBuffer[IO_MAX_PATH_LENGTH];
return !fs_rename(GetPath(Type, pOldFilename, aOldBuffer, sizeof(aOldBuffer)), GetPath(Type, pNewFilename, aNewBuffer, sizeof(aNewBuffer)));
}
virtual bool CreateFolder(const char *pFoldername, int Type)
{
if(Type < 0 || Type >= m_NumPaths) // there's no need for TYPE_APP, as we have already run this program :3
return false;
char aBuffer[IO_MAX_PATH_LENGTH];
return !fs_makedir(GetPath(Type, pFoldername, aBuffer, sizeof(aBuffer)));
}
virtual void GetCompletePath(int Type, const char *pDir, char *pBuffer, unsigned BufferSize)
{
if((Type < 0 || Type >= m_NumPaths) && Type != TYPE_APP)
{
if(BufferSize > 0)
pBuffer[0] = 0;
return;
}
GetPath(Type, pDir, pBuffer, BufferSize);
}
virtual bool GetHashAndSize(const char *pFilename, int StorageType, SHA256_DIGEST *pSha256, unsigned *pCrc, unsigned *pSize)
{
IOHANDLE File = OpenFile(pFilename, IOFLAG_READ, StorageType);
if(!File)
return false;
// get hash and size
SHA256_CTX Sha256Ctx;
sha256_init(&Sha256Ctx);
unsigned Crc = 0;
unsigned Size = 0;
unsigned char aBuffer[64 * 1024];
while(1)
{
unsigned Bytes = io_read(File, aBuffer, sizeof(aBuffer));
if(Bytes <= 0)
break;
sha256_update(&Sha256Ctx, aBuffer, Bytes);
Crc = crc32(Crc, aBuffer, Bytes);
Size += Bytes;
}
io_close(File);
*pSha256 = sha256_finish(&Sha256Ctx);
*pCrc = Crc;
*pSize = Size;
return true;
}
virtual bool GetFileTime(const char *pFilename, int StorageType, time_t *pCreated, time_t *pModified)
{
char aBuf[IO_MAX_PATH_LENGTH];
GetCompletePath(StorageType, pFilename, aBuf, sizeof(aBuf));
return !fs_file_time(aBuf, pCreated, pModified);
}
static IStorage *Create(const char *pApplicationName, int StorageType, int NumArgs, const char **ppArguments)
{
CStorage *p = new CStorage();
if(p && p->Init(pApplicationName, StorageType, NumArgs, ppArguments))
{
dbg_msg("storage", "initialisation failed");
delete p;
p = 0;
}
return p;
}
static IStorage *CreateTest()
{
CStorage *p = new CStorage();
if(!p)
{
return 0;
}
p->AddPath(".");
return p;
}
};
IStorage *CreateStorage(const char *pApplicationName, int StorageType, int NumArgs, const char **ppArguments) { return CStorage::Create(pApplicationName, StorageType, NumArgs, ppArguments); }
IStorage *CreateTestStorage() { return CStorage::CreateTest(); }