[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Libcdio-devel] [PATCH 1/4] Add read_iso_file helper function in iso-rea
From: |
Christophe Fergeau |
Subject: |
[Libcdio-devel] [PATCH 1/4] Add read_iso_file helper function in iso-read |
Date: |
Wed, 17 Oct 2012 13:45:34 +0200 |
Move ISO9660 specific code to a helper function in preparation for
adding UDF support to iso-read.
---
src/iso-read.c | 70 ++++++++++++++++++++++++++++++++++++----------------------
1 file changed, 43 insertions(+), 27 deletions(-)
diff --git a/src/iso-read.c b/src/iso-read.c
index 4820887..707ceef 100644
--- a/src/iso-read.c
+++ b/src/iso-read.c
@@ -200,54 +200,36 @@ init(void)
opts.iso9660_image = NULL;
}
-int
-main(int argc, char *argv[])
+static int read_iso_file(const char *iso_name, const char *src,
+ FILE *outfd, size_t *bytes_written)
{
iso9660_stat_t *statbuf;
- FILE *outfd;
int i;
iso9660_t *iso;
-
- init();
- /* Parse our arguments; every option seen by `parse_opt' will
- be reflected in `arguments'. */
- if (!parse_options(argc, argv)) {
- report(stderr,
- "error while parsing command line - try --help\n");
- return 2;
- }
-
- iso = iso9660_open (opts.iso9660_image);
+ iso = iso9660_open (iso_name);
if (NULL == iso) {
report(stderr,
"%s: Sorry, couldn't open ISO-9660 image file '%s'.\n",
- program_name, opts.iso9660_image);
+ program_name, src);
return 1;
}
- statbuf = iso9660_ifs_stat_translate (iso, opts.file_name);
+ statbuf = iso9660_ifs_stat_translate (iso, src);
if (NULL == statbuf)
{
report(stderr,
"%s: Could not get ISO-9660 file information out of %s"
" for file %s.\n",
- program_name, opts.iso9660_image, opts.file_name);
+ program_name, iso_name, src);
report(stderr,
"%s: iso-info may be able to show the contents of %s.\n",
- program_name, opts.iso9660_image);
+ program_name, iso_name);
return 2;
}
- if (!(outfd = fopen (opts.output_file, "wb")))
- {
- report(stderr,
- "%s: Could not open %s for writing: %s\n",
- program_name, opts.output_file, strerror(errno));
- return 3;
- }
/* Copy the blocks from the ISO-9660 filesystem to the local filesystem. */
for (i = 0; i < statbuf->size; i += ISO_BLOCKSIZE)
@@ -274,16 +256,50 @@ main(int argc, char *argv[])
return 5;
}
}
+ iso9660_close(iso);
+
+ *bytes_written = statbuf->size;
+ return 0;
+}
+
+int
+main(int argc, char *argv[])
+{
+ FILE *outfd;
+ int ret;
+ size_t bytes_written;
+
+ init();
+
+ /* Parse our arguments; every option seen by `parse_opt' will
+ be reflected in `arguments'. */
+ if (!parse_options(argc, argv)) {
+ report(stderr,
+ "error while parsing command line - try --help\n");
+ return 2;
+ }
+
+ if (!(outfd = fopen (opts.output_file, "wb")))
+ {
+ report(stderr,
+ "%s: Could not open %s for writing: %s\n",
+ program_name, opts.output_file, strerror(errno));
+ return 3;
+ }
+
+ ret = read_iso_file (opts.iso9660_image, opts.file_name,
+ outfd, &bytes_written);
+ if (ret != 0)
+ return ret;
fflush (outfd);
/* Make sure the file size has the exact same byte size. Without the
truncate below, the file will a multiple of ISO_BLOCKSIZE.
*/
- if (ftruncate (fileno (outfd), statbuf->size))
+ if (ftruncate (fileno (outfd), bytes_written))
perror ("ftruncate()");
fclose (outfd);
- iso9660_close(iso);
return 0;
}
--
1.7.12.1