tar
Advanced tools
Comparing version 3.2.0 to 4.0.0
'use strict' | ||
// wrapper around mkdirp for tar's needs. | ||
// TODO: This should probably be a class, not functionally | ||
// passing around state in a gazillion args. | ||
const mkdirp = require('mkdirp') | ||
@@ -20,2 +24,14 @@ const fs = require('fs') | ||
class CwdError extends Error { | ||
constructor (path, code) { | ||
super(code + ': Cannot cd into \'' + path + '\'') | ||
this.path = path | ||
this.code = code | ||
} | ||
get name () { | ||
return 'CwdError' | ||
} | ||
} | ||
const mkdir = module.exports = (dir, opt, cb) => { | ||
@@ -53,5 +69,12 @@ // if there's any overlap between mask and mode, | ||
if (cache && cache.get(dir) === true || dir === cwd) | ||
if (cache && cache.get(dir) === true) | ||
return done() | ||
if (dir === cwd) | ||
return fs.lstat(dir, (er, st) => { | ||
if (er || !st.isDirectory()) | ||
er = new CwdError(dir, er && er.code || 'ENOTDIR') | ||
done(er) | ||
}) | ||
if (preserve) | ||
@@ -62,6 +85,6 @@ return mkdirp(dir, mode, done) | ||
const parts = sub.split(/\/|\\/) | ||
mkdir_(cwd, parts, mode, cache, unlink, null, done) | ||
mkdir_(cwd, parts, mode, cache, unlink, cwd, null, done) | ||
} | ||
const mkdir_ = (base, parts, mode, cache, unlink, created, cb) => { | ||
const mkdir_ = (base, parts, mode, cache, unlink, cwd, created, cb) => { | ||
if (!parts.length) | ||
@@ -72,8 +95,12 @@ return cb(null, created) | ||
if (cache.get(part)) | ||
return mkdir_(part, parts, mode, cache, unlink, created, cb) | ||
fs.mkdir(part, mode, onmkdir(part, parts, mode, cache, unlink, created, cb)) | ||
return mkdir_(part, parts, mode, cache, unlink, cwd, created, cb) | ||
fs.mkdir(part, mode, onmkdir(part, parts, mode, cache, unlink, cwd, created, cb)) | ||
} | ||
const onmkdir = (part, parts, mode, cache, unlink, created, cb) => er => { | ||
const onmkdir = (part, parts, mode, cache, unlink, cwd, created, cb) => er => { | ||
if (er) { | ||
if (er.path && path.dirname(er.path) === cwd && | ||
(er.code === 'ENOTDIR' || er.code === 'ENOENT')) | ||
return cb(new CwdError(cwd, er.code)) | ||
fs.lstat(part, (statEr, st) => { | ||
@@ -83,3 +110,3 @@ if (statEr) | ||
else if (st.isDirectory()) | ||
mkdir_(part, parts, mode, cache, unlink, created, cb) | ||
mkdir_(part, parts, mode, cache, unlink, cwd, created, cb) | ||
else if (unlink) | ||
@@ -89,3 +116,3 @@ fs.unlink(part, er => { | ||
return cb(er) | ||
fs.mkdir(part, mode, onmkdir(part, parts, mode, cache, unlink, created, cb)) | ||
fs.mkdir(part, mode, onmkdir(part, parts, mode, cache, unlink, cwd, created, cb)) | ||
}) | ||
@@ -99,3 +126,3 @@ else if (st.isSymbolicLink()) | ||
created = created || part | ||
mkdir_(part, parts, mode, cache, unlink, created, cb) | ||
mkdir_(part, parts, mode, cache, unlink, cwd, created, cb) | ||
} | ||
@@ -131,5 +158,20 @@ } | ||
if (cache && cache.get(dir) === true || dir === cwd) | ||
if (cache && cache.get(dir) === true) | ||
return done() | ||
if (dir === cwd) { | ||
let ok = false | ||
let code = 'ENOTDIR' | ||
try { | ||
ok = fs.lstatSync(dir).isDirectory() | ||
} catch (er) { | ||
code = er.code | ||
} finally { | ||
if (!ok) | ||
throw new CwdError(dir, code) | ||
} | ||
done() | ||
return | ||
} | ||
if (preserve) | ||
@@ -153,2 +195,6 @@ return done(mkdirp.sync(dir, mode)) | ||
} catch (er) { | ||
if (er.path && path.dirname(er.path) === cwd && | ||
(er.code === 'ENOTDIR' || er.code === 'ENOENT')) | ||
return new CwdError(cwd, er.code) | ||
const st = fs.lstatSync(part) | ||
@@ -155,0 +201,0 @@ if (st.isDirectory()) { |
@@ -188,5 +188,12 @@ 'use strict' | ||
[ONERROR] (er, entry) { | ||
this.warn(er.message, er) | ||
this[UNPEND]() | ||
entry.resume() | ||
// Cwd has to exist, or else nothing works. That's serious. | ||
// Other errors are warnings, which raise the error in strict | ||
// mode, but otherwise continue on. | ||
if (er.name === 'CwdError') | ||
this.emit('error', er) | ||
else { | ||
this.warn(er.message, er) | ||
this[UNPEND]() | ||
entry.resume() | ||
} | ||
} | ||
@@ -193,0 +200,0 @@ |
@@ -5,3 +5,3 @@ { | ||
"description": "tar for node", | ||
"version": "3.2.0", | ||
"version": "4.0.0", | ||
"repository": { | ||
@@ -8,0 +8,0 @@ "type": "git", |
@@ -256,6 +256,11 @@ # node-tar | ||
Most extraction errors will cause a `warn` event to be emitted. If | ||
the `cwd` is missing, or not a directory, then the extraction will | ||
fail completely. | ||
The following options are supported: | ||
- `cwd` Extract files relative to the specified directory. Defaults | ||
to `process.cwd()`. [Alias: `C`] | ||
to `process.cwd()`. If provided, this must exist and must be a | ||
directory. [Alias: `C`] | ||
- `file` The archive file to extract. If not specified, then a | ||
@@ -526,6 +531,10 @@ Writable stream is returned where the archive data should be | ||
Most unpack errors will cause a `warn` event to be emitted. If the | ||
`cwd` is missing, or not a directory, then an error will be emitted. | ||
#### constructor(options) | ||
- `cwd` Extract files relative to the specified directory. Defaults | ||
to `process.cwd()`. | ||
to `process.cwd()`. If provided, this must exist and must be a | ||
directory. | ||
- `filter` A function that gets called with `(path, entry)` for each | ||
@@ -532,0 +541,0 @@ entry being unpacked. Return `true` to unpack the entry from the |
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
123634
2803
0
884