"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/asar/lib/disk.js" (8 Mar 2017, 4254 Bytes) of archive /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 (function() {
    2   var Filesystem, copyFileToSync, filesystemCache, fs, mkdirp, path, pickle, writeFileListToStream;
    3 
    4   fs = require('fs');
    5 
    6   path = require('path');
    7 
    8   mkdirp = require('mkdirp');
    9 
   10   pickle = require('chromium-pickle-js');
   11 
   12   Filesystem = require('./filesystem');
   13 
   14   filesystemCache = {};
   15 
   16   copyFileToSync = function(dest, src, filename) {
   17     var content, srcFile, stats, targetFile;
   18     srcFile = path.join(src, filename);
   19     targetFile = path.join(dest, filename);
   20     content = fs.readFileSync(srcFile);
   21     stats = fs.statSync(srcFile);
   22     mkdirp.sync(path.dirname(targetFile));
   23     return fs.writeFileSync(targetFile, content, {
   24       mode: stats.mode
   25     });
   26   };
   27 
   28   writeFileListToStream = function(dest, filesystem, out, list, metadata, callback) {
   29     var error, file, filename, stream, tr;
   30     if (list.length === 0) {
   31       out.end();
   32       return callback(null);
   33     }
   34     file = list[0];
   35     if (file.unpack) {
   36       filename = path.relative(filesystem.src, file.filename);
   37       try {
   38         copyFileToSync("" + dest + ".unpacked", filesystem.src, filename);
   39       } catch (_error) {
   40         error = _error;
   41         return callback(error);
   42       }
   43       return writeFileListToStream(dest, filesystem, out, list.slice(1), metadata, callback);
   44     } else {
   45       tr = metadata[file.filename].transformed;
   46       stream = fs.createReadStream((tr ? tr.path : file.filename));
   47       stream.pipe(out, {
   48         end: false
   49       });
   50       stream.on('error', callback);
   51       return stream.on('end', function() {
   52         return writeFileListToStream(dest, filesystem, out, list.slice(1), metadata, callback);
   53       });
   54     }
   55   };
   56 
   57   module.exports.writeFilesystem = function(dest, filesystem, files, metadata, callback) {
   58     var error, headerBuf, headerPickle, out, sizeBuf, sizePickle;
   59     try {
   60       headerPickle = pickle.createEmpty();
   61       headerPickle.writeString(JSON.stringify(filesystem.header));
   62       headerBuf = headerPickle.toBuffer();
   63       sizePickle = pickle.createEmpty();
   64       sizePickle.writeUInt32(headerBuf.length);
   65       sizeBuf = sizePickle.toBuffer();
   66     } catch (_error) {
   67       error = _error;
   68       return callback(error);
   69     }
   70     out = fs.createWriteStream(dest);
   71     out.on('error', callback);
   72     out.write(sizeBuf);
   73     return out.write(headerBuf, function() {
   74       return writeFileListToStream(dest, filesystem, out, files, metadata, callback);
   75     });
   76   };
   77 
   78   module.exports.readArchiveHeaderSync = function(archive) {
   79     var fd, header, headerBuf, headerPickle, size, sizeBuf, sizePickle;
   80     fd = fs.openSync(archive, 'r');
   81     try {
   82       sizeBuf = new Buffer(8);
   83       if (fs.readSync(fd, sizeBuf, 0, 8, null) !== 8) {
   84         throw new Error('Unable to read header size');
   85       }
   86       sizePickle = pickle.createFromBuffer(sizeBuf);
   87       size = sizePickle.createIterator().readUInt32();
   88       headerBuf = new Buffer(size);
   89       if (fs.readSync(fd, headerBuf, 0, size, null) !== size) {
   90         throw new Error('Unable to read header');
   91       }
   92     } finally {
   93       fs.closeSync(fd);
   94     }
   95     headerPickle = pickle.createFromBuffer(headerBuf);
   96     header = headerPickle.createIterator().readString();
   97     return {
   98       header: JSON.parse(header),
   99       headerSize: size
  100     };
  101   };
  102 
  103   module.exports.readFilesystemSync = function(archive) {
  104     var filesystem, header;
  105     if (!filesystemCache[archive]) {
  106       header = this.readArchiveHeaderSync(archive);
  107       filesystem = new Filesystem(archive);
  108       filesystem.header = header.header;
  109       filesystem.headerSize = header.headerSize;
  110       filesystemCache[archive] = filesystem;
  111     }
  112     return filesystemCache[archive];
  113   };
  114 
  115   module.exports.readFileSync = function(filesystem, filename, info) {
  116     var buffer, fd, offset;
  117     buffer = new Buffer(info.size);
  118     if (info.size <= 0) {
  119       return buffer;
  120     }
  121     if (info.unpacked) {
  122       buffer = fs.readFileSync(path.join("" + filesystem.src + ".unpacked", filename));
  123     } else {
  124       fd = fs.openSync(filesystem.src, 'r');
  125       try {
  126         offset = 8 + filesystem.headerSize + parseInt(info.offset);
  127         fs.readSync(fd, buffer, 0, info.size, offset);
  128       } finally {
  129         fs.closeSync(fd);
  130       }
  131     }
  132     return buffer;
  133   };
  134 
  135 }).call(this);