Whamcloud - gitweb
LU-2237 tests: new test for re-recreating last_rcvd
[fs/lustre-release.git] / lustre / tests / opendirunlink.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  */
34
35 /* for O_DIRECTORY */
36 #ifndef _GNU_SOURCE
37 #define _GNU_SOURCE
38 #endif
39
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <unistd.h>
43 #include <errno.h>
44 #include <fcntl.h>
45 #include <sys/types.h>
46 #include <sys/stat.h>
47 #include <dirent.h>
48 #include <string.h>
49
50 int main(int argc, char **argv)
51 {
52         char *dname1, *dname2;
53         int fddir1, fddir2, rc;
54         //DIR *dp;
55         struct stat st1, st2;
56
57         if (argc < 2 || argc > 3) {
58                 fprintf(stderr, "usage: %s dirname1 [dirname2]\n", argv[0]);
59                 exit(1);
60         }
61
62         dname1 = argv[1];
63         if (argc == 3)
64                 dname2 = argv[2];
65         else
66                 dname2 = argv[1];
67
68         //create the directory
69         fprintf(stderr, "creating directory %s\n", dname1);
70         rc = mkdir(dname1, 0744);
71         if (rc == -1) {
72                 fprintf(stderr, "creating %s fails: %s\n", 
73                         dname1, strerror(errno));
74                 exit(1);
75         }
76
77         // open the dir again
78         fprintf(stderr, "opening directory\n");
79         fddir1 = open(dname1, O_RDONLY | O_DIRECTORY);
80         if (fddir1 == -1) {
81                 fprintf(stderr, "open %s fails: %s\n",
82                         dname1, strerror(errno));
83                 exit(2);
84         }
85         
86         // doesn't matter if the two dirs are the same??
87         fddir2 = open(dname2, O_RDONLY | O_DIRECTORY);
88         if (fddir2 == -1) {
89                 fprintf(stderr, "open %s fails: %s\n",
90                         dname2, strerror(errno));
91                 exit(3);
92         }
93         
94         // another method
95 /*        
96         if ( (dp = opendir(dname2)) == NULL) {
97                 fprintf(stderr, "opendir() %s\n", strerror(errno));
98                 exit(1);
99         }
100         fddir = dirfd(dp);
101 */
102
103         // delete the dir
104         fprintf (stderr, "unlinking %s\n", dname1);
105         rc = rmdir(dname1);
106         if (rc) {
107                 fprintf(stderr, "unlink %s error: %s\n", 
108                         dname1, strerror(errno));
109                 exit(4);
110         }
111
112         if (access(dname2, F_OK) == 0){
113                 fprintf(stderr, "%s still exists\n", dname2);
114                 exit(5);
115         }
116
117         if (access(dname1, F_OK) == 0){
118                 fprintf(stderr, "%s still exists\n", dname1);
119                 exit(6);
120         }
121
122         // fchmod the dir
123         rc = fchmod (fddir1, 0777);
124         if(rc == -1)
125         {
126                 fprintf(stderr, "fchmod unlinked dir fails %s\n", 
127                         strerror(errno));
128                 exit(7);
129         }
130                 
131         // fstat two dirs to check if they are the same
132         rc = fstat(fddir1, &st1);
133         if(rc == -1)
134         {
135                 fprintf(stderr, "fstat unlinked dir %s fails %s\n", 
136                         dname1, strerror(errno));
137                 exit(8);
138         }
139
140         rc = fstat(fddir2, &st2);
141         if (rc == -1) {
142                 fprintf(stderr, "fstat dir %s fails %s\n",
143                         dname2, strerror(errno));
144                 exit(9);
145         }
146
147         if (st1.st_mode != st2.st_mode) {  // can we do this?
148                 fprintf(stderr, "fstat different value on %s and %s\n",                                 dname1, dname2);
149                 exit(10);
150         }        
151
152         fprintf(stderr, "Ok, everything goes well.\n");
153         return 0;
154 }