[ostree/wip/gs-dir-iter: 2/4] core: Deduplicate code converting struct stat -> GFileInfo
- From: Colin Walters <walters src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [ostree/wip/gs-dir-iter: 2/4] core: Deduplicate code converting struct stat -> GFileInfo
- Date: Wed, 17 Dec 2014 03:03:18 +0000 (UTC)
commit d1a568f150f55ad9e247c78117e78a4ed97927ec
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.c | 51 +++++++++++++------------------
src/libostree/ostree-repo-libarchive.c | 8 +----
src/libostree/ostree-repo.c | 10 +-----
src/libotutil/ot-gio-utils.c | 11 +++++++
src/libotutil/ot-gio-utils.h | 2 +
5 files changed, 37 insertions(+), 45 deletions(-)
---
diff --git a/src/libostree/ostree-core.c b/src/libostree/ostree-core.c
index b8fa114..3211eb1 100644
--- a/src/libostree/ostree-core.c
+++ b/src/libostree/ostree-core.c
@@ -1355,6 +1355,7 @@ file_header_parse (GVariant *metadata,
gboolean ret = FALSE;
guint32 uid, gid, mode, rdev;
const char *symlink_target;
+ struct stat stbuf;
gs_unref_object GFileInfo *ret_file_info = NULL;
gs_unref_variant GVariant *ret_xattrs = NULL;
@@ -1362,30 +1363,25 @@ file_header_parse (GVariant *metadata,
&uid, &gid, &mode, &rdev,
&symlink_target, &ret_xattrs);
- uid = GUINT32_FROM_BE (uid);
- gid = GUINT32_FROM_BE (gid);
- mode = GUINT32_FROM_BE (mode);
- rdev = GUINT32_FROM_BE (rdev);
+ stbuf.st_uid = GUINT32_FROM_BE (uid);
+ stbuf.st_gid = GUINT32_FROM_BE (gid);
+ stbuf.st_mode = GUINT32_FROM_BE (mode);
+ stbuf.st_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 = ot_default_struct_stat_to_gfile_info (&stbuf);
- if (S_ISREG (mode))
+ if (S_ISREG (stbuf.st_mode))
{
;
}
- else if (S_ISLNK (mode))
+ else if (S_ISLNK (stbuf.st_mode))
{
g_file_info_set_attribute_byte_string (ret_file_info, "standard::symlink-target", symlink_target);
}
else
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
- "Corrupted archive file; invalid mode %u", mode);
+ "Corrupted archive file; invalid mode %u", stbuf.st_mode);
goto out;
}
@@ -1415,6 +1411,7 @@ zlib_file_header_parse (GVariant *metadata,
gboolean ret = FALSE;
guint64 size;
guint32 uid, gid, mode, rdev;
+ struct stat stbuf;
const char *symlink_target;
gs_unref_object GFileInfo *ret_file_info = NULL;
gs_unref_variant GVariant *ret_xattrs = NULL;
@@ -1423,32 +1420,26 @@ 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);
+ stbuf.st_uid = GUINT32_FROM_BE (uid);
+ stbuf.st_gid = GUINT32_FROM_BE (gid);
+ stbuf.st_mode = GUINT32_FROM_BE (mode);
+ stbuf.st_rdev = GUINT32_FROM_BE (rdev);
+ ret_file_info = ot_default_struct_stat_to_gfile_info (&stbuf);
- 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))
+ if (S_ISREG (stbuf.st_mode))
{
;
}
- else if (S_ISLNK (mode))
+ else if (S_ISLNK (stbuf.st_mode))
{
g_file_info_set_attribute_byte_string (ret_file_info, "standard::symlink-target", symlink_target);
}
else
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
- "Corrupted archive file; invalid mode %u", mode);
+ "Corrupted archive file; invalid mode %u", stbuf.st_mode);
goto out;
}
diff --git a/src/libostree/ostree-repo-libarchive.c b/src/libostree/ostree-repo-libarchive.c
index 90517d2..15ba3e5 100644
--- a/src/libostree/ostree-repo-libarchive.c
+++ b/src/libostree/ostree-repo-libarchive.c
@@ -48,19 +48,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 = ot_default_struct_stat_to_gfile_info (st);
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 fb4ff91..0b3307d 100644
--- a/src/libostree/ostree-repo.c
+++ b/src/libostree/ostree-repo.c
@@ -1754,11 +1754,10 @@ query_info_for_bare_content_object (OstreeRepo *self,
goto out;
}
- ret_info = g_file_info_new ();
+ ret_info = ot_default_struct_stat_to_gfile_info (&stbuf);
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))
@@ -1766,8 +1765,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));
@@ -1786,11 +1783,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:
diff --git a/src/libotutil/ot-gio-utils.c b/src/libotutil/ot-gio-utils.c
index dff2113..66d4377 100644
--- a/src/libotutil/ot-gio-utils.c
+++ b/src/libotutil/ot-gio-utils.c
@@ -50,6 +50,17 @@ ot_gfile_type_for_mode (guint32 mode)
return G_FILE_TYPE_UNKNOWN;
}
+GFileInfo *
+ot_default_struct_stat_to_gfile_info (const struct stat *stbuf)
+{
+ GFileInfo *ret = g_file_info_new ();
+ g_file_info_set_attribute_uint32 (ret, "standard::type", ot_gfile_type_for_mode (stbuf->st_mode));
+ g_file_info_set_attribute_boolean (ret, "standard::is-symlink", S_ISLNK (stbuf->st_mode));
+ g_file_info_set_attribute_uint32 (ret, "unix::uid", stbuf->st_uid);
+ g_file_info_set_attribute_uint32 (ret, "unix::gid", stbuf->st_gid);
+ g_file_info_set_attribute_uint32 (ret, "unix::mode", stbuf->st_mode);
+ return ret;
+}
GFile *
ot_gfile_from_build_path (const char *first, ...)
diff --git a/src/libotutil/ot-gio-utils.h b/src/libotutil/ot-gio-utils.h
index b3cb1f8..d7c3945 100644
--- a/src/libotutil/ot-gio-utils.h
+++ b/src/libotutil/ot-gio-utils.h
@@ -35,6 +35,8 @@ G_BEGIN_DECLS
GFileType ot_gfile_type_for_mode (guint32 mode);
+GFileInfo *ot_default_struct_stat_to_gfile_info (const struct stat *stbuf);
+
GFile *ot_gfile_from_build_path (const char *first, ...) G_GNUC_NULL_TERMINATED;
GFile *ot_gfile_get_child_strconcat (GFile *parent, const char *first, ...) G_GNUC_NULL_TERMINATED;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]