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