]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - disk-utils/fsck.cramfs.c
wipefs: add --lock and LOCK_BLOCK_DEVICE
[thirdparty/util-linux.git] / disk-utils / fsck.cramfs.c
index 46568527822fe82cfbdb835a8b8f6eede1804a26..5145bcfd7aa80d8dc5f8a78e393aa96903a07bd5 100644 (file)
 #include <errno.h>
 #include <string.h>
 #include <getopt.h>
-#include <utime.h>
 #include <fcntl.h>
 
 /* We don't use our include/crc32.h, but crc32 from zlib!
  *
- * The zlib implemenation performs pre/post-conditioning. The util-linux
+ * The zlib implementation performs pre/post-conditioning. The util-linux
  * imlemenation requires post-conditioning (xor) in the applications.
  */
 #include <zlib.h>
 
+#include <sys/time.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/mman.h>
@@ -120,7 +120,7 @@ static void __attribute__((__noreturn__)) usage(void)
        fputs(_(" -b, --blocksize <size>   use this blocksize, defaults to page size\n"), out);
        fputs(_("     --extract[=<dir>]    test uncompression, optionally extract into <dir>\n"), out);
        fputs(USAGE_SEPARATOR, out);
-       print_usage_help_options(26);
+       printf(USAGE_HELP_OPTIONS(26));
 
        printf(USAGE_MAN_TAIL("fsck.cramfs(8)"));
        exit(FSCK_EX_OK);
@@ -131,12 +131,15 @@ static int get_superblock_endianness(uint32_t magic)
        if (magic == CRAMFS_MAGIC) {
                cramfs_is_big_endian = HOST_IS_BIG_ENDIAN;
                return 0;
-       } else if (magic ==
+       }
+
+       if (magic ==
                   u32_toggle_endianness(!HOST_IS_BIG_ENDIAN, CRAMFS_MAGIC)) {
                cramfs_is_big_endian = !HOST_IS_BIG_ENDIAN;
                return 0;
-       } else
-               return -1;
+       }
+
+       return -1;
 }
 
 static void test_super(int *start, size_t * length)
@@ -192,7 +195,7 @@ static void test_super(int *start, size_t * length)
                errx(FSCK_EX_ERROR, _("unsupported filesystem features"));
 
        /* What are valid superblock sizes? */
-       if (super.size < sizeof(struct cramfs_super))
+       if (super.size < *start + sizeof(struct cramfs_super))
                errx(FSCK_EX_UNCORRECTED, _("superblock size (%d) too small"),
                     super.size);
 
@@ -226,10 +229,15 @@ static void test_crc(int start)
                    mmap(NULL, super.size, PROT_READ | PROT_WRITE,
                         MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
                if (buf != MAP_FAILED) {
+                       ssize_t tmp;
                        if (lseek(fd, 0, SEEK_SET) == (off_t) -1)
                                err(FSCK_EX_ERROR, _("seek on %s failed"), filename);
-                       if (read(fd, buf, super.size) != (ssize_t) super.size)
+                       tmp = read(fd, buf, super.size);
+                       if (tmp < 0)
                                err(FSCK_EX_ERROR, _("cannot read %s"), filename);
+                       if (tmp != (ssize_t) super.size)
+                               errx(FSCK_EX_ERROR, _("failed to read %"PRIu32" bytes from file %s"),
+                                       super.size, filename);
                }
        }
        if (buf != MAP_FAILED) {
@@ -363,7 +371,7 @@ static int uncompress_block(void *src, size_t len)
        return stream.total_out;
 }
 
-#if !HAVE_LCHOWN
+#ifndef HAVE_LCHOWN
 #define lchown chown
 #endif
 
@@ -411,10 +419,9 @@ static void do_uncompress(char *path, int outfd, unsigned long offset,
                curr = next;
        } while (size);
 }
-
 static void change_file_status(char *path, struct cramfs_inode *i)
 {
-       struct utimbuf epoch = { 0, 0 };
+       const struct timeval epoch[] = { {0,0}, {0,0} };
 
        if (euid == 0) {
                if (lchown(path, i->uid, i->gid) < 0)
@@ -426,8 +433,8 @@ static void change_file_status(char *path, struct cramfs_inode *i)
        }
        if (S_ISLNK(i->mode))
                return;
-       if (utime(path, &epoch) < 0)
-               err(FSCK_EX_ERROR, _("utime failed: %s"), path);
+       if (utimes(path, epoch) < 0)
+               err(FSCK_EX_ERROR, _("utimes failed: %s"), path);
 }
 
 static void do_directory(char *path, struct cramfs_inode *i)
@@ -655,7 +662,7 @@ int main(int argc, char **argv)
        setlocale(LC_CTYPE, "");
        bindtextdomain(PACKAGE, LOCALEDIR);
        textdomain(PACKAGE);
-       atexit(close_stdout);
+       close_stdout_atexit();
 
        strutils_set_exitcode(FSCK_EX_USAGE);
 
@@ -669,8 +676,7 @@ int main(int argc, char **argv)
                        usage();
                        break;
                case 'V':
-                       printf(UTIL_LINUX_VERSION);
-                       return FSCK_EX_OK;
+                       print_version(FSCK_EX_OK);
                case 'x':
                        opt_extract = 1;
                        if(optarg)