-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathvolume.js
346 lines (312 loc) · 10.7 KB
/
volume.js
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
// Copyright 2014 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
'use strict';
/**
* Converts a c/c++ time_t variable to Date.
* @param {number} timestamp A c/c++ time_t variable.
* @return {!Date}
*/
function DateFromTimeT(timestamp) {
return new Date(1000 * timestamp);
}
/**
* Corrects metadata entries fields in order for them to be sent to Files.app.
* This function runs recursively for every entry in a directory.
* @param {!Object<string, !EntryMetadata>} entryMetadata The metadata to
* correct.
*/
function correctMetadata(entryMetadata) {
entryMetadata.index = parseInt(entryMetadata.index, 10);
entryMetadata.size = parseInt(entryMetadata.size, 10);
entryMetadata.modificationTime =
DateFromTimeT(entryMetadata.modificationTime);
if (entryMetadata.isDirectory) {
console.assert(entryMetadata.entries,
'The field "entries" is mandatory for dictionaries.');
for (var entry in entryMetadata.entries) {
correctMetadata(entryMetadata.entries[entry]);
}
}
}
/**
* Defines a volume object that contains information about archives' contents
* and performs operations on these contents.
* @constructor
* @param {!unpacker.Decompressor} decompressor The decompressor used to obtain
* data from archives.
* @param {!Entry} entry The entry corresponding to the volume's archive.
*/
unpacker.Volume = function(decompressor, entry) {
/**
* Used for restoring the opened file entry after resuming the event page.
* @type {!Entry}
*/
this.entry = entry;
/**
* @type {!unpacker.Decompressor}
*/
this.decompressor = decompressor;
/**
* The volume's metadata. The key is the full path to the file on this volume.
* For more details see
* https://developer.chrome.com/apps/fileSystemProvider#type-EntryMetadata
* @type {?Object<string, !EntryMetadata>}
*/
this.metadata = null;
/**
* A map with currently opened files. The key is a requestId value from the
* openFileRequested event and the value is the open file options.
* @type {!Object<!unpacker.types.RequestId,
* !unpacker.types.OpenFileRequestedOptions>}
*/
this.openedFiles = {};
/**
* Default encoding set for this archive. If empty, then not known.
* @type {string}
*/
this.encoding =
unpacker.Volume.ENCODING_TABLE[chrome.i18n.getUILanguage()] || '';
/**
* The default read metadata request id. -1 is ok as the request ids used by
* flleSystemProvider are greater than 0.
* @type {number}
*/
this.DEFAULT_READ_METADATA_REQUEST_ID = -1;
};
/**
* The default read metadata request id. -1 is ok as the request ids used by
* flleSystemProvider are greater than 0.
* @const {number}
*/
unpacker.Volume.DEFAULT_READ_METADATA_REQUEST_ID = -1;
/**
* Map from language codes to default charset encodings.
* @const {!Object<string, string>}
*/
unpacker.Volume.ENCODING_TABLE = {
ar: 'CP1256',
bg: 'CP1251',
ca: 'CP1252',
cs: 'CP1250',
da: 'CP1252',
de: 'CP1252',
el: 'CP1253',
en: 'CP1250',
en_GB: 'CP1250',
es: 'CP1252',
es_419: 'CP1252',
et: 'CP1257',
fa: 'CP1256',
fi: 'CP1252',
fil: 'CP1252',
fr: 'CP1252',
he: 'CP1255',
hi: 'UTF-8', // Another one may be better.
hr: 'CP1250',
hu: 'CP1250',
id: 'CP1252',
it: 'CP1252',
ja: 'CP932', // Alternatively SHIFT-JIS.
ko: 'CP949', // Alternatively EUC-KR.
lt: 'CP1257',
lv: 'CP1257',
ms: 'CP1252',
nl: 'CP1252',
no: 'CP1252',
pl: 'CP1250',
pt_BR: 'CP1252',
pt_PT: 'CP1252',
ro: 'CP1250',
ru: 'CP1251',
sk: 'CP1250',
sl: 'CP1250',
sr: 'CP1251',
sv: 'CP1252',
th: 'CP874', // Confirm!
tr: 'CP1254',
uk: 'CP1251',
vi: 'CP1258',
zh_CN: 'CP936',
zh_TW: 'CP950'
};
/**
* @return {boolean} True if volume is ready to be used.
*/
unpacker.Volume.prototype.isReady = function() {
return !!this.metadata;
};
/**
* @return {boolean} True if volume is in use.
*/
unpacker.Volume.prototype.inUse = function() {
return this.decompressor.hasRequestsInProgress() ||
Object.keys(this.openedFiles).length > 0;
};
/**
* Initializes the volume by reading its metadata.
* @param {function()} onSuccess Callback to execute on success.
* @param {function(!ProviderError)} onError Callback to execute on error.
*/
unpacker.Volume.prototype.initialize = function(onSuccess, onError) {
var requestId = unpacker.Volume.DEFAULT_READ_METADATA_REQUEST_ID;
this.decompressor.readMetadata(requestId, this.encoding, function(metadata) {
// Make a deep copy of metadata.
this.metadata = /** @type {!Object<string, !EntryMetadata>} */ (JSON.parse(
JSON.stringify(metadata)));
correctMetadata(this.metadata);
onSuccess();
}.bind(this), onError);
};
/**
* Obtains the metadata for a single entry in the archive. Assumes metadata is
* loaded.
* @param {!unpacker.types.GetMetadataRequestedOptions} options Options for
* getting the metadata of an entry.
* @param {function(!EntryMetadata)} onSuccess Callback to execute on success.
* @param {function(!ProviderError)} onError Callback to execute on error.
*/
unpacker.Volume.prototype.onGetMetadataRequested = function(options, onSuccess,
onError) {
console.assert(this.isReady(), 'Metadata must be loaded.');
var entryMetadata = this.getEntryMetadata_(options.entryPath);
if (!entryMetadata)
onError('NOT_FOUND');
else
onSuccess(entryMetadata);
};
/**
* Reads a directory contents from metadata. Assumes metadata is loaded.
* @param {!unpacker.types.ReadDirectoryRequestedOptions} options Options
* for reading the contents of a directory.
* @param {function(!Array<!EntryMetadata>, boolean)} onSuccess Callback to
* execute on success.
* @param {function(!ProviderError)} onError Callback to execute on error.
*/
unpacker.Volume.prototype.onReadDirectoryRequested = function(
options, onSuccess, onError) {
console.assert(this.isReady(), 'Metadata must be loaded.');
var directoryMetadata = this.getEntryMetadata_(options.directoryPath);
if (!directoryMetadata) {
onError('NOT_FOUND');
return;
}
if (!directoryMetadata.isDirectory) {
onError('NOT_A_DIRECTORY');
return;
}
// Convert dictionary entries to an array.
var entries = [];
for (var entry in directoryMetadata.entries) {
entries.push(directoryMetadata.entries[entry]);
}
onSuccess(entries, false /* Last call. */);
};
/**
* Opens a file for read or write.
* @param {!unpacker.types.OpenFileRequestedOptions} options Options for
* opening a file.
* @param {function()} onSuccess Callback to execute on success.
* @param {function(!ProviderError)} onError Callback to execute on error.
*/
unpacker.Volume.prototype.onOpenFileRequested = function(options, onSuccess,
onError) {
console.assert(this.isReady(), 'Metadata must be loaded.');
if (options.mode != 'READ') {
onError('INVALID_OPERATION');
return;
}
var metadata = this.getEntryMetadata_(options.filePath);
if (!metadata) {
onError('NOT_FOUND');
return;
}
this.openedFiles[options.requestId] = options;
this.decompressor.openFile(
options.requestId, metadata.index, this.encoding, function() {
onSuccess();
}.bind(this), function(error) {
delete this.openedFiles[options.requestId];
onError('FAILED');
}.bind(this));
};
/**
* Closes a file identified by options.openRequestId.
* @param {!unpacker.types.CloseFileRequestedOptions} options Options for
* closing a file.
* @param {function()} onSuccess Callback to execute on success.
* @param {function(!ProviderError)} onError Callback to execute on error.
*/
unpacker.Volume.prototype.onCloseFileRequested = function(options, onSuccess,
onError) {
console.assert(this.isReady(), 'Metadata must be loaded.');
var openRequestId = options.openRequestId;
var openOptions = this.openedFiles[openRequestId];
if (!openOptions) {
onError('INVALID_OPERATION');
return;
}
this.decompressor.closeFile(options.requestId, openRequestId, function() {
delete this.openedFiles[openRequestId];
onSuccess();
}.bind(this), onError);
};
/**
* Reads the contents of a file identified by options.openRequestId.
* @param {!unpacker.types.ReadFileRequestedOptions} options Options for
* reading a file's contents.
* @param {function(!ArrayBuffer, boolean)} onSuccess Callback to execute on
* success.
* @param {function(!ProviderError)} onError Callback to execute on error.
*/
unpacker.Volume.prototype.onReadFileRequested = function(options, onSuccess,
onError) {
console.assert(this.isReady(), 'Metadata must be loaded.');
var openOptions = this.openedFiles[options.openRequestId];
if (!openOptions) {
onError('INVALID_OPERATION');
return;
}
var offset = options.offset;
var length = options.length;
// Offset and length should be validated by the API.
console.assert(offset >= 0, 'Offset should be >= 0.');
console.assert(length >= 0, 'Length should be >= 0.');
var fileSize = this.getEntryMetadata_(openOptions.filePath).size;
if (offset >= fileSize || length == 0) { // No more data.
onSuccess(new ArrayBuffer(0), false /* Last call. */);
return;
}
length = Math.min(length, fileSize - offset);
this.decompressor.readFile(options.requestId, options.openRequestId,
offset, length, onSuccess, onError);
};
/**
* Gets the metadata for an entry based on its path.
* @param {string} entryPath The full path to the entry.
* @return {?Object} The correspondent metadata.
* @private
*/
unpacker.Volume.prototype.getEntryMetadata_ = function(entryPath) {
var pathArray = entryPath.split('/');
// Remove empty strings resulted after split. As paths start with '/' we will
// have an empty string at the beginning of pathArray and possible an
// empty string at the end for directories (e.g. /path/to/dir/). The code
// assumes entryPath cannot have consecutive '/'.
pathArray.splice(0, 1);
if (pathArray.length > 0) { // In case of 0 this is root directory.
var lastIndex = pathArray.length - 1;
if (pathArray[lastIndex] == '')
pathArray.splice(lastIndex);
}
// Get the actual metadata by iterating through every directory metadata
// on the path to the entry.
var entryMetadata = this.metadata;
for (var i = 0, limit = pathArray.length; i < limit; i++) {
if (!entryMetadata ||
!entryMetadata.isDirectory && i != limit - 1 /* Parent directory. */)
return null;
entryMetadata = entryMetadata.entries[pathArray[i]];
}
return entryMetadata;
};