Whamcloud - gitweb
libext2fs: explicitly ignore a possible unlink failure in ext2fs_free_icount
[tools/e2fsprogs.git] / misc / e2image.c
index e876ae8..d52accf 100644 (file)
  * %End-Header%
  */
 
+#ifndef _LARGEFILE_SOURCE
 #define _LARGEFILE_SOURCE
+#endif
+#ifndef _LARGEFILE64_SOURCE
 #define _LARGEFILE64_SOURCE
+#endif
 
 #include "config.h"
 #include <fcntl.h>
@@ -39,15 +43,16 @@ extern int optind;
 
 #include "ext2fs/ext2_fs.h"
 #include "ext2fs/ext2fs.h"
+#include "ext2fs/ext2fsP.h"
 #include "et/com_err.h"
 #include "uuid/uuid.h"
 #include "e2p/e2p.h"
 #include "ext2fs/e2image.h"
 #include "ext2fs/qcow2.h"
 
+#include "support/nls-enable.h"
+#include "support/plausible.h"
 #include "../version.h"
-#include "nls-enable.h"
-#include "plausible.h"
 
 #define QCOW_OFLAG_COPIED     (1LL << 63)
 #define NO_BLK ((blk64_t) -1)
@@ -99,7 +104,7 @@ static int get_bits_from_size(size_t size)
 
 static void usage(void)
 {
-       fprintf(stderr, _("Usage: %s [ -r|Q ] [ -fr ] device image-file\n"),
+       fprintf(stderr, _("Usage: %s [ -r|Q ] [ -f ] device image-file\n"),
                program_name);
        fprintf(stderr, _("       %s -I device image-file\n"), program_name);
        fprintf(stderr, _("       %s -ra  [  -cfnp  ] [ -o src_offset ] "
@@ -235,7 +240,7 @@ static void write_image_file(ext2_filsys fs, int fd)
        write_header(fd, NULL, sizeof(struct ext2_image_hdr), fs->blocksize);
        memset(&hdr, 0, sizeof(struct ext2_image_hdr));
 
-       hdr.offset_super = seek_relative(fd, 0);
+       hdr.offset_super = ext2fs_cpu_to_le32(seek_relative(fd, 0));
        retval = ext2fs_image_super_write(fs, fd, 0);
        if (retval) {
                com_err(program_name, retval, "%s",
@@ -243,7 +248,7 @@ static void write_image_file(ext2_filsys fs, int fd)
                exit(1);
        }
 
-       hdr.offset_inode = seek_relative(fd, 0);
+       hdr.offset_inode = ext2fs_cpu_to_le32(seek_relative(fd, 0));
        retval = ext2fs_image_inode_write(fs, fd,
                                  (fd != 1) ? IMAGER_FLAG_SPARSEWRITE : 0);
        if (retval) {
@@ -252,7 +257,7 @@ static void write_image_file(ext2_filsys fs, int fd)
                exit(1);
        }
 
-       hdr.offset_blockmap = seek_relative(fd, 0);
+       hdr.offset_blockmap = ext2fs_cpu_to_le32(seek_relative(fd, 0));
        retval = ext2fs_image_bitmap_write(fs, fd, 0);
        if (retval) {
                com_err(program_name, retval, "%s",
@@ -260,7 +265,7 @@ static void write_image_file(ext2_filsys fs, int fd)
                exit(1);
        }
 
-       hdr.offset_inodemap = seek_relative(fd, 0);
+       hdr.offset_inodemap = ext2fs_cpu_to_le32(seek_relative(fd, 0));
        retval = ext2fs_image_bitmap_write(fs, fd, IMAGER_FLAG_INODEMAP);
        if (retval) {
                com_err(program_name, retval, "%s",
@@ -268,23 +273,23 @@ static void write_image_file(ext2_filsys fs, int fd)
                exit(1);
        }
 
-       hdr.magic_number = EXT2_ET_MAGIC_E2IMAGE;
+       hdr.magic_number = ext2fs_cpu_to_le32(EXT2_ET_MAGIC_E2IMAGE);
        strcpy(hdr.magic_descriptor, "Ext2 Image 1.0");
        gethostname(hdr.fs_hostname, sizeof(hdr.fs_hostname));
        strncpy(hdr.fs_device_name, device_name, sizeof(hdr.fs_device_name)-1);
        hdr.fs_device_name[sizeof(hdr.fs_device_name) - 1] = 0;
-       hdr.fs_blocksize = fs->blocksize;
+       hdr.fs_blocksize = ext2fs_cpu_to_le32(fs->blocksize);
 
        if (stat(device_name, &st) == 0)
-               hdr.fs_device = st.st_rdev;
+               hdr.fs_device = ext2fs_cpu_to_le32(st.st_rdev);
 
        if (fstat(fd, &st) == 0) {
-               hdr.image_device = st.st_dev;
-               hdr.image_inode = st.st_ino;
+               hdr.image_device = ext2fs_cpu_to_le32(st.st_dev);
+               hdr.image_inode = ext2fs_cpu_to_le32(st.st_ino);
        }
        memcpy(hdr.fs_uuid, fs->super->s_uuid, sizeof(hdr.fs_uuid));
 
-       hdr.image_time = time(0);
+       hdr.image_time = ext2fs_cpu_to_le32(time(0));
        write_header(fd, &hdr, sizeof(struct ext2_image_hdr), fs->blocksize);
 }
 
@@ -547,7 +552,7 @@ static void sigint_handler(int unused EXT2FS_ATTR((unused)))
 
 #define calc_percent(a, b) ((int) ((100.0 * (((float) (a)) / \
                                             ((float) (b)))) + 0.5))
-#define calc_rate(t, b, d) (((float)(t) / ((1024 * 1024) / (b))) / (d))
+#define calc_rate(t, b, d) (((float)(t) / ((float)(1024 * 1024) / (b))) / (d))
 
 static int print_progress(blk64_t num, blk64_t total)
 {
@@ -869,8 +874,8 @@ static int init_refcount(struct ext2_qcow2_image *img, blk64_t table_offset)
        return ret;
 }
 
-static int initialize_qcow2_image(int fd, ext2_filsys fs,
-                           struct ext2_qcow2_image *image)
+static errcode_t initialize_qcow2_image(int fd, ext2_filsys fs,
+                                       struct ext2_qcow2_image *image)
 {
        struct ext2_qcow2_hdr *header;
        blk64_t total_size, offset;
@@ -878,6 +883,9 @@ static int initialize_qcow2_image(int fd, ext2_filsys fs,
        int cluster_bits = get_bits_from_size(fs->blocksize);
        struct ext2_super_block *sb = fs->super;
 
+       if (fs->blocksize < 1024)
+               return EINVAL;  /* Can never happen, but just in case... */
+
        /* Allocate header */
        ret = ext2fs_get_memzero(sizeof(struct ext2_qcow2_hdr), &header);
        if (ret)
@@ -1104,7 +1112,7 @@ static int update_refcount(int fd, struct ext2_qcow2_image *img,
        /*
         * We are relying on the fact that we are creating the qcow2
         * image sequentially, hence we will always allocate refcount
-        * block items sequentialy.
+        * block items sequentially.
         */
        ref->refcount_block[ref->refcount_block_index] = ext2fs_cpu_to_be16(1);
        ref->refcount_block_index++;
@@ -1281,7 +1289,7 @@ static void write_raw_image_file(ext2_filsys fs, int fd, int type, int flags)
 
        mark_table_blocks(fs);
        if (show_progress)
-               printf("%s", _("Scanning inodes...\n"));
+               fprintf(stderr, "%s", _("Scanning inodes...\n"));
 
        retval = ext2fs_open_inode_scan(fs, 0, &scan);
        if (retval) {
@@ -1370,7 +1378,8 @@ static void install_image(char *device, char *image_fn, int type)
 {
        errcode_t retval;
        ext2_filsys fs;
-       int open_flag = EXT2_FLAG_IMAGE_FILE | EXT2_FLAG_64BITS;
+       int open_flag = EXT2_FLAG_IMAGE_FILE | EXT2_FLAG_64BITS |
+                       EXT2_FLAG_IGNORE_CSUM_ERRORS;
        int fd = 0;
        io_manager      io_ptr;
        io_channel      io;
@@ -1417,7 +1426,7 @@ static void install_image(char *device, char *image_fn, int type)
 
        ext2fs_rewrite_to_io(fs, io);
 
-       seek_set(fd, fs->image_header->offset_inode);
+       seek_set(fd, ext2fs_le32_to_cpu(fs->image_header->offset_inode));
 
        retval = ext2fs_image_inode_read(fs, fd, 0);
        if (retval) {
@@ -1447,7 +1456,7 @@ int main (int argc, char ** argv)
        ext2_filsys fs;
        char *image_fn, offset_opt[64];
        struct ext2_qcow2_hdr *header = NULL;
-       int open_flag = EXT2_FLAG_64BITS;
+       int open_flag = EXT2_FLAG_64BITS | EXT2_FLAG_IGNORE_CSUM_ERRORS;
        int img_type = 0;
        int flags = 0;
        int mount_flags = 0;
@@ -1615,7 +1624,7 @@ skip_device:
                                _("Can not stat output\n"));
                        exit(1);
                }
-               if (S_ISBLK(st.st_mode))
+               if (ext2fsP_is_disk_device(st.st_mode))
                        output_is_blk = 1;
        }
        if (flags & E2IMAGE_IS_QCOW2_FLAG) {