]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - disk-utils/mkfs.cramfs.c
Make the ways of using output stream consistent in usage()
[thirdparty/util-linux.git] / disk-utils / mkfs.cramfs.c
index 729765078b7b3e225b9cfa71f9917b25fb237b86..482375351953f55b7b76c03c919c0a025c2949b8 100644 (file)
 
 /* 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 "blkdev.h"
 #include "c.h"
 #include "cramfs.h"
 #include "md5.h"
@@ -124,27 +125,28 @@ struct entry {
 
 static void __attribute__((__noreturn__)) usage(void)
 {
-       printf(
-               _("usage: %s [-h] [-v] [-b blksize] [-e edition] [-N endian] [-i file] "
-                 "[-n name] dirname outfile\n"
-                 " -v         be verbose\n"
-                 " -E         make all warnings errors "
-                   "(non-zero exit status)\n"
-                 " -b blksize use this blocksize, must equal page size\n"
-                 " -e edition set edition number (part of fsid)\n"
-                 " -N endian  set cramfs endianness (big|little|host), default host\n"
-                 " -i file    insert a file image into the filesystem\n"
-                 " -n name    set name of cramfs filesystem\n"
-                 " -p         pad by %d bytes for boot code\n"
-                 " -s         sort directory entries (old option, ignored)\n"
-                 " -z         make explicit holes\n"
-                 " dirname    root of the filesystem to be compressed\n"
-                 " outfile    output file\n"),
-               program_invocation_short_name, PAD_SIZE);
-
+       fputs(USAGE_HEADER, stdout);
+       fprintf(stdout, _(" %s [-h] [-v] [-b blksize] [-e edition] [-N endian] [-i file] [-n name] dirname outfile\n"),
+               program_invocation_short_name);
+       fputs(USAGE_SEPARATOR, stdout);
+       fputs(_("Make compressed ROM file system."), stdout);
+       fputs(USAGE_OPTIONS, stdout);
+       fputs(_(  " -v             be verbose"), stdout);
+       fputs(_(  " -E             make all warnings errors (non-zero exit status)"), stdout);
+       fputs(_(  " -b blksize     use this blocksize, must equal page size"), stdout);
+       fputs(_(  " -e edition     set edition number (part of fsid)"), stdout);
+       fprintf(stdout, _(" -N endian      set cramfs endianness (%s|%s|%s), default %s\n"), "big", "little", "host", "host");
+       fputs(_(  " -i file        insert a file image into the filesystem"), stdout);
+       fputs(_(  " -n name        set name of cramfs filesystem"), stdout);
+       fprintf(stdout, _(" -p             pad by %d bytes for boot code\n"), PAD_SIZE);
+       fputs(_(  " -s             sort directory entries (old option, ignored)"), stdout);
+       fputs(_(  " -z             make explicit holes"), stdout);
+       fputs(_(  " -l[=<mode>]    use exclusive device lock (yes, no or nonblock)"), stdout);
+       fputs(_(  " dirname        root of the filesystem to be compressed"), stdout);
+       fputs(_(  " outfile        output file"), stdout);
        fputs(USAGE_SEPARATOR, stdout);
-       printf(USAGE_HELP_OPTIONS(16));
-       printf(USAGE_MAN_TAIL("mkfs.cramfs(8)"));
+       fprintf(stdout, USAGE_HELP_OPTIONS(16));
+       fprintf(stdout, USAGE_MAN_TAIL("mkfs.cramfs(8)"));
        exit(MKFS_EX_OK);
 }
 
@@ -157,6 +159,8 @@ do_mmap(char *path, unsigned int size, unsigned int mode){
                return NULL;
 
        if (S_ISLNK(mode)) {
+               /* The link buffer is unnecessary to terminate by null as it's
+                * always used as buffer rather than a string */
                start = xmalloc(size);
                if (readlink(path, start, size) < 0) {
                        warn(_("readlink failed: %s"), path);
@@ -359,7 +363,7 @@ static unsigned int parse_directory(struct entry *root_entry, const char *name,
                        entry->size = parse_directory(root_entry, path, &entry->child, fslen_ub);
                } else if (S_ISREG(st.st_mode)) {
                        entry->path = xstrdup(path);
-                       if (entry->size && entry->size >= (1 << CRAMFS_SIZE_WIDTH)) {
+                       if (entry->size >= (1 << CRAMFS_SIZE_WIDTH)) {
                                warn_size = 1;
                                entry->size = (1 << CRAMFS_SIZE_WIDTH) - 1;
                        }
@@ -418,9 +422,9 @@ static unsigned int write_superblock(struct entry *root, char *base, int size)
 
        memset(super->name, 0x00, sizeof(super->name));
        if (opt_name)
-               strncpy((char *)super->name, opt_name, sizeof(super->name));
+               str2memcpy((char *)super->name, opt_name, sizeof(super->name));
        else
-               strncpy((char *)super->name, "Compressed", sizeof(super->name));
+               str2memcpy((char *)super->name, "Compressed", sizeof(super->name));
 
        super->root.mode = root->mode;
        super->root.uid = root->uid;
@@ -491,7 +495,7 @@ static unsigned int write_directory_structure(struct entry *entry, char *base, u
                        if (entry->child) {
                                if (stack_entries >= stack_size) {
                                        stack_size *= 2;
-                                       entry_stack = xrealloc(entry_stack, stack_size * sizeof(struct entry *));
+                                       entry_stack = xreallocarray(entry_stack, stack_size, sizeof(struct entry *));
                                }
                                entry_stack[stack_entries] = entry;
                                stack_entries++;
@@ -547,9 +551,9 @@ static int is_zero(unsigned char const *begin, unsigned len)
                             (len-- == 0 ||
                              (begin[3] == '\0' &&
                               memcmp(begin, begin + 4, len) == 0))))))));
