diff --git a/src/libostree/ostree-repo-commit.c b/src/libostree/ostree-repo-commit.c index a149c2e0d1..5e1b16a6c9 100644 --- a/src/libostree/ostree-repo-commit.c +++ b/src/libostree/ostree-repo-commit.c @@ -108,6 +108,7 @@ write_file_metadata_to_xattr (int fd, static gboolean commit_loose_object_trusted (OstreeRepo *self, + const char *checksum, OstreeObjectType objtype, const char *loose_path, GFile *temp_file, @@ -251,7 +252,7 @@ commit_loose_object_trusted (OstreeRepo *self, /* Ensure that in case of a power cut, these files have the data we * want. See http://lwn.net/Articles/322823/ */ - if (!self->disable_fsync) + if (!self->in_transaction && !self->commit_to_tmp_first && !self->disable_fsync) { if (fsync (fd) == -1) { @@ -267,20 +268,39 @@ commit_loose_object_trusted (OstreeRepo *self, if (!_ostree_repo_ensure_loose_objdir_at (self->objects_dir_fd, loose_path, cancellable, error)) goto out; - - if (G_UNLIKELY (renameat (self->tmp_dir_fd, temp_filename, - self->objects_dir_fd, loose_path) == -1)) - { - if (errno != EEXIST) - { - gs_set_error_from_errno (error, errno); - g_prefix_error (error, "Storing file '%s': ", temp_filename); - goto out; - } - else - (void) unlinkat (self->tmp_dir_fd, temp_filename, 0); - } + { + gs_free gchar *tmp_dest = NULL; + int dir; + const char *dest; + + if (self->in_transaction && self->commit_to_tmp_first) + { + tmp_dest = g_strdup_printf ("tmpobject-%s.%s", + checksum, + ostree_object_type_to_string (objtype)); + dir = self->tmp_dir_fd; + dest = tmp_dest; + } + else + { + dir = self->objects_dir_fd; + dest = loose_path; + } + + if (G_UNLIKELY (renameat (self->tmp_dir_fd, temp_filename, + dir, dest) == -1)) + { + if (errno != EEXIST) + { + gs_set_error_from_errno (error, errno); + g_prefix_error (error, "Storing file '%s': ", temp_filename); + goto out; + } + else + (void) unlinkat (self->tmp_dir_fd, temp_filename, 0); + } + } ret = TRUE; out: return ret; @@ -474,7 +494,7 @@ write_object (OstreeRepo *self, { if (!_ostree_repo_has_loose_object (self, expected_checksum, objtype, &have_obj, loose_objpath, - cancellable, error)) + NULL, cancellable, error)) goto out; if (have_obj) { @@ -655,7 +675,7 @@ write_object (OstreeRepo *self, } if (!_ostree_repo_has_loose_object (self, actual_checksum, objtype, - &have_obj, loose_objpath, + &have_obj, loose_objpath, NULL, cancellable, error)) goto out; @@ -663,7 +683,8 @@ write_object (OstreeRepo *self, if (do_commit) { - if (!commit_loose_object_trusted (self, objtype, loose_objpath, + if (!commit_loose_object_trusted (self, actual_checksum, + objtype, loose_objpath, temp_file, temp_filename, object_is_symlink, file_info, xattrs, temp_out, @@ -944,6 +965,7 @@ ostree_repo_prepare_transaction (OstreeRepo *self, static gboolean cleanup_tmpdir (OstreeRepo *self, + gboolean move_tmpobject, GCancellable *cancellable, GError **error) { @@ -966,6 +988,7 @@ cleanup_tmpdir (OstreeRepo *self, GFile *path; guint64 mtime; guint64 delta; + gs_free char *basename = NULL; if (!gs_file_enumerator_iterate (enumerator, &file_info, &path, cancellable, error)) @@ -973,6 +996,39 @@ cleanup_tmpdir (OstreeRepo *self, if (file_info == NULL) break; + if (move_tmpobject) + { + basename = g_file_get_basename (path); + if (strncmp (basename, "tmpobject-", 10) == 0) + { + char loose_path[_OSTREE_LOOSE_PATH_MAX]; + gs_free gchar *checksum = NULL; + OstreeObjectType type; + ostree_object_from_string (basename + 10, + &checksum, + &type); + + _ostree_loose_path (loose_path, checksum, type, self->mode); + + if (!_ostree_repo_ensure_loose_objdir_at (self->objects_dir_fd, loose_path, + cancellable, error)) + goto out; + + if (G_UNLIKELY (renameat (self->tmp_dir_fd, basename, + self->objects_dir_fd, loose_path) < 0)) + { + (void) unlinkat (self->tmp_dir_fd, basename, 0); + if (errno != EEXIST) + { + gs_set_error_from_errno (error, errno); + g_prefix_error (error, "Storing file '%s': ", loose_path); + goto out; + } + } + continue; + } + } + mtime = g_file_info_get_attribute_uint64 (file_info, "time::modified"); if (mtime > curtime_secs) continue; @@ -1109,7 +1165,13 @@ ostree_repo_commit_transaction (OstreeRepo *self, g_return_val_if_fail (self->in_transaction == TRUE, FALSE); - if (!cleanup_tmpdir (self, cancellable, error)) + if (syncfs (self->tmp_dir_fd) < 0) + { + gs_set_error_from_errno (error, errno); + goto out; + } + + if (!cleanup_tmpdir (self, TRUE, cancellable, error)) goto out; if (self->loose_object_devino_hash) @@ -1143,7 +1205,7 @@ ostree_repo_abort_transaction (OstreeRepo *self, if (!self->in_transaction) return TRUE; - if (!cleanup_tmpdir (self, cancellable, error)) + if (!cleanup_tmpdir (self, FALSE, cancellable, error)) goto out; if (self->loose_object_devino_hash) diff --git a/src/libostree/ostree-repo-private.h b/src/libostree/ostree-repo-private.h index b36e6d9405..522b25a5e9 100644 --- a/src/libostree/ostree-repo-private.h +++ b/src/libostree/ostree-repo-private.h @@ -59,6 +59,7 @@ struct OstreeRepo { gboolean inited; gboolean writable; gboolean in_transaction; + gboolean commit_to_tmp_first; gboolean disable_fsync; GHashTable *loose_object_devino_hash; GHashTable *updated_uncompressed_dirs; @@ -101,6 +102,7 @@ _ostree_repo_has_loose_object (OstreeRepo *self, OstreeObjectType objtype, gboolean *out_is_stored, char *loose_path_buf, + GFile **out_stored_path, GCancellable *cancellable, GError **error); diff --git a/src/libostree/ostree-repo-pull.c b/src/libostree/ostree-repo-pull.c index e61538af97..47e375cf85 100644 --- a/src/libostree/ostree-repo-pull.c +++ b/src/libostree/ostree-repo-pull.c @@ -1604,6 +1604,8 @@ ostree_repo_pull_with_options (OstreeRepo *self, (void) g_variant_lookup (options, "depth", "i", &pull_data->maxdepth); } + self->commit_to_tmp_first = TRUE; + g_return_val_if_fail (pull_data->maxdepth >= -1, FALSE); if (dir_to_pull) diff --git a/src/libostree/ostree-repo.c b/src/libostree/ostree-repo.c index f27911dea1..f753595351 100644 --- a/src/libostree/ostree-repo.c +++ b/src/libostree/ostree-repo.c @@ -1715,6 +1715,13 @@ load_metadata_internal (OstreeRepo *self, cancellable, error)) goto out; + if (self->commit_to_tmp_first && fd < 0) + { + sprintf (loose_path_buf, "tmpobject-%s.%s", sha256, ostree_object_type_to_string (objtype)); + if (!openat_allow_noent (self->tmp_dir_fd, loose_path_buf, &fd, cancellable, error)) + goto out; + } + if (fd != -1) { if (out_variant) @@ -2111,27 +2118,55 @@ _ostree_repo_has_loose_object (OstreeRepo *self, OstreeObjectType objtype, gboolean *out_is_stored, char *loose_path_buf, + GFile **out_stored_path, GCancellable *cancellable, GError **error) { gboolean ret = FALSE; struct stat stbuf; - int res; + int res = -1; + gboolean tmp_file = FALSE; - _ostree_loose_path (loose_path_buf, checksum, objtype, self->mode); + if (self->commit_to_tmp_first) + { + sprintf (loose_path_buf, "tmpobject-%s.%s", checksum, ostree_object_type_to_string (objtype)); + do + res = fstatat (self->tmp_dir_fd, loose_path_buf, &stbuf, AT_SYMLINK_NOFOLLOW); + while (G_UNLIKELY (res == -1 && errno == EINTR)); + if (res == -1 && errno != ENOENT) + { + gs_set_error_from_errno (error, errno); + goto out; + } + } - do - res = fstatat (self->objects_dir_fd, loose_path_buf, &stbuf, AT_SYMLINK_NOFOLLOW); - while (G_UNLIKELY (res == -1 && errno == EINTR)); - if (res == -1 && errno != ENOENT) + if (res == 0) + tmp_file = TRUE; + else { - gs_set_error_from_errno (error, errno); - goto out; + _ostree_loose_path (loose_path_buf, checksum, objtype, self->mode); + + do + res = fstatat (self->objects_dir_fd, loose_path_buf, &stbuf, AT_SYMLINK_NOFOLLOW); + while (G_UNLIKELY (res == -1 && errno == EINTR)); + if (res == -1 && errno != ENOENT) + { + gs_set_error_from_errno (error, errno); + goto out; + } } ret = TRUE; *out_is_stored = (res != -1); - out: + + if (out_stored_path) + { + if (res != -1) + *out_stored_path = g_file_resolve_relative_path (tmp_file ? self->tmp_dir : self->objects_dir, loose_path_buf); + else + *out_stored_path = NULL; + } +out: return ret; } @@ -2143,21 +2178,10 @@ _ostree_repo_find_object (OstreeRepo *self, GCancellable *cancellable, GError **error) { - gboolean ret = FALSE; gboolean has_object; char loose_path[_OSTREE_LOOSE_PATH_MAX]; - - if (!_ostree_repo_has_loose_object (self, checksum, objtype, &has_object, loose_path, - cancellable, error)) - goto out; - - ret = TRUE; - if (has_object) - *out_stored_path = g_file_resolve_relative_path (self->objects_dir, loose_path); - else - *out_stored_path = NULL; -out: - return ret; + return _ostree_repo_has_loose_object (self, checksum, objtype, &has_object, loose_path, + out_stored_path, cancellable, error); } /**