]> khamidou — GitHub mirrors - electron.git/commitdiff
Enable wrap arbitrary fs object
authorCheng Zhao <zcbenz@gmail.com>
Sun, 1 Feb 2015 07:31:14 +0000 (23:31 -0800)
committerCheng Zhao <zcbenz@gmail.com>
Mon, 2 Feb 2015 21:35:32 +0000 (13:35 -0800)
atom/common/lib/asar.coffee
atom/common/lib/init.coffee

index eb56bb98c26d7e8bd89dc00a5e9c16c9be3655be..713b6179887286d9062a2c7c98f415915c85636d 100644 (file)
@@ -1,6 +1,5 @@
 asar = process.atomBinding 'asar'
 child_process = require 'child_process'
-fs = require 'fs'
 path = require 'path'
 util = require 'util'
 
@@ -60,242 +59,243 @@ createNotFoundError = (asarPath, filePath) ->
   error.errno = -2
   error
 
-# Override fs APIs.
-lstatSync = fs.lstatSync
-fs.lstatSync = (p) ->
-  [isAsar, asarPath, filePath] = splitPath p
-  return lstatSync p unless isAsar
-
-  archive = getOrCreateArchive asarPath
-  throw new Error("Invalid package #{asarPath}") unless archive
-
-  stats = archive.stat filePath
-  throw createNotFoundError(asarPath, filePath) unless stats
-
-  asarStatsToFsStats stats
-
-lstat = fs.lstat
-fs.lstat = (p, callback) ->
-  [isAsar, asarPath, filePath] = splitPath p
-  return lstat p, callback unless isAsar
-
-  archive = getOrCreateArchive asarPath
-  return callback new Error("Invalid package #{asarPath}") unless archive
-
-  stats = getOrCreateArchive(asarPath).stat filePath
-  return callback createNotFoundError(asarPath, filePath) unless stats
-
-  process.nextTick -> callback null, asarStatsToFsStats stats
+# Override APIs that rely on passing file path instead of content to C++.
+overrideAPISync = (module, name, arg = 0) ->
+  old = module[name]
+  module[name] = ->
+    p = arguments[arg]
+    [isAsar, asarPath, filePath] = splitPath p
+    return old.apply this, arguments unless isAsar
 
-statSync = fs.statSync
-fs.statSync = (p) ->
-  [isAsar, asarPath, filePath] = splitPath p
-  return statSync p unless isAsar
+    archive = getOrCreateArchive asarPath
+    throw new Error("Invalid package #{asarPath}") unless archive
 
-  # Do not distinguish links for now.
-  fs.lstatSync p
+    newPath = archive.copyFileOut filePath
+    throw createNotFoundError(asarPath, filePath) unless newPath
 
-stat = fs.stat
-fs.stat = (p, callback) ->
-  [isAsar, asarPath, filePath] = splitPath p
-  return stat p, callback unless isAsar
+    arguments[arg] = newPath
+    old.apply this, arguments
 
-  # Do not distinguish links for now.
-  process.nextTick -> fs.lstat p, callback
+overrideAPI = (module, name, arg = 0) ->
+  old = module[name]
+  module[name] = ->
+    p = arguments[arg]
+    [isAsar, asarPath, filePath] = splitPath p
+    return old.apply this, arguments unless isAsar
 
-statSyncNoException = fs.statSyncNoException
-fs.statSyncNoException = (p) ->
-  [isAsar, asarPath, filePath] = splitPath p
-  return statSyncNoException p unless isAsar
+    callback = arguments[arguments.length - 1]
+    return overrideAPISync module, name, arg unless typeof callback is 'function'
 
-  archive = getOrCreateArchive asarPath
-  return false unless archive
-  stats = archive.stat filePath
-  return false unless stats
-  asarStatsToFsStats stats
+    archive = getOrCreateArchive asarPath
+    return callback new Error("Invalid package #{asarPath}") unless archive
 
-realpathSync = fs.realpathSync
-fs.realpathSync = (p) ->
-  [isAsar, asarPath, filePath] = splitPath p
-  return realpathSync.apply this, arguments unless isAsar
+    newPath = archive.copyFileOut filePath
+    return callback  createNotFoundError(asarPath, filePath) unless newPath
 
-  archive = getOrCreateArchive asarPath
-  throw new Error("Invalid package #{asarPath}") unless archive
+    arguments[arg] = newPath
+    old.apply this, arguments
 
