Whamcloud - gitweb
Mass conversion of all copyright messages to Oracle.
[fs/lustre-release.git] / lustre / tests / openfilleddirunlink.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 (c) 2004, 2010, Oracle and/or its affiliates. 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 char fname[1024];
53
54
55 int main(int argc, char **argv)
56 {
57         char *dname1;
58         int fddir1, rc;
59         int fd;
60
61         if (argc != 2) {
62                 fprintf(stderr, "usage: %s dirname1\n", argv[0]);
63                 exit(1);
64         }
65
66         dname1 = 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         sprintf(fname, "%s/0", dname1);
78         fprintf(stderr, "creating file %s\n", fname);
79         fd = creat(fname, 0666);
80         if (fd < 0) {
81                 fprintf(stderr, "creation %s fails: %s\n",
82                         fname, strerror(errno));
83                 exit(1);
84         }
85         close(fd);
86
87         // open the dir again
88         fprintf(stderr, "opening directory\n");
89         fddir1 = open(dname1, O_RDONLY | O_DIRECTORY);
90         if (fddir1 == -1) {
91                 fprintf(stderr, "open %s fails: %s\n",
92                         dname1, strerror(errno));
93                 exit(1);
94         }
95
96         // delete the dir
97         fprintf(stderr, "unlinking %s\n", dname1);
98         rc = rmdir(dname1);
99         if (rc == 0) {
100                 fprintf(stderr, "unlinked non-empty %s successfully\n",
101                         dname1);
102                 exit(1);
103         }
104
105         if (access(dname1, F_OK) != 0){
106                 fprintf(stderr, "can't access %s: %s\n",
107                         dname1, strerror(errno));
108                 exit(1);
109         }
110
111         fprintf(stderr, "Ok, everything goes well.\n");
112         return 0;
113 }