]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blobdiff - lib/ext2fs/imager.c
Merge branch 'maint' into next
[thirdparty/e2fsprogs.git] / lib / ext2fs / imager.c
index 5a6d0b9526611b98d00b064e6a2f7577efc2c2c7..f7f8df423aa7ae37b5ac907760cc78034bf49184 100644 (file)
@@ -13,6 +13,7 @@
  * %End-Header%
  */
 
+#include "config.h"
 #include <stdio.h>
 #include <string.h>
 #if HAVE_UNISTD_H
@@ -65,6 +66,7 @@ errcode_t ext2fs_image_inode_write(ext2_filsys fs, int fd, int flags)
        blk64_t         blk;
        ssize_t         actual;
        errcode_t       retval;
+       off_t           r;
 
        buf = malloc(fs->blocksize * BUF_BLOCKS);
        if (!buf)
@@ -96,7 +98,12 @@ errcode_t ext2fs_image_inode_write(ext2_filsys fs, int fd, int flags)
                                        blk++;
                                        left--;
                                        cp += fs->blocksize;
-                                       lseek(fd, fs->blocksize, SEEK_CUR);
+                                       r = ext2fs_llseek(fd, fs->blocksize,
+                                                         SEEK_CUR);
+                                       if (r < 0) {
+                                               retval = errno;
+                                               goto errout;
+                                       }
                                        continue;
                                }
                                /* Find non-zero blocks */
