1 /* xfts.c -- a wrapper for fts_open
2
3 Copyright (C) 2003-2023 Free Software Foundation, Inc.
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17
18 /* Written by Jim Meyering. */
19
20 #include <config.h>
21
22 #include <stdlib.h>
23 #include <errno.h>
24
25 #include "assure.h"
26 #include "xalloc.h"
27 #include "xfts.h"
28
29 /* Fail with a proper diagnostic if fts_open fails. */
30
31 FTS *
xfts_open(char * const * argv,int options,int (* compar)(const FTSENT **,const FTSENT **))32 xfts_open (char * const *argv, int options,
33 int (*compar) (const FTSENT **, const FTSENT **))
34 {
35 FTS *fts = fts_open (argv, options | FTS_CWDFD, compar);
36 if (fts == nullptr)
37 {
38 /* This can fail in two ways: out of memory or with errno==EINVAL,
39 which indicates it was called with invalid bit_flags. */
40 affirm (errno != EINVAL);
41 xalloc_die ();
42 }
43
44 return fts;
45 }
46
47 /* When fts_read returns FTS_DC to indicate a directory cycle,
48 it may or may not indicate a real problem. When a program like
49 chgrp performs a recursive traversal that requires traversing
50 symbolic links, it is *not* a problem. However, when invoked
51 with "-P -R", it deserves a warning. The fts_options member
52 records the options that control this aspect of fts's behavior,
53 so test that. */
54 bool
cycle_warning_required(FTS const * fts,FTSENT const * ent)55 cycle_warning_required (FTS const *fts, FTSENT const *ent)
56 {
57 #define ISSET(Fts,Opt) ((Fts)->fts_options & (Opt))
58 /* When dereferencing no symlinks, or when dereferencing only
59 those listed on the command line and we're not processing
60 a command-line argument, then a cycle is a serious problem. */
61 return ((ISSET (fts, FTS_PHYSICAL) && !ISSET (fts, FTS_COMFOLLOW))
62 || (ISSET (fts, FTS_PHYSICAL) && ISSET (fts, FTS_COMFOLLOW)
63 && ent->fts_level != FTS_ROOTLEVEL));
64 }
65