diff --git a/Makefile-ostree.am b/Makefile-ostree.am index aa04c946..cb0b7c6e 100644 --- a/Makefile-ostree.am +++ b/Makefile-ostree.am @@ -28,6 +28,7 @@ ostree_SOURCES = src/ostree/main.c \ src/ostree/ot-builtin-diff.c \ src/ostree/ot-builtin-fsck.c \ src/ostree/ot-builtin-init.c \ + src/ostree/ot-builtin-local-clone.c \ src/ostree/ot-builtin-log.c \ src/ostree/ot-builtin-run-triggers.c \ src/ostree/ot-builtin-remote.c \ diff --git a/src/ostree/main.c b/src/ostree/main.c index 88dd3138..83b85a2f 100644 --- a/src/ostree/main.c +++ b/src/ostree/main.c @@ -35,6 +35,7 @@ static OstreeBuiltin builtins[] = { { "init", ostree_builtin_init, 0 }, { "commit", ostree_builtin_commit, 0 }, { "compose", ostree_builtin_compose, 0 }, + { "local-clone", ostree_builtin_local_clone, 0 }, { "log", ostree_builtin_log, 0 }, #ifdef HAVE_LIBSOUP_GNOME { "pull", ostree_builtin_pull, 0 }, diff --git a/src/ostree/ot-builtin-local-clone.c b/src/ostree/ot-builtin-local-clone.c new file mode 100644 index 00000000..11a45031 --- /dev/null +++ b/src/ostree/ot-builtin-local-clone.c @@ -0,0 +1,228 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- + * + * Copyright (C) 2011 Colin Walters + * + * 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. + * + * Author: Colin Walters + */ + +#include "config.h" + +#include "ot-builtins.h" +#include "ostree.h" + +#include + +static GOptionEntry options[] = { + { NULL } +}; + +typedef struct { + OstreeRepo *src_repo; + OstreeRepo *dest_repo; + gboolean uids_differ; +} OtLocalCloneData; + +static gboolean +copy_dir_contents (GFile *src, + GFile *dest, + GCancellable *cancellable, + GError **error) +{ + gboolean ret = FALSE; + GFile *child_src = NULL; + GFile *child_dest = NULL; + GFileEnumerator *dir_enum = NULL; + GFileInfo *file_info = NULL; + GError *temp_error = NULL; + + dir_enum = g_file_enumerate_children (src, OSTREE_GIO_FAST_QUERYINFO, + G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, + cancellable, error); + if (!dir_enum) + goto out; + while ((file_info = g_file_enumerator_next_file (dir_enum, cancellable, &temp_error)) != NULL) + { + const char *name = g_file_info_get_name (file_info); + + g_clear_object (&child_src); + child_src = g_file_get_child (src, name); + g_clear_object (&child_dest); + child_dest = g_file_get_child (dest, name); + + if (!g_file_copy (child_src, child_dest, G_FILE_COPY_OVERWRITE | G_FILE_COPY_NOFOLLOW_SYMLINKS, + cancellable, NULL, NULL, error)) + goto out; + + g_clear_object (&file_info); + } + if (temp_error) + { + g_propagate_error (error, temp_error); + goto out; + } + + ret = TRUE; + out: + g_clear_object (&dir_enum); + g_clear_object (&child_src); + g_clear_object (&child_dest); + g_clear_object (&file_info); + return ret; +} + +static void +object_iter_callback (OstreeRepo *repo, + const char *checksum, + OstreeObjectType objtype, + GFile *objfile, + GFileInfo *file_info, + gpointer user_data) +{ + OtLocalCloneData *data = user_data; + GFile *dest = NULL; + GError *error = NULL; + GFile *tmpdir = NULL; + gboolean did_exist; + + if ((objtype == OSTREE_OBJECT_TYPE_FILE + && ostree_repo_is_archive (data->src_repo)) + || data->uids_differ) + { + tmpdir = ostree_repo_get_tmpdir (data->dest_repo); + dest = g_file_get_child (tmpdir, checksum); + + if (!ostree_unpack_object (objfile, objtype, + dest, NULL, &error)) + goto out; + if (!ostree_repo_store_object_trusted (data->dest_repo, + dest, + checksum, + objtype, + FALSE, + &did_exist, + &error)) + goto out; + } + else + { + if (!ostree_repo_store_object_trusted (data->dest_repo, + objfile, + checksum, + objtype, + FALSE, + &did_exist, + &error)) + goto out; + } + + out: + if (dest) + (void) g_file_delete (dest, NULL, NULL); + g_clear_object (&dest); + if (error != NULL) + { + g_printerr ("%s\n", error->message); + g_clear_error (&error); + } +} + +gboolean +ostree_builtin_local_clone (int argc, char **argv, const char *repo_path, GError **error) +{ + gboolean ret = FALSE; + GOptionContext *context; + const char *destination; + OtLocalCloneData data; + GFile *src_repo_dir = NULL; + GFile *dest_repo_dir = NULL; + GFileInfo *src_info = NULL; + GFileInfo *dest_info = NULL; + GFile *src_dir = NULL; + GFile *dest_dir = NULL; + + context = g_option_context_new ("DEST ... - Create new repository DEST"); + g_option_context_add_main_entries (context, options, NULL); + + if (!g_option_context_parse (context, &argc, &argv, error)) + goto out; + + data.src_repo = ostree_repo_new (repo_path); + if (!ostree_repo_check (data.src_repo, error)) + goto out; + + if (argc < 1) + { + gchar *help = g_option_context_get_help (context, TRUE, NULL); + g_printerr ("%s\n", help); + g_free (help); + g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, + "DESTINATION must be specified"); + goto out; + } + + destination = argv[1]; + + data.dest_repo = ostree_repo_new (destination); + if (!ostree_repo_check (data.dest_repo, error)) + goto out; + + src_repo_dir = ot_gfile_new_for_path (ostree_repo_get_path (data.src_repo)); + dest_repo_dir = ot_gfile_new_for_path (ostree_repo_get_path (data.dest_repo)); + + src_info = g_file_query_info (src_repo_dir, OSTREE_GIO_FAST_QUERYINFO, + G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, + NULL, error); + if (!src_info) + goto out; + dest_info = g_file_query_info (dest_repo_dir, OSTREE_GIO_FAST_QUERYINFO, + G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, + NULL, error); + if (!dest_info) + goto out; + + data.uids_differ = g_file_info_get_attribute_uint32 (src_info, "unix::uid") != g_file_info_get_attribute_uint32 (dest_info, "unix::uid"); + + if (!ostree_repo_iter_objects (data.src_repo, object_iter_callback, &data, error)) + goto out; + + src_dir = g_file_resolve_relative_path (src_repo_dir, "refs/heads"); + dest_dir = g_file_resolve_relative_path (dest_repo_dir, "refs/heads"); + if (!copy_dir_contents (src_dir, dest_dir, NULL, error)) + goto out; + g_clear_object (&src_dir); + g_clear_object (&dest_dir); + + src_dir = g_file_resolve_relative_path (src_repo_dir, "tags"); + dest_dir = g_file_resolve_relative_path (dest_repo_dir, "tags"); + if (!copy_dir_contents (src_dir, dest_dir, NULL, error)) + goto out; + + ret = TRUE; + out: + if (context) + g_option_context_free (context); + g_clear_object (&src_repo_dir); + g_clear_object (&dest_repo_dir); + g_clear_object (&src_info); + g_clear_object (&dest_info); + g_clear_object (&src_dir); + g_clear_object (&dest_dir); + g_clear_object (&data.src_repo); + g_clear_object (&data.dest_repo); + return ret; +} diff --git a/src/ostree/ot-builtins.h b/src/ostree/ot-builtins.h index bd985341..2b985229 100644 --- a/src/ostree/ot-builtins.h +++ b/src/ostree/ot-builtins.h @@ -44,6 +44,7 @@ gboolean ostree_builtin_commit (int argc, char **argv, const char *repo, GError gboolean ostree_builtin_compose (int argc, char **argv, const char *repo, GError **error); gboolean ostree_builtin_diff (int argc, char **argv, const char *repo, GError **error); gboolean ostree_builtin_init (int argc, char **argv, const char *repo, GError **error); +gboolean ostree_builtin_local_clone (int argc, char **argv, const char *repo, GError **error); gboolean ostree_builtin_log (int argc, char **argv, const char *repo, GError **error); gboolean ostree_builtin_pull (int argc, char **argv, const char *repo, GError **error); gboolean ostree_builtin_run_triggers (int argc, char **argv, const char *repo, GError **error); diff --git a/tests/t0000-basic.sh b/tests/t0000-basic.sh index df1c3d5d..0db15278 100755 --- a/tests/t0000-basic.sh +++ b/tests/t0000-basic.sh @@ -19,7 +19,7 @@ set -e -echo "1..15" +echo "1..17" . libtest.sh @@ -137,3 +137,14 @@ assert_file_has_content ${test_tmpdir}/show 'example.com' assert_file_has_content ${test_tmpdir}/show 'buildid' echo "ok metadata content" +cd ${test_tmpdir} +mkdir repo2 +ostree --repo=repo2 init +$OSTREE local-clone repo2 +echo "ok local clone" + +cd ${test_tmpdir} +ostree --repo=repo2 checkout test2 test2-checkout-from-local-clone +cd test2-checkout-from-local-clone +assert_file_has_content yet/another/tree/green 'leaf' +echo "ok local clone checkout" diff --git a/tests/t0001-archive.sh b/tests/t0001-archive.sh index 69e9b361..dc7bfa11 100755 --- a/tests/t0001-archive.sh +++ b/tests/t0001-archive.sh @@ -21,7 +21,7 @@ set -e . libtest.sh -echo '1..3' +echo '1..5' setup_test_repository "archive" echo "ok setup" @@ -35,3 +35,15 @@ assert_has_file baz/cow assert_file_has_content baz/cow moo assert_has_file baz/deeper/ohyeah echo "ok content" + +cd ${test_tmpdir} +mkdir repo2 +ostree --repo=repo2 init +$OSTREE local-clone repo2 +echo "ok local clone" + +cd ${test_tmpdir} +ostree --repo=repo2 checkout test2 test2-checkout-from-local-clone +cd test2-checkout-from-local-clone +assert_file_has_content baz/cow moo +echo "ok local clone checkout"