]> khamidou — GitHub mirrors - electron.git/commitdiff
Make asar work with internalModuleReadFile and internalModuleStat
authorCheng Zhao <zcbenz@gmail.com>
Wed, 10 Jun 2015 08:18:55 +0000 (16:18 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Wed, 10 Jun 2015 08:18:55 +0000 (16:18 +0800)
atom/common/lib/asar.coffee

index a17ac729253d6cf75ab6a7971fac622bf2701c8e..7ce89156285d9cf89f36b6a54a0f5415699dbdba 100644 (file)
@@ -310,6 +310,38 @@ exports.wrapFsWithAsar = (fs) ->
 
     files
 
+  internalModuleReadFile = process.binding('fs').internalModuleReadFile
+  process.binding('fs').internalModuleReadFile = (p) ->
+    [isAsar, asarPath, filePath] = splitPath p
+    return internalModuleReadFile p unless isAsar
+
+    archive = getOrCreateArchive asarPath
+    return undefined unless archive
+
+    info = archive.getFileInfo filePath
+    return undefined unless info
+    return '' if info.size is 0
+
+    buffer = new Buffer(info.size)
+    fd = archive.getFd()
+    retrun undefined unless fd >= 0
+
+    fs.readSync fd, buffer, 0, info.size, info.offset
+    buffer.toString 'utf8'
+
+  internalModuleStat = process.binding('fs').internalModuleStat
+  process.binding('fs').internalModuleStat = (p) ->
+    [isAsar, asarPath, filePath] = splitPath p
+    return internalModuleStat p unless isAsar
+
+    archive = getOrCreateArchive asarPath
+    return -34 unless archive  # -ENOENT
+
+    stats = archive.stat filePath
+    return -34 unless stats  # -ENOENT
+
+    if stats.isDirectory then return 1 else return 0
+
   overrideAPI fs, 'open'
   overrideAPI child_process, 'execFile'
   overrideAPISync process, 'dlopen', 1