[ostree] core: Add ls builtin



commit 87547827c81fb259a34309f9df8b0d7a23535834
Author: Colin Walters <walters verbum org>
Date:   Sun Nov 27 20:10:48 2011 -0500

    core: Add ls builtin

 Makefile-ostree.am               |    1 +
 src/libostree/ostree-repo-file.c |   30 ++++--
 src/ostree/main.c                |    1 +
 src/ostree/ot-builtin-ls.c       |  220 ++++++++++++++++++++++++++++++++++++++
 src/ostree/ot-builtins.h         |    1 +
 5 files changed, 245 insertions(+), 8 deletions(-)
---
diff --git a/Makefile-ostree.am b/Makefile-ostree.am
index cb0b7c6..20c0082 100644
--- a/Makefile-ostree.am
+++ b/Makefile-ostree.am
@@ -30,6 +30,7 @@ ostree_SOURCES = src/ostree/main.c \
 	src/ostree/ot-builtin-init.c \
 	src/ostree/ot-builtin-local-clone.c \
 	src/ostree/ot-builtin-log.c \
+	src/ostree/ot-builtin-ls.c \
 	src/ostree/ot-builtin-run-triggers.c \
 	src/ostree/ot-builtin-remote.c \
 	src/ostree/ot-builtin-rev-parse.c \
diff --git a/src/libostree/ostree-repo-file.c b/src/libostree/ostree-repo-file.c
index 2f3106c..fd9b30f 100644
--- a/src/libostree/ostree-repo-file.c
+++ b/src/libostree/ostree-repo-file.c
@@ -133,11 +133,15 @@ _ostree_repo_file_new_child (OstreeRepoFile *parent,
                              const char  *name)
 {
   OstreeRepoFile *self;
+  size_t len;
   
   self = g_object_new (OSTREE_TYPE_REPO_FILE, NULL);
   self->repo = g_object_ref (parent->repo);
   self->parent = g_object_ref (parent);
   self->name = g_strdup (name);
+  len = strlen(self->name);
+  if (self->name[len-1] == '/')
+    self->name[len-1] = '\0';
 
   return G_FILE (self);
 }
