From 272e04933338af39fec793ec62be32887d68e0b7 Mon Sep 17 00:00:00 2001 From: eeb Date: Mon, 9 Dec 2002 21:50:24 +0000 Subject: [PATCH] * Added some checking to sanity.sh + new utility checkstat (-h for usage) --- lustre/tests/checkstat.c | 229 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 229 insertions(+) create mode 100644 lustre/tests/checkstat.c diff --git a/lustre/tests/checkstat.c b/lustre/tests/checkstat.c new file mode 100644 index 0000000..0624d76 --- /dev/null +++ b/lustre/tests/checkstat.c @@ -0,0 +1,229 @@ +#include +#include +#include +#include +#include +#include +#include + +void +usage (char *argv0, int help) +{ + char *progname = strrchr(argv0, '/'); + + if (progname == NULL) + progname = argv0; + + fprintf (help ? stdout : stderr, + "Usage: %s [-p perms][-t type][-l link][-a][-v] file[s]\n", + progname); + + if (!help) + { + fprintf (stderr, " or try '-h' for help\n"); + exit (1); + } + + printf ("Check given files have...\n"); + printf (" -p permission file must have required permissions\n"); + printf (" -t dir|file|link file must be of the specified type\n"); + printf (" -l link_name file must be a link to the given name\n"); + printf (" -s size file must have the given size\n"); + printf (" -f follow symlinks\n"); + printf (" -a file must be absent\n"); + printf (" -v increase verbosity\n"); + printf (" -h print help\n"); + printf (" Exit status is 0 on success, 1 on failure\n"); +} + +int +main (int argc, char **argv) +{ + int c; + struct stat64 buf; + int perms = -1; + char *type = NULL; + long absent = 0; + char *checklink = NULL; + int verbose = 0; + long long size = -1; + int follow = 0; + char *term; + + while ((c = getopt (argc, argv, "p:t:l:s:avfh")) != -1) + switch (c) + { + case 'p': + perms = (int)strtol (optarg, &term, 0); + if (term == optarg) + { + fprintf (stderr, "Can't parse permission %s\n", optarg); + return (1); + } + break; + + case 'l': + checklink = optarg; + break; + + case 's': + size = strtoll (optarg, &term, 0); + if (term == optarg) + { + fprintf (stderr, "Can't parse size %s\n", optarg); + return (1); + } + break; + + case 't': + type = optarg; + break; + + case 'a': + absent = 1; + break; + + case 'v': + verbose++; + break; + + case 'f': + follow++; + break; + + case 'h': + usage (argv[0], 1); + return (0); + + default: + usage (argv[0], 0); + } + + if (optind == argc) + usage (argv[0], 0); + + do + { + char *fname = argv[optind]; + int rc = follow ? stat64 (fname, &buf) : lstat64 (fname, &buf); + + if (rc != 0) + { + if (!(absent && errno == ENOENT)) + { + if (verbose) + printf ("Can't %sstat %s: %s\n", + follow ? "" : "l", + fname, strerror (errno)); + return (1); + } + + if (verbose) + printf ("%s: absent OK\n", fname); + continue; + } + + if (type != NULL) + { + if (!strcmp (type, "d") || + !strcmp (type, "dir")) + { + if (!S_ISDIR (buf.st_mode)) + { + if (verbose) + printf ("%s is not a directory\n", + fname); + return (1); + } + } + else if (!strcmp (type, "f") || + !strcmp (type, "file")) + { + if (!S_ISREG (buf.st_mode)) + { + if (verbose) + printf ("%s is not a regular file\n", + fname); + return (1); + } + } + else if (!strcmp (type, "l") || + !strcmp (type, "link")) + { + if (!S_ISLNK (buf.st_mode)) + { + if (verbose) + printf ("%s is not a link\n", + fname); + return (1); + } + } + else + { + fprintf (stderr, "Can't parse file type %s\n", type); + return (1); + } + + if (verbose) + printf ("%s has type %s OK\n", fname, type); + } + + if (perms != -1) + { + if ((buf.st_mode & ~S_IFMT) != perms) + { + if (verbose) + printf ("%s has perms 0%o, not 0%o\n", + fname, (buf.st_mode & ~S_IFMT), perms); + return (1); + } + + if (verbose) + printf ("%s has perms 0%o OK\n", + fname, perms); + } + + if (size != -1) + { + if (buf.st_size != size) + { + if (verbose) + printf ("%s has size %Ld, not %Ld\n", + fname, (long long)buf.st_size, size); + return (1); + } + + if (verbose) + printf ("%s has size %Ld OK\n", fname, size); + } + + if (checklink != NULL) + { + static char lname[4<<10]; + + rc = readlink (fname, lname, sizeof (lname) - 1); + + if (rc < 0) + { + if (verbose) + printf ("%s: can't read link: %s\n", + fname, strerror (errno)); + return (1); + } + + lname[rc] = 0; + if (strcmp (checklink, lname)) + { + if (verbose) + printf ("%s is a link to %s and not %s\n", + fname, lname, checklink); + return (1); + } + + if (verbose) + printf ("%s links to %s OK\n", fname, checklink); + } + } while (++optind < argc); + + return (0); +} -- 1.8.3.1