Browse Source

Add asar-supported fs.accessSync implementation

Kevin Sawicki 8 years ago
parent
commit
30fbe92970
2 changed files with 53 additions and 0 deletions
  1. 30 0
      lib/common/asar.js
  2. 23 0
      spec/asar-spec.js

+ 30 - 0
lib/common/asar.js

@@ -409,6 +409,36 @@
       })
     }
 
+    const {accessSync} = fs
+    fs.accessSync = function (p, mode) {
+      const [isAsar, asarPath, filePath] = splitPath(p)
+      if (!isAsar) {
+        return accessSync.apply(this, arguments)
+      }
+      if (mode == null) {
+        mode = fs.constants.F_OK
+      }
+      const archive = getOrCreateArchive(asarPath)
+      if (!archive) {
+        invalidArchiveError(asarPath)
+      }
+      const info = archive.getFileInfo(filePath)
+      if (!info) {
+        notFoundError(asarPath, filePath)
+      }
+      if (info.unpacked) {
+        const realPath = archive.copyFileOut(filePath)
+        return fs.accessSync(realPath, mode)
+      }
+      const stats = getOrCreateArchive(asarPath).stat(filePath)
+      if (!stats) {
+        notFoundError(asarPath, filePath)
+      }
+      if (mode & fs.constants.W_OK) {
+        accessError(asarPath, filePath)
+      }
+    }
+
     const {readFile} = fs
     fs.readFile = function (p, options, callback) {
       const [isAsar, asarPath, filePath] = splitPath(p)

+ 23 - 0
spec/asar-spec.js

@@ -560,6 +560,29 @@ describe('asar package', function () {
       })
     })
 
+    describe('fs.accessSync', function () {
+      it('throws an error when called with write mode', function () {
+        var p = path.join(fixtures, 'asar', 'a.asar', 'file1')
+        assert.throws(function () {
+          fs.accessSync(p, fs.constants.R_OK | fs.constants.W_OK)
+        }, /EACCES/)
+      })
+
+      it('throws an error when called on non-existent file', function () {
+        var p = path.join(fixtures, 'asar', 'a.asar', 'not-exist')
+        assert.throws(function () {
+          fs.accessSync(p)
+        }, /ENOENT/)
+      })
+
+      it('allows write mode for unpacked files', function () {
+        var p = path.join(fixtures, 'asar', 'unpack.asar', 'a.txt')
+        assert.doesNotThrow(function () {
+          fs.accessSync(p, fs.constants.R_OK | fs.constants.W_OK)
+        })
+      })
+    })
+
     describe('child_process.fork', function () {
       it('opens a normal js file', function (done) {
         var child = ChildProcess.fork(path.join(fixtures, 'asar', 'a.asar', 'ping.js'))