On 26/06/16 21:26, Pádraig Brady wrote:
> On 22/06/16 15:54, Pádraig Brady wrote:
>> Cool, I'll add that test in your name to coreutils.
>
> Attached.
Small adjustment attached.
diff --git a/tests/rm/rm-readdir-fail.sh b/tests/rm/rm-readdir-fail.sh
index 20ea50e..15ef1d6 100755
--- a/tests/rm/rm-readdir-fail.sh
+++ b/tests/rm/rm-readdir-fail.sh
@@ -26,23 +26,23 @@ mkdir -p dir/notempty || framework_failure_
# Simulate "readdir" failure.
cat > k.c <<\EOF || framework_failure_
#define _GNU_SOURCE
+#include <dlfcn.h>
#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
-#include <dlfcn.h>
struct dirent *readdir (DIR *dirp)
{
- struct dirent *(*real_readdir)(DIR *dirp) = dlsym (RTLD_NEXT, "readdir");
- if (! real_readdir)
+ static struct dirent *(*real_readdir)(DIR *dirp);
+ if (! real_readdir && ! (real_readdir = dlsym (RTLD_NEXT, "readdir")))
{
fprintf (stderr, "Failed to find readdir()\n");
errno = ESRCH;
return NULL;
}
- struct dirent* d = real_readdir (dirp);
- if (! d)
+ static struct dirent* d;
+ if (! d && ! ( d = real_readdir (dirp)))
{
fprintf (stderr, "Failed to get dirent\n");
errno = ENOENT;
@@ -50,20 +50,17 @@ struct dirent *readdir (DIR *dirp)
}
/* Flag that LD_PRELOAD and above functions work. */
- static int done = 0;
- if (! done)
- {
- fclose (fopen ("preloaded", "w"));
- done++;
- }
+ static int count = 1;
+ if (count == 1)
+ fclose (fopen ("preloaded", "w"));
/* Return some entries to trigger partial read failure,
ensuring we don't return ignored '.' or '..' */
char const *readdir_partial = getenv ("READDIR_PARTIAL");
- if (readdir_partial && *readdir_partial && done <= 3)
+ if (readdir_partial && *readdir_partial && count <= 3)
{
- done++;
- d->d_name[0]='0'+done; d->d_name[1]='\0';
+ count++;
+ d->d_name[0]='0'+count; d->d_name[1]='\0';
#ifdef _DIRENT_HAVE_D_NAMLEN
_D_EXACT_NAMELEN(d)=2;
#endif
@@ -89,10 +86,11 @@ gcc_shared_ k.c k.so \
# Test if LD_PRELOAD works:
export READDIR_PARTIAL
for READDIR_PARTIAL in '' '1'; do
+ rm -f preloaded
(LD_PRELOAD=$LD_PRELOAD:./k.so returns_ 1 rm -Rf dir 2>>err) || fail=1
+ test -f preloaded ||
+ skip_ "internal test failure: maybe LD_PRELOAD doesn't work?"
done
-test -f preloaded ||
- skip_ "internal test failure: maybe LD_PRELOAD doesn't work?"
# First case is failure to read any items from dir, then assume empty.
# Generally that will be diagnosed when rm tries to rmdir().