@@ -190,6 +194,7 @@ do_resolve_commit (OstreeRepoFile  *self,
   root_metadata = NULL;
   self->tree_contents = root_contents;
   root_contents = NULL;
+  self->tree_metadata_checksum = g_strdup (tree_meta_checksum);
 
  out:
   ot_clear_gvariant (&commit);
@@ -274,6 +279,8 @@ _ostree_repo_file_ensure_resolved (OstreeRepoFile  *self,
     }
   else if (self->index == -1)
     {
+      if (!_ostree_repo_file_ensure_resolved (self->parent, error))
+        goto out;
       (void)do_resolve_nonroot (self, &(self->commit_resolve_error));
     }
   
@@ -377,8 +384,6 @@ _ostree_repo_file_tree_get_content_checksum (OstreeRepoFile  *self)
 GFile *
 _ostree_repo_file_nontree_get_local (OstreeRepoFile  *self)
 {
-  g_assert (!ostree_repo_is_archive (self->repo));
-
   return ostree_repo_get_object_path (self->repo, _ostree_repo_file_get_checksum (self), OSTREE_OBJECT_TYPE_FILE);
 }
 
@@ -407,7 +412,8 @@ _ostree_repo_file_get_checksum (OstreeRepoFile  *self)
   GVariant *dirs_variant;
   const char *checksum;
 
-  g_assert (self->parent);
+  if (!self->parent)
+    return self->tree_metadata_checksum;
 
   n = _ostree_repo_file_tree_find_child (self->parent, self->name, &is_dir, NULL);
   g_assert (n >= 0);
@@ -472,7 +478,7 @@ ostree_repo_file_get_path (GFile *file)
   for (parent = self->parent; parent; parent = parent->parent)
     parents = g_slist_prepend (parents, parent);
 
-  if (parents->next)
+  if (parents && parents->next)
     {
       for (iter = parents->next; iter; iter = iter->next)
         {
@@ -482,7 +488,8 @@ ostree_repo_file_get_path (GFile *file)
         }
     }
   g_string_append_c (buf, '/');
-  g_string_append (buf, self->name);
+  if (self->name)
+    g_string_append (buf, self->name);
 
   g_slist_free (parents);
 
@@ -624,11 +631,18 @@ ostree_repo_file_resolve_relative_path (GFile      *file,
   const char *rest;
   GFile *ret;
 
-  if (g_path_is_absolute (relative_path) && self->parent)
+  if (g_path_is_absolute (relative_path))
     {
       g_assert (*relative_path == '/');
-      return ostree_repo_file_resolve_relative_path ((GFile*)_ostree_repo_file_get_root (self),
-                                                     relative_path+1);
+
+      if (strcmp (relative_path, "/") == 0)
+        return g_object_ref (_ostree_repo_file_get_root (self)); 
+
+      if (self->parent)
+        return ostree_repo_file_resolve_relative_path ((GFile*)_ostree_repo_file_get_root (self),
+                                                       relative_path+1);
+      else
+        relative_path = relative_path+1;
     }
 
   rest = strchr (relative_path, '/');
diff --git a/src/ostree/main.c b/src/ostree/main.c
index 16a661e..5aea6c0 100644
--- a/src/ostree/main.c
+++ b/src/ostree/main.c
@@ -37,6 +37,7 @@ static OstreeBuiltin builtins[] = {
   { "compose", ostree_builtin_compose, 0 },
   { "local-clone", ostree_builtin_local_clone, 0 },
   { "log", ostree_builtin_log, 0 },
+  { "ls", ostree_builtin_ls, 0 },
 #ifdef HAVE_LIBSOUP_GNOME
   { "pull", ostree_builtin_pull, 0 },
 #endif
diff --git a/src/ostree/ot-builtin-ls.c b/src/ostree/ot-builtin-ls.c
new file mode 100644
index 0000000..c3d92cd
--- /dev/null
+++ b/src/ostree/ot-builtin-ls.c
@@ -0,0 +1,220 @@
+/* -*- 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 "ostree-repo-file.h"
+
+#include <glib/gi18n.h>
+
+static gboolean recursive;
+static gboolean checksum;
+static gboolean xattrs;
+
+static GOptionEntry options[] = {
+  { "recursive", 'R', 0, G_OPTION_ARG_NONE, &recursive, "Print directories recursively", NULL },
+  { "checksum", 'C', 0, G_OPTION_ARG_NONE, &checksum, "Print checksum", NULL },
+  { "xattrs", 'X', 0, G_OPTION_ARG_NONE, &xattrs, "Print extended attributes", NULL },
+};
+
+static void
+print_one_file (GFile     *f,
+                GFileInfo *file_info)
+{
+  GString *buf = NULL;
+  char type_c;
+  guint32 mode;
+  
+  buf = g_string_new ("");
+
+  type_c = '?';
+  mode = g_file_info_get_attribute_uint32 (file_info, "unix::mode");
+  switch (g_file_info_get_file_type (file_info))
+    {
+    case G_FILE_TYPE_REGULAR:
+      type_c = '-';
+      break;
+    case G_FILE_TYPE_DIRECTORY:
+      type_c = 'd';
+      break;
+    case G_FILE_TYPE_SYMBOLIC_LINK:
+      type_c = 'l';
+      break;
+    case G_FILE_TYPE_SPECIAL:
+      if (S_ISCHR(mode))
+        type_c = 'c';
+      else if (S_ISBLK(mode))
+        type_c = 'b';
+      break;
+    case G_FILE_TYPE_UNKNOWN:
+    case G_FILE_TYPE_SHORTCUT:
+    case G_FILE_TYPE_MOUNTABLE:
+      g_assert_not_reached ();
+      break;
+    }
+  g_string_append_c (buf, type_c);
+  g_string_append_printf (buf, "0%03o %u %u %" G_GUINT64_FORMAT " ",
+                          mode & ~S_IFMT,
+                          g_file_info_get_attribute_uint32 (file_info, "unix::uid"),
+                          g_file_info_get_attribute_uint32 (file_info, "unix::gid"),
+                          g_file_info_get_attribute_uint64 (file_info, "standard::size"));
+  
+  if (checksum)
+    g_string_append_printf (buf, "%s ", _ostree_repo_file_get_checksum ((OstreeRepoFile*)f));
+
+  if (xattrs)
+    {
+      GVariant *xattrs;
+      char *formatted;
+
+      if (!_ostree_repo_file_get_xattrs ((OstreeRepoFile*)f, &xattrs, NULL, NULL))
+        g_assert_not_reached ();
+      
+      formatted = g_variant_print (xattrs, TRUE);
+      g_string_append (buf, "{ ");
+      g_string_append (buf, formatted);
+      g_string_append (buf, "} ");
+      g_free (formatted);
+      g_variant_unref (xattrs);
+    }
+
+  g_string_append (buf, ot_gfile_get_path_cached (f));
+
+  if (g_file_info_get_file_type (file_info) == G_FILE_TYPE_SYMBOLIC_LINK)
+    g_string_append_printf (buf, " -> %s", g_file_info_get_attribute_byte_string (file_info, "standard::symlink-target"));
+      
+  g_print ("%s\n", buf->str);
+
+  g_string_free (buf, TRUE);
+}
+
+static gboolean
+print_directory_recurse (GFile    *f,
+                         GError  **error)
+{
+  gboolean ret = FALSE;
+  GFileEnumerator *dir_enum = NULL;
+  GFile *child = NULL;
+  GFileInfo *child_info = NULL;
+  GError *temp_error = NULL;
+
+  dir_enum = g_file_enumerate_children (f, OSTREE_GIO_FAST_QUERYINFO, 
+                                        G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
+                                        NULL, 
+                                        error);
+  if (!dir_enum)
+    goto out;
+  
+  while ((child_info = g_file_enumerator_next_file (dir_enum, NULL, &temp_error)) != NULL)
+    {
+      g_clear_object (&child);
+      child = g_file_get_child (f, g_file_info_get_name (child_info));
+
+      print_one_file (child, child_info);
+
+      if (g_file_info_get_file_type (child_info) == G_FILE_TYPE_DIRECTORY)
+        {
+          if (!print_directory_recurse (child, error))
+            goto out;
+        }
+
+      g_clear_object (&child_info);
+    }
+  if (temp_error)
+    {
+      g_propagate_error (error, temp_error);
+      goto out;
+    }
+
+  ret = TRUE;
+ out:
+  g_clear_object (&child_info);
+  g_clear_object (&child);
+  g_clear_object (&dir_enum);
+  return ret;
+}
+
+gboolean
+ostree_builtin_ls (int argc, char **argv, const char *repo_path, GError **error)
+{
+  GOptionContext *context;
+  gboolean ret = FALSE;
+  OstreeRepo *repo = NULL;
+  const char *rev;
+  int i;
+  GFile *root = NULL;
+  GFile *f = NULL;
+  GFileInfo *file_info = NULL;
+
+  context = g_option_context_new ("COMMIT PATH [PATH...] - List file paths");
+  g_option_context_add_main_entries (context, options, NULL);
+
+  if (!g_option_context_parse (context, &argc, &argv, error))
+    goto out;
+
+  repo = ostree_repo_new (repo_path);
+  if (!ostree_repo_check (repo, error))
+    goto out;
+
+  if (argc <= 2)
+    {
+      ot_util_usage_error (context, "An COMMIT and at least one PATH argument are required", error);
+      goto out;
+    }
+  rev = argv[1];
+
+  if (!ostree_repo_read_commit (repo, rev, &root, NULL, error))
+    goto out;
+
+  for (i = 2; i < argc; i++)
+    {
+      g_clear_object (&f);
+      f = g_file_resolve_relative_path (root, argv[i]);
+
+      g_clear_object (&file_info);
+      file_info = g_file_query_info (f, OSTREE_GIO_FAST_QUERYINFO,
+                                     G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
+                                     NULL, error);
+      if (!file_info)
+        goto out;
+      
+      print_one_file (f, file_info);
+
+      if (recursive && g_file_info_get_file_type (file_info) == G_FILE_TYPE_DIRECTORY)
+        {
+          if (!print_directory_recurse (f, error))
+            goto out;
+        }
+    }
+ 
+  ret = TRUE;
+ out:
+  g_clear_object (&root);
+  g_clear_object (&f);
+  g_clear_object (&file_info);
+  if (context)
+    g_option_context_free (context);
+  g_clear_object (&repo);
+  return ret;
+}
diff --git a/src/ostree/ot-builtins.h b/src/ostree/ot-builtins.h
index 2b98522..afef91f 100644
--- a/src/ostree/ot-builtins.h
+++ b/src/ostree/ot-builtins.h
@@ -46,6 +46,7 @@ gboolean ostree_builtin_diff (int argc, char **argv, const char *repo, GError **
 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_ls (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);
 gboolean ostree_builtin_fsck (int argc, char **argv, const char *repo, GError **error);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]