isosize: iterate over all arguments even when something fails

Earlier the command exit too early if one of the arguments failed.  After
this change all arguments are examined, and command return value will have
information what happen during processing.

Based on patch from Sami Kerola <kerolasa@iki.fi>

Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Karel Zak 2017-08-21 11:11:11 +02:00
parent a653878c45
commit c380180289
2 changed files with 45 additions and 13 deletions

View File

@ -32,6 +32,23 @@ filesystem can be marginally larger than the actual size of the
iso9660 filesystem. One reason for this is that cd writers
are allowed to add "run out" sectors at the end of an iso9660
image.
.SH "EXIT STATUS"
.RS
.PD 0
.TP
.B 0
success
.TP
.B 1
generic failure, such as invalid usage
.TP
.B 32
all failed
.TP
.B 64
some failed
.PD
.RE
.SH AVAILABILITY
The isosize command is part of the util-linux package and is available from
.UR https://\:www.kernel.org\:/pub\:/linux\:/utils\:/util-linux/

View File

@ -30,6 +30,9 @@
#include "strutils.h"
#include "closestream.h"
#define ISOSIZE_EXIT_ALLFAILED 32
#define ISOSIZE_EXIT_SOMEOK 64
static int is_iso(int fd)
{
char label[8];
@ -86,22 +89,26 @@ static int isonum_733(unsigned char *p, int xflag)
return (le);
}
static void isosize(int argc, char *filenamep, int xflag, long divisor)
static int isosize(int argc, char *filenamep, int xflag, long divisor)
{
int fd, nsecs, ssize;
int fd, nsecs, ssize, rc = -1;
unsigned char volume_space_size[8];
unsigned char logical_block_size[4];
if ((fd = open(filenamep, O_RDONLY)) < 0)
err(EXIT_FAILURE, _("cannot open %s"), filenamep);
if ((fd = open(filenamep, O_RDONLY)) < 0) {
warn(_("cannot open %s"), filenamep);
goto done;
}
if (is_iso(fd))
warnx(_("%s: might not be an ISO filesystem"), filenamep);
if (pread(fd, volume_space_size, sizeof(volume_space_size), 0x8050) <= 0 ||
pread(fd, logical_block_size, sizeof(logical_block_size), 0x8080) <= 0) {
if (pread(fd, volume_space_size, sizeof(volume_space_size), 0x8050) != sizeof(volume_space_size) ||
pread(fd, logical_block_size, sizeof(logical_block_size), 0x8080) != sizeof(logical_block_size)) {
if (errno)
err(EXIT_FAILURE, _("read error on %s"), filenamep);
errx(EXIT_FAILURE, _("read error on %s"), filenamep);
warn(_("read error on %s"), filenamep);
else
warnx(_("read error on %s"), filenamep);
goto done;
}
nsecs = isonum_733(volume_space_size, xflag);
@ -123,7 +130,11 @@ static void isosize(int argc, char *filenamep, int xflag, long divisor)
printf("%lld\n", (product * ssize) / divisor);
}
close(fd);
rc = 0;
done:
if (fd >= 0)
close(fd);
return rc;
}
static void __attribute__((__noreturn__)) usage(void)
@ -149,7 +160,7 @@ static void __attribute__((__noreturn__)) usage(void)
int main(int argc, char **argv)
{
int j, ct, opt, xflag = 0;
int j, ct_err = 0, ct, opt, xflag = 0;
long divisor = 0;
static const struct option longopts[] = {
@ -191,8 +202,12 @@ int main(int argc, char **argv)
errtryhelp(EXIT_FAILURE);
}
for (j = optind; j < argc; j++)
isosize(ct, argv[j], xflag, divisor);
for (j = optind; j < argc; j++) {
if (isosize(ct, argv[j], xflag, divisor) != 0)
ct_err++;
}
return EXIT_SUCCESS;
return ct == ct_err ? ISOSIZE_EXIT_ALLFAILED : /* all failed */
ct_err ? ISOSIZE_EXIT_SOMEOK : /* some ok */
EXIT_SUCCESS; /* all success */
}