mirror of
https://github.com/shelljs/shelljs.git
synced 2026-01-18 16:03:37 +00:00
Fix: rm behavior regarding symlinks (#598)
* Treat files and symlinks separately * Remove -f flag from dir symlink test * Simplify control flow
This commit is contained in:
parent
a7ca4d6a8a
commit
ded1a3e4a3
@ -253,7 +253,7 @@ test(
|
||||
);
|
||||
|
||||
test('remove symbolic link to a dir', t => {
|
||||
const result = shell.rm('-f', `${t.context.tmp}/rm/link_to_a_dir`);
|
||||
const result = shell.rm(`${t.context.tmp}/rm/link_to_a_dir`);
|
||||
t.falsy(shell.error());
|
||||
t.is(result.code, 0);
|
||||
t.falsy(fs.existsSync(`${t.context.tmp}/rm/link_to_a_dir`));
|
||||
|
||||
33
src/rm.js
33
src/rm.js
@ -123,31 +123,22 @@ function _rm(options, files) {
|
||||
}
|
||||
|
||||
// If here, path exists
|
||||
if (stats.isFile() || stats.isSymbolicLink()) {
|
||||
// Do not check for file writing permissions
|
||||
if (options.force) {
|
||||
common.unlinkSync(file);
|
||||
return;
|
||||
}
|
||||
|
||||
if (isWriteable(file)) {
|
||||
if (stats.isFile()) {
|
||||
if (options.force || isWriteable(file)) {
|
||||
// -f was passed, or file is writable, so it can be removed
|
||||
common.unlinkSync(file);
|
||||
} else {
|
||||
common.error('permission denied: ' + file, { continue: true });
|
||||
}
|
||||
|
||||
return;
|
||||
} // simple file
|
||||
|
||||
// Path is an existing directory, but no -r flag given
|
||||
if (stats.isDirectory() && !options.recursive) {
|
||||
common.error('path is a directory', { continue: true });
|
||||
return; // skip path
|
||||
}
|
||||
|
||||
// Recursively remove existing directory
|
||||
if (stats.isDirectory() && options.recursive) {
|
||||
rmdirSyncRecursive(file, options.force);
|
||||
} else if (stats.isDirectory()) {
|
||||
if (options.recursive) {
|
||||
// -r was passed, so directory can be removed
|
||||
rmdirSyncRecursive(file, options.force);
|
||||
} else {
|
||||
common.error('path is a directory', { continue: true });
|
||||
}
|
||||
} else if (stats.isSymbolicLink()) {
|
||||
common.unlinkSync(file);
|
||||
}
|
||||
}); // forEach(file)
|
||||
return '';
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user