"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/asar/lib/filesystem.js" (7 Feb 2017, 4502 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, UINT64, fs, path, tmp;
    3 
    4   fs = require('fs');
    5 
    6   path = require('path');
    7 
    8   tmp = require('tmp');
    9 
   10   UINT64 = require('cuint').UINT64;
   11 
   12   Filesystem = (function() {
   13     function Filesystem(src) {
   14       this.src = path.resolve(src);
   15       this.header = {
   16         files: {}
   17       };
   18       this.offset = UINT64(0);
   19     }
   20 
   21     Filesystem.prototype.searchNodeFromDirectory = function(p) {
   22       var dir, dirs, json, _i, _len;
   23       json = this.header;
   24       dirs = p.split(path.sep);
   25       for (_i = 0, _len = dirs.length; _i < _len; _i++) {
   26         dir = dirs[_i];
   27         if (dir !== '.') {
   28           json = json.files[dir];
   29         }
   30       }
   31       return json;
   32     };
   33 
   34     Filesystem.prototype.searchNodeFromPath = function(p) {
   35       var name, node, _base;
   36       p = path.relative(this.src, p);
   37       if (!p) {
   38         return this.header;
   39       }
   40       name = path.basename(p);
   41       node = this.searchNodeFromDirectory(path.dirname(p));
   42       if (node.files == null) {
   43         node.files = {};
   44       }
   45       if ((_base = node.files)[name] == null) {
   46         _base[name] = {};
   47       }
   48       return node.files[name];
   49     };
   50 
   51     Filesystem.prototype.insertDirectory = function(p, shouldUnpack) {
   52       var node;
   53       node = this.searchNodeFromPath(p);
   54       if (shouldUnpack) {
   55         node.unpacked = shouldUnpack;
   56       }
   57       return node.files = {};
   58     };
   59 
   60     Filesystem.prototype.insertFile = function(p, shouldUnpack, file, options, callback) {
   61       var dirNode, handler, node, tr,
   62         _this = this;
   63       dirNode = this.searchNodeFromPath(path.dirname(p));
   64       node = this.searchNodeFromPath(p);
   65       if (shouldUnpack || dirNode.unpacked) {
   66         node.size = file.stat.size;
   67         node.unpacked = true;
   68         process.nextTick(callback);
   69         return;
   70       }
   71       handler = function() {
   72         var size;
   73         size = file.transformed ? file.transformed.stat.size : file.stat.size;
   74         if (size > 4294967295) {
   75           throw new Error("" + p + ": file size can not be larger than 4.2GB");
   76         }
   77         node.size = size;
   78         node.offset = _this.offset.toString();
   79         if (process.platform !== 'win32' && file.stat.mode & 0x40) {
   80           node.executable = true;
   81         }
   82         _this.offset.add(UINT64(size));
   83         return callback();
   84       };
   85       tr = options.transform && options.transform(p);
   86       if (tr) {
   87         return tmp.file(function(err, path) {
   88           var out, stream;
   89           if (err) {
   90             return handler();
   91           }
   92           out = fs.createWriteStream(path);
   93           stream = fs.createReadStream(p);
   94           stream.pipe(tr).pipe(out);
   95           return tr.on('end', function() {
   96             file.transformed = {
   97               path: path,
   98               stat: fs.lstatSync(path)
   99             };
  100             return handler();
  101           });
  102         });
  103       } else {
  104         return process.nextTick(handler);
  105       }
  106     };
  107 
  108     Filesystem.prototype.insertLink = function(p, stat) {
  109       var link, node;
  110       link = path.relative(fs.realpathSync(this.src), fs.realpathSync(p));
  111       if (link.substr(0, 2) === '..') {
  112         throw new Error("" + p + ": file links out of the package");
  113       }
  114       node = this.searchNodeFromPath(p);
  115       return node.link = link;
  116     };
  117 
  118     Filesystem.prototype.listFiles = function() {
  119       var files, fillFilesFromHeader;
  120       files = [];
  121       fillFilesFromHeader = function(p, json) {
  122         var f, fullPath, _results;
  123         if (!json.files) {
  124           return;
  125         }
  126         _results = [];
  127         for (f in json.files) {
  128           fullPath = path.join(p, f);
  129           files.push(fullPath);
  130           _results.push(fillFilesFromHeader(fullPath, json.files[f]));
  131         }
  132         return _results;
  133       };
  134       fillFilesFromHeader('/', this.header);
  135       return files;
  136     };
  137 
  138     Filesystem.prototype.getNode = function(p) {
  139       var name, node;
  140       node = this.searchNodeFromDirectory(path.dirname(p));
  141       name = path.basename(p);
  142       if (name) {
  143         return node.files[name];
  144       } else {
  145         return node;
  146       }
  147     };
  148 
  149     Filesystem.prototype.getFile = function(p, followLinks) {
  150       var info;
  151       if (followLinks == null) {
  152         followLinks = true;
  153       }
  154       info = this.getNode(p);
  155       if (info.link && followLinks) {
  156         return this.getFile(info.link);
  157       } else {
  158         return info;
  159       }
  160     };
  161 
  162     return Filesystem;
  163 
  164   })();
  165 
  166   module.exports = Filesystem;
  167 
  168 }).call(this);