Whamcloud - gitweb
libext2fs/ismounted.c: check device id in advance to skip false device names
[tools/e2fsprogs.git] / lib / ext2fs / sparse_io.c
index a307859..5e0e2cd 100644 (file)
@@ -7,6 +7,10 @@
 #include "ext2_fs.h"
 #include "ext2fs.h"
 
+#ifndef O_BINARY
+#define O_BINARY 0
+#endif
+
 #if !defined(ENABLE_LIBSPARSE)
 static errcode_t sparse_open(const char *name EXT2FS_ATTR((unused)),
                             int flags EXT2FS_ATTR((unused)),
@@ -63,7 +67,7 @@ static void free_sparse_blocks(struct sparse_map *sm)
        sm->blocks = NULL;
 }
 
-static int sparse_import_segment(void *priv, const void *data, int len,
+static int sparse_import_segment(void *priv, const void *data, size_t len,
                                 unsigned int block, unsigned int nr_blocks)
 {
        struct sparse_map *sm = priv;
@@ -155,7 +159,7 @@ static errcode_t io_manager_configure(struct sparse_io_params *params,
                        goto err_alloc;
                }
                if (params->fd < 0) {
-                       sm->fd = open(params->file, O_CREAT | O_RDWR | O_TRUNC,
+                       sm->fd = open(params->file, O_CREAT | O_RDWR | O_TRUNC | O_BINARY,
                                      0644);
                        if (sm->fd < 0) {
                                retval = errno;
@@ -181,14 +185,22 @@ err_params:
 static errcode_t sparse_open_channel(struct sparse_io_params *sparse_params,
                                     int flags, io_channel *channel)
 {
+       errcode_t retval;
        io_channel io;
 
        io = calloc(1, sizeof(struct struct_io_channel));
        io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
        io->block_size = 0;
        io->refcount = 1;
+
+       retval = io_manager_configure(sparse_params, flags, io);
+       if (retval) {
+               free(io);
+               return retval;
+       }
+
        *channel = io;
-       return io_manager_configure(sparse_params, flags, io);
+       return 0;
 }
 
 static errcode_t read_sparse_argv(const char *name, bool is_fd,
@@ -206,11 +218,11 @@ static errcode_t read_sparse_argv(const char *name, bool is_fd,
        }
 
        if (is_fd) {
-               ret = sscanf(name, "%d:%llu:%u", &sparse_params->fd,
+               ret = sscanf(name, "(%d):%llu:%u", &sparse_params->fd,
                             (unsigned long long *)&sparse_params->blocks_count,
                             &sparse_params->block_size);
        } else {
-               ret = sscanf(name, "%[^:]%*[:]%llu%*[:]%u", sparse_params->file,
+               ret = sscanf(name, "(%[^)])%*[:]%llu%*[:]%u", sparse_params->file,
                             (unsigned long long *)&sparse_params->blocks_count,
                             &sparse_params->block_size);
        }
@@ -266,7 +278,8 @@ static errcode_t sparse_merge_blocks(struct sparse_map *sm, uint64_t start,
 
        buf = calloc(num, block_size);
        if (!buf) {
-               fprintf(stderr, "failed to alloc %lu\n", num * block_size);
+               fprintf(stderr, "failed to alloc %llu\n",
+                       (unsigned long long)num * block_size);
                return EXT2_ET_NO_MEMORY;
        }