Whamcloud - gitweb
- added test_3b which emulates recursive mount. Does not pass yet.
[fs/lustre-release.git] / lustre / tests / opendirunlink.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 #define _GNU_SOURCE
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <errno.h>
12 #include <fcntl.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <dirent.h>
16 #include <string.h>
17
18 int main(int argc, char **argv)
19 {
20         char *dname1, *dname2;
21         int fddir1, fddir2, rc;
22         //DIR *dp;
23         struct stat st1, st2;
24
25         if (argc < 2 || argc > 3) {
26                 fprintf(stderr, "usage: %s dirname1 [dirname2]\n", argv[0]);
27                 exit(1);
28         }
29
30         dname1 = argv[1];
31         if (argc == 3)
32                 dname2 = argv[2];
33         else
34                 dname2 = 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         // open the dir again
46         fprintf(stderr, "opening directory\n");
47         fddir1 = open(dname1, O_RDONLY | O_DIRECTORY);
48         if (fddir1 == -1) {
49                 fprintf(stderr, "open %s fails: %s\n",
50                         dname1, strerror(errno));
51                 exit(1);
52         }
53         
54         // doesn't matter if the two dirs are the same??
55         fddir2 = open(dname2, O_RDONLY | O_DIRECTORY);
56         if (fddir2 == -1) {
57                 fprintf(stderr, "open %s fails: %s\n",
58                         dname2, strerror(errno));
59                 exit(1);
60         }
61         
62         // another method
63 /*        
64         if ( (dp = opendir(dname2)) == NULL) {
65                 fprintf(stderr, "opendir() %s\n", strerror(errno));
66                 exit(1);
67         }
68         fddir = dirfd(dp);
69 */
70
71         // delete the dir
72         fprintf (stderr, "unlinking %s\n", dname1);
73         rc = rmdir(dname1);
74         if (rc) {
75                 fprintf(stderr, "unlink %s error: %s\n", 
76                         dname1, strerror(errno));
77                 exit(1);
78         }
79
80         if (access(dname2, F_OK) == 0){
81                 fprintf(stderr, "%s still exists\n", dname2);
82                 exit(1);
83         }
84
85         if (access(dname1, F_OK) == 0){
86                 fprintf(stderr, "%s still exists\n", dname1);
87                 exit(1);
88         }
89
90         // fchmod the dir
91         rc = fchmod (fddir1, 0777);
92         if(rc == -1)
93         {
94                 fprintf(stderr, "fchmod unlinked dir fails %s\n", 
95                         strerror(errno));
96                 exit(1);
97         }
98                 
99         // fstat two dirs to check if they are the same
100         rc = fstat(fddir1, &st1);
101         if(rc == -1)
102         {
103                 fprintf(stderr, "fstat unlinked dir %s fails %s\n", 
104                         dname1, strerror(errno));
105                 exit(1);
106         }
107
108         rc = fstat(fddir2, &st2);
109         if (rc == -1) {
110                 fprintf(stderr, "fstat dir %s fails %s\n",
111                         dname2, strerror(errno));
112                 exit(1);
113         }
114
115         if (st1.st_mode != st2.st_mode) {  // can we do this?
116                 fprintf(stderr, "fstat different value on %s and %s\n",                                 dname1, dname2);
117                 exit(1);
118         }        
119
120         fprintf(stderr, "Ok, everything goes well.\n");
121         return 0;
122 }
123