Commit 0fc66367 authored by Federico Vaga's avatar Federico Vaga Committed by Alessandro Rubini

tools/zio-dump: add -n to stop after reading N blocks

The -n option tells how many blocks to read from the block source.
The following example will read 3 blocks from zio-zero, channel 0

./zio-dump -n 3 /dev/zio/zzero-0000-0-0-*
Signed-off-by: 's avatarFederico Vaga <federico.vaga@gmail.com>
Acked-by: Alessandro Rubini's avatarAlessandro Rubini <rubini@gnudd.com>
parent 24f00e40
...@@ -177,25 +177,28 @@ void help(char *name) ...@@ -177,25 +177,28 @@ void help(char *name)
"Use: \"%s [<opts>] <ctrl-file> <data-file> [...]\"\n" "Use: \"%s [<opts>] <ctrl-file> <data-file> [...]\"\n"
" or \"%s -c <combined-file>\"\n", " or \"%s -c <combined-file>\"\n",
name, name, name); name, name, name);
fprintf(stderr, " -a dump attributes too\n"); fprintf(stderr, " -a dump attributes too\n");
fprintf(stderr, " -A dump all attributes\n"); fprintf(stderr, " -A dump all attributes\n");
fprintf(stderr, " -c combined file (control+data)\n"); fprintf(stderr, " -c combined file (control+data)\n");
fprintf(stderr, " -n <number> stop after that many blocks\n");
exit(1); exit(1);
} }
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
FILE *f; FILE *f;
char *rest;
char *outfname; char *outfname;
int *cfd; /* control file descriptors */ int *cfd; /* control file descriptors */
int *dfd; /* data file descriptors */ int *dfd; /* data file descriptors */
fd_set control_set, ready_set; fd_set control_set, ready_set;
int c, i, j, maxfd, ndev; int c, i, j, maxfd, ndev;
int combined = 0; int combined = 0;
unsigned long nblocks = -1; /* forever by default */
prgname = argv[0]; prgname = argv[0];
while ((c = getopt (argc, argv, "aAcm")) != -1) { while ((c = getopt (argc, argv, "aAcmn:")) != -1) {
switch(c) { switch(c) {
case 'a': case 'a':
opt_print_attr = 1; opt_print_attr = 1;
...@@ -209,6 +212,14 @@ int main(int argc, char **argv) ...@@ -209,6 +212,14 @@ int main(int argc, char **argv)
case 'm': case 'm':
opt_print_memaddr = 1; opt_print_memaddr = 1;
break; break;
case 'n':
nblocks = strtoul(optarg, &rest, 0);
if (rest && *rest) {
fprintf(stderr, "%s: not a number \"%s\"\n",
argv[0], optarg);
help(prgname);
}
break;
default: default:
help(prgname); help(prgname);
} }
...@@ -266,25 +277,29 @@ int main(int argc, char **argv) ...@@ -266,25 +277,29 @@ int main(int argc, char **argv)
setlinebuf(stdout); setlinebuf(stdout);
setbuf(f, NULL); setbuf(f, NULL);
/* now read control and then data, forever (unless "combined") */ if (!combined) {
while (!combined) { /* Read control and then data. Forever or nblocks if > 0 */
ready_set = control_set; while (nblocks) {
i = select(maxfd + 1, &ready_set, NULL, NULL, NULL); if (nblocks > 0)
if (i < 0 && errno == EINTR) nblocks--;
continue; ready_set = control_set;
if (i < 0) { i = select(maxfd + 1, &ready_set, NULL, NULL, NULL);
fprintf(stderr, "%s: select(): %s\n", prgname, if (i < 0 && errno == EINTR)
strerror(errno)); continue;
exit(1); if (i < 0) {
fprintf(stderr, "%s: select(): %s\n", prgname,
strerror(errno));
exit(1);
}
for (j = 0; j < ndev; j++)
if (FD_ISSET(cfd[j], &ready_set))
read_channel(cfd[j], dfd[j], f);
} }
for (j = 0; j < ndev; j++) exit(0);
if (FD_ISSET(cfd[j], &ready_set))
read_channel(cfd[j], dfd[j], f);
} }
/* /*
* So, we are reading one combined file. Just open it and * So, we are reading one combined file. Just open it and
* read it forever (the function read_channel() will block) * read it forever or nblocks if > 0; read_channel() is blocking.
*/ */
cfd[0] = open(argv[1], O_RDONLY); cfd[0] = open(argv[1], O_RDONLY);
if (cfd[0] < 0) { if (cfd[0] < 0) {
...@@ -292,6 +307,10 @@ int main(int argc, char **argv) ...@@ -292,6 +307,10 @@ int main(int argc, char **argv)
strerror(errno)); strerror(errno));
exit(1); exit(1);
} }
while (1) while (nblocks) {
read_channel(cfd[0], cfd[0], f); read_channel(cfd[0], cfd[0], f);
if (nblocks > 0)
nblocks--;
}
return 0;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment