2 test readdir/unlink pattern that OS/2 uses
3 tridge@samba.org July 2005
10 #include <sys/types.h>
15 #include "replace-test.h"
18 #define READDIR_SIZE 100
21 #define TESTDIR "test.dir"
23 static int test_readdir_os2_delete_ret
;
25 #define FAILED(d) (printf("failure: readdir [\nFailed for %s - %d = %s\n]\n", d, errno, strerror(errno)), test_readdir_os2_delete_ret = 1)
28 #define MIN(a,b) ((a)<(b)?(a):(b))
32 #define mkdir(d,m) _mkdir(d)
35 static void cleanup(void)
37 /* I'm a lazy bastard */
38 if (system("rm -rf " TESTDIR
)) {
41 mkdir(TESTDIR
, 0700) == 0 || FAILED("mkdir");
44 static void create_files(void)
47 for (i
=0;i
<NUM_FILES
;i
++) {
50 snprintf(fname
, sizeof(fname
), TESTDIR
"/test%u.txt", i
);
51 fd
= open(fname
, O_CREAT
|O_RDWR
, 0600);
61 static int os2_delete(DIR *d
)
63 off_t offsets
[READDIR_SIZE
];
66 char names
[READDIR_SIZE
][30];
68 /* scan, remembering offsets */
69 for (i
=0, de
=readdir(d
);
70 de
&& i
< READDIR_SIZE
;
72 offsets
[i
] = telldir(d
);
73 strcpy(names
[i
], de
->d_name
);
80 /* delete the first few */
81 for (j
=0; j
<MIN(i
, DELETE_SIZE
); j
++) {
83 snprintf(fname
, sizeof(fname
), TESTDIR
"/%s", names
[j
]);
84 unlink(fname
) == 0 || FAILED("unlink");
87 /* seek to just after the deletion */
88 seekdir(d
, offsets
[j
-1]);
90 /* return number deleted */
94 int test_readdir_os2_delete(void)
96 int total_deleted
= 0;
100 test_readdir_os2_delete_ret
= 0;
105 d
= opendir(TESTDIR
"/test0.txt");
106 if (d
!= NULL
) FAILED("opendir() on file succeed");
107 if (errno
!= ENOTDIR
) FAILED("opendir() on file didn't give ENOTDIR");
109 d
= opendir(TESTDIR
);
111 /* skip past . and .. */
113 strcmp(de
->d_name
, ".") == 0 || FAILED("match .");
115 strcmp(de
->d_name
, "..") == 0 || FAILED("match ..");
118 int n
= os2_delete(d
);
124 fprintf(stderr
, "Deleted %d files of %d\n", total_deleted
, NUM_FILES
);
126 rmdir(TESTDIR
) == 0 || FAILED("rmdir");
128 if (system("rm -rf " TESTDIR
) == -1) {
132 return test_readdir_os2_delete_ret
;