"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/asar/lib/asar.js" (11 Apr 2017, 7508 Bytes) of package /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, crawlFilesystem, createSnapshot, disk, fs, isUnpackDir, minimatch, mkdirp, os, path;
    3 
    4   fs = require('fs');
    5 
    6   path = require('path');
    7 
    8   os = require('os');
    9 
   10   minimatch = require('minimatch');
   11 
   12   mkdirp = require('mkdirp');
   13 
   14   Filesystem = require('./filesystem');
   15 
   16   disk = require('./disk');
   17 
   18   crawlFilesystem = require('./crawlfs');
   19 
   20   createSnapshot = require('./snapshot');
   21 
   22   isUnpackDir = function(path, pattern) {
   23     return path.indexOf(pattern) === 0 || minimatch(path, pattern);
   24   };
   25 
   26   module.exports.createPackage = function(src, dest, callback) {
   27     return module.exports.createPackageWithOptions(src, dest, {}, callback);
   28   };
   29 
   30   module.exports.createPackageWithOptions = function(src, dest, options, callback) {
   31     var dot;
   32     dot = options.dot;
   33     if (dot === void 0) {
   34       dot = true;
   35     }
   36     return crawlFilesystem(src, {
   37       dot: dot
   38     }, function(error, filenames, metadata) {
   39       if (error) {
   40         return callback(error);
   41       }
   42       module.exports.createPackageFromFiles(src, dest, filenames, metadata, options, callback);
   43     });
   44   };
   45 
   46   /*
   47   createPackageFromFiles - Create an asar-archive from a list of filenames
   48   src: Base path. All files are relative to this.
   49   dest: Archive filename (& path).
   50   filenames: Array of filenames relative to src.
   51   metadata: Object with filenames as keys and {type='directory|file|link', stat: fs.stat} as values. (Optional)
   52   options: The options.
   53   callback: The callback function. Accepts (err).
   54   */
   55 
   56 
   57   module.exports.createPackageFromFiles = function(src, dest, filenames, metadata, options, callback) {
   58     var file, filenamesSorted, files, filesystem, handleFile, insertsDone, missing, names, next, ordering, orderingFiles, pathComponent, pathComponents, str, total, _i, _j, _k, _l, _len, _len1, _len2, _len3;
   59     if (metadata == null) {
   60       metadata = {};
   61     }
   62     filesystem = new Filesystem(src);
   63     files = [];
   64     if (options.ordering) {
   65       orderingFiles = fs.readFileSync(options.ordering).toString().split('\n').map(function(line) {
   66         if (line.indexOf(':') !== -1) {
   67           line = line.split(':').pop();
   68         }
   69         line = line.trim();
   70         if (line[0] === '/') {
   71           line = line.slice(1);
   72         }
   73         return line;
   74       });
   75       ordering = [];
   76       for (_i = 0, _len = orderingFiles.length; _i < _len; _i++) {
   77         file = orderingFiles[_i];
   78         pathComponents = file.split(path.sep);
   79         str = src;
   80         for (_j = 0, _len1 = pathComponents.length; _j < _len1; _j++) {
   81           pathComponent = pathComponents[_j];
   82           str = path.join(str, pathComponent);
   83           ordering.push(str);
   84         }
   85       }
   86       filenamesSorted = [];
   87       missing = 0;
   88       total = filenames.length;
   89       for (_k = 0, _len2 = ordering.length; _k < _len2; _k++) {
   90         file = ordering[_k];
   91         if (filenamesSorted.indexOf(file) === -1 && filenames.indexOf(file) !== -1) {
   92           filenamesSorted.push(file);
   93         }
   94       }
   95       for (_l = 0, _len3 = filenames.length; _l < _len3; _l++) {
   96         file = filenames[_l];
   97         if (filenamesSorted.indexOf(file) === -1) {
   98           filenamesSorted.push(file);
   99           missing += 1;
  100         }
  101       }
  102       console.log("Ordering file has " + ((total - missing) / total * 100) + "% coverage.");
  103     } else {
  104       filenamesSorted = filenames;
  105     }
  106     handleFile = function(filename, done) {
  107       var dirName, shouldUnpack, stat, type;
  108       file = metadata[filename];
  109       if (!file) {
  110         stat = fs.lstatSync(filename);
  111         if (stat.isDirectory()) {
  112           type = 'directory';
  113         }
  114         if (stat.isFile()) {
  115           type = 'file';
  116         }
  117         if (stat.isSymbolicLink()) {
  118           type = 'link';
  119         }
  120         file = {
  121           stat: stat,
  122           type: type
  123         };
  124       }
  125       switch (file.type) {
  126         case 'directory':
  127           shouldUnpack = options.unpackDir ? isUnpackDir(path.relative(src, filename), options.unpackDir) : false;
  128           filesystem.insertDirectory(filename, shouldUnpack);
  129           break;
  130         case 'file':
  131           shouldUnpack = false;
  132           if (options.unpack) {
  133             shouldUnpack = minimatch(filename, options.unpack, {
  134               matchBase: true
  135             });
  136           }
  137           if (!shouldUnpack && options.unpackDir) {
  138             dirName = path.relative(src, path.dirname(filename));
  139             shouldUnpack = isUnpackDir(dirName, options.unpackDir);
  140           }
  141           files.push({
  142             filename: filename,
  143             unpack: shouldUnpack
  144           });
  145           filesystem.insertFile(filename, shouldUnpack, file, options, done);
  146           return;
  147         case 'link':
  148           filesystem.insertLink(filename, file.stat);
  149       }
  150       return process.nextTick(done);
  151     };
  152     insertsDone = function() {
  153       return mkdirp(path.dirname(dest), function(error) {
  154         if (error) {
  155           return callback(error);
  156         }
  157         return disk.writeFilesystem(dest, filesystem, files, metadata, function(error) {
  158           if (error) {
  159             return callback(error);
  160           }
  161           if (options.snapshot) {
  162             return createSnapshot(src, dest, filenames, metadata, options, callback);
  163           } else {
  164             return callback(null);
  165           }
  166         });
  167       });
  168     };
  169     names = filenamesSorted.slice();
  170     next = function(name) {
  171       if (!name) {
  172         return insertsDone();
  173       }
  174       return handleFile(name, function() {
  175         return next(names.shift());
  176       });
  177     };
  178     return next(names.shift());
  179   };
  180 
  181   module.exports.statFile = function(archive, filename, followLinks) {
  182     var filesystem;
  183     filesystem = disk.readFilesystemSync(archive);
  184     return filesystem.getFile(filename, followLinks);
  185   };
  186 
  187   module.exports.listPackage = function(archive) {
  188     return disk.readFilesystemSync(archive).listFiles();
  189   };
  190 
  191   module.exports.extractFile = function(archive, filename) {
  192     var filesystem;
  193     filesystem = disk.readFilesystemSync(archive);
  194     return disk.readFileSync(filesystem, filename, filesystem.getFile(filename));
  195   };
  196 
  197   module.exports.extractAll = function(archive, dest) {
  198     var content, destFilename, error, file, filename, filenames, filesystem, followLinks, linkDestPath, linkSrcPath, linkTo, relativePath, _i, _len, _results;
  199     filesystem = disk.readFilesystemSync(archive);
  200     filenames = filesystem.listFiles();
  201     followLinks = process.platform === 'win32';
  202     mkdirp.sync(dest);
  203     _results = [];
  204     for (_i = 0, _len = filenames.length; _i < _len; _i++) {
  205       filename = filenames[_i];
  206       filename = filename.substr(1);
  207       destFilename = path.join(dest, filename);
  208       file = filesystem.getFile(filename, followLinks);
  209       if (file.files) {
  210         _results.push(mkdirp.sync(destFilename));
  211       } else if (file.link) {
  212         linkSrcPath = path.dirname(path.join(dest, file.link));
  213         linkDestPath = path.dirname(destFilename);
  214         relativePath = path.relative(linkDestPath, linkSrcPath);
  215         try {
  216           fs.unlinkSync(destFilename);
  217         } catch (_error) {
  218           error = _error;
  219         }
  220         linkTo = path.join(relativePath, path.basename(file.link));
  221         _results.push(fs.symlinkSync(linkTo, destFilename));
  222       } else {
  223         content = disk.readFileSync(filesystem, filename, file);
  224         _results.push(fs.writeFileSync(destFilename, content));
  225       }
  226     }
  227     return _results;
  228   };
  229 
  230 }).call(this);