|
rpm-build |
0fba15 |
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
|
|
rpm-build |
0fba15 |
*
|
|
rpm-build |
0fba15 |
* Copyright (C) 2014,2015 Colin Walters <walters@verbum.org>.
|
|
rpm-build |
0fba15 |
*
|
|
rpm-build |
0fba15 |
* This library is free software; you can redistribute it and/or
|
|
rpm-build |
0fba15 |
* modify it under the terms of the GNU Lesser General Public
|
|
rpm-build |
0fba15 |
* License as published by the Free Software Foundation; either
|
|
rpm-build |
0fba15 |
* version 2 of the License, or (at your option) any later version.
|
|
rpm-build |
0fba15 |
*
|
|
rpm-build |
0fba15 |
* This library is distributed in the hope that it will be useful,
|
|
rpm-build |
0fba15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
rpm-build |
0fba15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
rpm-build |
0fba15 |
* Lesser General Public License for more details.
|
|
rpm-build |
0fba15 |
*
|
|
rpm-build |
0fba15 |
* You should have received a copy of the GNU Lesser General Public
|
|
rpm-build |
0fba15 |
* License along with this library; if not, write to the
|
|
rpm-build |
0fba15 |
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
rpm-build |
0fba15 |
* Boston, MA 02111-1307, USA.
|
|
rpm-build |
0fba15 |
*/
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
#include "config.h"
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
#include <string.h>
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
#include <glnx-dirfd.h>
|
|
rpm-build |
0fba15 |
#include <glnx-fdio.h>
|
|
rpm-build |
0fba15 |
#include <glnx-errors.h>
|
|
rpm-build |
0fba15 |
#include <glnx-local-alloc.h>
|
|
rpm-build |
0fba15 |
#include <glnx-shutil.h>
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
/**
|
|
rpm-build |
0fba15 |
* glnx_opendirat_with_errno:
|
|
rpm-build |
0fba15 |
* @dfd: File descriptor for origin directory
|
|
rpm-build |
0fba15 |
* @name: Pathname, relative to @dfd
|
|
rpm-build |
0fba15 |
* @follow: Whether or not to follow symbolic links
|
|
rpm-build |
0fba15 |
*
|
|
rpm-build |
0fba15 |
* Use openat() to open a directory, using a standard set of flags.
|
|
rpm-build |
0fba15 |
* This function sets errno.
|
|
rpm-build |
0fba15 |
*/
|
|
rpm-build |
0fba15 |
int
|
|
rpm-build |
0fba15 |
glnx_opendirat_with_errno (int dfd,
|
|
rpm-build |
0fba15 |
const char *path,
|
|
rpm-build |
0fba15 |
gboolean follow)
|
|
rpm-build |
0fba15 |
{
|
|
rpm-build |
0fba15 |
int flags = O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_CLOEXEC | O_NOCTTY;
|
|
rpm-build |
0fba15 |
if (!follow)
|
|
rpm-build |
0fba15 |
flags |= O_NOFOLLOW;
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
dfd = glnx_dirfd_canonicalize (dfd);
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
return openat (dfd, path, flags);
|
|
rpm-build |
0fba15 |
}
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
/**
|
|
rpm-build |
0fba15 |
* glnx_opendirat:
|
|
rpm-build |
0fba15 |
* @dfd: File descriptor for origin directory
|
|
rpm-build |
0fba15 |
* @path: Pathname, relative to @dfd
|
|
rpm-build |
0fba15 |
* @follow: Whether or not to follow symbolic links
|
|
rpm-build |
0fba15 |
* @error: Error
|
|
rpm-build |
0fba15 |
*
|
|
rpm-build |
0fba15 |
* Use openat() to open a directory, using a standard set of flags.
|
|
rpm-build |
0fba15 |
*/
|
|
rpm-build |
0fba15 |
gboolean
|
|
rpm-build |
0fba15 |
glnx_opendirat (int dfd,
|
|
rpm-build |
0fba15 |
const char *path,
|
|
rpm-build |
0fba15 |
gboolean follow,
|
|
rpm-build |
0fba15 |
int *out_fd,
|
|
rpm-build |
0fba15 |
GError **error)
|
|
rpm-build |
0fba15 |
{
|
|
rpm-build |
0fba15 |
int ret = glnx_opendirat_with_errno (dfd, path, follow);
|
|
rpm-build |
0fba15 |
if (ret == -1)
|
|
rpm-build |
0fba15 |
return glnx_throw_errno_prefix (error, "opendir(%s)", path);
|
|
rpm-build |
0fba15 |
*out_fd = ret;
|
|
rpm-build |
0fba15 |
return TRUE;
|
|
rpm-build |
0fba15 |
}
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
struct GLnxRealDirfdIterator
|
|
rpm-build |
0fba15 |
{
|
|
rpm-build |
0fba15 |
gboolean initialized;
|
|
rpm-build |
0fba15 |
int fd;
|
|
rpm-build |
0fba15 |
DIR *d;
|
|
rpm-build |
0fba15 |
};
|
|
rpm-build |
0fba15 |
typedef struct GLnxRealDirfdIterator GLnxRealDirfdIterator;
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
/**
|
|
rpm-build |
0fba15 |
* glnx_dirfd_iterator_init_at:
|
|
rpm-build |
0fba15 |
* @dfd: File descriptor, may be AT_FDCWD or -1
|
|
rpm-build |
0fba15 |
* @path: Path, may be relative to @dfd
|
|
rpm-build |
0fba15 |
* @follow: If %TRUE and the last component of @path is a symlink, follow it
|
|
rpm-build |
0fba15 |
* @out_dfd_iter: (out caller-allocates): A directory iterator, will be initialized
|
|
rpm-build |
0fba15 |
* @error: Error
|
|
rpm-build |
0fba15 |
*
|
|
rpm-build |
0fba15 |
* Initialize @out_dfd_iter from @dfd and @path.
|
|
rpm-build |
0fba15 |
*/
|
|
rpm-build |
0fba15 |
gboolean
|
|
rpm-build |
0fba15 |
glnx_dirfd_iterator_init_at (int dfd,
|
|
rpm-build |
0fba15 |
const char *path,
|
|
rpm-build |
0fba15 |
gboolean follow,
|
|
rpm-build |
0fba15 |
GLnxDirFdIterator *out_dfd_iter,
|
|
rpm-build |
0fba15 |
GError **error)
|
|
rpm-build |
0fba15 |
{
|
|
rpm-build |
0fba15 |
glnx_autofd int fd = -1;
|
|
rpm-build |
0fba15 |
if (!glnx_opendirat (dfd, path, follow, &fd, error))
|
|
rpm-build |
0fba15 |
return FALSE;
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
if (!glnx_dirfd_iterator_init_take_fd (&fd, out_dfd_iter, error))
|
|
rpm-build |
0fba15 |
return FALSE;
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
return TRUE;
|
|
rpm-build |
0fba15 |
}
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
/**
|
|
rpm-build |
0fba15 |
* glnx_dirfd_iterator_init_take_fd:
|
|
rpm-build |
0fba15 |
* @dfd: File descriptor - ownership is taken, and the value is set to -1
|
|
rpm-build |
0fba15 |
* @dfd_iter: A directory iterator
|
|
rpm-build |
0fba15 |
* @error: Error
|
|
rpm-build |
0fba15 |
*
|
|
rpm-build |
0fba15 |
* Steal ownership of @dfd, using it to initialize @dfd_iter for
|
|
rpm-build |
0fba15 |
* iteration.
|
|
rpm-build |
0fba15 |
*/
|
|
rpm-build |
0fba15 |
gboolean
|
|
rpm-build |
0fba15 |
glnx_dirfd_iterator_init_take_fd (int *dfd,
|
|
rpm-build |
0fba15 |
GLnxDirFdIterator *dfd_iter,
|
|
rpm-build |
0fba15 |
GError **error)
|
|
rpm-build |
0fba15 |
{
|
|
rpm-build |
0fba15 |
GLnxRealDirfdIterator *real_dfd_iter = (GLnxRealDirfdIterator*) dfd_iter;
|
|
rpm-build |
0fba15 |
DIR *d = fdopendir (*dfd);
|
|
rpm-build |
0fba15 |
if (!d)
|
|
rpm-build |
0fba15 |
return glnx_throw_errno_prefix (error, "fdopendir");
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
real_dfd_iter->fd = glnx_steal_fd (dfd);
|
|
rpm-build |
0fba15 |
real_dfd_iter->d = d;
|
|
rpm-build |
0fba15 |
real_dfd_iter->initialized = TRUE;
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
return TRUE;
|
|
rpm-build |
0fba15 |
}
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
/**
|
|
rpm-build |
0fba15 |
* glnx_dirfd_iterator_next_dent:
|
|
rpm-build |
0fba15 |
* @dfd_iter: A directory iterator
|
|
rpm-build |
0fba15 |
* @out_dent: (out) (transfer none): Pointer to dirent; do not free
|
|
rpm-build |
0fba15 |
* @cancellable: Cancellable
|
|
rpm-build |
0fba15 |
* @error: Error
|
|
rpm-build |
0fba15 |
*
|
|
rpm-build |
0fba15 |
* Read the next value from @dfd_iter, causing @out_dent to be
|
|
rpm-build |
0fba15 |
* updated. If end of stream is reached, @out_dent will be set
|
|
rpm-build |
0fba15 |
* to %NULL, and %TRUE will be returned.
|
|
rpm-build |
0fba15 |
*/
|
|
rpm-build |
0fba15 |
gboolean
|
|
rpm-build |
0fba15 |
glnx_dirfd_iterator_next_dent (GLnxDirFdIterator *dfd_iter,
|
|
rpm-build |
0fba15 |
struct dirent **out_dent,
|
|
rpm-build |
0fba15 |
GCancellable *cancellable,
|
|
rpm-build |
0fba15 |
GError **error)
|
|
rpm-build |
0fba15 |
{
|
|
rpm-build |
0fba15 |
GLnxRealDirfdIterator *real_dfd_iter = (GLnxRealDirfdIterator*) dfd_iter;
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
g_return_val_if_fail (out_dent, FALSE);
|
|
rpm-build |
0fba15 |
g_return_val_if_fail (dfd_iter->initialized, FALSE);
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
if (g_cancellable_set_error_if_cancelled (cancellable, error))
|
|
rpm-build |
0fba15 |
return FALSE;
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
do
|
|
rpm-build |
0fba15 |
{
|
|
rpm-build |
0fba15 |
errno = 0;
|
|
rpm-build |
0fba15 |
*out_dent = readdir (real_dfd_iter->d);
|
|
rpm-build |
0fba15 |
if (*out_dent == NULL && errno != 0)
|
|
rpm-build |
0fba15 |
return glnx_throw_errno_prefix (error, "readdir");
|
|
rpm-build |
0fba15 |
} while (*out_dent &&
|
|
rpm-build |
0fba15 |
(strcmp ((*out_dent)->d_name, ".") == 0 ||
|
|
rpm-build |
0fba15 |
strcmp ((*out_dent)->d_name, "..") == 0));
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
return TRUE;
|
|
rpm-build |
0fba15 |
}
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
/**
|
|
rpm-build |
0fba15 |
* glnx_dirfd_iterator_next_dent_ensure_dtype:
|
|
rpm-build |
0fba15 |
* @dfd_iter: A directory iterator
|
|
rpm-build |
0fba15 |
* @out_dent: (out) (transfer none): Pointer to dirent; do not free
|
|
rpm-build |
0fba15 |
* @cancellable: Cancellable
|
|
rpm-build |
0fba15 |
* @error: Error
|
|
rpm-build |
0fba15 |
*
|
|
rpm-build |
0fba15 |
* A variant of @glnx_dirfd_iterator_next_dent, which will ensure the
|
|
rpm-build |
0fba15 |
* `dent->d_type` member is filled in by calling `fstatat`
|
|
rpm-build |
0fba15 |
* automatically if the underlying filesystem type sets `DT_UNKNOWN`.
|
|
rpm-build |
0fba15 |
*/
|
|
rpm-build |
0fba15 |
gboolean
|
|
rpm-build |
0fba15 |
glnx_dirfd_iterator_next_dent_ensure_dtype (GLnxDirFdIterator *dfd_iter,
|
|
rpm-build |
0fba15 |
struct dirent **out_dent,
|
|
rpm-build |
0fba15 |
GCancellable *cancellable,
|
|
rpm-build |
0fba15 |
GError **error)
|
|
rpm-build |
0fba15 |
{
|
|
rpm-build |
0fba15 |
g_return_val_if_fail (out_dent, FALSE);
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
if (!glnx_dirfd_iterator_next_dent (dfd_iter, out_dent, cancellable, error))
|
|
rpm-build |
0fba15 |
return FALSE;
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
struct dirent *ret_dent = *out_dent;
|
|
rpm-build |
0fba15 |
if (ret_dent)
|
|
rpm-build |
0fba15 |
{
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
if (ret_dent->d_type == DT_UNKNOWN)
|
|
rpm-build |
0fba15 |
{
|
|
rpm-build |
0fba15 |
struct stat stbuf;
|
|
rpm-build |
0fba15 |
if (!glnx_fstatat (dfd_iter->fd, ret_dent->d_name, &stbuf, AT_SYMLINK_NOFOLLOW, error))
|
|
rpm-build |
0fba15 |
return FALSE;
|
|
rpm-build |
0fba15 |
ret_dent->d_type = IFTODT (stbuf.st_mode);
|
|
rpm-build |
0fba15 |
}
|
|
rpm-build |
0fba15 |
}
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
return TRUE;
|
|
rpm-build |
0fba15 |
}
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
/**
|
|
rpm-build |
0fba15 |
* glnx_dirfd_iterator_clear:
|
|
rpm-build |
0fba15 |
* @dfd_iter: Iterator, will be de-initialized
|
|
rpm-build |
0fba15 |
*
|
|
rpm-build |
0fba15 |
* Unset @dfd_iter, freeing any resources. If @dfd_iter is not
|
|
rpm-build |
0fba15 |
* initialized, do nothing.
|
|
rpm-build |
0fba15 |
*/
|
|
rpm-build |
0fba15 |
void
|
|
rpm-build |
0fba15 |
glnx_dirfd_iterator_clear (GLnxDirFdIterator *dfd_iter)
|
|
rpm-build |
0fba15 |
{
|
|
rpm-build |
0fba15 |
GLnxRealDirfdIterator *real_dfd_iter = (GLnxRealDirfdIterator*) dfd_iter;
|
|
rpm-build |
0fba15 |
/* fd is owned by dfd_iter */
|
|
rpm-build |
0fba15 |
if (!real_dfd_iter->initialized)
|
|
rpm-build |
0fba15 |
return;
|
|
rpm-build |
0fba15 |
(void) closedir (real_dfd_iter->d);
|
|
rpm-build |
0fba15 |
real_dfd_iter->initialized = FALSE;
|
|
rpm-build |
0fba15 |
}
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
/**
|
|
rpm-build |
0fba15 |
* glnx_fdrel_abspath:
|
|
rpm-build |
0fba15 |
* @dfd: Directory fd
|
|
rpm-build |
0fba15 |
* @path: Path
|
|
rpm-build |
0fba15 |
*
|
|
rpm-build |
0fba15 |
* Turn a fd-relative pair into something that can be used for legacy
|
|
rpm-build |
0fba15 |
* APIs expecting absolute paths.
|
|
rpm-build |
0fba15 |
*
|
|
rpm-build |
0fba15 |
* This is Linux specific, and only valid inside this process (unless
|
|
rpm-build |
0fba15 |
* you set up the child process to have the exact same fd number, but
|
|
rpm-build |
0fba15 |
* don't try that).
|
|
rpm-build |
0fba15 |
*/
|
|
rpm-build |
0fba15 |
char *
|
|
rpm-build |
0fba15 |
glnx_fdrel_abspath (int dfd,
|
|
rpm-build |
0fba15 |
const char *path)
|
|
rpm-build |
0fba15 |
{
|
|
rpm-build |
0fba15 |
dfd = glnx_dirfd_canonicalize (dfd);
|
|
rpm-build |
0fba15 |
if (dfd == AT_FDCWD)
|
|
rpm-build |
0fba15 |
return g_strdup (path);
|
|
rpm-build |
0fba15 |
return g_strdup_printf ("/proc/self/fd/%d/%s", dfd, path);
|
|
rpm-build |
0fba15 |
}
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
/**
|
|
rpm-build |
0fba15 |
* glnx_gen_temp_name:
|
|
rpm-build |
0fba15 |
* @tmpl: (type filename): template directory name, the last 6 characters will be replaced
|
|
rpm-build |
0fba15 |
*
|
|
rpm-build |
0fba15 |
* Replace the last 6 characters of @tmpl with random ASCII. You must
|
|
rpm-build |
0fba15 |
* use this in combination with a mechanism to ensure race-free file
|
|
rpm-build |
0fba15 |
* creation such as `O_EXCL`.
|
|
rpm-build |
0fba15 |
*/
|
|
rpm-build |
0fba15 |
void
|
|
rpm-build |
0fba15 |
glnx_gen_temp_name (gchar *tmpl)
|
|
rpm-build |
0fba15 |
{
|
|
rpm-build |
0fba15 |
g_return_if_fail (tmpl != NULL);
|
|
rpm-build |
0fba15 |
const size_t len = strlen (tmpl);
|
|
rpm-build |
0fba15 |
g_return_if_fail (len >= 6);
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
static const char letters[] =
|
|
rpm-build |
0fba15 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
rpm-build |
0fba15 |
static const int NLETTERS = sizeof (letters) - 1;
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
char *XXXXXX = tmpl + (len - 6);
|
|
rpm-build |
0fba15 |
for (int i = 0; i < 6; i++)
|
|
rpm-build |
0fba15 |
XXXXXX[i] = letters[g_random_int_range(0, NLETTERS)];
|
|
rpm-build |
0fba15 |
}
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
/**
|
|
rpm-build |
0fba15 |
* glnx_mkdtempat:
|
|
rpm-build |
0fba15 |
* @dfd: Directory fd
|
|
rpm-build |
0fba15 |
* @tmpl: (type filename): Initial template directory name, last 6 characters will be replaced
|
|
rpm-build |
0fba15 |
* @mode: permissions with which to create the temporary directory
|
|
rpm-build |
0fba15 |
* @out_tmpdir: (out caller-allocates): Initialized tempdir structure
|
|
rpm-build |
0fba15 |
* @error: Error
|
|
rpm-build |
0fba15 |
*
|
|
rpm-build |
0fba15 |
* Somewhat similar to g_mkdtemp_full(), but fd-relative, and returns a
|
|
rpm-build |
0fba15 |
* structure that uses autocleanups. Note that the supplied @dfd lifetime
|
|
rpm-build |
0fba15 |
* must match or exceed that of @out_tmpdir in order to remove the directory.
|
|
rpm-build |
0fba15 |
*/
|
|
rpm-build |
0fba15 |
gboolean
|
|
rpm-build |
0fba15 |
glnx_mkdtempat (int dfd, const char *tmpl, int mode,
|
|
rpm-build |
0fba15 |
GLnxTmpDir *out_tmpdir, GError **error)
|
|
rpm-build |
0fba15 |
{
|
|
rpm-build |
0fba15 |
g_return_val_if_fail (tmpl != NULL, FALSE);
|
|
rpm-build |
0fba15 |
g_return_val_if_fail (out_tmpdir != NULL, FALSE);
|
|
rpm-build |
0fba15 |
g_return_val_if_fail (!out_tmpdir->initialized, FALSE);
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
dfd = glnx_dirfd_canonicalize (dfd);
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
g_autofree char *path = g_strdup (tmpl);
|
|
rpm-build |
0fba15 |
for (int count = 0; count < 100; count++)
|
|
rpm-build |
0fba15 |
{
|
|
rpm-build |
0fba15 |
glnx_gen_temp_name (path);
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
/* Ideally we could use openat(O_DIRECTORY | O_CREAT | O_EXCL) here
|
|
rpm-build |
0fba15 |
* to create and open the directory atomically, but that’s not supported by
|
|
rpm-build |
0fba15 |
* current kernel versions: http://www.openwall.com/lists/oss-security/2014/11/26/14
|
|
rpm-build |
0fba15 |
* (Tested on kernel 4.10.10-200.fc25.x86_64). For the moment, accept a
|
|
rpm-build |
0fba15 |
* TOCTTOU race here. */
|
|
rpm-build |
0fba15 |
if (mkdirat (dfd, path, mode) == -1)
|
|
rpm-build |
0fba15 |
{
|
|
rpm-build |
0fba15 |
if (errno == EEXIST)
|
|
rpm-build |
0fba15 |
continue;
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
/* Any other error will apply also to other names we might
|
|
rpm-build |
0fba15 |
* try, and there are 2^32 or so of them, so give up now.
|
|
rpm-build |
0fba15 |
*/
|
|
rpm-build |
0fba15 |
return glnx_throw_errno_prefix (error, "mkdirat");
|
|
rpm-build |
0fba15 |
}
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
/* And open it */
|
|
rpm-build |
0fba15 |
glnx_autofd int ret_dfd = -1;
|
|
rpm-build |
0fba15 |
if (!glnx_opendirat (dfd, path, FALSE, &ret_dfd, error))
|
|
rpm-build |
0fba15 |
{
|
|
rpm-build |
0fba15 |
/* If we fail to open, let's try to clean up */
|
|
rpm-build |
0fba15 |
(void)unlinkat (dfd, path, AT_REMOVEDIR);
|
|
rpm-build |
0fba15 |
return FALSE;
|
|
rpm-build |
0fba15 |
}
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
/* Return the initialized directory struct */
|
|
rpm-build |
0fba15 |
out_tmpdir->initialized = TRUE;
|
|
rpm-build |
0fba15 |
out_tmpdir->src_dfd = dfd; /* referenced; see above docs */
|
|
rpm-build |
0fba15 |
out_tmpdir->fd = glnx_steal_fd (&ret_dfd);
|
|
rpm-build |
0fba15 |
out_tmpdir->path = g_steal_pointer (&path);
|
|
rpm-build |
0fba15 |
return TRUE;
|
|
rpm-build |
0fba15 |
}
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
/* Failure */
|
|
rpm-build |
0fba15 |
g_set_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
|
|
rpm-build |
0fba15 |
"glnx_mkdtempat ran out of combinations to try");
|
|
rpm-build |
0fba15 |
return FALSE;
|
|
rpm-build |
0fba15 |
}
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
/**
|
|
rpm-build |
0fba15 |
* glnx_mkdtemp:
|
|
rpm-build |
0fba15 |
* @tmpl: (type filename): Source template directory name, last 6 characters will be replaced
|
|
rpm-build |
0fba15 |
* @mode: permissions to create the temporary directory with
|
|
rpm-build |
0fba15 |
* @out_tmpdir: (out caller-allocates): Return location for tmpdir data
|
|
rpm-build |
0fba15 |
* @error: Return location for a #GError, or %NULL
|
|
rpm-build |
0fba15 |
*
|
|
rpm-build |
0fba15 |
* Similar to glnx_mkdtempat(), but will use g_get_tmp_dir() as the parent
|
|
rpm-build |
0fba15 |
* directory to @tmpl.
|
|
rpm-build |
0fba15 |
*
|
|
rpm-build |
0fba15 |
* Returns: %TRUE on success, %FALSE otherwise
|
|
rpm-build |
0fba15 |
* Since: UNRELEASED
|
|
rpm-build |
0fba15 |
*/
|
|
rpm-build |
0fba15 |
gboolean
|
|
rpm-build |
0fba15 |
glnx_mkdtemp (const gchar *tmpl,
|
|
rpm-build |
0fba15 |
int mode,
|
|
rpm-build |
0fba15 |
GLnxTmpDir *out_tmpdir,
|
|
rpm-build |
0fba15 |
GError **error)
|
|
rpm-build |
0fba15 |
{
|
|
rpm-build |
0fba15 |
g_autofree char *path = g_build_filename (g_get_tmp_dir (), tmpl, NULL);
|
|
rpm-build |
0fba15 |
return glnx_mkdtempat (AT_FDCWD, path, mode,
|
|
rpm-build |
0fba15 |
out_tmpdir, error);
|
|
rpm-build |
0fba15 |
}
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
static gboolean
|
|
rpm-build |
0fba15 |
_glnx_tmpdir_free (GLnxTmpDir *tmpd,
|
|
rpm-build |
0fba15 |
gboolean delete_dir,
|
|
rpm-build |
0fba15 |
GCancellable *cancellable,
|
|
rpm-build |
0fba15 |
GError **error)
|
|
rpm-build |
0fba15 |
{
|
|
rpm-build |
0fba15 |
/* Support being passed NULL so we work nicely in a GPtrArray */
|
|
rpm-build |
0fba15 |
if (!(tmpd && tmpd->initialized))
|
|
rpm-build |
0fba15 |
return TRUE;
|
|
rpm-build |
0fba15 |
g_assert_cmpint (tmpd->fd, !=, -1);
|
|
rpm-build |
0fba15 |
glnx_close_fd (&tmpd->fd);
|
|
rpm-build |
0fba15 |
g_assert (tmpd->path);
|
|
rpm-build |
0fba15 |
g_assert_cmpint (tmpd->src_dfd, !=, -1);
|
|
rpm-build |
0fba15 |
g_autofree char *path = tmpd->path; /* Take ownership */
|
|
rpm-build |
0fba15 |
tmpd->initialized = FALSE;
|
|
rpm-build |
0fba15 |
if (delete_dir)
|
|
rpm-build |
0fba15 |
{
|
|
rpm-build |
0fba15 |
if (!glnx_shutil_rm_rf_at (tmpd->src_dfd, path, cancellable, error))
|
|
rpm-build |
0fba15 |
return FALSE;
|
|
rpm-build |
0fba15 |
}
|
|
rpm-build |
0fba15 |
return TRUE;
|
|
rpm-build |
0fba15 |
}
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
/**
|
|
rpm-build |
0fba15 |
* glnx_tmpdir_delete:
|
|
rpm-build |
0fba15 |
* @tmpf: Temporary dir
|
|
rpm-build |
0fba15 |
* @cancellable: Cancellable
|
|
rpm-build |
0fba15 |
* @error: Error
|
|
rpm-build |
0fba15 |
*
|
|
rpm-build |
0fba15 |
* Deallocate a tmpdir, closing the fd and recursively deleting the path. This
|
|
rpm-build |
0fba15 |
* is normally called indirectly via glnx_tmpdir_cleanup() by the autocleanup
|
|
rpm-build |
0fba15 |
* attribute, but you can also invoke this directly.
|
|
rpm-build |
0fba15 |
*
|
|
rpm-build |
0fba15 |
* If an error occurs while deleting the filesystem path, @tmpf will still have
|
|
rpm-build |
0fba15 |
* been deallocated and should not be reused.
|
|
rpm-build |
0fba15 |
*
|
|
rpm-build |
0fba15 |
* See also `glnx_tmpdir_unset` to avoid deleting the path.
|
|
rpm-build |
0fba15 |
*/
|
|
rpm-build |
0fba15 |
gboolean
|
|
rpm-build |
0fba15 |
glnx_tmpdir_delete (GLnxTmpDir *tmpf, GCancellable *cancellable, GError **error)
|
|
rpm-build |
0fba15 |
{
|
|
rpm-build |
0fba15 |
return _glnx_tmpdir_free (tmpf, TRUE, cancellable, error);
|
|
rpm-build |
0fba15 |
}
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
/**
|
|
rpm-build |
0fba15 |
* glnx_tmpdir_unset:
|
|
rpm-build |
0fba15 |
* @tmpf: Temporary dir
|
|
rpm-build |
0fba15 |
* @cancellable: Cancellable
|
|
rpm-build |
0fba15 |
* @error: Error
|
|
rpm-build |
0fba15 |
*
|
|
rpm-build |
0fba15 |
* Deallocate a tmpdir, but do not delete the filesystem path. See also
|
|
rpm-build |
0fba15 |
* `glnx_tmpdir_delete()`.
|
|
rpm-build |
0fba15 |
*/
|
|
rpm-build |
0fba15 |
void
|
|
rpm-build |
0fba15 |
glnx_tmpdir_unset (GLnxTmpDir *tmpf)
|
|
rpm-build |
0fba15 |
{
|
|
rpm-build |
0fba15 |
(void) _glnx_tmpdir_free (tmpf, FALSE, NULL, NULL);
|
|
rpm-build |
0fba15 |
}
|