Whamcloud - gitweb
LU-17000 utils: handle_yaml_no_op() has wrong signature
[fs/lustre-release.git] / lustre / tests / opendirunlink.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
4  * Use is subject to license terms.
5  */
6 /*
7  * This file is part of Lustre, http://www.lustre.org/
8  */
9
10 /* for O_DIRECTORY */
11 #ifndef _GNU_SOURCE
12 #define _GNU_SOURCE
13 #endif
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <dirent.h>
23 #include <string.h>
24
25 int main(int argc, char **argv)
26 {
27         char *dname1, *dname2;
28         int fddir1, fddir2, rc;
29         struct stat st1, st2;
30
31         if (argc < 2 || argc > 3) {
32                 fprintf(stderr, "usage: %s dirname1 [dirname2]\n", argv[0]);
33                 exit(1);
34         }
35
36         dname1 = argv[1];
37         if (argc == 3)
38                 dname2 = argv[2];
39         else
40                 dname2 = argv[1];
41
42         /* create the directory */
43         fprintf(stderr, "creating directory %s\n", dname1);
44         rc = mkdir(dname1, 0744);
45         if (rc == -1) {
46                 fprintf(stderr, "creating %s fails: %s\n",
47                         dname1, strerror(errno));
48                 exit(1);
49         }
50
51         /* open the dir again */
52         fprintf(stderr, "opening directory\n");
53         fddir1 = open(dname1, O_RDONLY | O_DIRECTORY);
54         if (fddir1 == -1) {
55                 fprintf(stderr, "open %s fails: %s\n",
56                         dname1, strerror(errno));
57                 exit(2);
58         }
59
60         /* doesn't matter if the two dirs are the same?? */
61         fddir2 = open(dname2, O_RDONLY | O_DIRECTORY);
62         if (fddir2 == -1) {
63                 fprintf(stderr, "open %s fails: %s\n",
64                         dname2, strerror(errno));
65                 exit(3);
66         }
67
68         /* delete the dir */
69         fprintf(stderr, "unlinking %s\n", dname1);
70         rc = rmdir(dname1);
71         if (rc) {
72                 fprintf(stderr, "unlink %s error: %s\n",
73                         dname1, strerror(errno));
74                 exit(4);
75         }
76
77         if (access(dname2, F_OK) == 0) {
78                 fprintf(stderr, "%s still exists\n", dname2);
79                 exit(5);
80         }
81
82         if (access(dname1, F_OK) == 0) {
83                 fprintf(stderr, "%s still exists\n", dname1);
84                 exit(6);
85         }
86
87         /* fchmod the dir */
88         rc = fchmod(fddir1, 0777);
89         if (rc == -1) {
90                 fprintf(stderr, "fchmod unlinked dir fails %s\n",
91                         strerror(errno));
92                 exit(7);
93         }
94
95         /* fstat two dirs to check if they are the same */
96         rc = fstat(fddir1, &st1);
97         if (rc == -1) {
98                 fprintf(stderr, "fstat unlinked dir %s fails %s\n",
99                         dname1, strerror(errno));
100                 exit(8);
101         }
102
103         rc = fstat(fddir2, &st2);
104         if (rc == -1) {
105                 fprintf(stderr, "fstat dir %s fails %s\n",
106                         dname2, strerror(errno));
107                 exit(9);
108         }
109
110         if (st1.st_mode != st2.st_mode) {  /* can we do this? */
111                 fprintf(stderr, "fstat different value on %s and %s\n",
112                         dname1, dname2);
113                 exit(10);
114         }
115
116         fprintf(stderr, "Ok, everything goes well.\n");
117         return 0;
118 }