Commit fa8d3b00f9a1d0cdedcac3c26993f0b0230f2771

Authored by Simon Glass
1 parent 7e8ffa4ed8

x86: ifdtool: Display filename when file errors are reported

When a file is missing it helps to know which file. Update the error message
to print this information.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Bin Meng <bmeng.cn@gmail.com>

Showing 1 changed file with 18 additions and 12 deletions Side-by-side Diff

... ... @@ -465,6 +465,16 @@
465 465 return ret;
466 466 }
467 467  
  468 +static int perror_fname(const char *fmt, const char *fname)
  469 +{
  470 + char msg[strlen(fmt) + strlen(fname) + 1];
  471 +
  472 + sprintf(msg, fmt, fname);
  473 + perror(msg);
  474 +
  475 + return -1;
  476 +}
  477 +
468 478 /**
469 479 * write_image() - Write the image to a file
470 480 *
... ... @@ -481,10 +491,10 @@
481 491  
482 492 new_fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR |
483 493 S_IWUSR | S_IRGRP | S_IROTH);
484   - if (write(new_fd, image, size) != size) {
485   - perror("Error while writing");
486   - return -1;
487   - }
  494 + if (new_fd < 0)
  495 + return perror_fname("Could not open file '%s'", filename);
  496 + if (write(new_fd, image, size) != size)
  497 + return perror_fname("Could not write file '%s'", filename);
488 498 close(new_fd);
489 499  
490 500 return 0;
... ... @@ -586,14 +596,10 @@
586 596 int fd = open(fname, O_RDONLY);
587 597 struct stat buf;
588 598  
589   - if (fd == -1) {
590   - perror("Could not open file");
591   - return -1;
592   - }
593   - if (fstat(fd, &buf) == -1) {
594   - perror("Could not stat file");
595   - return -1;
596   - }
  599 + if (fd == -1)
  600 + return perror_fname("Could not open file '%s'", fname);
  601 + if (fstat(fd, &buf) == -1)
  602 + return perror_fname("Could not stat file '%s'", fname);
597 603 *sizep = buf.st_size;
598 604 debug("File %s is %d bytes\n", fname, *sizep);
599 605