summaryrefslogtreecommitdiff
path: root/src/ostree/ot-builtin-init.c
blob: 8dd282174942c5e5d7554f8c4b90d8eb15d185cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
 *
 * Copyright (C) 2011 Colin Walters <walters@verbum.org>
 *
 * 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 <walters@verbum.org>
 */

#include "config.h"

#include "ot-builtins.h"
#include "ostree.h"

#include <glib/gi18n.h>

static gboolean opt_archive;
static char *opt_mode = NULL;

static GOptionEntry options[] = {
  { "archive", 0, 0, G_OPTION_ARG_NONE, &opt_archive, "Initialize repository as archive", NULL },
  { "mode", 0, 0, G_OPTION_ARG_STRING, &opt_mode, "Initialize repository in given mode (bare, archive, archive-z)", NULL },
  { NULL }
};

#define DEFAULT_CONFIG_CONTENTS ("[core]\n" \
                                 "repo_version=1\n")


gboolean
ostree_builtin_init (int argc, char **argv, GFile *repo_path, GError **error)
{
  GOptionContext *context = NULL;
  gboolean ret = FALSE;
  __attribute__ ((unused)) GCancellable *cancellable = NULL;
  const char *mode_str = "bare";
  ot_lobj GFile *child = NULL;
  ot_lobj GFile *grandchild = NULL;
  ot_lobj OstreeRepo *repo = NULL;
  GString *config_data = NULL;

  context = g_option_context_new ("- Initialize a new empty repository");
  g_option_context_add_main_entries (context, options, NULL);

  if (!g_option_context_parse (context, &argc, &argv, error))
    goto out;

  child = g_file_get_child (repo_path, "config");

  config_data = g_string_new (DEFAULT_CONFIG_CONTENTS);
  if (opt_archive)
    mode_str = "archive";
  else if (opt_mode)
    {
      OstreeRepoMode mode;
      if (!ostree_repo_mode_from_string (opt_mode, &mode, error))
        goto out;
      mode_str = opt_mode;
    }
  g_string_append_printf (config_data, "mode=%s\n", mode_str);
  if (!g_file_replace_contents (child,
                                config_data->str,
                                config_data->len,
                                NULL, FALSE, 0, NULL,
                                NULL, error))
    goto out;

  g_clear_object (&child);
  child = g_file_get_child (repo_path, "objects");
  if (!g_file_make_directory (child, NULL, error))
    goto out;

  g_clear_object (&grandchild);
  grandchild = g_file_get_child (child, "pack");
  if (!g_file_make_directory (grandchild, NULL, error))
    goto out;

  g_clear_object (&child);
  child = g_file_get_child (repo_path, "tmp");
  if (!g_file_make_directory (child, NULL, error))
    goto out;

  g_clear_object (&child);
  child = g_file_get_child (repo_path, "refs");
  if (!g_file_make_directory (child, NULL, error))
    goto out;

  g_clear_object (&grandchild);
  grandchild = g_file_get_child (child, "heads");
  if (!g_file_make_directory (grandchild, NULL, error))
    goto out;

  g_clear_object (&grandchild);
  grandchild = g_file_get_child (child, "remotes");
  if (!g_file_make_directory (grandchild, NULL, error))
    goto out;

  g_clear_object (&child);
  child = g_file_get_child (repo_path, "tags");
  if (!g_file_make_directory (child, NULL, error))
    goto out;

  g_clear_object (&child);
  child = g_file_get_child (repo_path, "remote-cache");
  if (!g_file_make_directory (child, NULL, error))
    goto out;

  repo = ostree_repo_new (repo_path);
  if (!ostree_repo_check (repo, error))
    goto out;

  ret = TRUE;
 out:
  if (context)
    g_option_context_free (context);
  if (config_data)
    g_string_free (config_data, TRUE);
  return ret;
}