gdbm/0003-Fix-semantics-of-gdbm_load-r.patch
wangzhiqiang f549c60ebc sync branch master
fix semantics of gdbm_load -r. improve handling of -u in gdbm_load

Signed-off-by: wangzhiqiang <wangzhiqiang95@huawei.com>
(cherry picked from commit 6749d2328d0262ec4f47611116a3c384758c7218)
2022-07-05 20:57:22 +08:00

110 lines
3.6 KiB
Diff

From 0591202918948d41e331094b283ff699ab916c54 Mon Sep 17 00:00:00 2001
From: Sergey Poznyakoff <gray@gnu.org>
Date: Fri, 1 Jul 2022 14:03:22 +0300
Subject: [PATCH] Fix semantics of gdbm_load -r
Fixes https://puszcza.gnu.org.ua/bugs/index.php?573
* src/gdbm_load.c: New option: --update (-U)
The --replace (-r) option is valid only if used together with
--update.
* doc/gdbm.texi: Document changes.
---
doc/gdbm.texi | 23 ++++++++++++++++++++---
src/gdbm_load.c | 18 +++++++++++++++++-
2 files changed, 37 insertions(+), 4 deletions(-)
diff --git a/doc/gdbm.texi b/doc/gdbm.texi
index 4a0b1a7..f1e23ba 100644
--- a/doc/gdbm.texi
+++ b/doc/gdbm.texi
@@ -2772,9 +2772,17 @@ must be given as the second argument.
In general, if two arguments are given the second one is treated as
the name of the database to create, overriding the file name specified
-in the flat file.
+in the flat file. All existing keys will be removed from this
+database prior to loading from the dump. Use the @option{--update}
+(@option{-U}) option if it is not what you wish.
-The utility understands the following command line arguments:
+When given the @option{--update} (@option{-U}) option,
+@command{gdbm_load} will update the existing database with the data
+from the dump. It will bail out if the dump contains a key that is
+already present in the database. To silently overwrite existing keys,
+use the @option{--replace} (@option{-r}) option.
+
+The utility understands the following command line options:
@table @option
@@ -2800,7 +2808,16 @@ Do not restore file meta-data (ownership and mode) from the flat file.
@item -r
@itemx --replace
-Replace existing keys.
+Replace existing keys. This option can be used only together with
+@option{--update} (@option{-U}).
+
+@item -U
+@itemx --update
+Update an existing database. The database name must be given in the
+second argument to @command{gdbm_load}. The key/value pairs from the
+dump file will be added to that database, without removing the
+existing keys. To overwrite the existing keys from the dump file, use
+@option{--update --replace}.
@item -u @var{user}[:@var{group}]
@itemx --user=@var{user}[:@var{group}]
diff --git a/src/gdbm_load.c b/src/gdbm_load.c
index 2d96ada..dd5cdc5 100644
--- a/src/gdbm_load.c
+++ b/src/gdbm_load.c
@@ -32,8 +32,9 @@ gid_t owner_gid;
char *parseopt_program_doc = "load a GDBM database from a file";
char *parseopt_program_args = "FILE [DB_FILE]";
struct gdbm_option optab[] = {
- { 'r', "replace", NULL, N_("replace records in the existing database") },
+ { 'r', "replace", NULL, N_("replace records in the existing database (needs -U)") },
{ 'm', "mode", N_("MODE"), N_("set file mode") },
+ { 'U', "update", NULL, N_("update the existing database") },
{ 'u', "user", N_("NAME|UID[:NAME|GID]"), N_("set file owner") },
{ 'n', "no-meta", NULL, N_("do not attempt to set file meta-data") },
{ 'M', "mmap", NULL, N_("use memory mapping") },
@@ -139,6 +140,10 @@ main (int argc, char **argv)
}
break;
+ case 'U':
+ oflags = (oflags & ~GDBM_OPENMASK) | GDBM_WRCREAT;
+ break;
+
case 'u':
{
size_t len;
@@ -231,10 +236,21 @@ main (int argc, char **argv)
error (_("too many arguments; try `%s -h' for more info"), progname);
exit (EXIT_USAGE);
}
+
+ if (replace && (oflags & GDBM_OPENMASK) != GDBM_WRCREAT)
+ {
+ error (_("-r is useless without -U"));
+ exit (EXIT_USAGE);
+ }
filename = argv[0];
if (argc == 2)
dbname = argv[1];
+ else if (oflags & GDBM_WRCREAT)
+ {
+ error (_("-U requires DB_FILE to be supplied"));
+ exit (EXIT_USAGE);
+ }
else
dbname = NULL;
--
1.8.3.1