Ask a Question related to UNIX Programming, Design and Development.

  1. #1

    Default nftw(3) and FTW_D

    Hello,

    I'm having a problem finding out whether a particular entry stat'ed by the
    nftw(3) function is a directory checking the value of flag. nftw() is defined
    as such:

    int nftw(const char *dir, int (*fn)(const char *file, const struct stat *sb,
    int flag, struct FTW *s), int depth, int flags);

    It calls fn() on each directory entry, and flag is supposed to be FTW_D if the
    entry is a directory.

    If in fn() I check whether the entry is a directory with if (flag == FTW_D)
    the condition is never met (flag equals 5 if the entry is indeed a directory).
    In ftw.h, FTW_D is not #define'd as 5.

    I know I can check if the entry is a directory with the value of sb->st_mode &
    S_IFMT and it works, but I find it strange that checking FTW_DIR fails.

    Am I missing something?

    Thanks,
    Sam
    --
    "People sometimes ask me if it is a sin in the Church of Emacs to use vi.
    Using a free version of vi is not a sin; it's a penance".

    - Richard Stallman

    Sam Zoghaib Guest

  2. #2

    Default Re: nftw(3) and FTW_D

    Måns Rullgård wrote in article <yw1x65lc4i36.fsf@users.sourceforge.net> on
    Tuesday 05 August 2003 10:35 in comp.unix.programmer:

    [excerpt from ftw.h snipped]
    > Notice that there are several values indicating a directory. In
    > particular, the value 5 that you are seeing means that all
    > subdirectories have been visited. You'll need to check for several
    > values, not only FTW_D.
    >
    Ok, got it. In my case, though, checking for FTW_DP should be enough, because
    I'm calling nftw with flags == FTW_DEPTH, so fn() is called on all entries in
    directories before being called on the directory. I think fn() will never be
    called with flag == FTW_D

    Thank you,
    Sam
    --
    "Don't be afraid, I'm gonna give you the choice I never had..."

    - Lestat in "Interview with the Vampire" (Ann Rice, 1976)

    Sam Zoghaib Guest

  3. #3

    Default Re: nftw(3) and FTW_D

    Sam Zoghaib <NOsam@zep.homedns.orgSPAM> writes:
    >> Notice that there are several values indicating a directory. In
    >> particular, the value 5 that you are seeing means that all
    >> subdirectories have been visited. You'll need to check for several
    >> values, not only FTW_D.
    >>
    >
    > Ok, got it. In my case, though, checking for FTW_DP should be enough, because
    > I'm calling nftw with flags == FTW_DEPTH, so fn() is called on all entries in
    > directories before being called on the directory. I think fn() will never be
    > called with flag == FTW_D
    Unless you are sure, it's always wisest to check for all possible
    values, even if you think some are impossible.

    --
    Måns Rullgård
    [email]mru@users.sf.net[/email]
    Måns Rullgård Guest

  4. #4

    Default Re: nftw(3) and FTW_D

    Sam Zoghaib <NOsam@zep.homedns.orgSPAM> writes:
    >>> Ok, got it. In my case, though, checking for FTW_DP should be enough,
    >>> because I'm calling nftw with flags == FTW_DEPTH, so fn() is called on all
    >>> entries in directories before being called on the directory. I think fn()
    >>> will never be called with flag == FTW_D
    >>
    >> Unless you are sure, it's always wisest to check for all possible
    >> values, even if you think some are impossible.
    >
    > the man page says:
    >
    > FTW_DEPTH
    >
    > If set, do a depth-first search, that is, call the function for the
    > directory itself only after handling the contents of the directory
    > and its subdirectories.
    >
    > I guess that means flag never equals FTW_D. When fn() is called on
    > the directory, all its entries will have already been processed, so
    > it should det flag to FTW_DP.
    That would seem reasonable. However, I'd check for it anyway, just in
    case the program is ever run on some machine with a broken ftw
    implementation. For almost every function, there is some version of
    some Unix system where that function is broken. Doing the extra check
    doesn't take much time compared to a file access anyway, so it's not
    going to slow things down.

    --
    Måns Rullgård
    [email]mru@users.sf.net[/email]
    Måns Rullgård Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139