From: Michael Meffie Date: Sat, 21 May 2011 11:28:41 +0000 (-0400) Subject: volinfo: print errors to stderr X-Git-Tag: openafs-devel-1_7_1~477 X-Git-Url: http://git.openafs.org/?p=openafs.git;a=commitdiff_plain;h=b7fe087745923bdea4571870799e484da67221d2 volinfo: print errors to stderr Print volinfo errors messages to stderr instead of a mix of stdout and stderr. Print a consistent program name. Change-Id: Ifadae52d56a75ff7d73d639ff1dd893b1a926a28 Reviewed-on: http://gerrit.openafs.org/4693 Tested-by: BuildBot Reviewed-by: Derrick Brashear --- diff --git a/src/vol/vol-info.c b/src/vol/vol-info.c index f85c2c2..bfc7d44 100644 --- a/src/vol/vol-info.c +++ b/src/vol/vol-info.c @@ -43,6 +43,8 @@ #include #include +static const char *progname = "volinfo"; + int DumpVnodes = 0; /* Dump everything, i.e. summary of all vnodes */ int DumpInodeNumber = 0; /* Dump inode numbers with vnodes */ int DumpDate = 0; /* Dump vnode date (server modify date) with vnode */ @@ -115,15 +117,16 @@ ReadHdr1(IHandle_t * ih, char *to, int size, u_int magic, u_int version) if (vsn->magic != magic) { bad++; - printf("Inode %s: Bad magic %x (%x): IGNORED\n", - PrintInode(NULL, ih->ih_ino), vsn->magic, magic); + fprintf(stderr, "%s: Inode %s: Bad magic %x (%x): IGNORED\n", + progname, PrintInode(NULL, ih->ih_ino), vsn->magic, magic); } /* Check is conditional, in case caller wants to inspect version himself */ if (version && vsn->version != version) { bad++; - printf("Inode %s: Bad version %x (%x): IGNORED\n", - PrintInode(NULL, ih->ih_ino), vsn->version, version); + fprintf(stderr, "%s: Inode %s: Bad version %x (%x): IGNORED\n", + progname, + PrintInode(NULL, ih->ih_ino), vsn->version, version); } if (bad && fixheader) { vsn->magic = magic; @@ -132,7 +135,9 @@ ReadHdr1(IHandle_t * ih, char *to, int size, u_int magic, u_int version) PrintInode(NULL, ih->ih_ino)); code = IH_IWRITE(ih, 0, to, size); if (code != size) { - printf("Write failed; header left in damaged state\n"); + fprintf(stderr, + "%s: Write failed for inode %s; header left in damaged state\n", + progname, PrintInode(NULL, ih->ih_ino)); } } else { if (!dsizeOnly && !saveinodes) { @@ -204,7 +209,7 @@ handleit(struct cmd_syndesc *as, void *arock) #ifndef AFS_NT40_ENV if (geteuid() != 0) { - printf("vol-info must be run as root; sorry\n"); + fprintf(stderr, "%s: Must be run as root; sorry\n", progname); exit(1); } #endif @@ -270,14 +275,15 @@ handleit(struct cmd_syndesc *as, void *arock) err = VAttachPartitions(); if (err) { - printf("%d partitions had errors during attach.\n", err); + fprintf(stderr, "%s: %d partitions had errors during attach.\n", + progname, err); } if (partName) { partP = VGetPartition(partName, 0); if (!partP) { - printf("%s is not an AFS partition name on this server.\n", - partName); + fprintf(stderr, "%s: %s is not an AFS partition name on this server.\n", + progname, partName); exit(1); } } @@ -294,7 +300,9 @@ handleit(struct cmd_syndesc *as, void *arock) if (!partP) { partP = FindCurrentPartition(); if (!partP) { - printf("Current partition is not a vice partition.\n"); + fprintf(stderr, + "%s: Current partition is not a vice partition.\n", + progname); exit(1); } } @@ -322,7 +330,8 @@ FindCurrentPartition(void) break; } if (!dp) { - printf("Current drive is not a valid vice partition.\n"); + fprintf(stderr, "%s: Current drive is not a valid vice partition.\n", + progname); } return dp; } @@ -336,6 +345,8 @@ FindCurrentPartition(void) struct DiskPartition64 *dp; if (!getcwd(partName, 1023)) { + fprintf(stderr, "%s: Failed to get current working directory: ", + progname); perror("pwd"); exit(1); } @@ -347,7 +358,8 @@ FindCurrentPartition(void) if (!(dp = VGetPartition(partName, 0))) { if (tmp) *p = tmp; - printf("%s is not a valid vice partition.\n", partName); + fprintf(stderr, "%s: %s is not a valid vice partition.\n", progname, + partName); exit(1); } return dp; @@ -391,7 +403,8 @@ HandlePart(struct DiskPartition64 *partP) #endif if ((dirp = opendir(p)) == NULL) { - printf("Can't read directory %s; giving up\n", p); + fprintf(stderr, "%s: Can't read directory %s; giving up\n", progname, + p); exit(1); } if (dsizeOnly && !saveinodes) @@ -428,7 +441,7 @@ HandleVolume(struct DiskPartition64 *dp, char *name) char headerName[1024]; if (online) { - printf("volinfo: -online not supported\n"); + fprintf(stderr, "%s: -online not supported\n", progname); exit(1); } else { afs_sfsize_t n; @@ -437,7 +450,8 @@ HandleVolume(struct DiskPartition64 *dp, char *name) VPartitionPath(dp), name); if ((fd = OS_OPEN(headerName, O_RDONLY, 0666)) == INVALID_FD || OS_SIZE(fd) < 0) { - printf("Volinfo: Cannot read volume header %s\n", name); + fprintf(stderr, "%s: Cannot read volume header %s\n", progname, + name); OS_CLOSE(fd); exit(1); } @@ -445,13 +459,14 @@ HandleVolume(struct DiskPartition64 *dp, char *name) if (n != sizeof(diskHeader) || diskHeader.stamp.magic != VOLUMEHEADERMAGIC) { - printf("Volinfo: Error reading volume header %s\n", name); + fprintf(stderr, "%s: Error reading volume header %s\n", progname, + name); exit(1); } if (diskHeader.stamp.version != VOLUMEHEADERVERSION) { - printf - ("Volinfo: Volume %s, version number is incorrect; volume needs salvage\n", - name); + fprintf(stderr, + "%s: Volume %s, version number is incorrect; volume needs salvage\n", + progname, name); exit(1); } DiskToVolumeHeader(&header, &diskHeader); @@ -471,11 +486,14 @@ HandleVolume(struct DiskPartition64 *dp, char *name) IH_INIT(ih, dp->device, header.parent, header.volumeInfo); fdP = IH_OPEN(ih); if (fdP == NULL) { - perror("opening volume info"); + fprintf(stderr, "%s: Error opening volume info: ", progname); + perror("open"); exit(1); } code = FDH_SIZE(fdP); if (code == -1) { + fprintf(stderr, "%s: Error getting size of volume info: ", + progname); perror("fstat"); exit(1); } @@ -491,11 +509,16 @@ HandleVolume(struct DiskPartition64 *dp, char *name) IH_INIT(ih, dp->device, header.parent, header.smallVnodeIndex); fdP = IH_OPEN(ih); if (fdP == NULL) { - perror("opening small vnode index"); + fprintf(stderr, "%s: Error opening small vnode index: ", + progname); + perror("open"); exit(1); } code = FDH_SIZE(fdP); if (code == -1) { + fprintf(stderr, + "%s: Error getting size of small vnode index: ", + progname); perror("fstat"); exit(1); } @@ -510,11 +533,16 @@ HandleVolume(struct DiskPartition64 *dp, char *name) IH_INIT(ih, dp->device, header.parent, header.largeVnodeIndex); fdP = IH_OPEN(ih); if (fdP == NULL) { - perror("opening large vnode index"); + fprintf(stderr, "%s: Error opening large vnode index: ", + progname); + perror("open"); exit(1); } code = FDH_SIZE(fdP); if (code == -1) { + fprintf(stderr, + "%s: Error getting size of large vnode index: ", + progname); perror("fstat"); exit(1); } @@ -532,11 +560,16 @@ HandleVolume(struct DiskPartition64 *dp, char *name) IH_INIT(ih, dp->device, header.parent, header.linkTable); fdP = IH_OPEN(ih); if (fdP == NULL) { - perror("opening link table index"); + fprintf(stderr, "%s: Error opening link table index: ", + progname); + perror("open"); exit(1); } code = FDH_SIZE(fdP); if (code == -1) { + fprintf(stderr, + "%s: Error getting size of link table index: ", + progname); perror("fstat"); exit(1); } @@ -555,7 +588,8 @@ HandleVolume(struct DiskPartition64 *dp, char *name) OS_CLOSE(fd); vp = AttachVolume(dp, name, &header); if (!vp) { - printf("Volinfo: Error attaching volume header %s\n", name); + fprintf(stderr, "%s: Error attaching volume header %s\n", + progname, name); return; } } @@ -678,7 +712,7 @@ NT_date(FILETIME * ft) if (!FileTimeToLocalFileTime(ft, &lft) || !FileTimeToSystemTime(&lft, &st)) { - printf("Time conversion failed.\n"); + fprintf(stderr, "%s: Time conversion failed.\n", progname); exit(1); } sprintf(result[next = ((next + 1) & 7)], "%4d/%02d/%02d.%2d:%2d:%2d", @@ -693,7 +727,8 @@ GetFileInfo(FD_t fd, int *size, char **ctime, char **mtime, char **atime) #ifdef AFS_NT40_ENV BY_HANDLE_FILE_INFORMATION fi; if (!GetFileInformationByHandle(fd, &fi)) { - printf("GetFileInformationByHandle failed, exiting\n"); + fprintf(stderr, "%s: GetFileInformationByHandle failed, exiting\n", + progname); exit(1); } *size = (int)fi.nFileSizeLow; @@ -703,7 +738,7 @@ GetFileInfo(FD_t fd, int *size, char **ctime, char **mtime, char **atime) #else struct afs_stat_st status; if (afs_fstat(fd, &status) == -1) { - printf("fstat failed %d\n", errno); + fprintf(stderr, "%s: fstat failed %d\n", progname, errno); exit(1); } *size = (int)status.st_size; @@ -735,13 +770,13 @@ PrintVnodes(Volume * vp, VnodeClass class) fdP = IH_OPEN(ih); if (fdP == NULL) { - printf("open failed\n"); + fprintf(stderr, "%s: open failed: ", progname); exit(1); } file = FDH_FDOPEN(fdP, "r"); if (!file) { - printf("fdopen failed\n"); + fprintf(stderr, "%s: fdopen failed\n", progname); exit(1); } @@ -778,16 +813,18 @@ PrintVnodes(Volume * vp, VnodeClass class) IH_INIT(ih1, V_device(vp), V_parentId(vp), ino); fdP1 = IH_OPEN(ih1); if (fdP1 == NULL) { - printf("Can't open inode %s error %d (ignored)\n", - PrintInode(NULL, ino), errno); + fprintf(stderr, + "%s: Can't open inode %s error %d (ignored)\n", + progname, PrintInode(NULL, ino), errno); continue; } snprintf(nfile, sizeof nfile, "TmpInode.%s", PrintInode(NULL, ino)); ofd = afs_open(nfile, O_CREAT | O_RDWR | O_TRUNC, 0600); if (ofd < 0) { - printf("Can't create file %s; error %d (ignored)\n", - nfile, errno); + fprintf(stderr, + "%s: Can't create file %s; error %d (ignored)\n", + progname, nfile, errno); continue; } total = bad = 0; @@ -799,9 +836,9 @@ PrintVnodes(Volume * vp, VnodeClass class) IH_RELEASE(ih1); close(ofd); unlink(nfile); - printf - ("Error while reading from inode %s (%d - ignored)\n", - PrintInode(NULL, ino), errno); + fprintf(stderr, + "%s: Error while reading from inode %s (%d - ignored)\n", + progname, PrintInode(NULL, ino), errno); bad = 1; break; } @@ -813,9 +850,9 @@ PrintVnodes(Volume * vp, VnodeClass class) IH_RELEASE(ih1); close(ofd); unlink(nfile); - printf - ("Error while writing to \"%s\" (%d - ignored)\n", - nfile, errno); + fprintf(stderr, + "%s: Error while writing to \"%s\" (%d - ignored)\n", + progname, nfile, errno); bad = 1; break; }