]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
uuidgen: add option --count
authorKarel Zak <kzak@redhat.com>
Wed, 23 Aug 2023 09:21:22 +0000 (11:21 +0200)
committerKarel Zak <kzak@redhat.com>
Mon, 28 Aug 2023 09:26:01 +0000 (11:26 +0200)
This is mostly for testing purpose or performance tuning. The new
option allows generate multiple UUIDs using the enhanced capability of
the libuuid to cache time-based UUIDs.

 $ uuidgen --time --count 3
 638b9432-4196-11ee-bb1f-7824af891670
 638b94be-4196-11ee-bb1f-7824af891670
 638b94fa-4196-11ee-bb1f-7824af891670

Fixes: https://github.com/util-linux/util-linux/issues/2449
Signed-off-by: Karel Zak <kzak@redhat.com>
bash-completion/uuidgen
meson.build
misc-utils/Makemodule.am
misc-utils/uuidgen.1.adoc
misc-utils/uuidgen.c

index 8e64015c07a98f34dc281429b5807d1d8e6b5eb6..45f690d45eaed4f32ce4162f9b561d5187e88271 100644 (file)
@@ -13,6 +13,10 @@ _uuidgen_module()
                        COMPREPLY=( $(compgen -W "name" -- "$cur") )
                        return 0
                        ;;
+               '-C'|'--count')
+                       COMPREPLY=( $(compgen -W "number" -- $cur) )
+                       return 0
+                       ;;
                '-h'|'--help'|'-V'|'--version')
                        return 0
                        ;;
@@ -25,6 +29,7 @@ _uuidgen_module()
                                --namespace
                                --name
                                --md5
+                               --count
                                --sha1
                                --hex
                                --help
index 221ae373b6446449bd041fe1dea8b07c4168be2a..21cfdc6ce7cb66b6d685f8061fac4e4db0d98aae 100644 (file)
@@ -2619,7 +2619,8 @@ exe = executable(
   'uuidgen',
   uuidgen_sources,
   include_directories : includes,
-  link_with : [lib_uuid],
+  link_with : [lib_common,
+               lib_uuid],
   install_dir : usrbin_exec_dir,
   install : true)
 if not is_disabler(exe)
index adee4111fad16a101f88ab18ee264b4af60c59f3..dd6204afdd0385e2f67d5f7a0467433452fa3ab9 100644 (file)
@@ -104,7 +104,7 @@ usrbin_exec_PROGRAMS += uuidgen
 MANPAGES += misc-utils/uuidgen.1
 dist_noinst_DATA += misc-utils/uuidgen.1.adoc
 uuidgen_SOURCES = misc-utils/uuidgen.c
-uuidgen_LDADD = $(LDADD) libuuid.la
+uuidgen_LDADD = $(LDADD) libcommon.la libuuid.la
 uuidgen_CFLAGS = $(AM_CFLAGS) -I$(ul_libuuid_incdir)
 endif
 
index 3f71aa9a89331f40571290a9ffc9babc00c408de..e1d2cae059b6fcbe1f5286a953560b12f407ba23 100644 (file)
@@ -48,6 +48,9 @@ Generate the hash with the _namespace_ prefix. The _namespace_ is UUID, or '@ns'
 *-N*, *--name* _name_::
 Generate the hash of the _name_.
 
+*-C*, *--count* _num_::
+Generate multiple UUIDs using the enhanced capability of the libuuid to cache time-based UUIDs, thus resulting in improved performance. However, this holds no significance for other UUID types.
+
 *-x*, *--hex*::
 Interpret name _name_ as a hexadecimal string.
 
index 7dcd1f48cda5975dfdb82e8915fbda2ea59d3491..5af35ff96d2a2fc424c923a960429518e7409ea4 100644 (file)
@@ -17,6 +17,7 @@
 #include "nls.h"
 #include "c.h"
 #include "closestream.h"
