summaryrefslogtreecommitdiff
path: root/lib/backup-rename.c
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2017-07-30 10:53:32 -0700
committerPaul Eggert <eggert@cs.ucla.edu>2017-07-30 11:08:43 -0700
commitb6ac7d133c6a478b40f5767b0c9d8713775dbf6f (patch)
tree2a33204cec3a0180f274bb687ae4b6eae6924f94 /lib/backup-rename.c
parentd274292d1b143f20ea21e3781fc614f1fd438a00 (diff)
downloadgnulib-b6ac7d133c6a478b40f5767b0c9d8713775dbf6f.tar.gz
backup-rename: new module
It is like backupfile, except it avoids some race conditions, and it does not output to stderr or exit. * MODULES.html.sh: Add backup-rename. * lib/backup-find.c, lib/backup-internal.h, lib/backup-rename.c: * modules/backup-rename: New files. * lib/backupfile.c: Turn this into an internals file, which contains code common to backupfile and backup_rename. Do not include argmatch.h or xalloc.h: include xalloc-oversized.h. Include renameat2.h and fcntl.h. (BACKUP_NOMEM): New constant. (numbered_backup): New args BASE_OFFSET and *DIRPP. Do not exit on memory exhaustion; just return BACKUP_NOMEM. Caller changed. (backupfile_internal): Rename from find_backup_file_name. Support new arg RENAME. (backup_args, backup_types, get_version, xget_version): Move to lib/backup-find.c. * lib/backupfile.h (backup_file_rename): New decl. * modules/backupfile (Files): Add lib/backup-internal.h, lib/backup-find.c. (Depends-on): Add dirfd, fcntl, renameat2. (lib_SOURCES): Add backup-find.c.
Diffstat (limited to 'lib/backup-rename.c')
-rw-r--r--lib/backup-rename.c31
1 files changed, 31 insertions, 0 deletions
diff --git a/lib/backup-rename.c b/lib/backup-rename.c
new file mode 100644
index 0000000000..628542b902
--- /dev/null
+++ b/lib/backup-rename.c
@@ -0,0 +1,31 @@
+/* Rename a file to a backup name, Emacs style.
+
+ Copyright 2017 Free Software Foundation, Inc.
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include <config.h>
+
+#include "backup-internal.h"
+
+/* Rename the existing file FILE to a backup name, allocated with
+ malloc, and return the backup name. On failure return a null
+ pointer, setting errno. Do not call this function if backup_type
+ == no_backups. */
+
+char *
+backup_file_rename (char const *file, enum backup_type backup_type)
+{
+ return backupfile_internal (file, backup_type, true);
+}