lib/repo: Port tmpdir locking func to new style

Prep for future work.

Closes: #1168
Approved by: jlebon
This commit is contained in:
Colin Walters 2017-09-12 10:31:39 -04:00 committed by Atomic Bot
parent 93038bcf71
commit 8d3752a0d6
1 changed files with 16 additions and 25 deletions

View File

@ -5148,19 +5148,17 @@ _ostree_repo_allocate_tmpdir (int tmpdir_dfd,
GCancellable *cancellable, GCancellable *cancellable,
GError **error) GError **error)
{ {
gboolean ret = FALSE;
gboolean reusing_dir = FALSE;
gboolean did_lock;
g_autofree char *tmpdir_name = NULL;
glnx_fd_close int tmpdir_fd = -1;
g_auto(GLnxDirFdIterator) dfd_iter = { 0, };
g_return_val_if_fail (_ostree_repo_is_locked_tmpdir (tmpdir_prefix), FALSE); g_return_val_if_fail (_ostree_repo_is_locked_tmpdir (tmpdir_prefix), FALSE);
/* Look for existing tmpdir (with same prefix) to reuse */ /* Look for existing tmpdir (with same prefix) to reuse */
g_auto(GLnxDirFdIterator) dfd_iter = { 0, };
if (!glnx_dirfd_iterator_init_at (tmpdir_dfd, ".", FALSE, &dfd_iter, error)) if (!glnx_dirfd_iterator_init_at (tmpdir_dfd, ".", FALSE, &dfd_iter, error))
goto out; return FALSE;
gboolean reusing_dir = FALSE;
gboolean did_lock = FALSE;
g_autofree char *tmpdir_name = NULL;
glnx_fd_close int tmpdir_fd = -1;
while (tmpdir_name == NULL) while (tmpdir_name == NULL)
{ {
struct dirent *dent; struct dirent *dent;
@ -5168,7 +5166,7 @@ _ostree_repo_allocate_tmpdir (int tmpdir_dfd,
g_autoptr(GError) local_error = NULL; g_autoptr(GError) local_error = NULL;
if (!glnx_dirfd_iterator_next_dent (&dfd_iter, &dent, cancellable, error)) if (!glnx_dirfd_iterator_next_dent (&dfd_iter, &dent, cancellable, error))
goto out; return FALSE;
if (dent == NULL) if (dent == NULL)
break; break;
@ -5189,7 +5187,7 @@ _ostree_repo_allocate_tmpdir (int tmpdir_dfd,
else else
{ {
g_propagate_error (error, g_steal_pointer (&local_error)); g_propagate_error (error, g_steal_pointer (&local_error));
goto out; return FALSE;
} }
} }
@ -5198,12 +5196,12 @@ _ostree_repo_allocate_tmpdir (int tmpdir_dfd,
if (!_ostree_repo_try_lock_tmpdir (tmpdir_dfd, dent->d_name, if (!_ostree_repo_try_lock_tmpdir (tmpdir_dfd, dent->d_name,
file_lock_out, &did_lock, file_lock_out, &did_lock,
error)) error))
goto out; return FALSE;
if (!did_lock) if (!did_lock)
continue; continue;
/* Touch the reused directory so that we don't accidentally /* Touch the reused directory so that we don't accidentally
* remove it due to being old when cleaning up the tmpdir * remove it due to being old when cleaning up the tmpdir.
*/ */
(void)futimens (existing_tmpdir_fd, NULL); (void)futimens (existing_tmpdir_fd, NULL);
@ -5215,24 +5213,22 @@ _ostree_repo_allocate_tmpdir (int tmpdir_dfd,
while (tmpdir_name == NULL) while (tmpdir_name == NULL)
{ {
g_autofree char *tmpdir_name_template = g_strconcat (tmpdir_prefix, "XXXXXX", NULL);
glnx_fd_close int new_tmpdir_fd = -1;
/* No existing tmpdir found, create a new */ /* No existing tmpdir found, create a new */
g_autofree char *tmpdir_name_template = g_strconcat (tmpdir_prefix, "XXXXXX", NULL);
if (!glnx_mkdtempat (tmpdir_dfd, tmpdir_name_template, 0777, error)) if (!glnx_mkdtempat (tmpdir_dfd, tmpdir_name_template, 0777, error))
goto out; return FALSE;
glnx_fd_close int new_tmpdir_fd = -1;
if (!glnx_opendirat (tmpdir_dfd, tmpdir_name_template, FALSE, if (!glnx_opendirat (tmpdir_dfd, tmpdir_name_template, FALSE,
&new_tmpdir_fd, error)) &new_tmpdir_fd, error))
goto out; return FALSE;
/* Note, at this point we can race with another process that picks up this /* Note, at this point we can race with another process that picks up this
* new directory. If that happens we need to retry, making a new directory. */ * new directory. If that happens we need to retry, making a new directory. */
if (!_ostree_repo_try_lock_tmpdir (tmpdir_dfd, tmpdir_name_template, if (!_ostree_repo_try_lock_tmpdir (tmpdir_dfd, tmpdir_name_template,
file_lock_out, &did_lock, file_lock_out, &did_lock,
error)) error))
goto out; return FALSE;
if (!did_lock) if (!did_lock)
continue; continue;
@ -5242,16 +5238,11 @@ _ostree_repo_allocate_tmpdir (int tmpdir_dfd,
if (tmpdir_name_out) if (tmpdir_name_out)
*tmpdir_name_out = g_steal_pointer (&tmpdir_name); *tmpdir_name_out = g_steal_pointer (&tmpdir_name);
if (tmpdir_fd_out) if (tmpdir_fd_out)
*tmpdir_fd_out = glnx_steal_fd (&tmpdir_fd); *tmpdir_fd_out = glnx_steal_fd (&tmpdir_fd);
if (reusing_dir_out) if (reusing_dir_out)
*reusing_dir_out = reusing_dir; *reusing_dir_out = reusing_dir;
return TRUE;
ret = TRUE;
out:
return ret;
} }
/* See ostree-repo-private.h for more information about this */ /* See ostree-repo-private.h for more information about this */