Update mojo sdk to rev 1dc8a9a5db73d3718d99917fadf31f5fb2ebad4f
[chromium-blink-merge.git] / third_party / talloc / libreplace / test / os2_delete.c
blob8b52837018d9387323275fb6ef2fd6eff3b39202
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 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)
26 #ifndef MIN
27 #define MIN(a,b) ((a)<(b)?(a):(b))
28 #endif
30 #ifdef _WIN32
31 #define mkdir(d,m) _mkdir(d)
32 #endif
34 static void cleanup(void)
36 /* I'm a lazy bastard */
37 if (system("rm -rf " TESTDIR)) {
38 FAILED("system");
40 mkdir(TESTDIR, 0700) == 0 || FAILED("mkdir");
43 static void create_files(void)
45 int i;
46 for (i=0;i<NUM_FILES;i++) {
47 char fname[40];
48 int fd;
49 sprintf(fname, TESTDIR "/test%u.txt", i);
50 fd = open(fname, O_CREAT|O_RDWR, 0600);
51 if (fd < 0) {
52 FAILED("open");
54 if (close(fd) != 0) {
55 FAILED("close");
60 static int os2_delete(DIR *d)
62 off_t offsets[READDIR_SIZE];
63 int i, j;
64 struct dirent *de;
65 char names[READDIR_SIZE][30];
67 /* scan, remembering offsets */
68 for (i=0, de=readdir(d);
69 de && i < READDIR_SIZE;
70 de=readdir(d), i++) {
71 offsets[i] = telldir(d);
72 strcpy(names[i], de->d_name);
75 if (i == 0) {
76 return 0;
79 /* delete the first few */
80 for (j=0; j<MIN(i, DELETE_SIZE); j++) {
81 char fname[40];
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 */
90 return j;
93 int test_readdir_os2_delete(void)
95 int total_deleted = 0;
96 DIR *d;
97 struct dirent *de;
99 test_readdir_os2_delete_ret = 0;
101 cleanup();
102 create_files();
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 .. */
111 de = readdir(d);
112 strcmp(de->d_name, ".") == 0 || FAILED("match .");
113 de = readdir(d);
114 strcmp(de->d_name, "..") == 0 || FAILED("match ..");
116 while (1) {
117 int n = os2_delete(d);
118 if (n == 0) break;
119 total_deleted += n;
121 closedir(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) {
128 FAILED("system");
131 return test_readdir_os2_delete_ret;