Extract opendirat() helper function into libotutil

We were duplicating the code to do an opendirat() in a few places.
This commit is contained in:
Colin Walters 2014-09-16 11:15:36 -04:00
parent dfeb27eca5
commit b756a13a65
6 changed files with 94 additions and 14 deletions

View File

@ -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 \

View File

@ -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)

View File

@ -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)

View File

@ -0,0 +1,50 @@
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
*
* Copyright (C) 2014 Colin Walters <walters@verbum.org>
*
* 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;
}

View File

@ -0,0 +1,35 @@
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
*
* Copyright (C) 2014 Colin Walters <walters@verbum.org>.
*
* 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

View File

@ -40,6 +40,7 @@
#include <ot-waitable-queue.h>
#include <ot-keyfile-utils.h>
#include <ot-gio-utils.h>
#include <ot-fs-utils.h>
#include <ot-opt-utils.h>
#include <ot-unix-utils.h>
#include <ot-variant-utils.h>