-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstorage.cpp
More file actions
554 lines (478 loc) · 11.5 KB
/
storage.cpp
File metadata and controls
554 lines (478 loc) · 11.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ncbind.hpp>
#include <map>
#include <vector>
extern "C" {
#include "mz.h"
#include "mz_strm.h"
#include "mz_zip.h"
#include "mz_zip_rw.h"
}
#include "ioapi.h"
#include "narrow.h"
#ifdef _WIN32
#include <windows.h>
#endif
#define CASESENSITIVITY (0)
#define BASENAME TJS_W("zip")
extern void storeFilename(ttstr &name, const char *narrowName, bool utf8);
/**
* Zip 展開処理クラス
*
* mz_zip_reader はエントリ単位での順次読み込みのみ対応しているため、
* lock/unlock で 1 つの reader を直列化して使う。
*/
class UnzipBase {
public:
UnzipBase() : refCount(1), zr(NULL), zrstream(NULL), zipfile(), utf8(false), entryOpen(false) {
#ifdef _WIN32
::InitializeCriticalSection(&cs);
#endif
}
void AddRef() {
refCount++;
};
void Release() {
if (refCount == 1) {
delete this;
} else {
refCount--;
}
};
/**
* ZIPファイルを開く
* @param filename ファイル名
*/
bool init(const ttstr &filename) {
done();
zipfile = filename;
if (!openReader()) return false;
// UTF8 判定 + ディレクトリエントリ収集
lock();
int err = mz_zip_reader_goto_first_entry(zr);
bool first = true;
while (err == MZ_OK) {
mz_zip_file *file_info = NULL;
if (mz_zip_reader_entry_get_info(zr, &file_info) == MZ_OK && file_info) {
if (first) {
utf8 = (file_info->flag & MZ_ZIP_FLAG_UTF8) != 0;
first = false;
}
ttstr name;
storeFilename(name, file_info->filename ? file_info->filename : "", utf8);
entryName(name);
}
err = mz_zip_reader_goto_next_entry(zr);
}
unlock();
return true;
}
/**
* 個別の展開用ファイルを開く
*/
bool open(const ttstr &srcname, tjs_uint64 *size) {
if (!zr) return false;
lock();
closeEntry();
NarrowString nname(srcname, utf8);
if (mz_zip_reader_locate_entry(zr, (const char*)nname, CASESENSITIVITY ? 0 : 1) == MZ_OK) {
mz_zip_file *file_info = NULL;
if (size && mz_zip_reader_entry_get_info(zr, &file_info) == MZ_OK && file_info) {
*size = (tjs_uint64)file_info->uncompressed_size;
}
if (mz_zip_reader_entry_open(zr) == MZ_OK) {
entryOpen = true;
return true; // unlock は close() 側で行う
}
}
unlock();
return false;
}
/**
* 個別の展開用ファイルを最初から開きなおす
*/
bool reopenLocked(const ttstr &srcname) {
// すでに lock 済みでエントリ open 中の状態から呼ばれる
closeEntry();
// reader を作り直したほうが安全 (一度 entry を読み始めたあと
// その entry の先頭まで巻き戻す API がないため)
if (zr) {
mz_zip_reader_close(zr);
mz_zip_reader_delete(&zr);
}
if (zrstream) {
mz_stream_close(zrstream);
mz_stream_tvp_delete(&zrstream);
}
if (!openReader()) return false;
NarrowString nname(srcname, utf8);
if (mz_zip_reader_locate_entry(zr, (const char*)nname, CASESENSITIVITY ? 0 : 1) == MZ_OK) {
if (mz_zip_reader_entry_open(zr) == MZ_OK) {
entryOpen = true;
return true;
}
}
return false;
}
/**
* 個別の展開用ファイルからデータを読み込む
*/
tjs_uint read(void *pv, tjs_uint cb) {
if (!zr || !entryOpen) return 0;
int32_t r = mz_zip_reader_entry_read(zr, pv, (int32_t)cb);
return r > 0 ? (tjs_uint)r : 0;
}
bool CheckExistentStorage(const ttstr &name) {
bool ret = false;
if (zr) {
lock();
closeEntry();
NarrowString nname(name, utf8);
ret = mz_zip_reader_locate_entry(zr, (const char*)nname, CASESENSITIVITY ? 0 : 1) == MZ_OK;
unlock();
}
return ret;
}
void GetListAt(const ttstr &name, iTVPStorageLister *lister) {
ttstr fname = "/";
fname += name;
std::map<ttstr, FileNameList>::const_iterator it = dirEntryTable.find(fname);
if (it != dirEntryTable.end()) {
std::vector<ttstr>::const_iterator fit = it->second.begin();
while (fit != it->second.end()) {
lister->Add(*fit);
fit++;
}
}
}
/**
* 個別の展開用ファイルを閉じる
*/
void close() {
if (zr) {
closeEntry();
unlock();
}
}
bool isUtf8() const { return utf8; }
protected:
virtual ~UnzipBase() {
done();
#ifdef _WIN32
::DeleteCriticalSection(&cs);
#endif
}
bool openReader() {
iTJSBinaryStream *bs = NULL;
try {
bs = TVPCreateStream(zipfile, TJS_BS_READ);
} catch(...) {
bs = NULL;
}
if (!bs) return false;
zrstream = mz_stream_tvp_create();
mz_stream_tvp_attach(zrstream, bs, 1);
zr = mz_zip_reader_create();
if (mz_zip_reader_open(zr, zrstream) != MZ_OK) {
mz_zip_reader_delete(&zr);
mz_stream_tvp_delete(&zrstream);
return false;
}
return true;
}
void done() {
if (zr) {
mz_zip_reader_close(zr);
mz_zip_reader_delete(&zr);
zr = NULL;
}
if (zrstream) {
mz_stream_close(zrstream);
mz_stream_tvp_delete(&zrstream);
zrstream = NULL;
}
}
void closeEntry() {
if (zr && entryOpen) {
mz_zip_reader_entry_close(zr);
entryOpen = false;
}
}
void lock() {
#ifdef _WIN32
::EnterCriticalSection(&cs);
#endif
}
void unlock() {
#ifdef _WIN32
::LeaveCriticalSection(&cs);
#endif
}
void entryName(const ttstr &name) {
ttstr dname = TJS_W("/");
ttstr fname;
const tjs_char *p = name.c_str();
const tjs_char *q;
if ((q = TJS_strrchr(p, '/'))) {
dname += ttstr(p, q-p+1);
fname = ttstr(q+1);
} else {
fname = name;
}
dirEntryTable[dname].push_back(fname);
}
private:
int refCount;
void *zr;
void *zrstream;
ttstr zipfile;
bool utf8;
bool entryOpen;
#ifdef _WIN32
CRITICAL_SECTION cs;
#endif
typedef std::vector<ttstr> FileNameList;
std::map<ttstr, FileNameList> dirEntryTable;
// 読み出しのために UnzipStream に開放してもらう
public:
void unlockAfterRead() { unlock(); }
};
/**
* ZIP展開ストリームクラス
*/
class UnzipStream : public iTJSBinaryStream {
public:
UnzipStream(UnzipBase *unzip) : unzip(unzip), filename(), size(0), position(0) {
unzip->AddRef();
};
virtual tjs_uint64 TJS_INTF_METHOD Seek(tjs_int64 offset, tjs_int whence) {
tjs_int64 newpos;
switch (whence) {
case TJS_BS_SEEK_CUR: newpos = (tjs_int64)position + offset; break;
case TJS_BS_SEEK_END: newpos = (tjs_int64)size + offset; break;
case TJS_BS_SEEK_SET:
default: newpos = offset; break;
}
if (newpos < 0) newpos = 0;
if ((tjs_uint64)newpos > size) newpos = (tjs_int64)size;
// 後方シーク → エントリを開きなおして先頭に戻す
if ((tjs_uint64)newpos < position) {
if (!unzip->reopenLocked(filename)) {
return position;
}
position = 0;
}
// 前方スキップ
char dummy[4096];
while (position < (tjs_uint64)newpos) {
tjs_uint to_read = sizeof dummy;
if ((tjs_uint64)newpos - position < to_read) {
to_read = (tjs_uint)((tjs_uint64)newpos - position);
}
tjs_uint r = unzip->read(dummy, to_read);
if (r == 0) break;
position += r;
}
return position;
}
virtual tjs_uint TJS_INTF_METHOD Read(void *buffer, tjs_uint read_size) {
tjs_uint r = unzip->read(buffer, read_size);
position += r;
return r;
}
virtual tjs_uint TJS_INTF_METHOD Write(const void *buffer, tjs_uint write_size) {
return 0;
};
virtual void TJS_INTF_METHOD SetEndOfStorage() {
}
virtual tjs_uint64 TJS_INTF_METHOD GetSize() {
return size;
}
bool init(const ttstr &filename) {
this->filename = filename;
return unzip->open(filename, &size);
}
protected:
virtual ~UnzipStream() {
unzip->close();
unzip->Release();
}
private:
UnzipBase *unzip;
ttstr filename;
tjs_uint64 size;
tjs_uint64 position;
};
/**
* ZIPストレージ
*/
class ZipStorage : public iTVPStorageMedia
{
public:
ZipStorage() : refCount(1) {
}
virtual ~ZipStorage() {
std::map<ttstr, UnzipBase*>::iterator it = unzipTable.begin();
while (it != unzipTable.end()) {
it->second->Release();
it = unzipTable.erase(it);
}
}
public:
// -----------------------------------
// iTVPStorageMedia Interfaces
// -----------------------------------
virtual void TJS_INTF_METHOD AddRef() {
refCount++;
};
virtual void TJS_INTF_METHOD Release() {
if (refCount == 1) {
delete this;
} else {
refCount--;
}
};
virtual void TJS_INTF_METHOD GetName(ttstr &name) {
name = BASENAME;
}
virtual void TJS_INTF_METHOD NormalizeDomainName(ttstr &name) {
}
virtual void TJS_INTF_METHOD NormalizePathName(ttstr &name) {
}
virtual bool TJS_INTF_METHOD CheckExistentStorage(const ttstr &name) {
ttstr fname;
UnzipBase *unzip = getUnzip(name, fname);
return unzip ? unzip->CheckExistentStorage(fname) : false;
}
virtual iTJSBinaryStream * TJS_INTF_METHOD Open(const ttstr & name, tjs_uint32 flags) {
if (flags == TJS_BS_READ) {
ttstr fname;
UnzipBase *unzip = getUnzip(name, fname);
if (unzip) {
UnzipStream *stream = new UnzipStream(unzip);
if (stream) {
if (stream->init(fname)) {
return stream;
}
stream->Destruct();
stream = 0;
}
}
}
TVPThrowExceptionMessage(TJS_W("%1:cannot open zipfile"), name);
return NULL;
}
virtual void TJS_INTF_METHOD GetListAt(const ttstr &name, iTVPStorageLister * lister) {
ttstr fname;
UnzipBase *unzip = getUnzip(name, fname);
if (unzip) {
unzip->GetListAt(fname, lister);
}
}
virtual void TJS_INTF_METHOD GetLocallyAccessibleName(ttstr &name) {
name = "";
}
public:
/**
* zipファイルをファイルシステムとして mount します
* zip://ドメイン名/ファイル名 でアクセス可能になります。読み込み専用になります。
* @param name ドメイン名
* @param zipfile マウントするZIPファイル名
* @return マウントに成功したら true
*/
bool mount(const ttstr &name, const ttstr &zipfile) {
unmount(name);
UnzipBase *newUnzip = new UnzipBase();
if (newUnzip) {
if (newUnzip->init(zipfile)) {
unzipTable[name] = newUnzip;
return true;
} else {
newUnzip->Release();
}
}
return false;
}
/**
* zipファイルを unmount します
* @param name ドメイン名
* @return アンマウントに成功したら true
*/
bool unmount(const ttstr &name) {
std::map<ttstr, UnzipBase*>::iterator it = unzipTable.find(name);
if (it != unzipTable.end()) {
it->second->Release();
unzipTable.erase(it);
return true;
}
return false;
}
protected:
UnzipBase *getUnzip(const ttstr &name, ttstr &fname) {
ttstr dname;
const tjs_char *p = name.c_str();
const tjs_char *q;
if ((q = TJS_strchr(p, '/'))) {
dname = ttstr(p, q-p);
fname = ttstr(q+1);
} else {
TVPThrowExceptionMessage(TJS_W("invalid path:%1"), name);
}
std::map<ttstr, UnzipBase*>::const_iterator it = unzipTable.find(dname);
if (it != unzipTable.end()) {
return it->second;
}
return NULL;
}
private:
tjs_uint refCount;
std::map<ttstr, UnzipBase*> unzipTable;
};
/**
* メソッド追加用
*/
class StoragesZip {
public:
static void init() {
if (zip == NULL) {
zip = new ZipStorage();
TVPRegisterStorageMedia(zip);
}
}
static void done() {
if (zip != NULL) {
TVPUnregisterStorageMedia(zip);
zip->Release();
zip = NULL;
}
}
static bool mountZip(const tjs_char *name, const tjs_char *zipfile) {
if (zip) {
return zip->mount(ttstr(name), ttstr(zipfile));
}
return false;
}
static bool unmountZip(const tjs_char *name) {
if (zip) {
return zip->unmount(ttstr(name));
}
return false;
}
protected:
static ZipStorage *zip;
};
ZipStorage *StoragesZip::zip = NULL;
NCB_ATTACH_CLASS(StoragesZip, Storages) {
NCB_METHOD(mountZip);
NCB_METHOD(unmountZip);
};
void initZipStorage()
{
StoragesZip::init();
}
void doneZipStorage()
{
StoragesZip::done();
}