build: Remove build of replacetort
[Samba.git] / lib / replace / test / os2_delete.c
blob4b99ccf910344159067791f9e47a60038b04ca41
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>
15 #include "replace-test.h"
17 #define NUM_FILES 700
18 #define READDIR_SIZE 100
19 #define DELETE_SIZE 4
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)
27 #ifndef MIN
28 #define MIN(a,b) ((a)<(b)?(a):(b))
29 #endif
31 #ifdef _WIN32
32 #define mkdir(d,m) _mkdir(d)
33 #endif
35 static void cleanup(void)
37 /* I'm a lazy bastard */
38 if (system("rm -rf " TESTDIR)) {
39 FAILED("system");
41 mkdir(TESTDIR, 0700) == 0 || FAILED("mkdir");
44 static void create_files(void)
46 int i;
47 for (i=0;i<NUM_FILES;i++) {
48 char fname[40];
49 int fd;
50 snprintf(fname, sizeof(fname), TESTDIR "/test%u.txt", i);
51 fd = open(fname, O_CREAT|O_RDWR, 0600);
52 if (fd < 0) {
53 FAILED("open");
55 if (close(fd) != 0) {
56 FAILED("close");
61 static int os2_delete(DIR *d)
63 off_t offsets[READDIR_SIZE];
64 int i, j;
65 struct dirent *de;
66 char names[READDIR_SIZE][256];
68 /* scan, remembering offsets */
69 for (i=0, de=readdir(d);
70 de && i < READDIR_SIZE;
71 de=readdir(d), i++) {
72 offsets[i] = telldir(d);
73 /* strlcpy not available here */
74 snprintf(names[i], sizeof(names[i]), "%s", de->d_name);
77 if (i == 0) {
78 return 0;
81 /* delete the first few */
82 for (j=0; j<MIN(i, DELETE_SIZE); j++) {
83 char fname[40];
84 snprintf(fname, sizeof(fname), TESTDIR "/%s", names[j]);
85 unlink(fname) == 0 || FAILED("unlink");
88 /* seek to just after the deletion */
89 seekdir(d, offsets[j-1]);
91 /* return number deleted */
92 return j;
95 int test_readdir_os2_delete(void)
97 int total_deleted = 0;
98 DIR *d;
99 struct dirent *de;
101 test_readdir_os2_delete_ret = 0;
103 cleanup();
104 create_files();
106 d = opendir(TESTDIR "/test0.txt");
107 if (d != NULL) FAILED("opendir() on file succeed");
108 if (errno != ENOTDIR) FAILED("opendir() on file didn't give ENOTDIR");
110 d = opendir(TESTDIR);
112 /* skip past . and .. */
113 de = readdir(d);
114 strcmp(de->d_name, ".") == 0 || FAILED("match .");
115 de = readdir(d);
116 strcmp(de->d_name, "..") == 0 || FAILED("match ..");
118 while (1) {
119 int n = os2_delete(d);
120 if (n == 0) break;
121 total_deleted += n;
123 closedir(d);
125 fprintf(stderr, "Deleted %d files of %d\n", total_deleted, NUM_FILES);
127 rmdir(TESTDIR) == 0 || FAILED("rmdir");
129 if (system("rm -rf " TESTDIR) == -1) {
130 FAILED("system");
133 return test_readdir_os2_delete_ret;