Whamcloud - gitweb
- added test_3b which emulates recursive mount. Does not pass yet.
[fs/lustre-release.git] / lustre / tests / checkstat.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <unistd.h>
6 #include <errno.h>
7 #include <string.h>
8 #include <pwd.h>
9 #include <grp.h>
10
11 void
12 usage (char *argv0, int help)
13 {
14         char *progname = strrchr(argv0, '/');
15
16         if (progname == NULL)
17                 progname = argv0;
18
19         fprintf (help ? stdout : stderr,
20                  "Usage: %s [flags] file[s]\n",
21                  progname);
22
23         if (!help)
24         {
25                 fprintf (stderr, "   or try '-h' for help\n");
26                 exit (1);
27         }
28
29         printf ("Check given files have...\n");
30         printf (" -p    permission       file must have required permissions\n");
31         printf (" -t    dir|file|link    file must be of the specified type\n");
32         printf (" -l    link_name        file must be a link to the given name\n");
33         printf (" -s    size             file must have the given size\n");
34         printf (" -u    user             file must be owned by given user\n");
35         printf (" -g    group            file must be owned by given group\n");
36         printf (" -f                     follow symlinks\n");
37         printf (" -a                     file must be absent\n");
38         printf (" -v                     increase verbosity\n");
39         printf (" -h                     print help\n");
40         printf (" Exit status is 0 on success, 1 on failure\n");
41 }
42
43 int
44 main (int argc, char **argv)
45 {
46         int           c;
47         struct stat64 buf;
48         int           perms = -1;
49         uid_t         uid = (uid_t)-1;
50         gid_t         gid = (gid_t)-1;
51         char         *type = NULL;
52         long          absent = 0;
53         char         *checklink = NULL;
54         int           verbose = 0;
55         long long     size = -1;
56         int           follow = 0;
57         char         *term;
58
59         while ((c = getopt (argc, argv, "p:t:l:s:u:g:avfh")) != -1)
60                 switch (c)
61                 {
62                 case 'p':
63                         perms = (int)strtol (optarg, &term, 0);
64                         if (term == optarg)
65                         {
66                                 fprintf (stderr, "Can't parse permission %s\n", optarg);
67                                 return (1);
68                         }
69                         break;
70
71                 case 'l':
72                         checklink = optarg;
73                         break;
74
75                 case 's':
76                         size = strtoll (optarg, &term, 0);
77                         if (term == optarg)
78                         {
79                                 fprintf (stderr, "Can't parse size %s\n", optarg);
80                                 return (1);
81                         }
82                         break;
83
84                 case 'u':
85                         if (*optarg == '#')
86                         {
87                                 uid = (uid_t)strtol (optarg + 1, &term, 0);
88                                 if (term == optarg + 1)
89                                 {
90                                         fprintf (stderr, "Can't parse numeric uid %s\n", optarg);
91                                         return (1);
92                                 }
93                         }else {
94                                 int i,flag=0;
95                                 char * temp ;
96                                 temp = optarg;
97                                 for(i=0;i<strlen(optarg);i++)
98                                 {
99                                         if(!isdigit(*temp))     
100                                         {
101                                                 flag=1;
102                                                 break;
103                                         }
104                                         else
105                                                 temp++; 
106                                 }
107                                 if(flag)
108                                 {
109                                        struct passwd *pw = getpwnam (optarg);
110
111                                         if (pw == NULL)
112                                         {
113                                                 fprintf (stderr, "Can't find user %s\n", optarg);
114                                                 return (1);
115                                         }
116                                         uid = pw->pw_uid;
117         
118                                 }
119                                 else
120                                         uid=(uid_t)atol(optarg);
121                         }
122                         break;
123
124                 case 'g':
125                         if (*optarg == '#')
126                         {
127                                 gid = (gid_t)strtol (optarg + 1, &term, 0);
128                                 if (term == optarg + 1)
129                                 {
130                                         fprintf (stderr, "Can't parse numeric gid %s\n", optarg);
131                                         return (1);
132                                 }
133                         } else {
134                                 struct group *gr = getgrnam (optarg);
135
136                                 if (gr == NULL)
137                                 {
138                                         fprintf (stderr, "Can't find group %s\n", optarg);
139                                         return (1);
140                                 }
141                                 uid = gr->gr_gid;
142                         }
143                         break;
144
145                 case 't':
146                         type = optarg;
147                         break;
148
149                 case 'a':
150                         absent = 1;
151                         break;
152
153                 case 'v':
154                         verbose++;
155                         break;
156
157                 case 'f':
158                         follow++;
159                         break;
160
161                 case 'h':
162                         usage (argv[0], 1);
163                         return (0);
164
165                 default:
166                         usage (argv[0], 0);
167                 }
168
169         if (optind == argc)
170                 usage (argv[0], 0);
171
172         do
173         {
174                 char *fname = argv[optind];
175                 int rc = follow ? stat64 (fname, &buf) : lstat64 (fname, &buf);
176
177                 if (rc != 0)
178                 {
179                         if (!(absent && errno == ENOENT))
180                         {
181                                 if (verbose)
182                                         printf ("Can't %sstat %s: %s\n",
183                                                 follow ? "" : "l",
184                                                 fname, strerror (errno));
185                                 return (1);
186                         }
187
188                         if (verbose)
189                                 printf ("%s: absent OK\n", fname);
190                         continue;
191                 }
192
193                 if (absent)
194                 {
195                         if (verbose)
196                                 printf ("%s exists\n", fname);
197                         return (1);
198                 }
199
200                 if (type != NULL)
201                 {
202                         if (!strcmp (type, "d") ||
203                             !strcmp (type, "dir"))
204                         {
205                                 if (!S_ISDIR (buf.st_mode))
206                                 {
207                                         if (verbose)
208                                                 printf ("%s is not a directory\n",
209                                                          fname);
210                                         return (1);
211                                 }
212                         }
213                         else if (!strcmp (type, "f") ||
214                                  !strcmp (type, "file"))
215                         {
216                                 if (!S_ISREG (buf.st_mode))
217                                 {
218                                         if (verbose)
219                                                 printf ("%s is not a regular file\n",
220                                                         fname);
221                                         return (1);
222                                 }
223                         }
224                         else if (!strcmp (type, "l") ||
225                                  !strcmp (type, "link"))
226                         {
227                                 if (!S_ISLNK (buf.st_mode))
228                                 {
229                                         if (verbose)
230                                                 printf ("%s is not a link\n",
231                                                         fname);
232                                         return (1);
233                                 }
234                         }
235                         else
236                         {
237                                 fprintf (stderr, "Can't parse file type %s\n",
238                                          type);
239                                 return (1);
240                         }
241
242                         if (verbose)
243                                 printf ("%s has type %s OK\n", fname, type);
244                 }
245
246                 if (perms != -1)
247                 {
248                         if ((buf.st_mode & ~S_IFMT) != perms)
249                         {
250                                 if (verbose)
251                                         printf ("%s has perms 0%o, not 0%o\n",
252                                                 fname, (buf.st_mode & ~S_IFMT),
253                                                 perms);
254                                 return (1);
255                         }
256
257                         if (verbose)
258                                 printf ("%s has perms 0%o OK\n",
259                                         fname, perms);
260                 }
261
262                 if (size != -1)
263                 {
264                         if (buf.st_size != size)
265                         {
266                                 if (verbose)
267                                         printf ("%s has size %Ld, not %Ld\n",
268                                                 fname, (long long)buf.st_size,
269                                                 size);
270                                 return (1);
271                         }
272
273                         if (verbose)
274                                 printf ("%s has size %Ld OK\n", fname, size);
275                 }
276
277                 if (checklink != NULL)
278                 {
279                         static char lname[4<<10];
280
281                         rc = readlink (fname, lname, sizeof (lname) - 1);
282
283                         if (rc < 0)
284                         {
285                                 if (verbose)
286                                         printf ("%s: can't read link: %s\n",
287                                                 fname, strerror (errno));
288                                 return (1);
289                         }
290
291                         lname[rc] = 0;
292                         if (strcmp (checklink, lname))
293                         {
294                                 if (verbose)
295                                         printf ("%s is a link to %s and not %s\n",
296                                                 fname, lname, checklink);
297                                 return (1);
298                         }
299
300                         if (verbose)
301                                 printf ("%s links to %s OK\n", fname, checklink);
302                 }
303
304                 if (uid != (uid_t)-1)
305                 {
306                         if (buf.st_uid != uid)
307                         {
308                                 if (verbose)
309                                         printf ("%s is owned by user #%ld and not #%ld\n",
310                                                 fname, (long)buf.st_uid, (long)uid);
311                                 return (1);
312                         }
313
314                         if (verbose)
315                                 printf ("%s is owned by user #%ld OK\n",
316                                         fname, (long)uid);
317                 }
318
319                 if (gid != (gid_t)-1)
320                 {
321                         if (buf.st_gid != gid)
322                         {
323                                 if (verbose)
324                                         printf ("%s is owned by group #%ld and not #%ld\n",
325                                                 fname, (long)buf.st_gid, (long)gid);
326                                 return (1);
327                         }
328
329                         if (verbose)
330                                 printf ("%s is owned by group #%ld OK\n",
331                                         fname, (long)gid);
332                 }
333         } while (++optind < argc);
334
335         return (0);
336 }