Split rules.mk out of Makefile.
[Samba/bjacke.git] / source / tests / os2_delete.c
blobb3aaf67f4186d170598d8a2c0f40386fa1eac8e5
1 /*
2 test readdir/unlink pattern that OS/2 uses
3 tridge@samba.org July 2005
4 */
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <sys/stat.h>
9 #include <unistd.h>
10 #include <sys/types.h>
11 #include <dirent.h>
12 #include <errno.h>
13 #include <string.h>
14 #include <fcntl.h>
16 #define NUM_FILES 700
17 #define READDIR_SIZE 100
18 #define DELETE_SIZE 4
20 #define TESTDIR "test.dir"
22 #define FAILED(d) (fprintf(stderr, "Failed for %s - %s\n", d, strerror(errno)), exit(1), 1)
24 #ifndef MIN
25 #define MIN(a,b) ((a)<(b)?(a):(b))
26 #endif
28 static void cleanup(void)
30 /* I'm a lazy bastard */
31 system("rm -rf " TESTDIR);
32 mkdir(TESTDIR, 0700) == 0 || FAILED("mkdir");
35 static void create_files()
37 int i;
38 for (i=0;i<NUM_FILES;i++) {
39 char fname[40];
40 sprintf(fname, TESTDIR "/test%u.txt", i);
41 close(open(fname, O_CREAT|O_RDWR, 0600)) == 0 || FAILED("close");
45 static int os2_delete(DIR *d)
47 off_t offsets[READDIR_SIZE];
48 int i, j;
49 struct dirent *de;
50 char names[READDIR_SIZE][30];
52 /* scan, remembering offsets */
53 for (i=0, de=readdir(d);
54 de && i < READDIR_SIZE;
55 de=readdir(d), i++) {
56 offsets[i] = telldir(d);
57 strcpy(names[i], de->d_name);
60 if (i == 0) {
61 return 0;
64 /* delete the first few */
65 for (j=0; j<MIN(i, DELETE_SIZE); j++) {
66 char fname[40];
67 sprintf(fname, TESTDIR "/%s", names[j]);
68 unlink(fname) == 0 || FAILED("unlink");
71 /* seek to just after the deletion */
72 seekdir(d, offsets[j-1]);
74 /* return number deleted */
75 return j;
78 int main(void)
80 int total_deleted = 0;
81 DIR *d;
82 struct dirent *de;
84 cleanup();
85 create_files();
87 d = opendir(TESTDIR);
89 /* skip past . and .. */
90 de = readdir(d);
91 strcmp(de->d_name, ".") == 0 || FAILED("match .");
92 de = readdir(d);
93 strcmp(de->d_name, "..") == 0 || FAILED("match ..");
95 while (1) {
96 int n = os2_delete(d);
97 if (n == 0) break;
98 total_deleted += n;
100 closedir(d);
102 printf("Deleted %d files of %d\n", total_deleted, NUM_FILES);
104 rmdir(TESTDIR) == 0 || FAILED("rmdir");
106 return 0;
109 test readdir/unlink pattern that OS/2 uses
110 tridge@samba.org July 2005
113 #include <stdio.h>
114 #include <stdlib.h>
115 #include <sys/stat.h>
116 #include <unistd.h>
117 #include <sys/types.h>
118 #include <dirent.h>
119 #include <errno.h>
120 #include <string.h>
121 #include <fcntl.h>
123 #define NUM_FILES 700
124 #define READDIR_SIZE 100
125 #define DELETE_SIZE 4
127 #define TESTDIR "test.dir"
129 #define FAILED(d) (fprintf(stderr, "Failed for %s - %s\n", d, strerror(errno)), exit(1), 1)
131 #ifndef MIN
132 #define MIN(a,b) ((a)<(b)?(a):(b))
133 #endif
135 static void cleanup(void)
137 /* I'm a lazy bastard */
138 system("rm -rf " TESTDIR);
139 mkdir(TESTDIR, 0700) == 0 || FAILED("mkdir");
142 static void create_files()
144 int i;
145 for (i=0;i<NUM_FILES;i++) {
146 char fname[40];
147 sprintf(fname, TESTDIR "/test%u.txt", i);
148 close(open(fname, O_CREAT|O_RDWR, 0600)) == 0 || FAILED("close");
152 static int os2_delete(DIR *d)
154 off_t offsets[READDIR_SIZE];
155 int i, j;
156 struct dirent *de;
157 char names[READDIR_SIZE][30];
159 /* scan, remembering offsets */
160 for (i=0, de=readdir(d);
161 de && i < READDIR_SIZE;
162 de=readdir(d), i++) {
163 offsets[i] = telldir(d);
164 strcpy(names[i], de->d_name);
167 if (i == 0) {
168 return 0;
171 /* delete the first few */
172 for (j=0; j<MIN(i, DELETE_SIZE); j++) {
173 char fname[40];
174 sprintf(fname, TESTDIR "/%s", names[j]);
175 unlink(fname) == 0 || FAILED("unlink");
178 /* seek to just after the deletion */
179 seekdir(d, offsets[j-1]);
181 /* return number deleted */
182 return j;
185 int main(void)
187 int total_deleted = 0;
188 DIR *d;
189 struct dirent *de;
191 cleanup();
192 create_files();
194 d = opendir(TESTDIR);
196 /* skip past . and .. */
197 de = readdir(d);
198 strcmp(de->d_name, ".") == 0 || FAILED("match .");
199 de = readdir(d);
200 strcmp(de->d_name, "..") == 0 || FAILED("match ..");
202 while (1) {
203 int n = os2_delete(d);
204 if (n == 0) break;
205 total_deleted += n;
207 closedir(d);
209 printf("Deleted %d files of %d\n", total_deleted, NUM_FILES);
211 rmdir(TESTDIR) == 0 || FAILED("rmdir");
213 return 0;