core: Cleanup commitpartial file with fd-relative lookups

First, this is just a general continuation of the `GFile -> openat`
transition.

Second, it's preparatory work for fsck to gain awareness of partial
commits.
This commit is contained in:
Colin Walters 2015-04-05 14:07:03 -04:00
parent a68242c6c9
commit 279308b5b9
3 changed files with 55 additions and 28 deletions

View File

@ -112,6 +112,12 @@ _ostree_get_relative_static_delta_part_path (const char *from,
const char *to, const char *to,
guint i); guint i);
static inline char *
_ostree_get_commitpartial_path (const char *checksum)
{
return g_strconcat ("state/", checksum, ".commitpartial", NULL);
}
void void
_ostree_loose_path (char *buf, _ostree_loose_path (char *buf,
const char *checksum, const char *checksum,

View File

@ -43,10 +43,16 @@ prune_commitpartial_file (OstreeRepo *repo,
GError **error) GError **error)
{ {
gboolean ret = FALSE; gboolean ret = FALSE;
gs_unref_object GFile *objpath = ot_gfile_resolve_path_printf (repo->repodir, "state/%s.commitpartial", checksum); g_autofree char *path = _ostree_get_commitpartial_path (checksum);
if (!ot_gfile_ensure_unlinked (objpath, cancellable, error)) if (unlinkat (repo->repo_dir_fd, path, 0) != 0)
goto out; {
if (errno != ENOENT)
{
glnx_set_error_from_errno (error);
goto out;
}
}
ret = TRUE; ret = TRUE;
out: out:

View File

@ -713,15 +713,6 @@ on_metadata_written (GObject *object,
check_outstanding_requests_handle_error (pull_data, local_error); check_outstanding_requests_handle_error (pull_data, local_error);
} }
/* GFile pointing to the <repodir>/state/<checksum>.commitpartial file */
static GFile *
get_commitpartial_path (OstreeRepo *repo,
const char *commit)
{
gs_free char *commitpartial_filename = g_strdup_printf ("%s.commitpartial", commit);
return g_file_get_child (repo->state_dir, commitpartial_filename);
}
static void static void
meta_fetch_on_complete (GObject *object, meta_fetch_on_complete (GObject *object,
GAsyncResult *result, GAsyncResult *result,
@ -791,14 +782,17 @@ meta_fetch_on_complete (GObject *object,
/* Write the commitpartial file now while we're still fetching data */ /* Write the commitpartial file now while we're still fetching data */
if (objtype == OSTREE_OBJECT_TYPE_COMMIT) if (objtype == OSTREE_OBJECT_TYPE_COMMIT)
{ {
GFile *commitpartial_path = get_commitpartial_path (pull_data->repo, checksum); g_autofree char *commitpartial_path = _ostree_get_commitpartial_path (checksum);
glnx_fd_close int fd = -1;
if (!g_file_query_exists (commitpartial_path, NULL)) fd = openat (pull_data->repo->repo_dir_fd, commitpartial_path, O_EXCL | O_CREAT | O_WRONLY | O_CLOEXEC | O_NOCTTY, 0600);
if (fd == -1)
{ {
if (!g_file_replace_contents (commitpartial_path, "", 0, NULL, FALSE, if (errno != EEXIST)
G_FILE_CREATE_REPLACE_DESTINATION, NULL, {
pull_data->cancellable, error)) glnx_set_error_from_errno (error);
goto out; goto out;
}
} }
} }
@ -1132,9 +1126,10 @@ scan_one_metadata_object_c (OtPullData *pull_data,
/* For commits, check whether we only had a partial fetch */ /* For commits, check whether we only had a partial fetch */
if (!do_scan && objtype == OSTREE_OBJECT_TYPE_COMMIT) if (!do_scan && objtype == OSTREE_OBJECT_TYPE_COMMIT)
{ {
gs_unref_object GFile *commitpartial_file = get_commitpartial_path (pull_data->repo, tmp_checksum); g_autofree char *commitpartial_path = _ostree_get_commitpartial_path (tmp_checksum);
struct stat stbuf;
if (g_file_query_exists (commitpartial_file, NULL)) if (fstatat (pull_data->repo->repo_dir_fd, commitpartial_path, &stbuf, 0) == 0)
{ {
do_scan = TRUE; do_scan = TRUE;
pull_data->commitpartial_exists = TRUE; pull_data->commitpartial_exists = TRUE;
@ -2018,8 +2013,14 @@ ostree_repo_pull_with_options (OstreeRepo *self,
/* Create the state directory here - it's new with the commitpartial code, /* Create the state directory here - it's new with the commitpartial code,
* and may not exist in older repositories. * and may not exist in older repositories.
*/ */
if (!gs_file_ensure_directory (pull_data->repo->state_dir, FALSE, pull_data->cancellable, error)) if (mkdirat (pull_data->repo->repo_dir_fd, "state", 0777) != 0)
goto out; {
if (G_UNLIKELY (errno != EEXIST))
{
glnx_set_error_from_errno (error);
return FALSE;
}
}
pull_data->phase = OSTREE_PULL_PHASE_FETCHING_OBJECTS; pull_data->phase = OSTREE_PULL_PHASE_FETCHING_OBJECTS;
@ -2175,17 +2176,31 @@ ostree_repo_pull_with_options (OstreeRepo *self,
while (g_hash_table_iter_next (&hash_iter, &key, &value)) while (g_hash_table_iter_next (&hash_iter, &key, &value))
{ {
const char *checksum = value; const char *checksum = value;
gs_unref_object GFile *commitpartial_path = get_commitpartial_path (pull_data->repo, checksum); g_autofree char *commitpartial_path = _ostree_get_commitpartial_path (checksum);
if (!ot_gfile_ensure_unlinked (commitpartial_path, cancellable, error))
goto out; if (unlinkat (pull_data->repo->repo_dir_fd, commitpartial_path, 0) != 0)
{
if (errno != ENOENT)
{
glnx_set_error_from_errno (error);
goto out;
}
}
} }
g_hash_table_iter_init (&hash_iter, commits_to_fetch); g_hash_table_iter_init (&hash_iter, commits_to_fetch);
while (g_hash_table_iter_next (&hash_iter, &key, &value)) while (g_hash_table_iter_next (&hash_iter, &key, &value))
{ {
const char *commit = value; const char *commit = value;
gs_unref_object GFile *commitpartial_path = get_commitpartial_path (pull_data->repo, commit); g_autofree char *commitpartial_path = _ostree_get_commitpartial_path (commit);
if (!ot_gfile_ensure_unlinked (commitpartial_path, cancellable, error))
goto out; if (unlinkat (pull_data->repo->repo_dir_fd, commitpartial_path, 0) != 0)
{
if (errno != ENOENT)
{
glnx_set_error_from_errno (error);
goto out;
}
}
} }
} }