!54 Fix semantics of gdbm_load -r

fix https://gitee.com/src-openeuler/gdbm/issues/I5FA4V
使用-r时必须指定-U参数,逐条读取dump文件中的key,若数据库中已存在该key,则覆盖此条数据,对于数据库中的其他数据,则保留。
不使用-r时,将会清空数据库中所有的数据,然后把dump文件中的数据逐条写入
![输入图片说明](https://images.gitee.com/uploads/images/2022/0704/144022_635e16ea_8080932.png "-r.PNG")

单独使用-r会报错
![输入图片说明](https://images.gitee.com/uploads/images/2022/0704/152717_6d6503dd_8080932.png "-r1.PNG") 
From: @foolstrong 
Reviewed-by: @liuzhiqiang26 
Signed-off-by: @liuzhiqiang26
This commit is contained in:
openeuler-ci-bot 2022-07-04 13:54:54 +00:00 committed by Gitee
commit 79149e295b
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 128 additions and 2 deletions

View File

@ -0,0 +1,122 @@
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 | 25 ++++++++++++++++++++++---
src/gdbm_load.c | 20 ++++++++++++++++++--
2 files changed, 40 insertions(+), 5 deletions(-)
diff --git a/doc/gdbm.texi b/doc/gdbm.texi
index a738c85..d39144f 100644
--- a/doc/gdbm.texi
+++ b/doc/gdbm.texi
@@ -3210,6 +3210,8 @@ symmetry). A special code @samp{all} stands for all available error codes.
In boolean context, the @code{true} value is equivalent to @samp{all},
and @code{false} (i.e. variable unset) is equivalent to @samp{-all}.
+
+This variable cannot be set from interactive sessions.
@end deftypevr
@deftypevr {gdbmtool variable} string errormask
@@ -4029,9 +4031,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.
+
+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 arguments:
+The utility understands the following command line options:
@table @option
@@ -4057,7 +4067,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 41cb820..b2809e1 100644
--- a/src/gdbm_load.c
+++ b/src/gdbm_load.c
@@ -32,8 +32,9 @@ gid_t owner_gid;
char *parseopt_program_doc = N_("load a GDBM database from a file");
char *parseopt_program_args = N_("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;
@@ -228,13 +233,24 @@ main (int argc, char **argv)
if (argc > 2)
{
- error (_("too many arguments; try `%s -h' for more info"), progname);
+ 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

View File

@ -1,6 +1,6 @@
Name: gdbm
Version: 1.22
Release: 7
Release: 8
Epoch: 1
Summary: A library of database functions that work similar to the standard UNIX dbm
License: GPLv3+
@ -12,8 +12,9 @@ Patch1: Fix-binary-dump-format-for-key-and-or-data-of-zero-s.patch
Patch2: gdbm_dump-fix-command-line-error-detection.patch
Patch3: Fix-location-tracking-in-gdbmtool.-Fix-the-recover-c.patch
Patch4: Fix-coredump-in-gdbmtool-history-command.patch
Patch5: Fix-semantics-of-gdbm_load-r.patch
BuildRequires: gcc libtool gettext readline-devel bison flex
BuildRequires: gcc libtool gettext readline-devel bison flex texinfo
Provides: %{name}-libs
Provides: %{name}-libs%{?_isa}
@ -103,6 +104,9 @@ fi
%{_infodir}/*.info*
%changelog
* Mon Jul 4 2022 wangzhqiang <wangzhiqiang95@huawei.com> - 1:1.22-8
- DESC: Fix semantics of gdbm_load -r
* Tue Jun 28 2022 wangzhqiang <wangzhiqiang95@huawei.com> - 1:1.22-7
- DESC: Fix coredump in gdbmtool history command