1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
29 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
39 #include <sys/types.h>
48 usage (char *argv0, int help)
50 char *progname = strrchr(argv0, '/');
55 fprintf (help ? stdout : stderr,
56 "Usage: %s [flags] file[s]\n",
61 fprintf (stderr, " or try '-h' for help\n");
65 printf ("Check given files have...\n");
66 printf (" -p permission file must have required permissions\n");
67 printf (" -t dir|file|link file must be of the specified type\n");
68 printf (" -l link_name file must be a link to the given name\n");
69 printf (" -s size file must have the given size\n");
70 printf (" -u user file must be owned by given user\n");
71 printf (" -g group file must be owned by given group\n");
72 printf (" -f follow symlinks\n");
73 printf (" -a file must be absent\n");
74 printf (" -v increase verbosity\n");
75 printf (" -h print help\n");
76 printf (" Exit status is 0 on success, 1 on failure\n");
80 main (int argc, char **argv)
85 uid_t uid = (uid_t)-1;
86 gid_t gid = (gid_t)-1;
89 char *checklink = NULL;
95 while ((c = getopt (argc, argv, "p:t:l:s:u:g:avfh")) != -1)
99 perms = (int)strtol (optarg, &term, 0);
102 fprintf (stderr, "Can't parse permission %s\n", optarg);
112 size = strtoll (optarg, &term, 0);
115 fprintf (stderr, "Can't parse size %s\n", optarg);
123 uid = (uid_t)strtol (optarg + 1, &term, 0);
124 if (term == optarg + 1)
126 fprintf (stderr, "Can't parse numeric uid %s\n", optarg);
130 struct passwd *pw = getpwnam (optarg);
134 fprintf (stderr, "Can't find user %s\n", optarg);
144 gid = (gid_t)strtol (optarg + 1, &term, 0);
145 if (term == optarg + 1)
147 fprintf (stderr, "Can't parse numeric gid %s\n", optarg);
151 struct group *gr = getgrnam (optarg);
155 fprintf (stderr, "Can't find group %s\n", optarg);
191 char *fname = argv[optind];
192 int rc = follow ? stat64 (fname, &buf) : lstat64 (fname, &buf);
196 if (!(absent && errno == ENOENT))
199 printf ("Can't %sstat %s: %s\n",
201 fname, strerror (errno));
206 printf ("%s: absent OK\n", fname);
213 printf ("%s exists\n", fname);
219 if (!strcmp (type, "d") ||
220 !strcmp (type, "dir"))
222 if (!S_ISDIR (buf.st_mode))
225 printf ("%s is not a directory\n",
230 else if (!strcmp (type, "f") ||
231 !strcmp (type, "file"))
233 if (!S_ISREG (buf.st_mode))
236 printf ("%s is not a regular file\n",
241 else if (!strcmp (type, "l") ||
242 !strcmp (type, "link"))
244 if (!S_ISLNK (buf.st_mode))
247 printf ("%s is not a link\n",
254 fprintf (stderr, "Can't parse file type %s\n",
260 printf ("%s has type %s OK\n", fname, type);
265 if ((buf.st_mode & ~S_IFMT) != perms)
268 printf ("%s has perms 0%o, not 0%o\n",
269 fname, (buf.st_mode & ~S_IFMT),
275 printf ("%s has perms 0%o OK\n",
281 if (buf.st_size != size)
284 printf ("%s has size %Ld, not %Ld\n",
285 fname, (long long)buf.st_size,
291 printf ("%s has size %Ld OK\n", fname, size);
294 if (checklink != NULL)
296 static char lname[4<<10];
298 rc = readlink (fname, lname, sizeof (lname) - 1);
303 printf ("%s: can't read link: %s\n",
304 fname, strerror (errno));
309 if (strcmp (checklink, lname))
312 printf ("%s is a link to %s and not %s\n",
313 fname, lname, checklink);
318 printf ("%s links to %s OK\n", fname, checklink);
321 if (uid != (uid_t)-1)
323 if (buf.st_uid != uid)
326 printf ("%s is owned by user #%ld and not #%ld\n",
327 fname, (long)buf.st_uid, (long)uid);
332 printf ("%s is owned by user #%ld OK\n",
336 if (gid != (gid_t)-1)
338 if (buf.st_gid != gid)
341 printf ("%s is owned by group #%ld and not #%ld\n",
342 fname, (long)buf.st_gid, (long)gid);
347 printf ("%s is owned by group #%ld OK\n",
350 } while (++optind < argc);