1 From a3f4821c4a3f723d21c9298d54bee8a656bfd7fb Mon Sep 17 00:00:00 2001
2 From: Bernhard Voelker <mail@bernhard-voelker.de>
3 Date: Tue, 30 Jan 2018 23:30:09 +0100
4 Subject: [PATCH] find: make -delete honour the -ignore_readdir_race option
6 * find/pred.c (pred_delete): Return true when the -ignore_readdir_race
7 option is active and unlinkat() came back with ENOENT.
8 * doc/find.texi (Option -ignore_readdir_race): Document the change.
9 (Action -delete): Likewise.
10 * find/find.1: Likewise.
11 * NEWS (Bug Fixes): Mention the fix.
13 For now, it seems a bit hard to add a proper test for this,
14 so the following shell snippet demonstrates the race:
16 $ seq 10 | xargs touch
17 $ env time -f 'find exit status: %x\nfind time: %e' \
18 find -ignore_readdir_race -type f \
20 -exec sh -c 'sleep $(basename {})' \; \
21 -printf 'find deleted: %p\n' \
24 seq 10 | xargs rm -fv; \
27 Reported by Alexander Golubev in
28 https://savannah.gnu.org/bugs/?52981
30 Upstream-Status: Backport
31 Signed-off-by: Zhixiong Chi <zhixiong.chi@windriver.com>
35 doc/find.texi | 15 ++++++++++++++-
36 find/find.1 | 22 ++++++++++++++++++++++
37 find/pred.c | 6 ++++++
38 4 files changed, 46 insertions(+), 1 deletion(-)
40 diff --git a/NEWS b/NEWS
41 index 660c241..b86ec1e 100644
44 @@ -42,6 +42,10 @@ Updated the Danish translation.
48 +#52981: find: the '-delete' action no longer complains about disappeared files
49 + when the '-ignore_readdir_race' option is given, too. That action will
50 + also returns true in such a case now.
52 Applied patch #8688: Spelling fixes.
54 * Major changes in release 4.5.18, 2015-12-27
55 diff --git a/doc/find.texi b/doc/find.texi
56 index fdeb841..247c19a 100644
59 @@ -1418,7 +1418,15 @@ gives a significant increase in search speed.
60 If a file disappears after its name has been read from a directory but
61 before @code{find} gets around to examining the file with @code{stat},
62 don't issue an error message. If you don't specify this option, an
63 -error message will be issued. This option can be useful in system
64 +error message will be issued.
66 +Furthermore, @code{find} with the @samp{-ignore_readdir_race} option
67 +will ignore errors of the @samp{-delete} action in the case the file
68 +has disappeared since the parent directory was read: it will not output
69 +an error diagnostic, and the return code of the @samp{-delete} action
72 +This option can be useful in system
73 scripts (cron scripts, for example) that examine areas of the
74 filesystem that change frequently (mail queues, temporary directories,
75 and so forth), because this scenario is common for those sorts of
76 @@ -2779,6 +2787,11 @@ explicitly.
78 If @samp{-delete} fails, @code{find}'s exit status will be nonzero
79 (when it eventually exits).
81 +Together with the @samp{-ignore_readdir_race} option, @code{find} will
82 +ignore errors of the @samp{-delete} action in the case the file has disappeared
83 +since the parent directory was read: it will not output an error diagnostic, and
84 +the return code of the @samp{-delete} action will be true.
88 diff --git a/find/find.1 b/find/find.1
89 index 7b141b8..0eec41c 100644
92 @@ -479,6 +479,17 @@ one part of the filesystem with this option on and part of it with this option
93 off (if you need to do that, you will need to issue two \fBfind\fR commands
94 instead, one with the option and one without it).
99 +.B \-ignore_readdir_race
100 +option will ignore errors of the
102 +action in the case the file has disappeared since the parent directory was read:
103 +it will not output an error diagnostic, and the return code of the
105 +action will be true.
107 .IP "\-maxdepth \fIlevels\fR"
108 Descend at most \fIlevels\fR (a non-negative integer) levels of
109 directories below the starting-points.
110 @@ -1030,6 +1041,17 @@ and
115 +.B \-ignore_readdir_race
118 +will ignore errors of the
120 +action in the case the file has disappeared since the parent directory was
121 +read: it will not output an error diagnostic, and the return code of the
123 +action will be true.
125 .IP "\-exec \fIcommand\fR ;"
126 Execute \fIcommand\fR; true if 0 status is returned. All following
128 diff --git a/find/pred.c b/find/pred.c
129 index 32938fb..431f065 100644
132 @@ -324,6 +324,12 @@ pred_delete (const char *pathname, struct stat *stat_buf, struct predicate *pred
136 + if (ENOENT == errno && options.ignore_readdir_race)
138 + /* Ignore unlink() error for vanished files. */
144 if ((flags & AT_REMOVEDIR) == 0)