-  real = archive.realpath filePath
-  throw createNotFoundError(asarPath, filePath) if real is false
+# Override fs APIs.
+exports.wrapFsWithAsar = (fs) ->
+  lstatSync = fs.lstatSync
+  fs.lstatSync = (p) ->
+    [isAsar, asarPath, filePath] = splitPath p
+    return lstatSync p unless isAsar
 
-  path.join realpathSync(asarPath), real
+    archive = getOrCreateArchive asarPath
+    throw new Error("Invalid package #{asarPath}") unless archive
 
-realpath = fs.realpath
-fs.realpath = (p, cache, callback) ->
-  [isAsar, asarPath, filePath] = splitPath p
-  return realpath.apply this, arguments unless isAsar
+    stats = archive.stat filePath
+    throw createNotFoundError(asarPath, filePath) unless stats
 
-  if typeof cache is 'function'
-    callback = cache
-    cache = undefined
+    asarStatsToFsStats stats
 
-  archive = getOrCreateArchive asarPath
-  return callback new Error("Invalid package #{asarPath}") unless archive
+  lstat = fs.lstat
+  fs.lstat = (p, callback) ->
+    [isAsar, asarPath, filePath] = splitPath p
+    return lstat p, callback unless isAsar
 
-  real = archive.realpath filePath
-  return callback createNotFoundError(asarPath, filePath) if real is false
+    archive = getOrCreateArchive asarPath
+    return callback new Error("Invalid package #{asarPath}") unless archive
 
-  realpath asarPath, (err, p) ->
-    return callback err if err
-    callback null, path.join(p, real)
+    stats = getOrCreateArchive(asarPath).stat filePath
+    return callback createNotFoundError(asarPath, filePath) unless stats
 
-exists = fs.exists
-fs.exists = (p, callback) ->
-  [isAsar, asarPath, filePath] = splitPath p
-  return exists p, callback unless isAsar
+    process.nextTick -> callback null, asarStatsToFsStats stats
 
-  archive = getOrCreateArchive asarPath
-  return callback new Error("Invalid package #{asarPath}") unless archive
+  statSync = fs.statSync
+  fs.statSync = (p) ->
+    [isAsar, asarPath, filePath] = splitPath p
+    return statSync p unless isAsar
 
-  process.nextTick -> callback archive.stat(filePath) isnt false
+    # Do not distinguish links for now.
+    fs.lstatSync p
 
-existsSync = fs.existsSync
-fs.existsSync = (p) ->
-  [isAsar, asarPath, filePath] = splitPath p
-  return existsSync p unless isAsar
+  stat = fs.stat
+  fs.stat = (p, callback) ->
+    [isAsar, asarPath, filePath] = splitPath p
+    return stat p, callback unless isAsar
 
-  archive = getOrCreateArchive asarPath
-  return false unless archive
+    # Do not distinguish links for now.
+    process.nextTick -> fs.lstat p, callback
 
-  archive.stat(filePath) isnt false
+  statSyncNoException = fs.statSyncNoException
+  fs.statSyncNoException = (p) ->
+    [isAsar, asarPath, filePath] = splitPath p
+    return statSyncNoException p unless isAsar
 
-open = fs.open
-readFile = fs.readFile
-fs.readFile = (p, options, callback) ->
-  [isAsar, asarPath, filePath] = splitPath p
-  return readFile.apply this, arguments unless isAsar
+    archive = getOrCreateArchive asarPath
+    return false unless archive
+    stats = archive.stat filePath
+    return false unless stats
+    asarStatsToFsStats stats
 
-  if typeof options is 'function'
-    callback = options
-    options = undefined
+  realpathSync = fs.realpathSync
+  fs.realpathSync = (p) ->
+    [isAsar, asarPath, filePath] = splitPath p
+    return realpathSync.apply this, arguments unless isAsar
 
-  archive = getOrCreateArchive asarPath
-  return callback new Error("Invalid package #{asarPath}") unless archive
+    archive = getOrCreateArchive asarPath
+    throw new Error("Invalid package #{asarPath}") unless archive
 
-  info = archive.getFileInfo filePath
-  return callback createNotFoundError(asarPath, filePath) unless info
+    real = archive.realpath filePath
+    throw createNotFoundError(asarPath, filePath) if real is false
 
-  if not options
-    options = encoding: null, flag: 'r'
-  else if util.isString options
-    options = encoding: options, flag: 'r'
-  else if not util.isObject options
-    throw new TypeError('Bad arguments')
+    path.join realpathSync(asarPath), real
 
