Whamcloud - gitweb
Some racy problems happened when sanity-quota.sh run on buffalo.
[fs/lustre-release.git] / lustre / tests / toexcl.c
1 #include <sys/types.h>
2 #include <sys/stat.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <fcntl.h>
6 #include <errno.h>
7 #include <string.h>
8 #include <unistd.h>
9
10 void
11 usage (char *argv0, int help)
12 {
13         char *progname = strrchr(argv0, '/');
14
15         if (progname == NULL)
16                 progname = argv0;
17         
18         fprintf (help ? stdout : stderr,
19                  "Usage: %s [-e] file\n", progname);
20         
21         if (!help)
22         {
23                 fprintf (stderr, "   or try '-h' for help\n");
24                 exit (1);
25         }
26         
27         printf ("Create the given file with O_EXCL...\n");
28         printf (" -e    expect EEXIST\n");
29         printf (" -h    print help");
30         printf (" Exit status is 0 on success, 1 on failure\n");
31 }
32
33 int main(int argc, char **argv)
34 {
35         int rc;
36         int want_eexist = 0;
37         
38         while ((rc = getopt (argc, argv, "eh")) != -1)
39                 switch (rc)
40                 {
41                 case 'e':
42                         want_eexist = 1;
43                         break;
44                 case 'h':
45                         usage (argv[1], 1);
46                         return (0);
47                 default:
48                         usage (argv[0], 0);
49                 }
50         
51         if (optind != argc - 1) { 
52                 usage (argv[0], 0);
53                 return 1;
54         }
55
56         rc = open(argv[optind], O_CREAT|O_EXCL, 0644);
57         if (rc == -1)
58         {
59                 if (want_eexist && errno == EEXIST)
60                 {
61                         printf("open failed: %s (expected)\n", strerror(errno));
62                         return (0);
63                 }
64                 printf("open failed: %s\n", strerror(errno));
65                 return (1);
66         } else {
67                 if (want_eexist)
68                 {
69                         printf("open success (expecting EEXIST).\n");
70                         return (1);
71                 }
72                 printf("open success.\n");
73                 return (0);
74         }
75         
76         return ((rc == 0) ? 0 : 1);
77 }