Whamcloud - gitweb
LU-1347 style: removes obsolete EXPORT_SYMTAB macros
[fs/lustre-release.git] / lustre / tests / openfilleddirunlink.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) 2004, 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 char fname[1024];
51
52
53 int main(int argc, char **argv)
54 {
55         char *dname1;
56         int fddir1, rc;
57         int fd;
58
59         if (argc != 2) {
60                 fprintf(stderr, "usage: %s dirname1\n", argv[0]);
61                 exit(1);
62         }
63
64         dname1 = argv[1];
65
66         //create the directory
67         fprintf(stderr, "creating directory %s\n", dname1);
68         rc = mkdir(dname1, 0744);
69         if (rc == -1) {
70                 fprintf(stderr, "creating %s fails: %s\n",
71                         dname1, strerror(errno));
72                 exit(1);
73         }
74
75         sprintf(fname, "%s/0", dname1);
76         fprintf(stderr, "creating file %s\n", fname);
77         fd = creat(fname, 0666);
78         if (fd < 0) {
79                 fprintf(stderr, "creation %s fails: %s\n",
80                         fname, strerror(errno));
81                 exit(1);
82         }
83         close(fd);
84
85         // open the dir again
86         fprintf(stderr, "opening directory\n");
87         fddir1 = open(dname1, O_RDONLY | O_DIRECTORY);
88         if (fddir1 == -1) {
89                 fprintf(stderr, "open %s fails: %s\n",
90                         dname1, strerror(errno));
91                 exit(1);
92         }
93
94         // delete the dir
95         fprintf(stderr, "unlinking %s\n", dname1);
96         rc = rmdir(dname1);
97         if (rc == 0) {
98                 fprintf(stderr, "unlinked non-empty %s successfully\n",
99                         dname1);
100                 exit(1);
101         }
102
103         if (access(dname1, F_OK) != 0){
104                 fprintf(stderr, "can't access %s: %s\n",
105                         dname1, strerror(errno));
106                 exit(1);
107         }
108
109         fprintf(stderr, "Ok, everything goes well.\n");
110         return 0;
111 }