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",
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");
79 /* using realpath() implies the paths must be resolved/exist
80 * so this will fail for dangling links
82 int check_canonical(char *lname, char *checklink, int verbose)
85 char *checklink_canon;
87 lname_canon = realpath(lname, NULL);
88 if (lname_canon == NULL) {
90 printf("%s: can't canonicalize: %s\n",
91 lname, strerror(errno));
95 checklink_canon = realpath(checklink, NULL);
96 if (checklink_canon == NULL) {
98 printf("%s: can't canonicalize: %s\n",
99 checklink, strerror(errno));
103 if (strcmp(checklink_canon, lname_canon)) {
105 free(checklink_canon);
109 free(checklink_canon);
114 main(int argc, char **argv)
119 uid_t uid = (uid_t)-1;
120 gid_t gid = (gid_t)-1;
123 char *checklink = NULL;
129 while ((c = getopt(argc, argv, "p:t:l:s:u:g:avfh")) != -1)
132 perms = (int)strtol(optarg, &term, 0);
133 if (term == optarg) {
134 fprintf(stderr, "Can't parse permission %s\n",
145 size = strtoll(optarg, &term, 0);
146 if (term == optarg) {
147 fprintf(stderr, "Can't parse size %s\n",
154 if (*optarg == '#') {
155 uid = (uid_t)strtol(optarg + 1, &term, 0);
157 if (term == optarg + 1) {
159 "Can't parse numeric uid %s\n",
164 struct passwd *pw = getpwnam(optarg);
167 fprintf(stderr, "Can't find user %s\n",
176 if (*optarg == '#') {
177 gid = (gid_t)strtol(optarg + 1, &term, 0);
179 if (term == optarg + 1) {
181 "Can't parse numeric gid %s\n",
186 struct group *gr = getgrnam(optarg);
190 "Can't find group %s\n",
226 char *fname = argv[optind];
227 int rc = follow ? stat64(fname, &buf) : lstat64(fname, &buf);
230 if (!(absent && errno == ENOENT)) {
232 printf("Can't %sstat %s: %s\n",
234 fname, strerror(errno));
239 printf("%s: absent OK\n", fname);
245 printf("%s exists\n", fname);
250 if (!strcmp(type, "d") ||
251 !strcmp(type, "dir")) {
252 if (!S_ISDIR(buf.st_mode)) {
254 printf("%s is not a directory\n",
258 } else if (!strcmp(type, "f") ||
259 !strcmp(type, "file")) {
260 if (!S_ISREG(buf.st_mode)) {
262 printf("%s is not a regular file\n",
266 } else if (!strcmp(type, "l") ||
267 !strcmp(type, "link")) {
268 if (!S_ISLNK(buf.st_mode)) {
270 printf("%s is not a link\n",
275 fprintf(stderr, "Can't parse file type %s\n",
281 printf("%s has type %s OK\n", fname, type);
285 if ((buf.st_mode & ~S_IFMT) != perms) {
287 printf("%s has perms 0%o, not 0%o\n",
288 fname, (buf.st_mode & ~S_IFMT),
294 printf("%s has perms 0%o OK\n",
299 if (buf.st_size != size) {
301 printf("%s has size %lld, not %lld\n",
302 fname, (long long)buf.st_size,
308 printf("%s has size %lld OK\n", fname, size);
312 static char lname[4 << 10];
314 rc = readlink(fname, lname, sizeof(lname) - 1);
318 printf("%s: can't read link: %s\n",
319 fname, strerror(errno));
325 /* just in case, try to also match the canonicalized
328 if (strcmp(checklink, lname) &&
329 check_canonical(lname, checklink, verbose)) {
331 printf("%s is a link to %s and not %s\n",
332 fname, lname, checklink);
337 printf("%s links to %s OK\n", fname, checklink);
340 if (uid != (uid_t)-1) {
341 if (buf.st_uid != uid) {
343 printf("%s is owned by user #%ld and not #%ld\n",
344 fname, (long)buf.st_uid, (long)uid);
349 printf("%s is owned by user #%ld OK\n",
353 if (gid != (gid_t)-1) {
354 if (buf.st_gid != gid) {
356 printf("%s is owned by group #%ld and not #%ld\n",
357 fname, (long)buf.st_gid, (long)gid);
362 printf("%s is owned by group #%ld OK\n",
365 } while (++optind < argc);