r22418: Support running under launchd. We abstract the method of obtaining
[tprouty/samba.git] / source / tests / os2_delete.c
1 /*
2   test readdir/unlink pattern that OS/2 uses
3   tridge@samba.org July 2005
4 */
5
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>
15
16 #define NUM_FILES 700
17 #define READDIR_SIZE 100
18 #define DELETE_SIZE 4
19
20 #define TESTDIR "test.dir"
21
22 #define FAILED(d) (fprintf(stderr, "Failed for %s - %s\n", d, strerror(errno)), exit(1), 1)
23
24 #ifndef MIN
25 #define MIN(a,b) ((a)<(b)?(a):(b))
26 #endif
27
28 static void cleanup(void)
29 {
30         /* I'm a lazy bastard */
31         system("rm -rf " TESTDIR);
32         mkdir(TESTDIR, 0700) == 0 || FAILED("mkdir");
33 }
34
35 static void create_files()
36 {
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");
42         }
43 }
44
45 static int os2_delete(DIR *d)
46 {
47         off_t offsets[READDIR_SIZE];
48         int i, j;
49         struct dirent *de;
50         char names[READDIR_SIZE][30];
51
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);
58         }
59
60         if (i == 0) {
61                 return 0;
62         }
63
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");
69         }
70
71         /* seek to just after the deletion */
72         seekdir(d, offsets[j-1]);
73
74         /* return number deleted */
75         return j;
76 }
77
78 int main(void)
79 {
80         int total_deleted = 0;
81         DIR *d;
82         struct dirent *de;
83
84         cleanup();
85         create_files();
86         
87         d = opendir(TESTDIR);
88
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 ..");
94
95         while (1) {
96                 int n = os2_delete(d);
97                 if (n == 0) break;
98                 total_deleted += n;
99         }
100         closedir(d);
101
102         printf("Deleted %d files of %d\n", total_deleted, NUM_FILES);
103
104         rmdir(TESTDIR) == 0 || FAILED("rmdir");
105
106         return 0;
107 }