@@ -188,6 +195,14 @@ errcode_t ext2fs_image_super_write(ext2_filsys fs, int fd,
        char            *buf, *cp;
        ssize_t         actual;
        errcode_t       retval;
+#ifdef WORDS_BIGENDIAN
+       unsigned int    groups_per_block;
+       struct          ext2_group_desc *gdp;
+       int             j;
+#endif
+
+       if (fs->group_desc == NULL)
+               return EXT2_ET_NO_GDESC;
 
        buf = malloc(fs->blocksize);
        if (!buf)
@@ -197,7 +212,17 @@ errcode_t ext2fs_image_super_write(ext2_filsys fs, int fd,
         * Write out the superblock
         */
        memset(buf, 0, fs->blocksize);
+#ifdef WORDS_BIGENDIAN
+       /*
+        * We're writing out superblock so let's convert
+        * it to little endian and then back if needed
+        */
+       ext2fs_swap_super(fs->super);
+       memcpy(buf, fs->super, SUPERBLOCK_SIZE);
+       ext2fs_swap_super(fs->super);
+#else
        memcpy(buf, fs->super, SUPERBLOCK_SIZE);
+#endif
        actual = write(fd, buf, fs->blocksize);
        if (actual == -1) {
                retval = errno;
@@ -211,8 +236,34 @@ errcode_t ext2fs_image_super_write(ext2_filsys fs, int fd,
        /*
         * Now write out the block group descriptors
         */
+
        cp = (char *) fs->group_desc;
+
+#ifdef WORDS_BIGENDIAN
+       /*
+        * Convert group descriptors to little endian and back
+        * if needed
+        */
+       groups_per_block = EXT2_DESC_PER_BLOCK(fs->super);
+       for (j=0; j < groups_per_block*fs->desc_blocks; j++) {
+               gdp = ext2fs_group_desc(fs, fs->group_desc, j);
+               if (gdp)
+                       ext2fs_swap_group_desc2(fs, gdp);
+       }
+#endif
+
        actual = write(fd, cp, fs->blocksize * fs->desc_blocks);
+
+
+#ifdef WORDS_BIGENDIAN
+       groups_per_block = EXT2_DESC_PER_BLOCK(fs->super);
+       for (j=0; j < groups_per_block*fs->desc_blocks; j++) {
+               gdp = ext2fs_group_desc(fs, fs->group_desc, j);
+               if (gdp)
+                       ext2fs_swap_group_desc2(fs, gdp);
+       }
+#endif
+
        if (actual == -1) {
                retval = errno;
                goto errout;
@@ -278,10 +329,10 @@ errout:
 errcode_t ext2fs_image_bitmap_write(ext2_filsys fs, int fd, int flags)
 {
        ext2fs_generic_bitmap   bmap;
-       errcode_t               err, retval;
+       errcode_t               retval;
        ssize_t                 actual;
-       __u32                   itr, cnt, size;
-       int                     c, total_size;
+       size_t                  c;
+       __u64                   itr, cnt, size, total_size;
        char                    buf[1024];
 
        if (flags & IMAGER_FLAG_INODEMAP) {
@@ -291,7 +342,6 @@ errcode_t ext2fs_image_bitmap_write(ext2_filsys fs, int fd, int flags)
                                return retval;
                }
                bmap = fs->inode_map;
-               err = EXT2_ET_MAGIC_INODE_BITMAP;
                itr = 1;
                cnt = EXT2_INODES_PER_GROUP(fs->super) * fs->group_desc_count;
                size = (EXT2_INODES_PER_GROUP(fs->super) / 8);
@@ -302,10 +352,9 @@ errcode_t ext2fs_image_bitmap_write(ext2_filsys fs, int fd, int flags)
                                return retval;
                }
                bmap = fs->block_map;
-               err = EXT2_ET_MAGIC_BLOCK_BITMAP;
                itr = fs->super->s_first_data_block;
-               cnt = EXT2_BLOCKS_PER_GROUP(fs->super) * fs->group_desc_count;
-               size = EXT2_BLOCKS_PER_GROUP(fs->super) / 8;
+               cnt = EXT2_GROUPS_TO_CLUSTERS(fs->super, fs->group_desc_count);
+               size = EXT2_CLUSTERS_PER_GROUP(fs->super) / 8;
        }
        total_size = size * fs->group_desc_count;
 
@@ -338,9 +387,9 @@ errcode_t ext2fs_image_bitmap_write(ext2_filsys fs, int fd, int flags)
                        if (c > (int) sizeof(buf))
                                c = sizeof(buf);
                        actual = write(fd, buf, c);
-                       if (actual == -1)
+                       if (actual < 0)
                                return errno;
-                       if (actual != c)
+                       if ((size_t) actual != c)
                                return EXT2_ET_SHORT_WRITE;
                        size -= c;
                }
@@ -355,8 +404,8 @@ errcode_t ext2fs_image_bitmap_write(ext2_filsys fs, int fd, int flags)
 errcode_t ext2fs_image_bitmap_read(ext2_filsys fs, int fd, int flags)
 {
        ext2fs_generic_bitmap   bmap;
-       errcode_t               err, retval;
-       __u32                   itr, cnt;
+       errcode_t               retval;
+       __u64                   itr, cnt;
        char                    buf[1024];
        unsigned int            size;
        ssize_t                 actual;
@@ -368,7 +417,6 @@ errcode_t ext2fs_image_bitmap_read(ext2_filsys fs, int fd, int flags)
                                return retval;
                }
                bmap = fs->inode_map;
-               err = EXT2_ET_MAGIC_INODE_BITMAP;
                itr = 1;
                cnt = EXT2_INODES_PER_GROUP(fs->super) * fs->group_desc_count;
                size = (EXT2_INODES_PER_GROUP(fs->super) / 8);
@@ -379,9 +427,8 @@ errcode_t ext2fs_image_bitmap_read(ext2_filsys fs, int fd, int flags)
                                return retval;
                }
                bmap = fs->block_map;
-               err = EXT2_ET_MAGIC_BLOCK_BITMAP;
                itr = fs->super->s_first_data_block;
-               cnt = EXT2_BLOCKS_PER_GROUP(fs->super) * fs->group_desc_count;
+               cnt = EXT2_GROUPS_TO_BLOCKS(fs->super, fs->group_desc_count);
                size = EXT2_BLOCKS_PER_GROUP(fs->super) / 8;
        }