libndr: Add ndr_map_error2errno
[Samba/gebeck_regimport.git] / lib / replace / test / os2_delete.c
bloba11ed3b370bfefddda6532bac837da861645fcf8
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, 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][30];
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 strcpy(names[i], de->d_name);
76 if (i == 0) {
77 return 0;
80 /* delete the first few */
81 for (j=0; j<MIN(i, DELETE_SIZE); j++) {
82 char fname[40];
83 snprintf(fname, sizeof(fname), TESTDIR "/%s", names[j]);
84 unlink(fname) == 0 || FAILED("unlink");
87 /* seek to just after the deletion */
88 seekdir(d, offsets[j-1]);
90 /* return number deleted */
91 return j;
94 int test_readdir_os2_delete(void)
96 int total_deleted = 0;
97 DIR *d;
98 struct dirent *de;
100 test_readdir_os2_delete_ret = 0;
102 cleanup();
103 create_files();
105 d = opendir(TESTDIR "/test0.txt");
106 if (d != NULL) FAILED("opendir() on file succeed");
107 if (errno != ENOTDIR) FAILED("opendir() on file didn't give ENOTDIR");
109 d = opendir(TESTDIR);
111 /* skip past . and .. */
112 de = readdir(d);
113 strcmp(de->d_name, ".") == 0 || FAILED("match .");
114 de = readdir(d);
115 strcmp(de->d_name, "..") == 0 || FAILED("match ..");
117 while (1) {
118 int n = os2_delete(d);
119 if (n == 0) break;
120 total_deleted += n;
122 closedir(d);
124 fprintf(stderr, "Deleted %d files of %d\n", total_deleted, NUM_FILES);
126 rmdir(TESTDIR) == 0 || FAILED("rmdir");
128 if (system("rm -rf " TESTDIR) == -1) {
129 FAILED("system");
132 return test_readdir_os2_delete_ret;