[ostree] core: Deduplicate code converting struct stat -> GFileInfo



commit c4efbf67182072ed95bbdacf20f18da03484e47c
Author: Colin Walters <walters verbum org>
Date:   Tue Dec 16 21:14:10 2014 -0500

    core: Deduplicate code converting struct stat -> GFileInfo
    
    We were doing the same thing in a number of places, make a helper
    function.

 src/libostree/ostree-core-private.h    |    2 +
 src/libostree/ostree-core.c            |   42 +++++++++++++++++++++----------
 src/libostree/ostree-repo-libarchive.c |    9 ++----
 src/libostree/ostree-repo.c            |   10 +-------
 4 files changed, 34 insertions(+), 29 deletions(-)
---
diff --git a/src/libostree/ostree-core-private.h b/src/libostree/ostree-core-private.h
index 851e74b..294edfa 100644
--- a/src/libostree/ostree-core-private.h
+++ b/src/libostree/ostree-core-private.h
@@ -81,6 +81,8 @@ _ostree_make_temporary_symlink_at (int             tmp_dirfd,
                                    GCancellable   *cancellable,
                                    GError        **error);
 
+GFileInfo * _ostree_header_gfile_info_new (mode_t mode, uid_t uid, gid_t gid);
+
 /* XX + / + checksum-2 + . + extension, but let's just use 256 for a
  * bit of overkill.
  */
diff --git a/src/libostree/ostree-core.c b/src/libostree/ostree-core.c
index b8fa114..78dd711 100644
--- a/src/libostree/ostree-core.c
+++ b/src/libostree/ostree-core.c
@@ -1264,6 +1264,31 @@ _ostree_loose_path (char              *buf,
   _ostree_loose_path_with_suffix (buf, checksum, objtype, mode, "");
 }
 
