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