1 | 'use strict';
|
2 |
|
3 | var _promise = require('babel-runtime/core-js/promise');
|
4 |
|
5 | var _promise2 = _interopRequireDefault(_promise);
|
6 |
|
7 | var _regenerator = require('babel-runtime/regenerator');
|
8 |
|
9 | var _regenerator2 = _interopRequireDefault(_regenerator);
|
10 |
|
11 | var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');
|
12 |
|
13 | var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);
|
14 |
|
15 | var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
|
16 |
|
17 | var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
|
18 |
|
19 | var _createClass2 = require('babel-runtime/helpers/createClass');
|
20 |
|
21 | var _createClass3 = _interopRequireDefault(_createClass2);
|
22 |
|
23 | var _typeof2 = require('babel-runtime/helpers/typeof');
|
24 |
|
25 | var _typeof3 = _interopRequireDefault(_typeof2);
|
26 |
|
27 | var _structFu = require('@nraynaud/struct-fu');
|
28 |
|
29 | var _structFu2 = _interopRequireDefault(_structFu);
|
30 |
|
31 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
32 |
|
33 | var VHD_UTIL_DEBUG = 0;
|
34 | var debug = VHD_UTIL_DEBUG ? function (str) {
|
35 | return console.log('[vhd-util]' + str);
|
36 | } : function () {};
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | var VHD_FOOTER_SIZE = 512;
|
50 | var VHD_HEADER_SIZE = 1024;
|
51 | var VHD_SECTOR_SIZE = 512;
|
52 |
|
53 |
|
54 | var VHD_ENTRY_SIZE = 4;
|
55 |
|
56 | var VHD_PARENT_LOCATOR_ENTRIES = 8;
|
57 | var VHD_PLATFORM_CODE_NONE = 0;
|
58 |
|
59 |
|
60 | var HARD_DISK_TYPE_DYNAMIC = 3;
|
61 | var HARD_DISK_TYPE_DIFFERENCING = 4;
|
62 |
|
63 |
|
64 | var BLOCK_UNUSED = 0xFFFFFFFF;
|
65 | var BIT_MASK = 0x80;
|
66 |
|
67 |
|
68 |
|
69 | var fuFooter = _structFu2.default.struct([_structFu2.default.char('cookie', 8),
|
70 | _structFu2.default.uint32('features'),
|
71 | _structFu2.default.uint32('fileFormatVersion'),
|
72 | _structFu2.default.struct('dataOffset', [_structFu2.default.uint32('high'),
|
73 | _structFu2.default.uint32('low')
|
74 | ]), _structFu2.default.uint32('timestamp'),
|
75 | _structFu2.default.char('creatorApplication', 4),
|
76 | _structFu2.default.uint32('creatorVersion'),
|
77 | _structFu2.default.uint32('creatorHostOs'),
|
78 | _structFu2.default.struct('originalSize', [
|
79 | _structFu2.default.uint32('high'),
|
80 | _structFu2.default.uint32('low')
|
81 | ]), _structFu2.default.struct('currentSize', [
|
82 | _structFu2.default.uint32('high'),
|
83 | _structFu2.default.uint32('low')
|
84 | ]), _structFu2.default.struct('diskGeometry', [_structFu2.default.uint16('cylinders'),
|
85 | _structFu2.default.uint8('heads'),
|
86 | _structFu2.default.uint8('sectorsPerTrackCylinder')
|
87 | ]), _structFu2.default.uint32('diskType'),
|
88 | _structFu2.default.uint32('checksum'),
|
89 | _structFu2.default.uint8('uuid', 16),
|
90 | _structFu2.default.char('saved'),
|
91 | _structFu2.default.char('hidden'),
|
92 | _structFu2.default.char('reserved', 426)
|
93 | ]);
|
94 |
|
95 | var fuHeader = _structFu2.default.struct([_structFu2.default.char('cookie', 8), _structFu2.default.struct('dataOffset', [_structFu2.default.uint32('high'), _structFu2.default.uint32('low')]), _structFu2.default.struct('tableOffset', [
|
96 | _structFu2.default.uint32('high'), _structFu2.default.uint32('low')]), _structFu2.default.uint32('headerVersion'), _structFu2.default.uint32('maxTableEntries'),
|
97 | _structFu2.default.uint32('blockSize'),
|
98 | _structFu2.default.uint32('checksum'), _structFu2.default.uint8('parentUuid', 16), _structFu2.default.uint32('parentTimestamp'), _structFu2.default.uint32('reserved1'), _structFu2.default.char16be('parentUnicodeName', 512), _structFu2.default.struct('parentLocatorEntry', [_structFu2.default.uint32('platformCode'), _structFu2.default.uint32('platformDataSpace'), _structFu2.default.uint32('platformDataLength'), _structFu2.default.uint32('reserved'), _structFu2.default.struct('platformDataOffset', [
|
99 | _structFu2.default.uint32('high'), _structFu2.default.uint32('low')])], VHD_PARENT_LOCATOR_ENTRIES), _structFu2.default.char('reserved2', 256)]);
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | var SIZE_OF_32_BITS = Math.pow(2, 32);
|
106 | var uint32ToUint64 = function uint32ToUint64(fu) {
|
107 | return fu.high * SIZE_OF_32_BITS + fu.low;
|
108 | };
|
109 |
|
110 |
|
111 | var getVhdVersion = function getVhdVersion(major, minor) {
|
112 | return major << 16 | minor & 0x0000FFFF;
|
113 | };
|
114 |
|
115 |
|
116 | var sectorsRoundUp = function sectorsRoundUp(bytes) {
|
117 | return Math.floor((bytes + VHD_SECTOR_SIZE - 1) / VHD_SECTOR_SIZE);
|
118 | };
|
119 | var sectorsRoundUpNoZero = function sectorsRoundUpNoZero(bytes) {
|
120 | return sectorsRoundUp(bytes) || 1;
|
121 | };
|
122 | var sectorsToBytes = function sectorsToBytes(sectors) {
|
123 | return sectors * VHD_SECTOR_SIZE;
|
124 | };
|
125 |
|
126 |
|
127 | var mapTestBit = function mapTestBit(map, bit) {
|
128 | return (map[bit >> 3] << (bit & 7) & BIT_MASK) !== 0;
|
129 | };
|
130 | var mapSetBit = function mapSetBit(map, bit) {
|
131 | map[bit >> 3] |= BIT_MASK >> (bit & 7);
|
132 | };
|
133 |
|
134 | var packField = function packField(field, value, buf) {
|
135 | var offset = field.offset;
|
136 |
|
137 |
|
138 | field.pack(value, buf, (typeof offset === 'undefined' ? 'undefined' : (0, _typeof3.default)(offset)) !== 'object' ? { bytes: offset, bits: 0 } : offset);
|
139 | };
|
140 |
|
141 | var unpackField = function unpackField(field, buf) {
|
142 | var offset = field.offset;
|
143 |
|
144 |
|
145 | return field.unpack(buf, (typeof offset === 'undefined' ? 'undefined' : (0, _typeof3.default)(offset)) !== 'object' ? { bytes: offset, bits: 0 } : offset);
|
146 | };
|
147 |
|
148 |
|
149 |
|
150 |
|
151 | function checksumStruct(rawStruct, checksumField) {
|
152 | var sum = 0;
|
153 |
|
154 |
|
155 | packField(checksumField, 0, rawStruct);
|
156 |
|
157 | for (var i = 0; i < VHD_FOOTER_SIZE; i++) {
|
158 | sum = sum + rawStruct[i] & 0xFFFFFFFF;
|
159 | }
|
160 |
|
161 | sum = 0xFFFFFFFF - sum;
|
162 |
|
163 |
|
164 | packField(checksumField, sum, rawStruct);
|
165 |
|
166 | return sum;
|
167 | }
|
168 |
|
169 | function getParentLocatorSize(parentLocatorEntry) {
|
170 | var platformDataSpace = parentLocatorEntry.platformDataSpace;
|
171 |
|
172 |
|
173 | if (platformDataSpace < VHD_SECTOR_SIZE) {
|
174 | return sectorsToBytes(platformDataSpace);
|
175 | }
|
176 |
|
177 | return platformDataSpace % VHD_SECTOR_SIZE === 0 ? platformDataSpace : 0;
|
178 | }
|
179 |
|
180 |
|
181 |
|
182 | var Vhd = function () {
|
183 | function Vhd(handler, path) {
|
184 | (0, _classCallCheck3.default)(this, Vhd);
|
185 |
|
186 | this._handler = handler;
|
187 | this._path = path;
|
188 | }
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 | (0, _createClass3.default)(Vhd, [{
|
198 | key: 'getEndOfHeaders',
|
199 | value: function getEndOfHeaders() {
|
200 | var header = this.header;
|
201 |
|
202 |
|
203 | var end = uint32ToUint64(this.footer.dataOffset) + VHD_HEADER_SIZE;
|
204 |
|
205 | var blockAllocationTableSize = sectorsToBytes(sectorsRoundUpNoZero(header.maxTableEntries * VHD_ENTRY_SIZE));
|
206 |
|
207 |
|
208 | end = Math.max(end, uint32ToUint64(header.tableOffset) + blockAllocationTableSize);
|
209 |
|
210 | for (var i = 0; i < VHD_PARENT_LOCATOR_ENTRIES; i++) {
|
211 | var entry = header.parentLocatorEntry[i];
|
212 |
|
213 | if (entry.platformCode !== VHD_PLATFORM_CODE_NONE) {
|
214 | var dataOffset = uint32ToUint64(entry.platformDataOffset);
|
215 |
|
216 |
|
217 | end = Math.max(end, dataOffset + getParentLocatorSize(entry));
|
218 | }
|
219 | }
|
220 |
|
221 | debug('End of headers: ' + end + '.');
|
222 |
|
223 | return end;
|
224 | }
|
225 |
|
226 |
|
227 |
|
228 | }, {
|
229 | key: 'getEndOfData',
|
230 | value: function getEndOfData() {
|
231 | var end = Math.floor(this.getEndOfHeaders() / VHD_SECTOR_SIZE);
|
232 |
|
233 | var maxTableEntries = this.header.maxTableEntries;
|
234 |
|
235 | for (var i = 0; i < maxTableEntries; i++) {
|
236 | var blockAddr = this.readAllocationTableEntry(i);
|
237 |
|
238 | if (blockAddr !== BLOCK_UNUSED) {
|
239 |
|
240 | blockAddr += this.sectorsPerBlock + this.sectorsOfBitmap;
|
241 |
|
242 | end = Math.max(end, blockAddr);
|
243 | }
|
244 | }
|
245 |
|
246 | debug('End of data: ' + end + '.');
|
247 |
|
248 | return sectorsToBytes(end);
|
249 | }
|
250 |
|
251 |
|
252 |
|
253 |
|
254 | }, {
|
255 | key: 'getFooterStart',
|
256 | value: function () {
|
257 | var _ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee() {
|
258 | var stats;
|
259 | return _regenerator2.default.wrap(function _callee$(_context) {
|
260 | while (1) {
|
261 | switch (_context.prev = _context.next) {
|
262 | case 0:
|
263 | _context.next = 2;
|
264 | return this._handler.getSize(this._path);
|
265 |
|
266 | case 2:
|
267 | stats = _context.sent;
|
268 | return _context.abrupt('return', stats.size - VHD_FOOTER_SIZE);
|
269 |
|
270 | case 4:
|
271 | case 'end':
|
272 | return _context.stop();
|
273 | }
|
274 | }
|
275 | }, _callee, this);
|
276 | }));
|
277 |
|
278 | function getFooterStart() {
|
279 | return _ref.apply(this, arguments);
|
280 | }
|
281 |
|
282 | return getFooterStart;
|
283 | }()
|
284 |
|
285 |
|
286 |
|
287 | }, {
|
288 | key: 'readHeaderAndFooter',
|
289 | value: function () {
|
290 | var _ref2 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee2() {
|
291 | var buf, sum, sumToTest, header, sectorsPerBlock, sectorsOfBitmap;
|
292 | return _regenerator2.default.wrap(function _callee2$(_context2) {
|
293 | while (1) {
|
294 | switch (_context2.prev = _context2.next) {
|
295 | case 0:
|
296 | _context2.next = 2;
|
297 | return this._handler.createReadStream(this._path, {
|
298 | start: 0,
|
299 | end: VHD_FOOTER_SIZE + VHD_HEADER_SIZE - 1
|
300 | });
|
301 |
|
302 | case 2:
|
303 | _context2.t0 = _context2.sent;
|
304 | _context2.next = 5;
|
305 | return streamToBuffer(_context2.t0);
|
306 |
|
307 | case 5:
|
308 | buf = _context2.sent;
|
309 | sum = unpackField(fuFooter.fields.checksum, buf);
|
310 | sumToTest = checksumStruct(buf, fuFooter.fields.checksum);
|
311 |
|
312 |
|
313 |
|
314 | if (!(sumToTest !== sum)) {
|
315 | _context2.next = 10;
|
316 | break;
|
317 | }
|
318 |
|
319 | throw new Error('Bad checksum in vhd. Expected: ' + sum + '. Given: ' + sumToTest + '. (data=' + buf.toString('hex') + ')');
|
320 |
|
321 | case 10:
|
322 | header = this.header = fuHeader.unpack(buf.slice(VHD_FOOTER_SIZE));
|
323 |
|
324 | this.footer = fuFooter.unpack(buf);
|
325 |
|
326 |
|
327 |
|
328 | sectorsPerBlock = this.sectorsPerBlock = Math.floor(header.blockSize / VHD_SECTOR_SIZE);
|
329 |
|
330 |
|
331 |
|
332 |
|
333 | sectorsOfBitmap = this.sectorsOfBitmap = sectorsRoundUpNoZero(sectorsPerBlock >> 3);
|
334 |
|
335 |
|
336 |
|
337 | this.fullBlockSize = sectorsToBytes(sectorsPerBlock + sectorsOfBitmap);
|
338 |
|
339 |
|
340 |
|
341 | this.bitmapSize = sectorsToBytes(sectorsOfBitmap);
|
342 |
|
343 | case 16:
|
344 | case 'end':
|
345 | return _context2.stop();
|
346 | }
|
347 | }
|
348 | }, _callee2, this);
|
349 | }));
|
350 |
|
351 | function readHeaderAndFooter() {
|
352 | return _ref2.apply(this, arguments);
|
353 | }
|
354 |
|
355 | return readHeaderAndFooter;
|
356 | }()
|
357 |
|
358 |
|
359 |
|
360 | }, {
|
361 | key: 'hasBlockAllocationTableMap',
|
362 | value: function hasBlockAllocationTableMap() {
|
363 | return this.footer.fileFormatVersion > getVhdVersion(1, 0);
|
364 | }
|
365 |
|
366 |
|
367 |
|
368 | }, {
|
369 | key: 'readBlockTable',
|
370 | value: function () {
|
371 | var _ref3 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee3() {
|
372 | var header, offset, size;
|
373 | return _regenerator2.default.wrap(function _callee3$(_context3) {
|
374 | while (1) {
|
375 | switch (_context3.prev = _context3.next) {
|
376 | case 0:
|
377 | header = this.header;
|
378 | offset = uint32ToUint64(header.tableOffset);
|
379 | size = sectorsToBytes(sectorsRoundUpNoZero(header.maxTableEntries * VHD_ENTRY_SIZE));
|
380 | _context3.next = 5;
|
381 | return this._handler.createReadStream(this._path, {
|
382 | start: offset,
|
383 | end: offset + size - 1
|
384 | });
|
385 |
|
386 | case 5:
|
387 | _context3.t0 = _context3.sent;
|
388 | _context3.next = 8;
|
389 | return streamToBuffer(_context3.t0);
|
390 |
|
391 | case 8:
|
392 | this.blockTable = _context3.sent;
|
393 |
|
394 | case 9:
|
395 | case 'end':
|
396 | return _context3.stop();
|
397 | }
|
398 | }
|
399 | }, _callee3, this);
|
400 | }));
|
401 |
|
402 | function readBlockTable() {
|
403 | return _ref3.apply(this, arguments);
|
404 | }
|
405 |
|
406 | return readBlockTable;
|
407 | }()
|
408 |
|
409 |
|
410 |
|
411 | }, {
|
412 | key: 'readAllocationTableEntry',
|
413 | value: function readAllocationTableEntry(entry) {
|
414 | return this.blockTable.readUInt32BE(entry * VHD_ENTRY_SIZE);
|
415 | }
|
416 |
|
417 |
|
418 |
|
419 | }, {
|
420 | key: 'readBlockData',
|
421 | value: function () {
|
422 | var _ref4 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee4(blockAddr) {
|
423 | var blockSize, handler, path, blockDataAddr, footerStart, isPadded, size, buf;
|
424 | return _regenerator2.default.wrap(function _callee4$(_context4) {
|
425 | while (1) {
|
426 | switch (_context4.prev = _context4.next) {
|
427 | case 0:
|
428 | blockSize = this.header.blockSize;
|
429 | handler = this._handler;
|
430 | path = this._path;
|
431 | blockDataAddr = sectorsToBytes(blockAddr + this.sectorsOfBitmap);
|
432 | _context4.next = 6;
|
433 | return this.getFooterStart();
|
434 |
|
435 | case 6:
|
436 | footerStart = _context4.sent;
|
437 | isPadded = footerStart < blockDataAddr + blockSize;
|
438 |
|
439 |
|
440 |
|
441 | size = isPadded ? footerStart - blockDataAddr : sectorsToBytes(this.sectorsPerBlock);
|
442 |
|
443 |
|
444 | debug('Read block data at: ' + blockDataAddr + '. (size=' + size + ')');
|
445 |
|
446 | _context4.next = 12;
|
447 | return handler.createReadStream(path, {
|
448 | start: blockDataAddr,
|
449 | end: blockDataAddr + size - 1
|
450 | });
|
451 |
|
452 | case 12:
|
453 | _context4.t0 = _context4.sent;
|
454 | _context4.next = 15;
|
455 | return streamToBuffer(_context4.t0);
|
456 |
|
457 | case 15:
|
458 | buf = _context4.sent;
|
459 |
|
460 | if (!isPadded) {
|
461 | _context4.next = 18;
|
462 | break;
|
463 | }
|
464 |
|
465 | return _context4.abrupt('return', Buffer.concat([buf, new Buffer(blockSize - size).fill(0)]));
|
466 |
|
467 | case 18:
|
468 | return _context4.abrupt('return', buf);
|
469 |
|
470 | case 19:
|
471 | case 'end':
|
472 | return _context4.stop();
|
473 | }
|
474 | }
|
475 | }, _callee4, this);
|
476 | }));
|
477 |
|
478 | function readBlockData(_x) {
|
479 | return _ref4.apply(this, arguments);
|
480 | }
|
481 |
|
482 | return readBlockData;
|
483 | }()
|
484 |
|
485 |
|
486 |
|
487 |
|
488 |
|
489 | }, {
|
490 | key: 'readBlockBitmap',
|
491 | value: function () {
|
492 | var _ref5 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee5(blockAddr) {
|
493 | var bitmapSize, offset;
|
494 | return _regenerator2.default.wrap(function _callee5$(_context5) {
|
495 | while (1) {
|
496 | switch (_context5.prev = _context5.next) {
|
497 | case 0:
|
498 | bitmapSize = this.bitmapSize;
|
499 | offset = sectorsToBytes(blockAddr);
|
500 |
|
501 |
|
502 | debug('Read bitmap at: ' + offset + '. (size=' + bitmapSize + ')');
|
503 |
|
504 | _context5.next = 5;
|
505 | return this._handler.createReadStream(this._path, {
|
506 | start: offset,
|
507 | end: offset + bitmapSize - 1
|
508 | });
|
509 |
|
510 | case 5:
|
511 | _context5.t0 = _context5.sent;
|
512 | return _context5.abrupt('return', streamToBuffer(_context5.t0));
|
513 |
|
514 | case 7:
|
515 | case 'end':
|
516 | return _context5.stop();
|
517 | }
|
518 | }
|
519 | }, _callee5, this);
|
520 | }));
|
521 |
|
522 | function readBlockBitmap(_x2) {
|
523 | return _ref5.apply(this, arguments);
|
524 | }
|
525 |
|
526 | return readBlockBitmap;
|
527 | }()
|
528 |
|
529 |
|
530 |
|
531 |
|
532 |
|
533 |
|
534 |
|
535 | }, {
|
536 | key: '_write',
|
537 | value: function () {
|
538 | var _ref6 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee6(buffer, offset) {
|
539 | return _regenerator2.default.wrap(function _callee6$(_context6) {
|
540 | while (1) {
|
541 | switch (_context6.prev = _context6.next) {
|
542 | case 0:
|
543 | return _context6.abrupt('return', this._handler.createOutputStream(this._path, {
|
544 | start: offset,
|
545 | flags: 'r+'
|
546 | }).then(function (stream) {
|
547 | return new _promise2.default(function (resolve, reject) {
|
548 | stream.on('error', reject);
|
549 | stream.write(buffer, function () {
|
550 | stream.end();
|
551 | resolve();
|
552 | });
|
553 | });
|
554 | }));
|
555 |
|
556 | case 1:
|
557 | case 'end':
|
558 | return _context6.stop();
|
559 | }
|
560 | }
|
561 | }, _callee6, this);
|
562 | }));
|
563 |
|
564 | function _write(_x3, _x4) {
|
565 | return _ref6.apply(this, arguments);
|
566 | }
|
567 |
|
568 | return _write;
|
569 | }()
|
570 |
|
571 |
|
572 |
|
573 | }, {
|
574 | key: 'writeAllocationTableEntry',
|
575 | value: function writeAllocationTableEntry(entry, value) {
|
576 | this.blockTable.writeUInt32BE(value, entry * VHD_ENTRY_SIZE);
|
577 | }
|
578 |
|
579 |
|
580 |
|
581 |
|
582 | }, {
|
583 | key: 'createBlock',
|
584 | value: function () {
|
585 | var _ref7 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee7(blockId) {
|
586 | var offset, blockAddr, blockTable, fullBlockSize, tableOffset, entry;
|
587 | return _regenerator2.default.wrap(function _callee7$(_context7) {
|
588 | while (1) {
|
589 | switch (_context7.prev = _context7.next) {
|
590 | case 0:
|
591 |
|
592 | offset = this.getEndOfData();
|
593 |
|
594 |
|
595 |
|
596 | if (offset % VHD_SECTOR_SIZE) {
|
597 | offset += VHD_SECTOR_SIZE - offset % VHD_SECTOR_SIZE;
|
598 | }
|
599 |
|
600 | blockAddr = Math.floor(offset / VHD_SECTOR_SIZE);
|
601 | blockTable = this.blockTable;
|
602 | fullBlockSize = this.fullBlockSize;
|
603 |
|
604 | debug('Create block at ' + blockAddr + '. (size=' + fullBlockSize + ', offset=' + offset + ')');
|
605 |
|
606 |
|
607 | this.writeAllocationTableEntry(blockId, blockAddr);
|
608 |
|
609 | tableOffset = uint32ToUint64(this.header.tableOffset);
|
610 | entry = blockId * VHD_ENTRY_SIZE;
|
611 |
|
612 |
|
613 |
|
614 | _context7.next = 11;
|
615 | return this._write(new Buffer(fullBlockSize).fill(0), offset);
|
616 |
|
617 | case 11:
|
618 | _context7.next = 13;
|
619 | return this._write(blockTable.slice(entry, entry + VHD_ENTRY_SIZE), tableOffset + entry);
|
620 |
|
621 | case 13:
|
622 | return _context7.abrupt('return', blockAddr);
|
623 |
|
624 | case 14:
|
625 | case 'end':
|
626 | return _context7.stop();
|
627 | }
|
628 | }
|
629 | }, _callee7, this);
|
630 | }));
|
631 |
|
632 | function createBlock(_x5) {
|
633 | return _ref7.apply(this, arguments);
|
634 | }
|
635 |
|
636 | return createBlock;
|
637 | }()
|
638 |
|
639 |
|
640 |
|
641 | }, {
|
642 | key: 'writeBlockBitmap',
|
643 | value: function () {
|
644 | var _ref8 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee8(blockAddr, bitmap) {
|
645 | var bitmapSize, offset;
|
646 | return _regenerator2.default.wrap(function _callee8$(_context8) {
|
647 | while (1) {
|
648 | switch (_context8.prev = _context8.next) {
|
649 | case 0:
|
650 | bitmapSize = this.bitmapSize;
|
651 |
|
652 | if (!(bitmap.length !== bitmapSize)) {
|
653 | _context8.next = 3;
|
654 | break;
|
655 | }
|
656 |
|
657 | throw new Error('Bitmap length is not correct ! ' + bitmap.length);
|
658 |
|
659 | case 3:
|
660 | offset = sectorsToBytes(blockAddr);
|
661 |
|
662 |
|
663 | debug('Write bitmap at: ' + offset + '. (size=' + bitmapSize + ', data=' + bitmap.toString('hex') + ')');
|
664 | _context8.next = 7;
|
665 | return this._write(bitmap, sectorsToBytes(blockAddr));
|
666 |
|
667 | case 7:
|
668 | case 'end':
|
669 | return _context8.stop();
|
670 | }
|
671 | }
|
672 | }, _callee8, this);
|
673 | }));
|
674 |
|
675 | function writeBlockBitmap(_x6, _x7) {
|
676 | return _ref8.apply(this, arguments);
|
677 | }
|
678 |
|
679 | return writeBlockBitmap;
|
680 | }()
|
681 | }, {
|
682 | key: 'writeBlockSectors',
|
683 | value: function () {
|
684 | var _ref9 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee9(block, beginSectorId, n) {
|
685 | var blockAddr, endSectorId, offset, bitmap, i;
|
686 | return _regenerator2.default.wrap(function _callee9$(_context9) {
|
687 | while (1) {
|
688 | switch (_context9.prev = _context9.next) {
|
689 | case 0:
|
690 | blockAddr = this.readAllocationTableEntry(block.id);
|
691 |
|
692 | if (!(blockAddr === BLOCK_UNUSED)) {
|
693 | _context9.next = 5;
|
694 | break;
|
695 | }
|
696 |
|
697 | _context9.next = 4;
|
698 | return this.createBlock(block.id);
|
699 |
|
700 | case 4:
|
701 | blockAddr = _context9.sent;
|
702 |
|
703 | case 5:
|
704 | endSectorId = beginSectorId + n;
|
705 | offset = blockAddr + this.sectorsOfBitmap + beginSectorId;
|
706 |
|
707 |
|
708 | debug('Write block data at: ' + offset + '. (counter=' + n + ', blockId=' + block.id + ', blockSector=' + beginSectorId + ')');
|
709 |
|
710 | _context9.next = 10;
|
711 | return this._write(block.data.slice(sectorsToBytes(beginSectorId), sectorsToBytes(endSectorId)), sectorsToBytes(offset));
|
712 |
|
713 | case 10:
|
714 | _context9.next = 12;
|
715 | return this.readBlockBitmap(this.bitmapSize, blockAddr);
|
716 |
|
717 | case 12:
|
718 | bitmap = _context9.sent;
|
719 |
|
720 |
|
721 | for (i = beginSectorId; i < endSectorId; ++i) {
|
722 | mapSetBit(bitmap, i);
|
723 | }
|
724 |
|
725 | _context9.next = 16;
|
726 | return this.writeBlockBitmap(blockAddr, bitmap);
|
727 |
|
728 | case 16:
|
729 | case 'end':
|
730 | return _context9.stop();
|
731 | }
|
732 | }
|
733 | }, _callee9, this);
|
734 | }));
|
735 |
|
736 | function writeBlockSectors(_x8, _x9, _x10) {
|
737 | return _ref9.apply(this, arguments);
|
738 | }
|
739 |
|
740 | return writeBlockSectors;
|
741 | }()
|
742 |
|
743 |
|
744 |
|
745 | }, {
|
746 | key: 'coalesceBlock',
|
747 | value: function () {
|
748 | var _ref10 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee10(child, blockAddr, blockId) {
|
749 | var blockData, blockBitmap, sectorsPerBlock, i, sectors;
|
750 | return _regenerator2.default.wrap(function _callee10$(_context10) {
|
751 | while (1) {
|
752 | switch (_context10.prev = _context10.next) {
|
753 | case 0:
|
754 | _context10.next = 2;
|
755 | return child.readBlockData(blockAddr);
|
756 |
|
757 | case 2:
|
758 | blockData = _context10.sent;
|
759 | _context10.next = 5;
|
760 | return child.readBlockBitmap(blockAddr);
|
761 |
|
762 | case 5:
|
763 | blockBitmap = _context10.sent;
|
764 |
|
765 |
|
766 | debug('Coalesce block ' + blockId + ' at ' + blockAddr + '.');
|
767 |
|
768 |
|
769 | sectorsPerBlock = child.sectorsPerBlock;
|
770 | i = 0;
|
771 |
|
772 | case 9:
|
773 | if (!(i < sectorsPerBlock)) {
|
774 | _context10.next = 26;
|
775 | break;
|
776 | }
|
777 |
|
778 | if (mapTestBit(blockBitmap, i)) {
|
779 | _context10.next = 12;
|
780 | break;
|
781 | }
|
782 |
|
783 | return _context10.abrupt('continue', 23);
|
784 |
|
785 | case 12:
|
786 | sectors = 0;
|
787 |
|
788 |
|
789 |
|
790 | case 13:
|
791 | if (!(sectors + i < sectorsPerBlock)) {
|
792 | _context10.next = 19;
|
793 | break;
|
794 | }
|
795 |
|
796 | if (mapTestBit(blockBitmap, sectors + i)) {
|
797 | _context10.next = 16;
|
798 | break;
|
799 | }
|
800 |
|
801 | return _context10.abrupt('break', 19);
|
802 |
|
803 | case 16:
|
804 | sectors++;
|
805 | _context10.next = 13;
|
806 | break;
|
807 |
|
808 | case 19:
|
809 |
|
810 |
|
811 | debug('Coalesce block: write. (offset=' + i + ', sectors=' + sectors + ')');
|
812 | _context10.next = 22;
|
813 | return this.writeBlockSectors({ id: blockId, data: blockData }, i, sectors);
|
814 |
|
815 | case 22:
|
816 |
|
817 | i += sectors;
|
818 |
|
819 | case 23:
|
820 | i++;
|
821 | _context10.next = 9;
|
822 | break;
|
823 |
|
824 | case 26:
|
825 | case 'end':
|
826 | return _context10.stop();
|
827 | }
|
828 | }
|
829 | }, _callee10, this);
|
830 | }));
|
831 |
|
832 | function coalesceBlock(_x11, _x12, _x13) {
|
833 | return _ref10.apply(this, arguments);
|
834 | }
|
835 |
|
836 | return coalesceBlock;
|
837 | }()
|
838 |
|
839 |
|
840 |
|
841 | }, {
|
842 | key: 'writeFooter',
|
843 | value: function () {
|
844 | var _ref11 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee11() {
|
845 | var footer, offset, rawFooter;
|
846 | return _regenerator2.default.wrap(function _callee11$(_context11) {
|
847 | while (1) {
|
848 | switch (_context11.prev = _context11.next) {
|
849 | case 0:
|
850 | footer = this.footer;
|
851 | offset = this.getEndOfData();
|
852 | rawFooter = fuFooter.pack(footer);
|
853 |
|
854 |
|
855 | footer.checksum = checksumStruct(rawFooter, fuFooter.fields.checksum);
|
856 | debug('Write footer at: ' + offset + ' (checksum=' + footer.checksum + '). (data=' + rawFooter.toString('hex') + ')');
|
857 |
|
858 | _context11.next = 7;
|
859 | return this._write(rawFooter, 0);
|
860 |
|
861 | case 7:
|
862 | _context11.next = 9;
|
863 | return this._write(rawFooter, offset);
|
864 |
|
865 | case 9:
|
866 | case 'end':
|
867 | return _context11.stop();
|
868 | }
|
869 | }
|
870 | }, _callee11, this);
|
871 | }));
|
872 |
|
873 | function writeFooter() {
|
874 | return _ref11.apply(this, arguments);
|
875 | }
|
876 |
|
877 | return writeFooter;
|
878 | }()
|
879 | }, {
|
880 | key: 'writeHeader',
|
881 | value: function () {
|
882 | var _ref12 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee12() {
|
883 | var header, rawHeader, offset;
|
884 | return _regenerator2.default.wrap(function _callee12$(_context12) {
|
885 | while (1) {
|
886 | switch (_context12.prev = _context12.next) {
|
887 | case 0:
|
888 | header = this.header;
|
889 | rawHeader = fuHeader.pack(header);
|
890 |
|
891 | header.checksum = checksumStruct(rawHeader, fuHeader.fields.checksum);
|
892 | offset = VHD_FOOTER_SIZE;
|
893 |
|
894 | debug('Write header at: ' + offset + ' (checksum=' + header.checksum + '). (data=' + rawHeader.toString('hex') + ')');
|
895 | _context12.next = 7;
|
896 | return this._write(rawHeader, offset);
|
897 |
|
898 | case 7:
|
899 | case 'end':
|
900 | return _context12.stop();
|
901 | }
|
902 | }
|
903 | }, _callee12, this);
|
904 | }));
|
905 |
|
906 | function writeHeader() {
|
907 | return _ref12.apply(this, arguments);
|
908 | }
|
909 |
|
910 | return writeHeader;
|
911 | }()
|
912 | }]);
|
913 | return Vhd;
|
914 | }();
|
915 |
|
\ | No newline at end of file |