2 test readdir/unlink pattern that OS/2 uses
3 tridge@samba.org July 2005
10 #include <sys/types.h>
17 #define READDIR_SIZE 100
20 #define TESTDIR "test.dir"
22 static int test_readdir_os2_delete_ret
;
24 #define FAILED(d) (printf("failure: readdir [\nFailed for %s - %d = %s\n]\n", d, errno, strerror(errno)), test_readdir_os2_delete_ret = 1, 1)
27 #define MIN(a,b) ((a)<(b)?(a):(b))
31 #define mkdir(d,m) _mkdir(d)
34 static void cleanup(void)
36 /* I'm a lazy bastard */
37 if (system("rm -rf " TESTDIR
)) {
40 mkdir(TESTDIR
, 0700) == 0 || FAILED("mkdir");
43 static void create_files(void)
46 for (i
=0;i
<NUM_FILES
;i
++) {
49 sprintf(fname
, TESTDIR
"/test%u.txt", i
);
50 fd
= open(fname
, O_CREAT
|O_RDWR
, 0600);
60 static int os2_delete(DIR *d
)
62 off_t offsets
[READDIR_SIZE
];
65 char names
[READDIR_SIZE
][30];
67 /* scan, remembering offsets */
68 for (i
=0, de
=readdir(d
);
69 de
&& i
< READDIR_SIZE
;
71 offsets
[i
] = telldir(d
);
72 strcpy(names
[i
], de
->d_name
);
79 /* delete the first few */
80 for (j
=0; j
<MIN(i
, DELETE_SIZE
); j
++) {
82 sprintf(fname
, TESTDIR
"/%s", names
[j
]);
83 unlink(fname
) == 0 || FAILED("unlink");
86 /* seek to just after the deletion */
87 seekdir(d
, offsets
[j
-1]);
89 /* return number deleted */
93 int test_readdir_os2_delete(void)
95 int total_deleted
= 0;
99 test_readdir_os2_delete_ret
= 0;
104 d
= opendir(TESTDIR
"/test0.txt");
105 if (d
!= NULL
) FAILED("opendir() on file succeed");
106 if (errno
!= ENOTDIR
) FAILED("opendir() on file didn't give ENOTDIR");
108 d
= opendir(TESTDIR
);
110 /* skip past . and .. */
112 strcmp(de
->d_name
, ".") == 0 || FAILED("match .");
114 strcmp(de
->d_name
, "..") == 0 || FAILED("match ..");
117 int n
= os2_delete(d
);
123 fprintf(stderr
, "Deleted %d files of %d\n", total_deleted
, NUM_FILES
);
125 rmdir(TESTDIR
) == 0 || FAILED("rmdir");
127 if (system("rm -rf " TESTDIR
) == -1) {
131 return test_readdir_os2_delete_ret
;