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.
32 * Copyright (c) 2011, Whamcloud, Inc.
35 * Test program to compare the attributes of a files to verify that it
36 * desired file attributes are present. This file predates availability
37 * of the stat(3) utility and is deprecated. Either test(3) ([ ]) or
38 * stat(3) should be used in all new tests.
40 * This file is part of Lustre, http://www.lustre.org/
41 * Lustre is a trademark of Sun Microsystems, Inc.
46 #include <sys/types.h>
55 usage (char *argv0, int help)
57 char *progname = strrchr(argv0, '/');
62 fprintf (help ? stdout : stderr,
63 "Usage: %s [flags] file[s]\n",
68 fprintf (stderr, " or try '-h' for help\n");
72 printf ("Check given files have...\n");
73 printf (" -p permission file must have required permissions\n");
74 printf (" -t dir|file|link file must be of the specified type\n");
75 printf (" -l link_name file must be a link to the given name\n");
76 printf (" -s size file must have the given size\n");
77 printf (" -u user file must be owned by given user\n");
78 printf (" -g group file must be owned by given group\n");
79 printf (" -f follow symlinks\n");
80 printf (" -a file must be absent\n");
81 printf (" -v increase verbosity\n");
82 printf (" -h print help\n");
83 printf (" Exit status is 0 on success, 1 on failure\n");
87 main (int argc, char **argv)
92 uid_t uid = (uid_t)-1;
93 gid_t gid = (gid_t)-1;
96 char *checklink = NULL;
102 while ((c = getopt (argc, argv, "p:t:l:s:u:g:avfh")) != -1)
106 perms = (int)strtol (optarg, &term, 0);
109 fprintf (stderr, "Can't parse permission %s\n", optarg);
119 size = strtoll (optarg, &term, 0);
122 fprintf (stderr, "Can't parse size %s\n", optarg);
130 uid = (uid_t)strtol (optarg + 1, &term, 0);
131 if (term == optarg + 1)
133 fprintf (stderr, "Can't parse numeric uid %s\n", optarg);
137 struct passwd *pw = getpwnam (optarg);
141 fprintf (stderr, "Can't find user %s\n", optarg);
151 gid = (gid_t)strtol (optarg + 1, &term, 0);
152 if (term == optarg + 1)
154 fprintf (stderr, "Can't parse numeric gid %s\n", optarg);
158 struct group *gr = getgrnam (optarg);
162 fprintf (stderr, "Can't find group %s\n", optarg);
198 char *fname = argv[optind];
199 int rc = follow ? stat64 (fname, &buf) : lstat64 (fname, &buf);
203 if (!(absent && errno == ENOENT))
206 printf ("Can't %sstat %s: %s\n",
208 fname, strerror (errno));
213 printf ("%s: absent OK\n", fname);
220 printf ("%s exists\n", fname);
226 if (!strcmp (type, "d") ||
227 !strcmp (type, "dir"))
229 if (!S_ISDIR (buf.st_mode))
232 printf ("%s is not a directory\n",
237 else if (!strcmp (type, "f") ||
238 !strcmp (type, "file"))
240 if (!S_ISREG (buf.st_mode))
243 printf ("%s is not a regular file\n",
248 else if (!strcmp (type, "l") ||
249 !strcmp (type, "link"))
251 if (!S_ISLNK (buf.st_mode))
254 printf ("%s is not a link\n",
261 fprintf (stderr, "Can't parse file type %s\n",
267 printf ("%s has type %s OK\n", fname, type);
272 if ((buf.st_mode & ~S_IFMT) != perms)
275 printf ("%s has perms 0%o, not 0%o\n",
276 fname, (buf.st_mode & ~S_IFMT),
282 printf ("%s has perms 0%o OK\n",
287 if (buf.st_size != size) {
289 printf("%s has size %lld, not %lld\n",
290 fname, (long long)buf.st_size,
296 printf("%s has size %lld OK\n", fname, size);
299 if (checklink != NULL)
301 static char lname[4<<10];
303 rc = readlink (fname, lname, sizeof (lname) - 1);
308 printf ("%s: can't read link: %s\n",
309 fname, strerror (errno));
314 if (strcmp (checklink, lname))
317 printf ("%s is a link to %s and not %s\n",
318 fname, lname, checklink);
323 printf ("%s links to %s OK\n", fname, checklink);
326 if (uid != (uid_t)-1)
328 if (buf.st_uid != uid)
331 printf ("%s is owned by user #%ld and not #%ld\n",
332 fname, (long)buf.st_uid, (long)uid);
337 printf ("%s is owned by user #%ld OK\n",
341 if (gid != (gid_t)-1)
343 if (buf.st_gid != gid)
346 printf ("%s is owned by group #%ld and not #%ld\n",
347 fname, (long)buf.st_gid, (long)gid);
352 printf ("%s is owned by group #%ld OK\n",
355 } while (++optind < argc);