Whamcloud - gitweb
branch: HEAD
[fs/lustre-release.git] / lustre / tests / openfilleddirunlink.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  */
4
5 /* for O_DIRECTORY */
6 #ifndef _GNU_SOURCE
7 #define _GNU_SOURCE
8 #endif
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <errno.h>
14 #include <fcntl.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <dirent.h>
18 #include <string.h>
19
20 char fname[1024];
21
22
23 int main(int argc, char **argv)
24 {
25         char *dname1;
26         int fddir1, rc;
27         int fd;
28
29         if (argc != 2) {
30                 fprintf(stderr, "usage: %s dirname1\n", argv[0]);
31                 exit(1);
32         }
33
34         dname1 = argv[1];
35
36         //create the directory
37         fprintf(stderr, "creating directory %s\n", dname1);
38         rc = mkdir(dname1, 0744);
39         if (rc == -1) {
40                 fprintf(stderr, "creating %s fails: %s\n",
41                         dname1, strerror(errno));
42                 exit(1);
43         }
44
45         sprintf(fname, "%s/0", dname1);
46         fprintf(stderr, "creating file %s\n", fname);
47         fd = creat(fname, 0666);
48         if (fd < 0) {
49                 fprintf(stderr, "creation %s fails: %s\n",
50                         fname, strerror(errno));
51                 exit(1);
52         }
53         close(fd);
54
55         // open the dir again
56         fprintf(stderr, "opening directory\n");
57         fddir1 = open(dname1, O_RDONLY | O_DIRECTORY);
58         if (fddir1 == -1) {
59                 fprintf(stderr, "open %s fails: %s\n",
60                         dname1, strerror(errno));
61                 exit(1);
62         }
63
64         // delete the dir
65         fprintf(stderr, "unlinking %s\n", dname1);
66         rc = rmdir(dname1);
67         if (rc == 0) {
68                 fprintf(stderr, "unlinked non-empty %s successfully\n",
69                         dname1);
70                 exit(1);
71         }
72
73         if (access(dname1, F_OK) != 0){
74                 fprintf(stderr, "can't access %s: %s\n",
75                         dname1, strerror(errno));
76                 exit(1);
77         }
78
79         fprintf(stderr, "Ok, everything goes well.\n");
80         return 0;
81 }