-       else
-               /* Never create holes. */
-               return 0;
+
+       /* Never create holes. */
+       return 0;
 }
 
 /*
@@ -705,6 +709,7 @@ int main(int argc, char **argv)
        loff_t fslen_ub = sizeof(struct cramfs_super);
        unsigned int fslen_max;
        char const *dirname, *outfile;
+       char *lockmode = 0;
        uint32_t crc = crc32(0L, NULL, 0);
        int c;
        cramfs_is_big_endian = HOST_IS_BIG_ENDIAN; /* default is to use host order */
@@ -714,21 +719,21 @@ int main(int argc, char **argv)
        setlocale(LC_ALL, "");
        bindtextdomain(PACKAGE, LOCALEDIR);
        textdomain(PACKAGE);
-       atexit(close_stdout);
+       close_stdout_atexit();
 
        if (argc > 1) {
                /* first arg may be one of our standard longopts */
                if (!strcmp(argv[1], "--help"))
                        usage();
                if (!strcmp(argv[1], "--version")) {
-                       printf(UTIL_LINUX_VERSION);
+                       print_version(EXIT_SUCCESS);
                        exit(MKFS_EX_OK);
                }
        }
        strutils_set_exitcode(MKFS_EX_USAGE);
 
        /* command line options */
-       while ((c = getopt(argc, argv, "hb:Ee:i:n:N:psVvz")) != EOF) {
+       while ((c = getopt(argc, argv, "hb:Ee:i:n:N:l::psVvz")) != EOF) {
                switch (c) {
                case 'h':
                        usage();
@@ -759,6 +764,14 @@ int main(int argc, char **argv)
                        image_length = st.st_size; /* may be padded later */
                        fslen_ub += (image_length + 3); /* 3 is for padding */
                        break;
+               case 'l':
+                        lockmode = "1";
+                       if (optarg) {
+                                if (*optarg == '=')
+                                       optarg++;
+                               lockmode = optarg;
+                       }
+                       break;
                case 'n':
                        opt_name = optarg;
                        break;
@@ -770,8 +783,7 @@ int main(int argc, char **argv)
                        /* old option, ignored */
                        break;
                case 'V':
-                       printf(UTIL_LINUX_VERSION);
-                       exit(MKFS_EX_OK);
+                       print_version(MKFS_EX_OK);
                case 'v':
                        verbose = 1;
                        break;
@@ -799,6 +811,9 @@ int main(int argc, char **argv)
        if (fd < 0)
                err(MKFS_EX_USAGE, _("cannot open %s"), outfile);
 
+        if (blkdev_lock(fd, outfile, lockmode) != 0)
+               exit(MKFS_EX_ERROR);
+
        root_entry = xcalloc(1, sizeof(struct entry));
        root_entry->mode = st.st_mode;
        root_entry->uid = st.st_uid;