+#include "strutils.h"
 
 static void __attribute__((__noreturn__)) usage(void)
 {
@@ -29,14 +30,15 @@ static void __attribute__((__noreturn__)) usage(void)
        fputs(_("Create a new UUID value.\n"), out);
 
        fputs(USAGE_OPTIONS, out);
-       fputs(_(" -r, --random        generate random-based uuid\n"), out);
-       fputs(_(" -t, --time          generate time-based uuid\n"), out);
-       fputs(_(" -n, --namespace ns  generate hash-based uuid in this namespace\n"), out);
-       printf(_("                       available namespaces: %s\n"), "@dns @url @oid @x500");
-       fputs(_(" -N, --name name     generate hash-based uuid from this name\n"), out);
-       fputs(_(" -m, --md5           generate md5 hash\n"), out);
-       fputs(_(" -s, --sha1          generate sha1 hash\n"), out);
-       fputs(_(" -x, --hex           interpret name as hex string\n"), out);
+       fputs(_(" -r, --random          generate random-based uuid\n"), out);
+       fputs(_(" -t, --time            generate time-based uuid\n"), out);
+       fputs(_(" -n, --namespace <ns>  generate hash-based uuid in this namespace\n"), out);
+       printf(_("                        available namespaces: %s\n"), "@dns @url @oid @x500");
+       fputs(_(" -N, --name <name>     generate hash-based uuid from this name\n"), out);
+       fputs(_(" -m, --md5             generate md5 hash\n"), out);
+       fputs(_(" -C, --count <num>     generate more uuids in loop\n"), out);
+       fputs(_(" -s, --sha1            generate sha1 hash\n"), out);
+       fputs(_(" -x, --hex             interpret name as hex string\n"), out);
        fputs(USAGE_SEPARATOR, out);
        printf(USAGE_HELP_OPTIONS(21));
        printf(USAGE_MAN_TAIL("uuidgen(1)"));
@@ -88,6 +90,7 @@ main (int argc, char *argv[])
        char   *namespace = NULL, *name = NULL;
        size_t namelen = 0;
        uuid_t ns, uu;
+       unsigned int count = 1, i;
 
        static const struct option longopts[] = {
                {"random", no_argument, NULL, 'r'},
@@ -97,6 +100,7 @@ main (int argc, char *argv[])
                {"namespace", required_argument, NULL, 'n'},
                {"name", required_argument, NULL, 'N'},
                {"md5", no_argument, NULL, 'm'},
+               {"count", required_argument, NULL, 'C'},
                {"sha1", no_argument, NULL, 's'},
                {"hex", no_argument, NULL, 'x'},
                {NULL, 0, NULL, 0}
@@ -107,7 +111,7 @@ main (int argc, char *argv[])
        textdomain(PACKAGE);
        close_stdout_atexit();
 
-       while ((c = getopt_long(argc, argv, "rtVhn:N:msx", longopts, NULL)) != -1)
+       while ((c = getopt_long(argc, argv, "C:rtVhn:N:msx", longopts, NULL)) != -1) {
                switch (c) {
                case 't':
                        do_type = UUID_TYPE_DCE_TIME;
@@ -124,6 +128,9 @@ main (int argc, char *argv[])
                case 'm':
                        do_type = UUID_TYPE_DCE_MD5;
                        break;
+               case 'C':
+                       count = strtou32_or_err(optarg, _("invalid count argument"));
+                       break;
                case 's':
                        do_type = UUID_TYPE_DCE_SHA1;
                        break;
@@ -138,6 +145,7 @@ main (int argc, char *argv[])
                default:
                        errtryhelp(EXIT_FAILURE);
                }
+       }
 
        if (namespace) {
                if (!name) {
@@ -165,43 +173,45 @@ main (int argc, char *argv[])
                        name = unhex(name, &namelen);
        }
 
-       switch (do_type) {
-       case UUID_TYPE_DCE_TIME:
-               uuid_generate_time(uu);
-               break;
-       case UUID_TYPE_DCE_RANDOM:
-               uuid_generate_random(uu);
-               break;
-       case UUID_TYPE_DCE_MD5:
-       case UUID_TYPE_DCE_SHA1:
-               if (namespace[0] == '@' && namespace[1] != '\0') {
-                       const uuid_t *uuidptr;
-
-                       uuidptr = uuid_get_template(&namespace[1]);
-                       if (uuidptr == NULL) {
-                               warnx(_("unknown namespace alias: '%s'"), namespace);
-                               errtryhelp(EXIT_FAILURE);
-                       }
-                       memcpy(ns, *uuidptr, sizeof(ns));
-               } else {
-                       if (uuid_parse(namespace, ns) != 0) {
-                               warnx(_("invalid uuid for namespace: '%s'"), namespace);
-                               errtryhelp(EXIT_FAILURE);
+       for (i = 0; i < count; i++) {
+               switch (do_type) {
+               case UUID_TYPE_DCE_TIME:
+                       uuid_generate_time(uu);
+                       break;
+               case UUID_TYPE_DCE_RANDOM:
+                       uuid_generate_random(uu);
+                       break;
+               case UUID_TYPE_DCE_MD5:
+               case UUID_TYPE_DCE_SHA1:
+                       if (namespace[0] == '@' && namespace[1] != '\0') {
+                               const uuid_t *uuidptr;
+
+                               uuidptr = uuid_get_template(&namespace[1]);
+                               if (uuidptr == NULL) {
+                                       warnx(_("unknown namespace alias: '%s'"), namespace);
+                                       errtryhelp(EXIT_FAILURE);
+                               }
+                               memcpy(ns, *uuidptr, sizeof(ns));
+                       } else {
+                               if (uuid_parse(namespace, ns) != 0) {
+                                       warnx(_("invalid uuid for namespace: '%s'"), namespace);
+                                       errtryhelp(EXIT_FAILURE);
+                               }
                        }
+                       if (do_type == UUID_TYPE_DCE_MD5)
+                               uuid_generate_md5(uu, ns, name, namelen);
+                       else
+                               uuid_generate_sha1(uu, ns, name, namelen);
+                       break;
+               default:
+                       uuid_generate(uu);
+                       break;
                }
-               if (do_type == UUID_TYPE_DCE_MD5)
-                       uuid_generate_md5(uu, ns, name, namelen);
-               else
-                       uuid_generate_sha1(uu, ns, name, namelen);
-               break;
-       default:
-               uuid_generate(uu);
-               break;
-       }
 
-       uuid_unparse(uu, str);
+               uuid_unparse(uu, str);
 
-       printf("%s\n", str);
+               printf("%s\n", str);
+       }
 
        if (is_hex)
                free(name);