Whamcloud - gitweb
fde7d36f3ad18711ec0a69a2f2304dd53317054d
[fs/lustre-release.git] / lustre / tests / opendevunlink.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  */
4
5 #define _GNU_SOURCE
6
7 #include <stdio.h>
8 #include <errno.h>
9 #include <fcntl.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <dirent.h>
13 #include <string.h>
14
15 int main(int argc, char **argv)
16 {
17         char *dname1, *dname2;
18         int fddev1, fddev2, rc;
19         //DIR *dp;
20         struct stat st1, st2;
21
22         if (argc < 2 || argc > 3) {
23                 fprintf(stderr, "usage: %s filename1 [filename2]\n", argv[0]);
24                 exit(1);
25         }
26
27         dname1 = argv[1];
28         if (argc == 3)
29                 dname2 = argv[2];
30         else
31                 dname2 = argv[1];
32
33         //create the special file (right now only test on pipe)
34         fprintf(stderr, "creating special file %s\n", dname1);
35         rc = mknod(dname1, 0777|S_IFIFO, 0);
36         if (rc == -1) {
37                 fprintf(stderr, "creating %s fails: %s\n", 
38                         dname1, strerror(errno));
39                 exit(1);
40         }
41
42         // open the special file again
43         fprintf(stderr, "opening file\n");
44         fddev1 = open(dname1, O_RDONLY | O_NONBLOCK);
45         if (fddev1 == -1) {
46                 fprintf(stderr, "open %s fails: %s\n",
47                         dname1, strerror(errno));
48                 exit(1);
49         }
50         
51         // doesn't matter if the two dirs are the same??
52         fddev2 = open(dname2, O_RDONLY | O_NONBLOCK);
53         if (fddev2 == -1) {
54                 fprintf(stderr, "open %s fails: %s\n",
55                         dname2, strerror(errno));
56                 exit(1);
57         }
58         
59         // delete the special file
60         fprintf (stderr, "unlinking %s\n", dname1);
61         rc = unlink(dname1);
62         if (rc) {
63                 fprintf(stderr, "unlink %s error: %s\n", 
64                         dname1, strerror(errno));
65                 exit(1);
66         }
67
68         if (access(dname2, F_OK) == 0){
69                 fprintf(stderr, "%s still exists\n", dname2);
70                 exit(1);
71         }
72
73         if (access(dname1, F_OK) == 0){
74                 fprintf(stderr, "%s still exists\n", dname1);
75                 exit(1);
76         }
77
78         // fchmod one special file
79         rc = fchmod (fddev1, 0777);
80         if(rc == -1)
81         {
82                 fprintf(stderr, "fchmod unlinked special file %s fails: %s\n", 
83                         dname1, strerror(errno));
84                 exit(1);
85         }
86                 
87         // fstat two files to check if they are the same
88         rc = fstat(fddev1, &st1);
89         if(rc == -1)
90         {
91                 fprintf(stderr, "fstat unlinked special file %s fails: %s\n", 
92                         dname1, strerror(errno));
93                 exit(1);
94         }
95
96         rc = fstat(fddev2, &st2);
97         if (rc == -1) {
98                 fprintf(stderr, "fstat file %s fails: %s\n",
99                         dname2, strerror(errno));
100                 exit(1);
101         }
102
103         if (st1.st_mode != st2.st_mode) {  // can we do this?
104                 fprintf(stderr, "fstat different value on %s and %s\n",                                 dname1, dname2);
105                 exit(1);
106         }        
107
108         fprintf(stderr, "Ok, everything goes well.\n");
109         return 0;
110 }
111