Skip to content

Remove contents of symlink to dir with rm -rf#688

Merged
freitagbr merged 5 commits intomasterfrom
rm-rf-dir-symlink
Apr 15, 2017
Merged

Remove contents of symlink to dir with rm -rf#688
freitagbr merged 5 commits intomasterfrom
rm-rf-dir-symlink

Conversation

@freitagbr
Copy link
Copy Markdown
Contributor

Fix the behavior of rm such that:

  1. rm('-rf', 'link_to_dir/') deletes contents of real dir, but keeps dir and symlink
  2. rm('-rf', 'link_to_dir') deletes the symlink itself

Fixes #587

@freitagbr freitagbr requested a review from nfischer March 8, 2017 06:25
@freitagbr freitagbr added the fix Bug/defect, or a fix for such a problem label Mar 8, 2017
src/rm.js Outdated
try {
stats = fs.statSync(file);
} catch (e) {
// symlink is bad, so just remove it
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Change this to something like "symlink is broken, so only delete the link, not its contents"

var dirpath = path.resolve(path.dirname(link), dir);
var fromSymlink = true;
rmdirSyncRecursive(dirpath, options.force, fromSymlink);
} else {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think you can ignore the options.recursive check and use handleDirectory instead.

Although this won't catch the "link ➡️ link ➡️ directory" case, it only catches the "link ➡️ directory" case.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Also, do we really need to do all this work here? Does it really not work to call fs.readdirSync() on the link itself? That works for me when I tried on Node v7. Then we probably don't need the fromSymlink stuff.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

You're right, fs.readdirSync() works when passed symlinks, so that can be simplified. I think the fromSymlink logic still needs to be there, so that rmdirSyncRecursive knows not to delete the directory.


// if was directory was referenced through a symbolic link,
// do not remove the directory itself
if (fromSymlink) return;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you specify that while we do not remove the directory itself, we do in fact remove its contents?

@freitagbr freitagbr force-pushed the rm-rf-dir-symlink branch from 03602b2 to accd3b2 Compare March 8, 2017 16:57
@freitagbr
Copy link
Copy Markdown
Contributor Author

@nfischer Can you help diagnose the failing test on windows? I don't have a windows machine to test with.

@nfischer
Copy link
Copy Markdown
Member

@freitagbr probably has to do with how Windows handles sym links differently. I'd recommend skipping the tests on Windows.

@codecov-io
Copy link
Copy Markdown

codecov-io commented Apr 14, 2017

Codecov Report

Merging #688 into master will increase coverage by 0.48%.
The diff coverage is 93.54%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #688      +/-   ##
==========================================
+ Coverage   94.33%   94.81%   +0.48%     
==========================================
  Files          33       33              
  Lines        1183     1293     +110     
==========================================
+ Hits         1116     1226     +110     
  Misses         67       67
Impacted Files Coverage Δ
src/rm.js 97.01% <93.54%> (-2.99%) ⬇️
src/common.js 98.52% <0%> (+1.82%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7949759...1a00e72. Read the comment docs.

@freitagbr freitagbr merged commit 7feabda into master Apr 15, 2017
@freitagbr freitagbr deleted the rm-rf-dir-symlink branch April 15, 2017 07:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix Bug/defect, or a fix for such a problem

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants