[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Libcdio-devel] [PATCH 2/4] Add UDF support to iso-read
From: |
Christophe Fergeau |
Subject: |
[Libcdio-devel] [PATCH 2/4] Add UDF support to iso-read |
Date: |
Wed, 17 Oct 2012 13:45:35 +0200 |
---
src/Makefile.am | 2 +-
src/iso-read.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
2 files changed, 68 insertions(+), 2 deletions(-)
diff --git a/src/Makefile.am b/src/Makefile.am
index 3a1bf15..2339fe9 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -66,7 +66,7 @@ endif
if BUILD_ISO_READ
iso_read_SOURCES = iso-read.c util.c util.h $(GETOPT_C)
-iso_read_LDADD = $(LIBISO9660_LIBS) $(LIBCDIO_LIBS) $(LTLIBICONV)
+iso_read_LDADD = $(LIBUDF_LIBS) $(LIBISO9660_LIBS) $(LIBCDIO_LIBS)
$(LTLIBICONV)
bin_iso_read = iso-read
man_iso_read = iso-read.1
endif
diff --git a/src/iso-read.c b/src/iso-read.c
index 707ceef..6c89f42 100644
--- a/src/iso-read.c
+++ b/src/iso-read.c
@@ -28,6 +28,7 @@
#endif
#include <cdio/cdio.h>
#include <cdio/iso9660.h>
+#include <cdio/udf.h>
#ifdef HAVE_STDIO_H
#include <stdio.h>
@@ -44,6 +45,8 @@
#include "getopt.h"
+#define CEILING(x, y) ((x+(y-1))/y)
+
/* Used by `main' to communicate with `parse_opt'. And global options
*/
static struct arguments
@@ -262,6 +265,64 @@ static int read_iso_file(const char *iso_name, const char
*src,
return 0;
}
+static int read_udf_file(const char *iso_name, const char *src,
+ FILE *outfd, size_t *bytes_written)
+{
+ udf_t *p_udf;
+
+ p_udf = udf_open (iso_name);
+
+ if (NULL == p_udf) {
+ fprintf(stderr, "Sorry, couldn't open %s as something using UDF\n",
+ iso_name);
+ return 1;
+ } else {
+ udf_dirent_t *p_udf_root = udf_get_root(p_udf, true, 0);
+ udf_dirent_t *p_udf_file = NULL;
+ if (NULL == p_udf_root) {
+ fprintf(stderr, "Sorry, couldn't find / in %s\n",
+ iso_name);
+ return 1;
+ }
+
+ p_udf_file = udf_fopen(p_udf_root, src);
+ if (!p_udf_file) {
+ fprintf(stderr, "Sorry, couldn't find %s in %s\n",
+ src, iso_name);
+ return 2;
+
+ }
+
+ {
+ uint64_t i_file_length = udf_get_file_length(p_udf_file);
+ const unsigned int i_blocks = (unsigned int) CEILING(i_file_length,
UDF_BLOCKSIZE);
+ unsigned int i;
+ for (i = 0; i < i_blocks ; i++) {
+ char buf[UDF_BLOCKSIZE] = {'\0',};
+ ssize_t i_read = udf_read_block(p_udf_file, buf, 1);
+
+ if ( i_read < 0 ) {
+ fprintf(stderr, "Error reading UDF file %s at block %u\n",
+ src, i);
+ return 4;
+ }
+
+ fwrite (buf, i_read, 1, outfd);
+
+ if (ferror (outfd)) {
+ perror ("fwrite()");
+ return 5;
+ }
+ }
+
+ udf_dirent_free(p_udf_root);
+ udf_close(p_udf);
+ *bytes_written = i_file_length;
+ }
+ }
+ return 0;
+}
+
int
main(int argc, char *argv[])
{
@@ -287,8 +348,13 @@ main(int argc, char *argv[])
return 3;
}
- ret = read_iso_file (opts.iso9660_image, opts.file_name,
+ ret = read_udf_file (opts.iso9660_image, opts.file_name,
outfd, &bytes_written);
+ if (ret == 1)
+ {
+ ret = read_iso_file (opts.iso9660_image, opts.file_name,
+ outfd, &bytes_written);
+ }
if (ret != 0)
return ret;
--
1.7.12.1