From b756a13a65c122a091c489ae737b82df02cd0d59 Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Tue, 16 Sep 2014 11:15:36 -0400 Subject: Extract opendirat() helper function into libotutil We were duplicating the code to do an opendirat() in a few places. --- Makefile-otutil.am | 2 ++ src/libostree/ostree-repo.c | 2 +- src/libostree/ostree-sysroot-deploy.c | 18 ++++--------- src/libotutil/ot-fs-utils.c | 50 +++++++++++++++++++++++++++++++++++ src/libotutil/ot-fs-utils.h | 35 ++++++++++++++++++++++++ src/libotutil/otutil.h | 1 + 6 files changed, 94 insertions(+), 14 deletions(-) create mode 100644 src/libotutil/ot-fs-utils.c create mode 100644 src/libotutil/ot-fs-utils.h diff --git a/Makefile-otutil.am b/Makefile-otutil.am index 8f77543d..ffd38e09 100644 --- a/Makefile-otutil.am +++ b/Makefile-otutil.am @@ -22,6 +22,8 @@ noinst_LTLIBRARIES += libotutil.la libotutil_la_SOURCES = \ src/libotutil/ot-checksum-utils.c \ src/libotutil/ot-checksum-utils.h \ + src/libotutil/ot-fs-utils.c \ + src/libotutil/ot-fs-utils.h \ src/libotutil/ot-keyfile-utils.c \ src/libotutil/ot-keyfile-utils.h \ src/libotutil/ot-opt-utils.c \ diff --git a/src/libostree/ostree-repo.c b/src/libostree/ostree-repo.c index daa532e7..7069d01d 100644 --- a/src/libostree/ostree-repo.c +++ b/src/libostree/ostree-repo.c @@ -1100,7 +1100,7 @@ list_loose_objects (OstreeRepo *self, buf[0] = hexchars[c >> 4]; buf[1] = hexchars[c & 0xF]; buf[2] = '\0'; - dfd = openat (self->objects_dir_fd, buf, O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_CLOEXEC); + dfd = ot_opendirat (self->objects_dir_fd, buf, FALSE); if (dfd == -1) { if (errno == ENOENT) diff --git a/src/libostree/ostree-sysroot-deploy.c b/src/libostree/ostree-sysroot-deploy.c index ca12a3a9..fbb3ea1d 100644 --- a/src/libostree/ostree-sysroot-deploy.c +++ b/src/libostree/ostree-sysroot-deploy.c @@ -170,12 +170,8 @@ copy_dir_recurse_fsync (int src_parent_dfd, struct dirent *dent; gs_unref_variant GVariant *xattrs = NULL; - src_dfd = openat (src_parent_dfd, name, O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_CLOEXEC); - if (src_dfd == -1) - { - ot_util_set_error_from_errno (error, errno); - goto out; - } + if (!ot_gopendirat (src_parent_dfd, name, TRUE, &src_dfd, error)) + goto out; /* Create with mode 0700, we'll fchmod/fchown later */ if (mkdirat (dest_parent_dfd, name, 0700) != 0) @@ -184,12 +180,8 @@ copy_dir_recurse_fsync (int src_parent_dfd, goto out; } - dest_dfd = openat (dest_parent_dfd, name, O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_CLOEXEC); - if (dest_dfd == -1) - { - ot_util_set_error_from_errno (error, errno); - goto out; - } + if (!ot_gopendirat (dest_parent_dfd, name, TRUE, &dest_dfd, error)) + goto out; /* Clone all xattrs first, so we get the SELinux security context * right. This will allow other users access if they have ACLs, but @@ -315,7 +307,7 @@ copy_modified_config_file (int orig_etc_fd, if (parent_slash != NULL) { parent_path = g_strndup (path, parent_slash - path); - dest_parent_dfd = openat (new_etc_fd, parent_path, O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW | O_NOCTTY); + dest_parent_dfd = ot_opendirat (new_etc_fd, parent_path, FALSE); if (dest_parent_dfd == -1) { if (errno == ENOENT) diff --git a/src/libotutil/ot-fs-utils.c b/src/libotutil/ot-fs-utils.c new file mode 100644 index 00000000..1d02003e --- /dev/null +++ b/src/libotutil/ot-fs-utils.c @@ -0,0 +1,50 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- + * + * Copyright (C) 2014 Colin Walters + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include "config.h" + +#include "ot-fs-utils.h" + +int +ot_opendirat (int dfd, const char *path, gboolean follow) +{ + int flags = O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_CLOEXEC | O_NOCTTY; + if (!follow) + flags |= O_NOFOLLOW; + return openat (dfd, path, flags); +} + +gboolean +ot_gopendirat (int dfd, + const char *path, + gboolean follow, + int *out_fd, + GError **error) +{ + int ret = ot_opendirat (dfd, path, follow); + if (ret == -1) + { + ot_util_set_error_from_errno (error, errno); + return FALSE; + } + *out_fd = ret; + return TRUE; +} + diff --git a/src/libotutil/ot-fs-utils.h b/src/libotutil/ot-fs-utils.h new file mode 100644 index 00000000..d7ece4fa --- /dev/null +++ b/src/libotutil/ot-fs-utils.h @@ -0,0 +1,35 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- + * + * Copyright (C) 2014 Colin Walters . + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#pragma once + +#include "ot-unix-utils.h" + +G_BEGIN_DECLS + +int ot_opendirat (int dfd, const char *path, gboolean follow); +gboolean ot_gopendirat (int dfd, + const char *path, + gboolean follow, + int *out_fd, + GError **error); + +G_END_DECLS + diff --git a/src/libotutil/otutil.h b/src/libotutil/otutil.h index e0e30d51..c78a9c69 100644 --- a/src/libotutil/otutil.h +++ b/src/libotutil/otutil.h @@ -40,6 +40,7 @@ #include #include #include +#include #include #include #include -- cgit v1.2.1