-  flag = options.flag || 'r'
-  encoding = options.encoding
+  realpath = fs.realpath
+  fs.realpath = (p, cache, callback) ->
+    [isAsar, asarPath, filePath] = splitPath p
+    return realpath.apply this, arguments unless isAsar
 
-  buffer = new Buffer(info.size)
-  open archive.path, flag, (error, fd) ->
-    return callback error if error
-    fs.read fd, buffer, 0, info.size, info.offset, (error) ->
-      fs.close fd, ->
-        callback error, if encoding then buffer.toString encoding else buffer
+    if typeof cache is 'function'
+      callback = cache
+      cache = undefined
 
-openSync = fs.openSync
-readFileSync = fs.readFileSync
-fs.readFileSync = (p, options) ->
-  [isAsar, asarPath, filePath] = splitPath p
-  return readFileSync.apply this, arguments unless isAsar
+    archive = getOrCreateArchive asarPath
+    return callback new Error("Invalid package #{asarPath}") unless archive
 
-  archive = getOrCreateArchive asarPath
-  throw new Error("Invalid package #{asarPath}") unless archive
+    real = archive.realpath filePath
+    return callback createNotFoundError(asarPath, filePath) if real is false
 
-  info = archive.getFileInfo filePath
-  throw createNotFoundError(asarPath, filePath) unless info
+    realpath asarPath, (err, p) ->
+      return callback err if err
+      callback null, path.join(p, real)
 
-  if not options
-    options = encoding: null, flag: 'r'
-  else if util.isString options
-    options = encoding: options, flag: 'r'
-  else if not util.isObject options
-    throw new TypeError('Bad arguments')
+  exists = fs.exists
+  fs.exists = (p, callback) ->
+    [isAsar, asarPath, filePath] = splitPath p
+    return exists p, callback unless isAsar
 
-  flag = options.flag || 'r'
-  encoding = options.encoding
+    archive = getOrCreateArchive asarPath
+    return callback new Error("Invalid package #{asarPath}") unless archive
 
-  buffer = new Buffer(info.size)
-  fd = openSync archive.path, flag
-  try
-    fs.readSync fd, buffer, 0, info.size, info.offset
-  catch e
-    throw e
-  finally
-    fs.closeSync fd
-  if encoding then buffer.toString encoding else buffer
+    process.nextTick -> callback archive.stat(filePath) isnt false
 
-readdir = fs.readdir
-fs.readdir = (p, callback) ->
-  [isAsar, asarPath, filePath] = splitPath p
-  return readdir.apply this, arguments unless isAsar
+  existsSync = fs.existsSync
+  fs.existsSync = (p) ->
+    [isAsar, asarPath, filePath] = splitPath p
+    return existsSync p unless isAsar
 
-  archive = getOrCreateArchive asarPath
-  return callback new Error("Invalid package #{asarPath}") unless archive
+    archive = getOrCreateArchive asarPath
+    return false unless archive
 
-  files = archive.readdir filePath
-  return callback createNotFoundError(asarPath, filePath) unless files
+    archive.stat(filePath) isnt false
 
-  process.nextTick -> callback null, files
+  open = fs.open
+  readFile = fs.readFile
+  fs.readFile = (p, options, callback) ->
+    [isAsar, asarPath, filePath] = splitPath p
+    return readFile.apply this, arguments unless isAsar
 
-readdirSync = fs.readdirSync
-fs.readdirSync = (p) ->
-  [isAsar, asarPath, filePath] = splitPath p
-  return readdirSync.apply this, arguments unless isAsar
+    if typeof options is 'function'
+      callback = options
+      options = undefined
 
-  archive = getOrCreateArchive asarPath
-  throw new Error("Invalid package #{asarPath}") unless archive
+    archive = getOrCreateArchive asarPath
+    return callback new Error("Invalid package #{asarPath}") unless archive
 
-  files = archive.readdir filePath
-  throw createNotFoundError(asarPath, filePath) unless files
+    info = archive.getFileInfo filePath
+    return callback createNotFoundError(asarPath, filePath) unless info
+
+    if not options
+      options = encoding: null, flag: 'r'
+    else if util.isString options
+      options = encoding: options, flag: 'r'
+    else if not util.isObject options
+      throw new TypeError('Bad arguments')
+
+    flag = options.flag || 'r'
+    encoding = options.encoding
+
+    buffer = new Buffer(info.size)
+    open archive.path, flag, (error, fd) ->
+      return callback error if error
+      fs.read fd, buffer, 0, info.size, info.offset, (error) ->
+        fs.close fd, ->
+          callback error, if encoding then buffer.toString encoding else buffer
+
+  openSync = fs.openSync
+  readFileSync = fs.readFileSync
+  fs.readFileSync = (p, options) ->
+    [isAsar, asarPath, filePath] = splitPath p
+    return readFileSync.apply this, arguments unless isAsar
 
-  files
+    archive = getOrCreateArchive asarPath
+    throw new Error("Invalid package #{asarPath}") unless archive
 
-# Override APIs that rely on passing file path instead of content to C++.
-overrideAPISync = (module, name, arg = 0) ->
-  old = module[name]
-  module[name] = ->
-    p = arguments[arg]
+    info = archive.getFileInfo filePath
+    throw createNotFoundError(asarPath, filePath) unless info
+
+    if not options
+      options = encoding: null, flag: 'r'
+    else if util.isString options
+      options = encoding: options, flag: 'r'
+    else if not util.isObject options
+      throw new TypeError('Bad arguments')
+
+    flag = options.flag || 'r'
+    encoding = options.encoding
+
+    buffer = new Buffer(info.size)
+    fd = openSync archive.path, flag
+    try
+      fs.readSync fd, buffer, 0, info.size, info.offset
+    catch e
+      throw e
+    finally
+      fs.closeSync fd
+    if encoding then buffer.toString encoding else buffer
+
+  readdir = fs.readdir
+  fs.readdir = (p, callback) ->
     [isAsar, asarPath, filePath] = splitPath p
-    return old.apply this, arguments unless isAsar
+    return readdir.apply this, arguments unless isAsar
 
     archive = getOrCreateArchive asarPath
-    throw new Error("Invalid package #{asarPath}") unless archive
+    return callback new Error("Invalid package #{asarPath}") unless archive
 
-    newPath = archive.copyFileOut filePath
-    throw createNotFoundError(asarPath, filePath) unless newPath
+    files = archive.readdir filePath
+    return callback createNotFoundError(asarPath, filePath) unless files
 
-    arguments[arg] = newPath
-    old.apply this, arguments
+    process.nextTick -> callback null, files
 
-overrideAPI = (module, name, arg = 0) ->
-  old = module[name]
-  module[name] = ->
-    p = arguments[arg]
+  readdirSync = fs.readdirSync
+  fs.readdirSync = (p) ->
     [isAsar, asarPath, filePath] = splitPath p
-    return old.apply this, arguments unless isAsar
-
-    callback = arguments[arguments.length - 1]
-    return overrideAPISync module, name, arg unless typeof callback is 'function'
+    return readdirSync.apply this, arguments unless isAsar
 
     archive = getOrCreateArchive asarPath
-    return callback new Error("Invalid package #{asarPath}") unless archive
+    throw new Error("Invalid package #{asarPath}") unless archive
 
-    newPath = archive.copyFileOut filePath
-    return callback  createNotFoundError(asarPath, filePath) unless newPath
+    files = archive.readdir filePath
+    throw createNotFoundError(asarPath, filePath) unless files
 
-    arguments[arg] = newPath
-    old.apply this, arguments
+    files
 
-overrideAPI fs, 'open'
-overrideAPI child_process, 'execFile'
-overrideAPISync process, 'dlopen', 1
-overrideAPISync require('module')._extensions, '.node', 1
-overrideAPISync fs, 'openSync'
-overrideAPISync child_process, 'fork'
+  overrideAPI fs, 'open'
+  overrideAPI child_process, 'execFile'
+  overrideAPISync process, 'dlopen', 1
+  overrideAPISync require('module')._extensions, '.node', 1
+  overrideAPISync fs, 'openSync'
+  overrideAPISync child_process, 'fork'
index b8d67355c05fab76d09de789c1824b3884a90524..2d7fc73b994ec1b43f58078433e7df77640b879a 100644 (file)
@@ -1,4 +1,5 @@
 process = global.process
+fs      = require 'fs'
 path    = require 'path'
 timers  = require 'timers'
 Module  = require 'module'
@@ -36,4 +37,5 @@ if process.type is 'browser'
   global.setInterval = wrapWithActivateUvLoop timers.setInterval
 
 # Add support for asar packages.
-require './asar'
+asar = require './asar'
+asar.wrapFsWithAsar fs