4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, Intel Corporation.
29 * Test program to compare the attributes of a files to verify that it
30 * desired file attributes are present. This file predates availability
31 * of the stat(3) utility and is deprecated. Either test(3) ([ ]) or
32 * stat(3) should be used in all new tests.
34 * This file is part of Lustre, http://www.lustre.org/
35 * Lustre is a trademark of Sun Microsystems, Inc.
40 #include <sys/types.h>
49 usage (char *argv0, int help)
51 char *progname = strrchr(argv0, '/');
56 fprintf (help ? stdout : stderr,
57 "Usage: %s [flags] file[s]\n",
62 fprintf (stderr, " or try '-h' for help\n");
66 printf ("Check given files have...\n");
67 printf (" -p permission file must have required permissions\n");
68 printf (" -t dir|file|link file must be of the specified type\n");
69 printf (" -l link_name file must be a link to the given name\n");
70 printf (" -s size file must have the given size\n");
71 printf (" -u user file must be owned by given user\n");
72 printf (" -g group file must be owned by given group\n");
73 printf (" -f follow symlinks\n");
74 printf (" -a file must be absent\n");
75 printf (" -v increase verbosity\n");
76 printf (" -h print help\n");
77 printf (" Exit status is 0 on success, 1 on failure\n");
81 main (int argc, char **argv)
86 uid_t uid = (uid_t)-1;
87 gid_t gid = (gid_t)-1;
90 char *checklink = NULL;
96 while ((c = getopt (argc, argv, "p:t:l:s:u:g:avfh")) != -1)
100 perms = (int)strtol (optarg, &term, 0);
103 fprintf (stderr, "Can't parse permission %s\n", optarg);
113 size = strtoll (optarg, &term, 0);
116 fprintf (stderr, "Can't parse size %s\n", optarg);
124 uid = (uid_t)strtol (optarg + 1, &term, 0);
125 if (term == optarg + 1)
127 fprintf (stderr, "Can't parse numeric uid %s\n", optarg);
131 struct passwd *pw = getpwnam (optarg);
135 fprintf (stderr, "Can't find user %s\n", optarg);
145 gid = (gid_t)strtol (optarg + 1, &term, 0);
146 if (term == optarg + 1)
148 fprintf (stderr, "Can't parse numeric gid %s\n", optarg);
152 struct group *gr = getgrnam (optarg);
156 fprintf (stderr, "Can't find group %s\n", optarg);
192 char *fname = argv[optind];
193 int rc = follow ? stat64 (fname, &buf) : lstat64 (fname, &buf);
197 if (!(absent && errno == ENOENT))
200 printf ("Can't %sstat %s: %s\n",
202 fname, strerror (errno));
207 printf ("%s: absent OK\n", fname);
214 printf ("%s exists\n", fname);
220 if (!strcmp (type, "d") ||
221 !strcmp (type, "dir"))
223 if (!S_ISDIR (buf.st_mode))
226 printf ("%s is not a directory\n",
231 else if (!strcmp (type, "f") ||
232 !strcmp (type, "file"))
234 if (!S_ISREG (buf.st_mode))
237 printf ("%s is not a regular file\n",
242 else if (!strcmp (type, "l") ||
243 !strcmp (type, "link"))
245 if (!S_ISLNK (buf.st_mode))
248 printf ("%s is not a link\n",
255 fprintf (stderr, "Can't parse file type %s\n",
261 printf ("%s has type %s OK\n", fname, type);
266 if ((buf.st_mode & ~S_IFMT) != perms)
269 printf ("%s has perms 0%o, not 0%o\n",
270 fname, (buf.st_mode & ~S_IFMT),
276 printf ("%s has perms 0%o OK\n",
281 if (buf.st_size != size) {
283 printf("%s has size %lld, not %lld\n",
284 fname, (long long)buf.st_size,
290 printf("%s has size %lld OK\n", fname, size);
293 if (checklink != NULL)
295 static char lname[4<<10];
297 rc = readlink (fname, lname, sizeof (lname) - 1);
302 printf ("%s: can't read link: %s\n",
303 fname, strerror (errno));
308 if (strcmp (checklink, lname))
311 printf ("%s is a link to %s and not %s\n",
312 fname, lname, checklink);
317 printf ("%s links to %s OK\n", fname, checklink);
320 if (uid != (uid_t)-1)
322 if (buf.st_uid != uid)
325 printf ("%s is owned by user #%ld and not #%ld\n",
326 fname, (long)buf.st_uid, (long)uid);
331 printf ("%s is owned by user #%ld OK\n",
335 if (gid != (gid_t)-1)
337 if (buf.st_gid != gid)
340 printf ("%s is owned by group #%ld and not #%ld\n",
341 fname, (long)buf.st_gid, (long)gid);
346 printf ("%s is owned by group #%ld OK\n",
349 } while (++optind < argc);