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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, Intel Corporation.
33 * Test program to compare the attributes of a files to verify that it
34 * desired file attributes are present. This file predates availability
35 * of the stat(3) utility and is deprecated. Either test(3) ([ ]) or
36 * stat(3) should be used in all new tests.
38 * This file is part of Lustre, http://www.lustre.org/
39 * Lustre is a trademark of Sun Microsystems, Inc.
44 #include <sys/types.h>
53 usage (char *argv0, int help)
55 char *progname = strrchr(argv0, '/');
60 fprintf (help ? stdout : stderr,
61 "Usage: %s [flags] file[s]\n",
66 fprintf (stderr, " or try '-h' for help\n");
70 printf ("Check given files have...\n");
71 printf (" -p permission file must have required permissions\n");
72 printf (" -t dir|file|link file must be of the specified type\n");
73 printf (" -l link_name file must be a link to the given name\n");
74 printf (" -s size file must have the given size\n");
75 printf (" -u user file must be owned by given user\n");
76 printf (" -g group file must be owned by given group\n");
77 printf (" -f follow symlinks\n");
78 printf (" -a file must be absent\n");
79 printf (" -v increase verbosity\n");
80 printf (" -h print help\n");
81 printf (" Exit status is 0 on success, 1 on failure\n");
85 main (int argc, char **argv)
90 uid_t uid = (uid_t)-1;
91 gid_t gid = (gid_t)-1;
94 char *checklink = NULL;
100 while ((c = getopt (argc, argv, "p:t:l:s:u:g:avfh")) != -1)
104 perms = (int)strtol (optarg, &term, 0);
107 fprintf (stderr, "Can't parse permission %s\n", optarg);
117 size = strtoll (optarg, &term, 0);
120 fprintf (stderr, "Can't parse size %s\n", optarg);
128 uid = (uid_t)strtol (optarg + 1, &term, 0);
129 if (term == optarg + 1)
131 fprintf (stderr, "Can't parse numeric uid %s\n", optarg);
135 struct passwd *pw = getpwnam (optarg);
139 fprintf (stderr, "Can't find user %s\n", optarg);
149 gid = (gid_t)strtol (optarg + 1, &term, 0);
150 if (term == optarg + 1)
152 fprintf (stderr, "Can't parse numeric gid %s\n", optarg);
156 struct group *gr = getgrnam (optarg);
160 fprintf (stderr, "Can't find group %s\n", optarg);
196 char *fname = argv[optind];
197 int rc = follow ? stat64 (fname, &buf) : lstat64 (fname, &buf);
201 if (!(absent && errno == ENOENT))
204 printf ("Can't %sstat %s: %s\n",
206 fname, strerror (errno));
211 printf ("%s: absent OK\n", fname);
218 printf ("%s exists\n", fname);
224 if (!strcmp (type, "d") ||
225 !strcmp (type, "dir"))
227 if (!S_ISDIR (buf.st_mode))
230 printf ("%s is not a directory\n",
235 else if (!strcmp (type, "f") ||
236 !strcmp (type, "file"))
238 if (!S_ISREG (buf.st_mode))
241 printf ("%s is not a regular file\n",
246 else if (!strcmp (type, "l") ||
247 !strcmp (type, "link"))
249 if (!S_ISLNK (buf.st_mode))
252 printf ("%s is not a link\n",
259 fprintf (stderr, "Can't parse file type %s\n",
265 printf ("%s has type %s OK\n", fname, type);
270 if ((buf.st_mode & ~S_IFMT) != perms)
273 printf ("%s has perms 0%o, not 0%o\n",
274 fname, (buf.st_mode & ~S_IFMT),
280 printf ("%s has perms 0%o OK\n",
285 if (buf.st_size != size) {
287 printf("%s has size %lld, not %lld\n",
288 fname, (long long)buf.st_size,
294 printf("%s has size %lld OK\n", fname, size);
297 if (checklink != NULL)
299 static char lname[4<<10];
301 rc = readlink (fname, lname, sizeof (lname) - 1);
306 printf ("%s: can't read link: %s\n",
307 fname, strerror (errno));
312 if (strcmp (checklink, lname))
315 printf ("%s is a link to %s and not %s\n",
316 fname, lname, checklink);
321 printf ("%s links to %s OK\n", fname, checklink);
324 if (uid != (uid_t)-1)
326 if (buf.st_uid != uid)
329 printf ("%s is owned by user #%ld and not #%ld\n",
330 fname, (long)buf.st_uid, (long)uid);
335 printf ("%s is owned by user #%ld OK\n",
339 if (gid != (gid_t)-1)
341 if (buf.st_gid != gid)
344 printf ("%s is owned by group #%ld and not #%ld\n",
345 fname, (long)buf.st_gid, (long)gid);
350 printf ("%s is owned by group #%ld OK\n",
353 } while (++optind < argc);