Skip to content

Commit

Permalink
core: Deduplicate code converting struct stat -> GFileInfo
Browse files Browse the repository at this point in the history
We were doing the same thing in a number of places, make a helper
function.
  • Loading branch information
cgwalters committed Dec 19, 2014
1 parent 880328b commit c4efbf6
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 29 deletions.
2 changes: 2 additions & 0 deletions src/libostree/ostree-core-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
42 changes: 28 additions & 14 deletions src/libostree/ostree-core.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
{
Expand Down Expand Up @@ -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))
{
Expand Down
9 changes: 3 additions & 6 deletions src/libostree/ostree-repo-libarchive.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "config.h"

#include "ostree-core-private.h"
#include "ostree-repo-private.h"
#include "ostree-mutable-tree.h"

Expand All @@ -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)
{
Expand Down
10 changes: 1 addition & 9 deletions src/libostree/ostree-repo.c
Original file line number Diff line number Diff line change
Expand Up @@ -1802,20 +1802,17 @@ 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))
{
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));
Expand All @@ -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:
Expand Down

0 comments on commit c4efbf6

Please sign in to comment.