+/**
+ * _ostree_header_gfile_info_new:
+ * @mode: File mode
+ * @uid: File uid
+ * @gid: File gid
+ *
+ * OSTree only stores a subset of file attributes; for example,
+ * timestamps are intentionally not stored.  This function creates a
+ * #GFileInfo based on the attributes of a `struct stat` that match
+ * those file attributes.
+ *
+ * Returns: (transfer full): A new #GFileInfo mapping a subset of @stbuf.
+ */
+GFileInfo *
+_ostree_header_gfile_info_new (mode_t mode, uid_t uid, gid_t gid)
+{
+  GFileInfo *ret = g_file_info_new ();
+  g_file_info_set_attribute_uint32 (ret, "standard::type", ot_gfile_type_for_mode (mode));
+  g_file_info_set_attribute_boolean (ret, "standard::is-symlink", S_ISLNK (mode));
+  g_file_info_set_attribute_uint32 (ret, "unix::uid", uid);
+  g_file_info_set_attribute_uint32 (ret, "unix::gid", gid);
+  g_file_info_set_attribute_uint32 (ret, "unix::mode", mode);
+  return ret;
+}
+
 /*
  * _ostree_loose_path_with_suffix:
  * @buf: Output buffer, must be _OSTREE_LOOSE_PATH_MAX in size
@@ -1367,12 +1392,7 @@ file_header_parse (GVariant         *metadata,
   mode = GUINT32_FROM_BE (mode);
   rdev = GUINT32_FROM_BE (rdev);
 
-  ret_file_info = g_file_info_new ();
-  g_file_info_set_attribute_uint32 (ret_file_info, "standard::type", ot_gfile_type_for_mode (mode));
-  g_file_info_set_attribute_boolean (ret_file_info, "standard::is-symlink", S_ISLNK (mode));
-  g_file_info_set_attribute_uint32 (ret_file_info, "unix::uid", uid);
-  g_file_info_set_attribute_uint32 (ret_file_info, "unix::gid", gid);
-  g_file_info_set_attribute_uint32 (ret_file_info, "unix::mode", mode);
+  ret_file_info = _ostree_header_gfile_info_new (mode, uid, gid);
 
   if (S_ISREG (mode))
     {
@@ -1423,19 +1443,13 @@ zlib_file_header_parse (GVariant         *metadata,
                  &uid, &gid, &mode, &rdev,
                  &symlink_target, &ret_xattrs);
 
-  size = GUINT64_FROM_BE (size);
   uid = GUINT32_FROM_BE (uid);
   gid = GUINT32_FROM_BE (gid);
   mode = GUINT32_FROM_BE (mode);
   rdev = GUINT32_FROM_BE (rdev);
+  ret_file_info = _ostree_header_gfile_info_new (mode, uid, gid);
 
-  ret_file_info = g_file_info_new ();
-  g_file_info_set_size (ret_file_info, size);
-  g_file_info_set_attribute_uint32 (ret_file_info, "standard::type", ot_gfile_type_for_mode (mode));
-  g_file_info_set_attribute_boolean (ret_file_info, "standard::is-symlink", S_ISLNK (mode));
-  g_file_info_set_attribute_uint32 (ret_file_info, "unix::uid", uid);
-  g_file_info_set_attribute_uint32 (ret_file_info, "unix::gid", gid);
-  g_file_info_set_attribute_uint32 (ret_file_info, "unix::mode", mode);
+  g_file_info_set_size (ret_file_info, GUINT64_FROM_BE (size));
 
   if (S_ISREG (mode))
     {
diff --git a/src/libostree/ostree-repo-libarchive.c b/src/libostree/ostree-repo-libarchive.c
index 90517d2..e25d623 100644
--- a/src/libostree/ostree-repo-libarchive.c
+++ b/src/libostree/ostree-repo-libarchive.c
@@ -22,6 +22,7 @@
 
 #include "config.h"
 
+#include "ostree-core-private.h"
 #include "ostree-repo-private.h"
 #include "ostree-mutable-tree.h"
 
@@ -48,19 +49,15 @@ file_info_from_archive_entry_and_modifier (OstreeRepo *repo,
                                            struct archive_entry *entry,
                                            OstreeRepoCommitModifier *modifier)
 {
-  gs_unref_object GFileInfo *info = g_file_info_new ();
+  gs_unref_object GFileInfo *info = NULL;
   GFileInfo *modified_info = NULL;
   const struct stat *st;
   guint32 file_type;
 
   st = archive_entry_stat (entry);
 
+  info = _ostree_header_gfile_info_new (st->st_mode, st->st_uid, st->st_gid);
   file_type = ot_gfile_type_for_mode (st->st_mode);
-  g_file_info_set_attribute_boolean (info, "standard::is-symlink", S_ISLNK (st->st_mode));
-  g_file_info_set_attribute_uint32 (info, "standard::type", file_type);
-  g_file_info_set_attribute_uint32 (info, "unix::uid", st->st_uid);
-  g_file_info_set_attribute_uint32 (info, "unix::gid", st->st_gid);
-  g_file_info_set_attribute_uint32 (info, "unix::mode", st->st_mode);
 
   if (file_type == G_FILE_TYPE_REGULAR)
     {
diff --git a/src/libostree/ostree-repo.c b/src/libostree/ostree-repo.c
index 38471a6..2d649aa 100644
--- a/src/libostree/ostree-repo.c
+++ b/src/libostree/ostree-repo.c
@@ -1802,11 +1802,10 @@ query_info_for_bare_content_object (OstreeRepo      *self,
       goto out;
     }
 
-  ret_info = g_file_info_new ();
+  ret_info = _ostree_header_gfile_info_new (stbuf.st_mode, stbuf.st_uid, stbuf.st_gid);
 
   if (S_ISREG (stbuf.st_mode))
     {
-      g_file_info_set_file_type (ret_info, G_FILE_TYPE_REGULAR);
       g_file_info_set_size (ret_info, stbuf.st_size);
     }
   else if (S_ISLNK (stbuf.st_mode))
@@ -1814,8 +1813,6 @@ query_info_for_bare_content_object (OstreeRepo      *self,
       char targetbuf[PATH_MAX+1];
       ssize_t len;
 
-      g_file_info_set_file_type (ret_info, G_FILE_TYPE_SYMBOLIC_LINK);
-      
       do
         len = readlinkat (self->objects_dir_fd, loose_path_buf, targetbuf, sizeof (targetbuf) - 1);
       while (G_UNLIKELY (len == -1 && errno == EINTR));
@@ -1834,11 +1831,6 @@ query_info_for_bare_content_object (OstreeRepo      *self,
       goto out;
     }
 
-  g_file_info_set_attribute_boolean (ret_info, "standard::is-symlink", S_ISLNK (stbuf.st_mode));
-  g_file_info_set_attribute_uint32 (ret_info, "unix::uid", stbuf.st_uid);
-  g_file_info_set_attribute_uint32 (ret_info, "unix::gid", stbuf.st_gid);
-  g_file_info_set_attribute_uint32 (ret_info, "unix::mode", stbuf.st_mode);
-
   ret = TRUE;
   gs_transfer_out_value (out_info, &ret_info);
  out:


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