Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions bin/asar.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,12 @@ program.command('extract-file <archive> <filename>')
.alias('ef')
.description('extract one file from archive')
.action(function (archive, filename) {
require('fs').writeFileSync(require('path').basename(filename),
asar.extractFile(archive, filename))
const fileData = asar.extractFile(archive, filename)
if (typeof fileData === 'undefined') {
console.error(`The file "${filename}" was not found in the archive.`)
return
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is in the CLI part of the repository, so it makes more sense here to process.exit(1)... although if an exception is thrown, the changes here probably aren't needed.

Copy link
Author

@Edythator Edythator Jan 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is in the CLI part of the repository, so it makes more sense here to process.exit(1)... although if an exception is thrown, the changes here probably aren't needed.

Yeah, I couldn't really add a process.exit(1) to start with since that messes up the test.

}
require('fs').writeFileSync(require('path').basename(filename), fileData)
})

program.command('extract <archive> <dest>')
Expand Down
7 changes: 6 additions & 1 deletion lib/asar.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,12 @@ module.exports.listPackage = function (archive, options) {

module.exports.extractFile = function (archive, filename) {
const filesystem = disk.readFilesystemSync(archive)
return disk.readFileSync(filesystem, filename, filesystem.getFile(filename))
const file = filesystem.getFile(filename)
if (typeof file === 'undefined') {
return undefined
}

return disk.readFileSync(filesystem, filename, file)
}

module.exports.extractAll = function (archive, dest) {
Expand Down
4 changes: 4 additions & 0 deletions lib/filesystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ class Filesystem {
followLinks = typeof followLinks === 'undefined' ? true : followLinks
const info = this.getNode(p)

if (typeof info === 'undefined') {
return undefined
}

// if followLinks is false we don't resolve symlinks
if (info.link && followLinks) {
return this.getFile(info.link)
Expand Down
3 changes: 3 additions & 0 deletions test/cli-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ describe('command line interface', function () {
assert.strictEqual(actual, expected)
})
*/
it('should throw an error when trying to extract a non-existent file', async () => {
return assertAsarOutputMatches('ef test/input/extractthis.asar dir1/non-existent', 'test/expected/extractthis-non-existent-file.txt')
})
it('should extract an archive', async () => {
await execAsar('e test/input/extractthis.asar tmp/extractthis-cli/')
return compDirs('tmp/extractthis-cli/', 'test/expected/extractthis')
Expand Down
1 change: 1 addition & 0 deletions test/expected/extractthis-non-existent-file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The file "dir1/non-existent" was not found in the archive.