Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve broken symlink and other error handling #13

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

matthijskooijman
Copy link

This PR makes a few changes that came up when working on (a replacement PR for) arduino/arduino-cli#1201, but which probably are a good idea by themselves. This PR contains four commits:

  1. Make ReadDirRecursive not error out on all Stat() errors, but instead just add such problematic files to the returned list and let the caller decide whether these files are really needed at all and an error is appropriate. I needed this for broken symlinks (which return DoesNotExist), but it also seemed useful to ignore permission errors, and let the caller decide.
  2. Added an Lstat() method, needed for the next commit.
  3. Make CopyDirTo() really ignore symlinks, as was seemingly intended based on a comment in the code. I noticed this problem when adding a broken symlink to the testdata (needed to test the next commit), and then discovered that a broken symlink made CopyDirTo() fail, and a correct symlink was actually duplicated, potentially leading to a lot more diskspace. Now symlinks are ignored entirely, at least until properly copying them is implemented.
  4. Fix FilterOutDirs incorrectly marking files with stat errors (such as broken symlinks) as directories and filtering them out. I needed this because commit 1. ensures that broken symlinks end up in the ReadDirRecursive() output, but inside arduino-cli FilterOutDirs() would again throw them away, IMHO inappropriately.

Note also arduino/arduino-cli#1353 (comment), which makes an additional suggestion on improving the ReadDirRecursive API (adding a callback to control pruning the recursion). If implemented, this might remove the need for commit 1., especially if the callback could also control whether an error is returned on a failed stat call.

These can occur during recursion, but are not necessarily a problem of
the ReadDir, so just assume such problematic files are not directories
and return them. The caller can then figure out whether they are even
needed at all, and an error should be returned. This allows simply
ignoring a broken symlink when it would not even be used because of its
extension.
There was some code that claimed to ignore symlinks, but:
 - It would run *after* recursive directory copying, so any symlinks to
   directories would still be copied (potentially creating a destination
   directory that is a lot bigger than the original)
 - It checked for the symlink bit in the result of Stat(), but that
   resolves the symlink and returns the mode bits of the target, so that
   code would never fire.

This fixes both problems by moving the check for symlinks up, and using
Lstat() instead of Stat(), so symlinks are ignored as intended.
Broken symlinks would previously be treated as directories and filtered
out, due to the use of IsNotDir (which defaults to false on Stat
errors), but it makes more sense to only filter out elements that are
certainly directories, so use IsDir (which also defaults to false on
Stat errors) and negate its result.
@CLAassistant
Copy link

CLAassistant commented Sep 8, 2021

CLA assistant check
All committers have signed the CLA.

@codecov-commenter
Copy link

codecov-commenter commented Sep 8, 2021

Codecov Report

Merging #13 (429170e) into master (43bf0b4) will increase coverage by 0.92%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #13      +/-   ##
==========================================
+ Coverage   66.66%   67.59%   +0.92%     
==========================================
  Files           5        5              
  Lines         285      287       +2     
==========================================
+ Hits          190      194       +4     
+ Misses         64       63       -1     
+ Partials       31       30       -1     
Flag Coverage Δ
unit 67.59% <100.00%> (+0.92%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
list.go 87.75% <100.00%> (ø)
paths.go 67.00% <100.00%> (+1.34%) ⬆️

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 43bf0b4...429170e. Read the comment docs.

@per1234 per1234 added type: bug Perceived defect in any part of project type: enhancement Proposed improvement topic: code Related to content of the project itself labels Sep 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: code Related to content of the project itself type: bug Perceived defect in any part of project type: enhancement Proposed improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants