Whamcloud - gitweb
b=15967
[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 #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 int main(int argc, char **argv)
21 {
22         char *dname1, *dname2;
23         int fddir1, fddir2, rc;
24         //DIR *dp;
25         struct stat st1, st2;
26
27         if (argc < 2 || argc > 3) {
28                 fprintf(stderr, "usage: %s dirname1 [dirname2]\n", argv[0]);
29                 exit(1);
30         }
31
32         dname1 = argv[1];
33         if (argc == 3)
34                 dname2 = argv[2];
35         else
36                 dname2 = argv[1];
37
38         //create the directory
39         fprintf(stderr, "creating directory %s\n", dname1);
40         rc = mkdir(dname1, 0744);
41         if (rc == -1) {
42                 fprintf(stderr, "creating %s fails: %s\n", 
43                         dname1, strerror(errno));
44                 exit(1);
45         }
46
47         // open the dir again
48         fprintf(stderr, "opening directory\n");
49         fddir1 = open(dname1, O_RDONLY | O_DIRECTORY);
50         if (fddir1 == -1) {
51                 fprintf(stderr, "open %s fails: %s\n",
52                         dname1, strerror(errno));
53                 exit(2);
54         }
55         
56         // doesn't matter if the two dirs are the same??
57         fddir2 = open(dname2, O_RDONLY | O_DIRECTORY);
58         if (fddir2 == -1) {
59                 fprintf(stderr, "open %s fails: %s\n",
60                         dname2, strerror(errno));
61                 exit(3);
62         }
63         
64         // another method
65 /*        
66         if ( (dp = opendir(dname2)) == NULL) {
67                 fprintf(stderr, "opendir() %s\n", strerror(errno));
68                 exit(1);
69         }
70         fddir = dirfd(dp);
71 */
72
73         // delete the dir
74         fprintf (stderr, "unlinking %s\n", dname1);
75         rc = rmdir(dname1);
76         if (rc) {
77                 fprintf(stderr, "unlink %s error: %s\n", 
78                         dname1, strerror(errno));
79                 exit(4);
80         }
81
82         if (access(dname2, F_OK) == 0){
83                 fprintf(stderr, "%s still exists\n", dname2);
84                 exit(5);
85         }
86
87         if (access(dname1, F_OK) == 0){
88                 fprintf(stderr, "%s still exists\n", dname1);
89                 exit(6);
90         }
91
92         // fchmod the dir
93         rc = fchmod (fddir1, 0777);
94         if(rc == -1)
95         {
96                 fprintf(stderr, "fchmod unlinked dir fails %s\n", 
97                         strerror(errno));
98                 exit(7);
99         }
100                 
101         // fstat two dirs to check if they are the same
102         rc = fstat(fddir1, &st1);
103         if(rc == -1)
104         {
105                 fprintf(stderr, "fstat unlinked dir %s fails %s\n", 
106                         dname1, strerror(errno));
107                 exit(8);
108         }
109
110         rc = fstat(fddir2, &st2);
111         if (rc == -1) {
112                 fprintf(stderr, "fstat dir %s fails %s\n",
113                         dname2, strerror(errno));
114                 exit(9);
115         }
116
117         if (st1.st_mode != st2.st_mode) {  // can we do this?
118                 fprintf(stderr, "fstat different value on %s and %s\n",                                 dname1, dname2);
119                 exit(10);
120         }        
121
122         fprintf(stderr, "Ok, everything goes well.\n");
123         return 0;
124 }
125