haltcase / glob

Pure Nim library for matching file paths against Unix style glob patterns.
https://glob.bolingen.me
MIT License
61 stars 5 forks source link

symlinks should not be followed by default; can cause infinite recursion or list too many things because of just 1 bad symlink #17

Closed timotheecour closed 6 years ago

timotheecour commented 6 years ago
tree
.
└── foo
    └── bar
        └── foo_link -> ../

NOTE: these occur in practice, eg on OSX in app folders IIRC.

import glob,strutils,sequtils
echo toSeq(glob.walkGlob("foo", includeDirs=true)).join("\n")
nim c -o:app test.nim
foo/bar
foo/bar/foo_link
foo/bar/foo_link/bar
foo/bar/foo_link/bar/foo_link                                                                                                                                                                                                                      foo/bar/foo_link/bar/foo_link/bar
foo/bar/foo_link/bar/foo_link/bar/foo_link
foo/bar/foo_link/bar/foo_link/bar/foo_link/bar
foo/bar/foo_link/bar/foo_link/bar/foo_link/bar/foo_link
foo/bar/foo_link/bar/foo_link/bar/foo_link/bar/foo_link/bar
foo/bar/foo_link/bar/foo_link/bar/foo_link/bar/foo_link/bar/foo_link
etc... (82 entries)

Note: curiously, this terminates gracefully without error after 82 lines (maybe an OS limit on path depth, since I'm seeing same behavior with D and python):

./app && echo ok || echo errror
ok
./app | wc -l
82

Unix find has the correct default (not follow symlinks), and only follow symlinks if -L is provided, see https://unix.stackexchange.com/a/31121/211423 for more on that rationale. (either symlink recursion as shown above or a single symlink deep down pointing to ~ or / will cause a ton of entries.)

NOTE: alternative would be to not have an option followSymlinks and follow symlinks, and let user filter out symlinks via filterDescend from https://github.com/citycide/glob/issues/8 ; however that's a common enough use case that it should have its own option IMO; also it's indeed a tricky thing to handle (eg infinite recursion) so right here in glob is the right place to handle that.