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:
Brandon Freitag 2016-12-08 23:45:35 -08:00 committed by Nate Fischer
parent a7ca4d6a8a
commit ded1a3e4a3
2 changed files with 13 additions and 22 deletions

View File

@ -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`));

View File

@ -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 '';