From: "Fabian Groffen" <grobian@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] proj/portage-utils:master commit in: libq/
Date: Sun, 26 Dec 2021 13:59:53 +0000 (UTC) [thread overview]
Message-ID: <1640526529.117869a1b0d19c5e3a6b59e9e43e6849ed92efd4.grobian@gentoo> (raw)
commit: 117869a1b0d19c5e3a6b59e9e43e6849ed92efd4
Author: Fabian Groffen <grobian <AT> gentoo <DOT> org>
AuthorDate: Sun Dec 26 13:48:49 2021 +0000
Commit: Fabian Groffen <grobian <AT> gentoo <DOT> org>
CommitDate: Sun Dec 26 13:48:49 2021 +0000
URL: https://gitweb.gentoo.org/proj/portage-utils.git/commit/?id=117869a1
libq/move_file: code to move a file or copy+remove it
This code originated from qmerge.c, it was adapted to be more generic in
a function.
Signed-off-by: Fabian Groffen <grobian <AT> gentoo.org>
libq/Makefile.am | 1 +
libq/move_file.c | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
libq/move_file.h | 21 +++++++++++
3 files changed, 127 insertions(+)
diff --git a/libq/Makefile.am b/libq/Makefile.am
index da100b6..c0402a4 100644
--- a/libq/Makefile.am
+++ b/libq/Makefile.am
@@ -11,6 +11,7 @@ QFILES = \
hash.c hash.h \
human_readable.c human_readable.h \
i18n.h \
+ move_file.c move_file.h \
prelink.c prelink.h \
profile.c profile.h \
rmspace.c rmspace.h \
diff --git a/libq/move_file.c b/libq/move_file.c
new file mode 100644
index 0000000..07cf69f
--- /dev/null
+++ b/libq/move_file.c
@@ -0,0 +1,105 @@
+/*
+ * Copyright 2005-2021 Gentoo Authors
+ * Distributed under the terms of the GNU General Public License v2
+ *
+ * Copyright 2005-2010 Ned Ludd - <solar@gentoo.org>
+ * Copyright 2005-2014 Mike Frysinger - <vapier@gentoo.org>
+ * Copyright 2018- Fabian Groffen - <grobian@gentoo.org>
+ */
+
+#include "main.h"
+
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include "stat-time.h"
+
+#include "copy_file.h"
+#include "move_file.h"
+
+int
+move_file(int rootfd_src, const char *name_src,
+ int rootfd_dst, const char *name_dst,
+ struct stat *stat_src)
+{
+ /* first try fast path -- src/dst are same device, else
+ * fall back to slow path -- manual read/write */
+ if (renameat(rootfd_src, name_src, rootfd_dst, name_dst) != 0) {
+ int fd_src;
+ int fd_dst;
+ char tmpname_dst[_Q_PATH_MAX];
+ struct stat st;
+ struct timespec times[2];
+
+ fd_src = openat(rootfd_src, name_src, O_RDONLY|O_CLOEXEC);
+ if (fd_src < 0) {
+ warnp("could not read source file %s", name_src);
+ return fd_src;
+ }
+
+ if (stat_src == NULL) {
+ if (fstat(fd_src, &st) != 0) {
+ warnp("could not stat source file %s", name_src);
+ return -1;
+ }
+
+ stat_src = &st;
+ }
+
+ /* do not write the file in place ...
+ * will fail with files that are in use
+ * plus it isn't atomic, so we could leave a mess */
+ snprintf(tmpname_dst, sizeof(tmpname_dst), ".%u.%s",
+ getpid(), name_dst);
+ fd_dst = openat(rootfd_dst, tmpname_dst,
+ O_WRONLY|O_CLOEXEC|O_CREAT|O_TRUNC,
+ stat_src->st_mode);
+ if (fd_dst < 0) {
+ warnp("could not open destination file %s (for %s)",
+ tmpname_dst, name_dst);
+ close(fd_src);
+ return fd_dst;
+ }
+
+ /* make sure owner/mode is sane before we write out data */
+ if (fchown(fd_dst, stat_src->st_uid, stat_src->st_gid) != 0) {
+ warnp("could not set ownership (%zu/%zu) for %s",
+ (size_t)stat_src->st_uid, (size_t)stat_src->st_gid, name_dst);
+ return -1;
+ }
+ if (fchmod(fd_dst, stat_src->st_mode) != 0) {
+ warnp("could not set permission (%u) for %s",
+ (int)stat_src->st_mode, name_dst);
+ return -1;
+ }
+
+ /* do the actual data copy */
+ if (copy_file_fd(fd_src, fd_dst)) {
+ warnp("could not write to file %s", name_dst);
+ if (unlinkat(rootfd_dst, tmpname_dst, 0) != 0) {
+ /* don't care */;
+ }
+ close(fd_src);
+ close(fd_dst);
+ return -1;
+ }
+
+ /* Preserve the file times */
+ times[0] = get_stat_atime(&st);
+ times[1] = get_stat_mtime(&st);
+ futimens(fd_dst, times);
+
+ close(fd_src);
+ close(fd_dst);
+
+ /* finally move the new tmp dst file to the right place, which
+ * should be on the same FS/device now */
+ if (renameat(rootfd_dst, tmpname_dst, rootfd_dst, name_dst)) {
+ warnp("could not rename %s to %s", tmpname_dst, name_dst);
+ return -1;
+ }
+ }
+
+ return 0;
+}
diff --git a/libq/move_file.h b/libq/move_file.h
new file mode 100644
index 0000000..a454165
--- /dev/null
+++ b/libq/move_file.h
@@ -0,0 +1,21 @@
+/*
+ * Copyright 2005-2021 Gentoo Authors
+ * Distributed under the terms of the GNU General Public License v2
+ *
+ * Copyright 2005-2010 Ned Ludd - <solar@gentoo.org>
+ * Copyright 2005-2014 Mike Frysinger - <vapier@gentoo.org>
+ * Copyright 2018- Fabian Groffen - <grobian@gentoo.org>
+ */
+
+#ifndef _MOVE_FILE_H
+
+#define _MOVE_FILE_H 1
+
+#include <sys/types.h>
+#include <sys/stat.h>
+
+int move_file(int rootfd_src, const char *name_src,
+ int rootfd_dst, const char *name_dst,
+ struct stat *stat_src);
+
+#endif
next reply other threads:[~2021-12-26 14:00 UTC|newest]
Thread overview: 197+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-12-26 13:59 Fabian Groffen [this message]
-- strict thread matches above, loose matches on Subject: below --
2025-04-21 14:44 [gentoo-commits] proj/portage-utils:master commit in: libq/ Fabian Groffen
2024-07-03 19:44 Fabian Groffen
2024-04-08 19:27 Fabian Groffen
2024-02-01 8:21 Fabian Groffen
2024-02-01 8:21 Fabian Groffen
2024-01-31 20:41 Fabian Groffen
2024-01-31 19:30 Fabian Groffen
2024-01-31 19:29 Fabian Groffen
2024-01-27 13:28 Fabian Groffen
2023-04-21 19:11 Fabian Groffen
2023-01-30 14:14 Fabian Groffen
2022-05-26 14:36 Fabian Groffen
2022-05-26 14:36 Fabian Groffen
2022-05-20 17:15 Fabian Groffen
2022-05-20 17:15 Fabian Groffen
2022-05-19 8:32 Fabian Groffen
2022-05-19 8:16 Fabian Groffen
2022-05-19 7:45 Fabian Groffen
2022-02-12 17:13 Fabian Groffen
2022-02-12 17:13 Fabian Groffen
2022-02-06 14:51 Fabian Groffen
2022-02-06 14:29 Fabian Groffen
2022-02-06 13:27 Fabian Groffen
2022-02-06 13:27 Fabian Groffen
2022-02-06 12:22 Fabian Groffen
2021-12-29 12:20 Fabian Groffen
2021-12-26 13:59 Fabian Groffen
2021-12-26 13:59 Fabian Groffen
2021-12-26 13:59 Fabian Groffen
2021-12-13 8:39 Fabian Groffen
2021-12-13 8:39 Fabian Groffen
2021-11-13 14:27 Fabian Groffen
2021-10-09 12:13 Fabian Groffen
2021-10-04 6:28 Fabian Groffen
2021-10-04 6:28 Fabian Groffen
2021-10-03 10:49 Fabian Groffen
2021-06-23 7:14 Fabian Groffen
2021-06-14 9:34 Fabian Groffen
2021-06-14 9:34 Fabian Groffen
2021-06-14 9:34 Fabian Groffen
2021-06-14 9:34 Fabian Groffen
2021-06-14 9:34 Fabian Groffen
2021-06-14 9:34 Fabian Groffen
2021-06-01 19:43 Fabian Groffen
2021-05-23 10:54 Fabian Groffen
2021-05-10 9:15 Fabian Groffen
2021-04-29 15:04 Fabian Groffen
2021-04-29 13:47 Fabian Groffen
2021-04-29 13:24 Fabian Groffen
2021-03-13 12:44 Fabian Groffen
2021-02-20 12:06 Fabian Groffen
2021-02-20 11:44 Fabian Groffen
2021-02-17 20:23 Fabian Groffen
2021-02-17 20:23 Fabian Groffen
2021-01-15 20:05 Fabian Groffen
2020-06-27 9:38 Fabian Groffen
2020-06-07 10:41 Fabian Groffen
2020-05-25 18:19 Fabian Groffen
2020-05-25 18:02 Fabian Groffen
2020-05-25 13:26 Fabian Groffen
2020-05-25 11:20 Fabian Groffen
2020-05-25 11:06 Fabian Groffen
2020-05-25 10:43 Fabian Groffen
2020-05-25 10:43 Fabian Groffen
2020-05-25 10:43 Fabian Groffen
2020-05-25 10:43 Fabian Groffen
2020-05-25 10:43 Fabian Groffen
2020-05-17 12:35 Fabian Groffen
2020-05-17 12:35 Fabian Groffen
2020-02-03 13:17 Fabian Groffen
2020-02-03 13:09 Fabian Groffen
2020-01-26 19:31 Fabian Groffen
2020-01-22 19:54 Fabian Groffen
2020-01-22 19:54 Fabian Groffen
2020-01-20 19:54 Fabian Groffen
2020-01-20 19:34 Fabian Groffen
2020-01-19 19:36 Fabian Groffen
2020-01-19 19:09 Fabian Groffen
2020-01-19 19:09 Fabian Groffen
2020-01-19 19:09 Fabian Groffen
2020-01-19 19:09 Fabian Groffen
2020-01-19 16:37 Fabian Groffen
2020-01-19 12:37 Fabian Groffen
2020-01-19 10:05 Fabian Groffen
2020-01-19 9:49 Fabian Groffen
2020-01-19 9:49 Fabian Groffen
2020-01-17 8:22 Fabian Groffen
2020-01-05 16:08 Fabian Groffen
2020-01-05 16:08 Fabian Groffen
2020-01-05 16:08 Fabian Groffen
2020-01-02 15:09 Fabian Groffen
2020-01-02 14:07 Fabian Groffen
2020-01-02 14:07 Fabian Groffen
2020-01-02 14:07 Fabian Groffen
2020-01-02 11:55 Fabian Groffen
2020-01-02 11:19 Fabian Groffen
2019-12-30 17:24 Fabian Groffen
2019-12-27 21:19 Fabian Groffen
2019-12-27 16:57 Fabian Groffen
2019-12-27 16:57 Fabian Groffen
2019-11-29 13:22 Fabian Groffen
2019-11-20 17:23 Fabian Groffen
2019-11-19 20:28 Fabian Groffen
2019-11-17 15:12 Fabian Groffen
2019-11-17 15:12 Fabian Groffen
2019-11-13 18:19 Fabian Groffen
2019-11-13 15:48 Fabian Groffen
2019-11-13 15:20 Fabian Groffen
2019-11-09 10:29 Fabian Groffen
2019-09-26 14:06 Fabian Groffen
2019-09-26 14:06 Fabian Groffen
2019-09-26 14:06 Fabian Groffen
2019-09-26 14:06 Fabian Groffen
2019-09-26 13:00 Fabian Groffen
2019-09-25 15:05 Fabian Groffen
2019-09-21 19:53 Fabian Groffen
2019-09-21 19:53 Fabian Groffen
2019-07-14 18:51 Fabian Groffen
2019-07-13 15:37 Fabian Groffen
2019-07-13 9:50 Fabian Groffen
2019-07-12 18:04 Fabian Groffen
2019-06-19 7:41 Fabian Groffen
2019-06-10 10:09 Fabian Groffen
2019-06-05 7:57 Fabian Groffen
2019-05-21 14:12 Fabian Groffen
2019-05-14 20:19 Fabian Groffen
2019-05-14 20:19 Fabian Groffen
2019-05-11 11:11 Fabian Groffen
2019-05-11 7:14 Fabian Groffen
2019-05-11 7:14 Fabian Groffen
2019-05-10 15:32 Fabian Groffen
2019-05-10 15:32 Fabian Groffen
2019-05-10 15:32 Fabian Groffen
2019-05-07 6:19 Fabian Groffen
2019-05-06 16:04 Fabian Groffen
2019-05-06 16:04 Fabian Groffen
2019-05-05 20:05 Fabian Groffen
2019-05-05 18:13 Fabian Groffen
2019-05-05 8:58 Fabian Groffen
2019-05-04 11:53 Fabian Groffen
2019-05-03 11:45 Fabian Groffen
2019-05-02 15:17 Fabian Groffen
2019-05-01 19:09 Fabian Groffen
2019-04-30 8:20 Fabian Groffen
2019-04-30 7:54 Fabian Groffen
2019-04-28 17:10 Fabian Groffen
2019-04-28 16:21 Fabian Groffen
2019-04-28 16:02 Fabian Groffen
2019-04-27 8:38 Fabian Groffen
2019-04-25 17:36 Fabian Groffen
2019-04-25 9:22 Fabian Groffen
2019-04-25 9:22 Fabian Groffen
2019-04-25 9:22 Fabian Groffen
2019-04-25 9:22 Fabian Groffen
2019-04-19 11:47 Fabian Groffen
2019-03-27 10:55 Fabian Groffen
2019-03-11 20:55 Fabian Groffen
2019-03-09 18:58 Fabian Groffen
2019-02-27 20:53 Fabian Groffen
2019-02-27 20:53 Fabian Groffen
2019-02-05 14:19 Fabian Groffen
2018-12-20 20:02 Fabian Groffen
2018-12-20 20:02 Fabian Groffen
2018-12-20 18:24 Fabian Groffen
2018-04-09 7:15 Fabian Groffen
2018-04-05 13:31 Fabian Groffen
2018-04-05 12:46 Fabian Groffen
2018-04-03 20:00 Fabian Groffen
2018-03-26 18:41 Fabian Groffen
2018-03-25 14:13 Fabian Groffen
2018-03-25 14:00 Fabian Groffen
2018-03-23 20:17 Fabian Groffen
2018-03-23 11:56 Fabian Groffen
2018-03-23 11:29 Fabian Groffen
2017-12-29 11:45 Fabian Groffen
2017-12-29 11:45 Fabian Groffen
2017-12-29 11:45 Fabian Groffen
2016-12-29 2:25 Mike Frysinger
2016-11-12 17:23 Mike Frysinger
2016-02-14 1:26 Mike Frysinger
2016-02-14 1:26 Mike Frysinger
2015-11-26 8:43 Mike Frysinger
2015-10-15 22:00 Mike Frysinger
2015-10-15 22:00 Mike Frysinger
2015-05-31 8:31 Mike Frysinger
2015-05-19 17:37 Mike Frysinger
2015-02-24 1:26 Mike Frysinger
2015-02-24 1:26 Mike Frysinger
2015-02-24 1:26 Mike Frysinger
2015-02-21 18:06 Mike Frysinger
2015-02-16 11:47 Mike Frysinger
2014-03-11 4:53 Mike Frysinger
2014-03-08 5:51 Mike Frysinger
2014-03-08 5:51 Mike Frysinger
2014-03-08 5:51 Mike Frysinger
2014-03-08 5:51 Mike Frysinger
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1640526529.117869a1b0d19c5e3a6b59e9e43e6849ed92efd4.grobian@gentoo \
--to=grobian@gentoo.org \
--cc=gentoo-commits@lists.gentoo.org \
--cc=gentoo-dev@lists.gentoo.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox