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

Fix #393: add flag to dereference links #1136

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

Conversation

agrzeslak
Copy link

@agrzeslak agrzeslak commented Oct 17, 2022

Fixes: #393

There is currently no equivalent to ls's -L/--dereference flag. This is often undesirable, as it is rare that a user is interested in the details of the link itself. As such, the -X/--dereference flag has been added which implements this functionality. I have chosen -X as -L was already taken, and in my mind -X was similar to "across", in the sense that you are traversing "across" the link. The similar -H/--dereference-command-line flag from ls has also not been implemented in this PR, however it should be straightforward to add, and I may add it to this PR if I have some additional time or it is requested. I am more than happy to accommodate other suggestions, or change my implementation in any way that would be more acceptable to the maintainers.

Output without the -X flag (identical to before this PR):

cargo run -- -l
drwxr-xr-x   - root    23 Sep 14:39 dir
.rw-r--r-- 450 root    23 Sep 12:24 file.txt
lrwxrwxrwx  14 andrzej 27 Sep 14:17 symlink-loop-0 -> symlink-loop-1
lrwxrwxrwx  14 andrzej 27 Sep 14:17 symlink-loop-1 -> symlink-loop-0
lrwxrwxrwx  20 andrzej 23 Sep 12:26 symlink-to-another-file -> dir/another-file.txt
lrwxrwxrwx   3 andrzej 23 Sep 12:26 symlink-to-dir -> dir
lrwxrwxrwx   8 andrzej 23 Sep 12:23 symlink-to-file -> file.txt
lrwxrwxrwx  15 andrzej 27 Sep 14:15 symlink-to-symlink-to-file -> symlink-to-file

Output with the -X flag (links are now dereferenced):

cargo run -- -lX
drwxr-xr-x   - root 23 Sep 14:39 dir
.rw-r--r-- 450 root 23 Sep 12:24 file.txt
----------   - -    -            symlink-loop-0 -> symlink-loop-1
----------   - -    -            symlink-loop-1 -> symlink-loop-0
lrw-r--r--   0 root 27 Sep 16:16 symlink-to-another-file -> dir/another-file.txt
lrwxr-xr-x   - root 23 Sep 14:39 symlink-to-dir -> dir
lrw-r--r-- 450 root 23 Sep 12:24 symlink-to-file -> file.txt
lrw-r--r-- 450 root 23 Sep 12:24 symlink-to-symlink-to-file -> symlink-to-file

Output when using the analogous -L flag within ls:

ls -lL
ls: cannot access 'symlink-loop-0': Too many levels of symbolic links
ls: cannot access 'symlink-loop-1': Too many levels of symbolic links
total 20
drwxr-xr-x 2 root root 4096 Sep 23 14:39 dir
-rw-r--r-- 1 root root  450 Sep 23 12:24 file.txt
l????????? ? ?    ?       ?            ? symlink-loop-0
l????????? ? ?    ?       ?            ? symlink-loop-1
-rw-r--r-- 1 root root    0 Sep 27 16:16 symlink-to-another-file
drwxr-xr-x 2 root root 4096 Sep 23 14:39 symlink-to-dir
-rw-r--r-- 1 root root  450 Sep 23 12:24 symlink-to-file
-rw-r--r-- 1 root root  450 Sep 23 12:24 symlink-to-symlink-to-file

Add the `-X`/`--dereference` flags which are used to enable
dereferencing through symbolic links, similar to ls's `-L`. Added the
`link_target_recurse` method in order to recursively call the existing
`link_target` method to get the final target at the end of the chain of
links. In order to appropriately display the final output and play
nicely with existing functionality, a `deref_links` variable has been
added to a few places such that it can be propagated until the time it
is needed during rendering. Following commits will use this information
to display dereferenced information.
@agrzeslak agrzeslak changed the title Implement 393: dereferencing links. Fix #393: add option to dereference symlinks. Oct 18, 2022
@agrzeslak agrzeslak changed the title Fix #393: add option to dereference symlinks. Fix #393: add option to dereference links. Oct 18, 2022
@agrzeslak agrzeslak changed the title Fix #393: add option to dereference links. Fix #393: add option to dereference links Oct 18, 2022
@agrzeslak agrzeslak changed the title Fix #393: add option to dereference links Fix #393: add flag to dereference links Oct 18, 2022
@bd-g
Copy link

bd-g commented May 16, 2023

Updates? This seems like a good feature to bring it more in parity with ls

@pierzchalski
Copy link

I too would find this useful. Anything blocking this (beyond the merge conflicts)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: follow symlinks like ls -L, --dereference, or be even friendlier
3 participants