summaryrefslogtreecommitdiff
path: root/lib/fts.c
Commit message (Collapse)AuthorAgeFilesLines
* fts: minor simplificationPaul Eggert2019-03-171-1/+0
| | | | * lib/fts.c (fts_safe_changedir): Remove redundant assignment.
* fts: Optimize on Android.Bruno Haible2019-01-271-1/+1
| | | | * lib/fts.c: Treat Android like Linux.
* maint: Run 'make update-copyright'Paul Eggert2019-01-011-1/+1
|
* backupfile: new dir_fd argsPaul Eggert2018-10-231-25/+1
| | | | | | | | | | | | | | | | | | | | | | New module opendirat with code taken from fts. Use this module to let backupfile use a directory file descriptor. * NEWS: Document the incompatible change. * lib/backup-find.c (find_backup_file_name): * lib/backup-rename.c (backup_file_rename): New arg DIR_FD. * lib/backupfile.c: Include stdint.h, for SIZE_MAX. (SIZE_MAX): Remove. Include opendirat.h rather than dirent--.h. (check_extension): New args DIR_FD and BASE_MAX. All callers changed. (numbered_backup): New args DIR_FD and PNEW_FD. All callers changed. (backupfile_internal): New arg DIR_FD. All callers changed. * lib/fts.c: Include opendirat.h. (opendirat): Move to opendirat.c. * lib/opendirat.c, lib/opendirat.h, modules/opendirat: New files. * modules/backupfile (Depends-on): Remove dirfd, opendir. Add opendirat. * modules/fts (Depends-on): Remove fdopendir, openat-safer. Add opendirat.
* fts: remove FTS_NOATIMEBernhard Voelker2018-10-081-7/+4
| | | | | | | | | | | | | This reverts commit da4d6974013c822af1498941e32db774b2031765. We cannot guarantee that O_NOATIME works: e.g. openat fails with EPERM if the effective user ID of the caller does not match the owner of the file and the caller is not privileged. Downstream findutils has never picked up FTS_NOATIME. Discussed at <https://lists.gnu.org/r/bug-gnulib/2018-09/msg00122.html>. * lib/fts_.h (FTS_NOATIME): Remove bit flag. (FTS_OPTIONMASK): Adjust. * lib/fts.c (diropen, fts_open, fts_build): Likewise. (fd_ring_check): Likewise.
* fts: avoid a memory leak edge casePádraig Brady2018-05-201-0/+1
| | | | | | | | * lib/fts.c (fts_open): Set an appropriate fts_level so that an immediate fts_close() will free the allocation. * tests/test-fts.c (fts_dealloc): Add a test case which will trigger under valgrind or address sanitizer. Fixes https://bugs.gnu.org/31439
* fts: add commentPaul Eggert2018-04-111-1/+5
| | | | | | * lib/fts.c (fts_build): Explain why ==, not >. See remark by Bernhard Voelker in: https://lists.gnu.org/r/bug-gnulib/2018-04/msg00041.html
* fts: fix bug in find across filesystemsPaul Eggert2018-04-111-24/+31
| | | | | | | | | | | This fixes a bug I introduced last summer. Problem reported by Kamil Dudka in: https://lists.gnu.org/r/bug-gnulib/2018-04/msg00033.html * lib/fts.c (filesystem_type, dirent_inode_sort_may_be_useful) (leaf_optimization): New arg for file descriptor. All callers changed. (fts_build): Check for whether inodes should be sorted before closing the directory.
* fts: treat CIFS like NFSPaul Eggert2018-04-051-1/+7
| | | | | | | | Problem reported by Kamil Dudka in: https://lists.gnu.org/r/bug-gnulib/2018-04/msg00015.html * lib/fts.c (S_MAGIC_CIFS): New macro. (dirent_inode_sort_may_be_useful, leaf_optimization): Treat CIFS like NFS.
* maint: Run 'make update-copyright'Paul Eggert2018-01-011-1/+1
|
* all: Replace many more http URLs by https URLs. Update stale URLs.Bruno Haible2017-09-141-2/+2
| | | | | * users.txt: Remove mention of 'newts'. * lib/localename.c: Update comment about LANG_SOTHO.
* all: prefer https: URLsPaul Eggert2017-09-131-1/+1
|
* * lib/fts.c: Fix XFS comments.Paul Eggert2017-09-081-3/+3
|
* fts: fix cloexec racesPaul Eggert2017-08-121-19/+8
| | | | | | | | | | | * lib/fts.c [!_LIBC]: Do not include dirent--.h, unistd--.h, cloexec.h. (opendirat, diropen): Use O_CLOEXEC instead of set_cloexec_flag. (fts_build): Use F_DUPD_CLOEXEC rinstad of set_cloexec_flag. (fd_ring_check): Set cloexec flag on new file descriptors. (fts_build, fd_ring_check): While we’re at it, make sure the resulting file descriptor is not 0, 1, or 2, since that is easy. * modules/fts (Depends-on): Remove cloexec, dirent-safer, dup, fcntl-safer, unistd-safer. Add fcntl.
* fts: port recent changes to CentOS 6Paul Eggert2017-08-101-2/+8
| | | | | | | | | Problem reported by Tom G. Christensen in: http://lists.gnu.org/archive/html/bug-gnulib/2017-08/msg00064.html * lib/fts.c (fsword): New type. (struct dev_type, filesystem_type): Use it. * m4/fts.m4 (gl_FUNC_FTS_CORE): Check for __fsword_t. Also, check for f_type only if fstatfs and sys/vfs.h work.
* Don't interpret EOVERFLOW to mean nonexistencePaul Eggert2017-07-301-3/+1
| | | | | | | | | | | | | * lib/fts.c (fts_stat): If lstat fails, report its errno, which may be EOVERFLOW; this is likely more useful than reporting the stat errno. * lib/glob.c (link_stat): Rename from link_exists2_p and return -1/0 instead of 0/1. Caller changed. * lib/glob.c (link_exists_p): * lib/renameat2.c (rename_noreplace, renameat2): * lib/tempname.c (try_nocreate): If errno == EOVERFLOW then the directory entry exists, so do not act as if it does not exist.
* fts: simplify fts_buildPaul Eggert2017-07-251-25/+15
| | | | | * lib/fts.c (fts_build): Simplify, and be lazier about calling leaf_optimization.
* fts: three levels of leaf optimizationPaul Eggert2017-07-251-17/+37
| | | | | | | | | * lib/fts.c (enum leaf_optimization): New type with three values. (S_MAGIC_AFS): New macro. Sort them. (leaf_optimization): Rename from leaf_optimization_applies, and return enum leaf_optimization instead of bool. All uses changed. Add cases for unknown type and for AFS. (fts_build): Don’t rely on link counts if NO_LEAF_OPTIMIZATION.
* fts: cache dirent_inode_sort_may_be_useful tooPaul Eggert2017-07-251-106/+96
| | | | | | | | | | | * lib/fts.c (struct dev_type): New struct. (DEV_TYPE_HT_INITIAL_SIZE): New constant. (dev_type_hash, dev_type_compare, filesystem_type): New functions. (dirent_inode_sort_may_be_useful, leaf_optimization_applies): Now takes FTSENT const *, not int. All uses changed. Use filesystem_type to cache. (link_count_optimize_ok): Remove. Caller changed to use leaf_optimization_applies, which now uses shared cache.
* fts: introduce MIN_DIR_NLINKPaul Eggert2017-07-251-5/+14
| | | | | * lib/fts.c (MIN_DIR_NLINK): New constant. Use it instead of 2, whenever we are talking about link counts.
* fts: nlink_t signedness fixupsPaul Eggert2017-07-251-17/+9
| | | | | | | | | | * lib/fts.c (fts_open): Set rootparent n_dirs_remaining to -1 so that root need not be a special case later. (fts_read): Remove now-redundant test for fts_level. Do not assume that nlink_t is signed. (fts_build): Remove useless decrement of nlinks. (fts_stat): Avoid unlikely signed integer overflow later, if nlink_t is signed.
* fallthrough: update for GCC 7/8Jim Meyering2017-05-191-2/+10
| | | | | | | | | | | | | | | | * lib/quotearg.c (FALLTHROUGH): New macro. Use it whenever one switch case falls through into the next, replacing "/* Fall through */" comments. This exposed one instance of an unwarranted "fall through" comment: unwarranted because it preceded a "goto" label not a case statement. * lib/freopen-safer.c (freopen_safer): Likewise. * lib/fts.c (leaf_optimization_applies): Likewise. * lib/unistr/u8-uctomb-aux.c (u8_uctomb_aux): Likewise. * tests/test-getopt_long.h (getopt_long_loop): Likewise. * tests/test-tsearch.c (mangle_tree): Likewise. Also include tests/macros.h for the definition. * tests/test-argp.c (group1_parser): Likewise. * tests/test-getopt.h (getopt_loop): Likewise.
* version-etc: new yearPaul Eggert2017-01-011-1/+1
| | | | | | | | | | * build-aux/gendocs.sh (version): * doc/gendocs_template: * doc/gendocs_template_min: * doc/gnulib.texi: * lib/version-etc.c (COPYRIGHT_YEAR): Update copyright dates by hand in templates and the like. * all files: Run 'make update-copyright'.
* flexmember: port better to GCC + valgrindPaul Eggert2016-09-071-11/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | With a char[] flexible array member in a struct with nontrivial alignment, GCC-generated code can access past the end of the array, because GCC assumes there are padding bytes to get the struct aligned. So the common idiom of malloc (offsetof (struct s, m), n) does not properly allocate an n-byte trailing member, as malloc’s argument should be the next multiple of alignof (struct s). See GCC Bug#66661: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66661 Although C11 apparently permits this GCC optimization (i.e., there was a bug in Gnulib not in GCC), possibly this is a defect in C11. See the thread containing: https://gcc.gnu.org/ml/gcc-patches/2016-09/msg00317.html * lib/flexmember.h: New file. * lib/fnmatch.c, lib/fts.c, lib/glob.c, lib/idcache.c: * lib/localename.c, lib/time_rz.c: Include flexmember.h. * lib/fnmatch_loop.c (struct patternlist): * lib/localename.c (struct hash_node): Use FLEXIBLE_ARRAY_MEMBER. * lib/fnmatch_loop.c (EXT): * lib/fts.c (fts_alloc): * lib/glob.c (glob_in_dir): * lib/idcache.c (getuser, getuidbyname, getgroup, getgidbyname): * lib/localename.c (gl_lock_define_initialized): * lib/time_rz.c (tzalloc): Use FLEXSIZEOF instead of offsetof. * m4/flexmember.m4 (AC_C_FLEXIBLE_ARRAY_MEMBER): Check that the size of the struct can be taken. * modules/flexmember (Files): Add lib/flexmember.h. * modules/fnmatch, modules/glob, modules/localename (Depends-on): Add flexmember.
* fts: handle readdir() errorsPádraig Brady2016-06-261-2/+12
| | | | | | | | | | | | | * lib/fts.c (fts_build): readdir(3) returns NULL when finished, but also upon error when it will also set errno. Therefore flag the error case from readdir(). We treat the case where no items are read the same as if the dir can't be accessed, i.e. by setting fts_errno to FTS_DNR. The bug was initially reported by Peter Benie http://bugzilla.opensuse.org/show_bug.cgi?id=984910 where it was mentioned that readdir() may fail when an NFS server has a poor readdir cookie implementation.
* Use GCC_LINT, not lintPaul Eggert2016-05-301-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | FreeBSD and Cygwin #define _Noreturn to empty if 'lint' is defined. Problem reported by Ken Brown in: http://bugs.gnu.org/23640 * doc/posix-headers/stdnoreturn.texi (stdnoreturn.h): Document problem with lint and _Noreturn. * lib/diffseq.h (IF_LINT, IF_LINT2): * lib/fts.c (sccsid): * lib/getndelim2.c (IF_LINT): * lib/gl_anylinked_list2.h (gl_linked_iterator) (gl_linked_iterator_from_to): * lib/gl_anytree_list2.h (gl_tree_iterator) (gl_tree_iterator_from_to): * lib/gl_anytree_oset.h (gl_tree_iterator): * lib/gl_array_list.c (gl_array_iterator) (gl_array_iterator_from_to): * lib/gl_array_oset.c (gl_array_iterator): * lib/gl_carray_list.c (gl_carray_iterator) (gl_carray_iterator_from_to): * lib/idcache.c: * lib/inet_ntop.c (IF_LINT): * lib/regcomp.c (build_charclass_op, create_tree): * lib/regex_internal.c (re_acquire_state) (re_acquire_state_context): * lib/trigl.c (rcsid): * lib/trim.c (IF_LINT): * lib/vasnprintf.c (IF_LINT): * lib/verify.h (assume): Treat GCC_LINT like lint.
* fts: don't unconditionally use leaf optimization for NFSPádraig Brady2016-01-181-9/+10
| | | | | | | | NFS st_nlink are not accurate on all implementations, leading to aborts() if that assumption is made. See <https://bugzilla.redhat.com/1299169> * lib/fts.c (leaf_optimization_applies): Remove NFS from the white list, and document the issue.
* version-etc: new yearPaul Eggert2016-01-011-1/+1
| | | | | | | | | | * build-aux/gendocs.sh (version): * doc/gendocs_template: * doc/gendocs_template_min: * doc/gnulib.texi: * lib/version-etc.c (COPYRIGHT_YEAR): Update copyright dates by hand in templates and the like. * all files: Run 'make update-copyright'.
* fts: ensure leaf optimization is used for NFSKamil Dudka2015-12-201-0/+5
| | | | | | NFS provides usable dirent.d_type but not necessarily for all entries of large directories. See <https://bugzilla.redhat.com/1252549> * lib/fts.c (leaf_optimization_applies): Append NFS on the white list.
* fts: enable leaf optimization for XFSPádraig Brady2015-12-201-0/+2
| | | | | | | | | | | | | | | | | | | | | | XFS provides usable dirent.d_type only for DT_DIR, but the noleaf optimization still applies, as confirmed with: test $(($(find . -maxdepth 1 -type d | wc -l) + 1)) = $(stat -c %h .) Enabling this gives significant traversal speedup. Testing with find(1) gives: $ time find/find-before /usr/share >/dev/null real 0m0.410s user 0m0.145s sys 0m0.266s $ time find/find-after /usr/share >/dev/null real 0m0.278s user 0m0.147s sys 0m0.131s * lib/fts.c (leaf_optimization_applies): Add XFS to the white list.
* fts: port to C11 alignofPaul Eggert2015-10-181-3/+8
| | | | | | | | * doc/posix-headers/stdalign.texi (stdalign.h): Document the C11 restriction. * lib/fts.c: Include stddef.h, for max_align_t. (fts_alloc): Align using max_align_t, not FTSENT. * modules/fts (Depends-on): Add stddef.
* fts: avoid reading beyond the heap allocationPádraig Brady2015-06-251-0/+7
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | GCC 5.1.1 with -O2 and -fsanitize=address reports the following from `chmod a+rx ..` for example: ERROR: AddressSanitizer: heap-buffer-overflow on address 0x61200000be48 READ of size 4 at 0x61200000be48 thread T0 #0 0x40f0a1 in fts_stat lib/fts.c:1821 #1 0x4141ec in fts_open lib/fts.c:514 #2 0x40ca6e in xfts_open lib/xfts.c:36 #3 0x402c35 in process_files src/chmod.c:336 #4 0x402c35 in main src/chmod.c:566 #5 0x7f8fdc38678f in __libc_start_main (/lib64/libc.so.6+0x2078f) #6 0x404608 in _start (/home/padraig/git/coreutils/src/chmod+0x404608) 0x61200000be4b is located 0 bytes to the right of 267-byte region [0x61200000bd40,0x61200000be4b) allocated by thread T0 here: #0 0x7f8fdd4cfa0a in malloc (/lib64/libasan.so.2+0x98a0a) #1 0x40f22c in fts_alloc lib/fts.c:1916 The read of size 4 from a heap object of size 3 is indeed invalid, though this may be due to incorrect padding assumptions by GCC, see: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66661 The read is coming from the a[2] access in ISDOT() which GCC seems to assume can access according to the alignment of the main structure, since the flexible array members are being accessed directly. One could cast the parameter to ISDOT() to (char const*) to restrict to byte at a time access, however it seems more correct and maintainable to increase the buffer size to the appropriate alignment to avoid this issue. For reference some notes on alignment and flexible array members are at: https://sites.google.com/site/embeddedmonologue/home/c-programming/data-alignment-structure-padding-and-flexible-array-member * lib/fts.c (fts_alloc): Increase allocation to alignof(FTSENT). * modules/fts: Depend on stdalign.
* fts: port to GCC 5.1 with --enable-gcc-warningsPaul Eggert2015-04-271-1/+1
| | | | | | | | | | | Without this fix, GCC 5.1 (correctly) warns about a subscript error on the fts_name component of FTSENT. It's actually a flexible member, so define it that way on C99 or later hosts. * lib/fts.c (fts_alloc): Use offsetof, not sizeof, for a structure that now has a flexible array member. * lib/fts_.h (__FLEXIBLE_ARRAY_MEMBER): New macro. (FTSENT): fts_name is now flexible on C99-or-later platforms. * modules/fts (Depends-on): Add flexmember.
* fts: remove redundant close() (trivial)Jaroslav Skarvada2015-02-201-3/+0
| | | | | * lib/fts.c (fts_read): Remove redundant call to close(). Spotted by coverity.
* fts: avoid crash when a cycle is added while traversingKamil Dudka2015-02-171-3/+9
| | | | | | | | This could be triggered by auto-mounting a recursive bind mount. Reported by Michael Chapman in: https://bugzilla.redhat.com/1188498 * lib/fts.c (fts_read): Avoid removing the original hash table item when leaving a directory that caused a cycle, and preserve the FTS_DC flag.
* version-etc: new yearPaul Eggert2014-12-311-1/+1
| | | | | | * doc/gnulib.texi: * lib/version-etc.c (COPYRIGHT_YEAR): Update copyright date. * all files: Run 'make update-copyright'.
* assure: new modulePaul Eggert2014-12-201-1/+1
| | | | | | | | | | | | | | | This works better than 'assert' when compiling with -DNDEBUG, as it avoids some compiler diagnostics in that case. Reported by Norihiro Tanaka in: http://lists.gnu.org/archive/html/bug-gnulib/2014-12/msg00215.html * MODULES.html.sh (func_all_modules): Add 'assure'. * lib/assure.h, modules/assure: New files. * lib/chdir-long.c, lib/cycle-check.c, lib/fchdir.c, lib/fts.c: * lib/poll.c, lib/savewd.c, lib/utimens.c, lib/xstrtol.c: Prefer 'assure' to 'assert'. * modules/chdir-long, modules/cycle-check, modules/fchdir: * modules/poll, modules/savewd, modules/utimens, modules/xstrtol: Depend on 'assure'.
* fts: port to C89Paul Eggert2014-11-101-2/+2
| | | | | | Problem reported for MSVC 16 by Gisle Vanem in: http://lists.gnu.org/archive/html/bug-gnulib/2014-11/msg00027.html * lib/fts.c (fts_build): Avoid declaration before statement.
* fts: avoid unnecessary strlen callsEric Blake2014-04-091-0/+3
| | | | | | | | Not all platforms have _D_EXACT_NAMLEN. * lib/fts.c (_D_EXACT_NAMLEN): Restore definition when needed. Signed-off-by: Eric Blake <eblake@redhat.com>
* fts: avoid unnecessary strlen callsPaul Eggert2014-04-091-2/+3
| | | | | * lib/fts.c (fts_build): Go back to using _D_EXECT_NAMLEN when that can be faster than strlen.
* fts: avoid unnecessary strlen callsBernhard Voelker2014-04-091-8/+6
| | | | | | | | | * lib/fts.c (_D_EXACT_NAMLEN): Remove macro. (fts_build): Store the length of the dp->d_name entry in a local variable instead of calling strlen() several times via the above, removed macro. For 'rm -rf some-dir' with e.g. 1M directory entries, this speeds up the run by ~4%, and reduces the execution time by about a third if run via "ltrace -c rm -rf some-dir".
* maint: update copyrightEric Blake2014-01-011-1/+1
| | | | | | I ran 'make update-copyright'. Signed-off-by: Eric Blake <eblake@redhat.com>
* maint: update all copyright year number rangesEric Blake2013-01-011-1/+1
| | | | | | Run "make update-copyright". Compare to commit 1602f0a from last year. Signed-off-by: Eric Blake <eblake@redhat.com>
* ftruncate, fts, lstat, openat, raise: no 'static inline'Paul Eggert2012-11-291-2/+2
| | | | | | | | | | | | | | | * lib/ftruncate.c (chsize_nothrow): * lib/fts.c (opendirat, diropen): * lib/lstat.c (orig_lstat): * lib/openat.c (orig_openat): * lib/raise.c (raise_nothrow): Now static, not static inline. * m4/ftruncate.m4 (gl_PREREQ_FTRUNCATE): * m4/fts.m4 (gl_FUNC_FTS_CORE): * m4/lstat.m4 (gl_PREREQ_LSTAT): * m4/openat.m4 (gl_PREREQ_OPENAT): * m4/raise.m4 (gl_PREREQ_RAISE): Do not require AC_C_INLINE.
* fts: introduce FTS_VERBATIMDmitry V. Levin2012-11-171-6/+9
| | | | | | | | | | | | | | | This gives clients the option to disable stripping of trailing slashes from input path names during fts_open initialization. The recent change v0.0-7611-g3a9002d that made fts_open strip trailing slashes from input path names had a negative impact on findutils that relies on the old fts_open behavior to implement POSIX requirement that each path operand of the find utility shall be evaluated unaltered as it was provided, including all trailing slash characters. * lib/fts_.h (FTS_VERBATIM): New bit flag. (FTS_OPTIONMASK, FTS_NAMEONLY, FTS_STOP): Adjust. * lib/fts.c (fts_open): Honor it.
* fts: reduce two or more trailing slashes to just one, usuallyJim Meyering2012-09-041-0/+8
| | | | | | | | | | | | | | | | | | | | | | * lib/fts.c (fts_open): Upon initialization, if a name ends in two or more slashes, trim all but the final one. But if a name consists solely of two slashes, don't modify it. If it consists solely of three or more slashes, strip all but one. This is part of the solution to a minor problem with rm: it would print a bogus ELOOP diagnostic when failing to remove the slash-decorated name of a symlink-to-directory: $ mkdir d && ln -s d s && env rm -r s/ rm: cannot remove 's': Too many levels of symbolic links With the change below and a trivial don't-trim-trailing-slashes adjustment to remove.c, it does this: $ env rm -r s/ rm: cannot remove 's/': Not a directory Improved by: Eric Blake
* fts: when there is no risk of overlap, use memcpy, not memmoveJim Meyering2012-09-041-1/+1
| | | | * lib/fts.c (fts_alloc): Fix unjustified memmove: s/memmove/memcpy/
* maint: spelling fixesPaul Eggert2012-02-051-1/+1
|
* In commentary, do not use ` to quote.Paul Eggert2012-01-051-5/+5
|
* maint: update all copyright year number rangesJim Meyering2012-01-011-1/+1
| | | | Run "make update-copyright".