core: More cleanup of GIO utility API

This commit is contained in:
Colin Walters 2011-11-18 08:42:18 -05:00
parent 231f7ed76c
commit 9cf15ed151
4 changed files with 29 additions and 73 deletions

View File

@ -179,13 +179,13 @@ ostree_repo_new (const char *path)
static gboolean static gboolean
parse_rev_file (OstreeRepo *self, parse_rev_file (OstreeRepo *self,
const char *path, GFile *f,
char **sha256, char **sha256,
GError **error) G_GNUC_UNUSED; GError **error) G_GNUC_UNUSED;
static gboolean static gboolean
parse_rev_file (OstreeRepo *self, parse_rev_file (OstreeRepo *self,
const char *path, GFile *f,
char **sha256, char **sha256,
GError **error) GError **error)
{ {
@ -194,7 +194,9 @@ parse_rev_file (OstreeRepo *self,
gboolean ret = FALSE; gboolean ret = FALSE;
char *rev = NULL; char *rev = NULL;
rev = ot_util_get_file_contents_utf8 (path, &temp_error); if (!ot_gfile_load_contents_utf8 (f, &rev, NULL, NULL, &temp_error))
goto out;
if (rev == NULL) if (rev == NULL)
{ {
if (g_error_matches (temp_error, G_FILE_ERROR, G_FILE_ERROR_NOENT)) if (g_error_matches (temp_error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
@ -215,14 +217,11 @@ parse_rev_file (OstreeRepo *self,
if (g_str_has_prefix (rev, "ref: ")) if (g_str_has_prefix (rev, "ref: "))
{ {
GFile *ref; GFile *ref;
const char *ref_path;
char *ref_sha256; char *ref_sha256;
gboolean subret; gboolean subret;
ref = g_file_resolve_relative_path (priv->local_heads_dir, rev + 5); ref = g_file_resolve_relative_path (priv->local_heads_dir, rev + 5);
ref_path = ot_gfile_get_path_cached (ref); subret = parse_rev_file (self, ref, &ref_sha256, error);
subret = parse_rev_file (self, ref_path, &ref_sha256, error);
g_clear_object (&ref); g_clear_object (&ref);
if (!subret) if (!subret)
@ -333,7 +332,7 @@ ostree_repo_resolve_rev (OstreeRepo *self,
child_path = ot_gfile_get_path_cached (child); child_path = ot_gfile_get_path_cached (child);
} }
if (!ot_util_gfile_load_contents_utf8 (child, NULL, &ret_rev, NULL, &temp_error)) if (!ot_gfile_load_contents_utf8 (child, &ret_rev, NULL, NULL, &temp_error))
{ {
if (allow_noent && g_error_matches (temp_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND)) if (allow_noent && g_error_matches (temp_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
{ {
@ -756,16 +755,19 @@ prepare_dir_for_checksum_get_object_path (OstreeRepo *self,
OstreeObjectType type, OstreeObjectType type,
GError **error) GError **error)
{ {
GFile *f = NULL;
char *checksum_dir = NULL; char *checksum_dir = NULL;
char *object_path = NULL; char *object_path = NULL;
object_path = ostree_repo_get_object_path (self, checksum, type); object_path = ostree_repo_get_object_path (self, checksum, type);
checksum_dir = g_path_get_dirname (object_path); checksum_dir = g_path_get_dirname (object_path);
f = ot_gfile_new_for_path (checksum_dir);
if (!ot_util_ensure_directory (checksum_dir, FALSE, error)) if (!ot_gfile_ensure_directory (f, FALSE, error))
goto out; goto out;
out: out:
g_clear_object (&f);
g_free (checksum_dir); g_free (checksum_dir);
return object_path; return object_path;
} }
@ -1359,7 +1361,7 @@ ostree_repo_write_ref (OstreeRepo *self,
{ {
dir = g_file_get_child (priv->remote_heads_dir, remote); dir = g_file_get_child (priv->remote_heads_dir, remote);
if (!ot_util_ensure_directory (ot_gfile_get_path_cached (dir), FALSE, error)) if (!ot_gfile_ensure_directory (dir, FALSE, error))
goto out; goto out;
} }

View File

@ -30,13 +30,13 @@
#include "otutil.h" #include "otutil.h"
gboolean gboolean
ot_util_ensure_directory (const char *path, gboolean with_parents, GError **error) ot_gfile_ensure_directory (GFile *dir,
gboolean with_parents,
GError **error)
{ {
GFile *dir;
GError *temp_error = NULL; GError *temp_error = NULL;
gboolean ret = FALSE; gboolean ret = FALSE;
dir = ot_gfile_new_for_path (path);
if (with_parents) if (with_parents)
ret = g_file_make_directory_with_parents (dir, NULL, &temp_error); ret = g_file_make_directory_with_parents (dir, NULL, &temp_error);
else else
@ -54,33 +54,15 @@ ot_util_ensure_directory (const char *path, gboolean with_parents, GError **erro
ret = TRUE; ret = TRUE;
out: out:
g_clear_object (&dir);
return ret;
}
char *
ot_util_get_file_contents_utf8 (const char *path,
GError **error)
{
GFile *file = NULL;
char *ret = NULL;
file = ot_gfile_new_for_path (path);
if (!ot_util_gfile_load_contents_utf8 (file, NULL, &ret, NULL, error))
goto out;
out:
g_clear_object (&file);
return ret; return ret;
} }
gboolean gboolean
ot_util_gfile_load_contents_utf8 (GFile *file, ot_gfile_load_contents_utf8 (GFile *file,
GCancellable *cancellable, char **contents_out,
char **contents_out, char **etag_out,
char **etag_out, GCancellable *cancellable,
GError **error) GError **error)
{ {
char *ret_contents = NULL; char *ret_contents = NULL;
char *ret_etag = NULL; char *ret_etag = NULL;
@ -115,31 +97,6 @@ ot_util_gfile_load_contents_utf8 (GFile *file,
return ret; return ret;
} }
GInputStream *
ot_util_read_file_noatime (GFile *file, GCancellable *cancellable, GError **error)
{
GInputStream *ret = NULL;
int fd;
int flags = O_RDONLY;
const char *path = NULL;
path = ot_gfile_get_path_cached (file);
#ifdef O_NOATIME
flags |= O_NOATIME;
#endif
fd = open (path, flags);
if (fd < 0)
{
ot_util_set_error_from_errno (error, errno);
goto out;
}
ret = (GInputStream*)g_unix_input_stream_new (fd, TRUE);
out:
return ret;
}
/* Like g_file_new_for_path, but only do local stuff, not GVFS */ /* Like g_file_new_for_path, but only do local stuff, not GVFS */
GFile * GFile *
ot_gfile_new_for_path (const char *path) ot_gfile_new_for_path (const char *path)

View File

@ -33,17 +33,13 @@ const char *ot_gfile_get_path_cached (GFile *file);
const char *ot_gfile_get_basename_cached (GFile *file); const char *ot_gfile_get_basename_cached (GFile *file);
gboolean ot_util_ensure_directory (const char *path, gboolean with_parents, GError **error); gboolean ot_gfile_ensure_directory (GFile *dir, gboolean with_parents, GError **error);
char * ot_util_get_file_contents_utf8 (const char *path, GError **error); gboolean ot_gfile_load_contents_utf8 (GFile *file,
char **contents_out,
gboolean ot_util_gfile_load_contents_utf8 (GFile *file, char **etag_out,
GCancellable *cancellable, GCancellable *cancellable,
char **contents_out, GError **error);
char **etag_out,
GError **error);
GInputStream *ot_util_read_file_noatime (GFile *file, GCancellable *cancellable, GError **error);
G_END_DECLS G_END_DECLS

View File

@ -292,6 +292,7 @@ ostree_builtin_pull (int argc, char **argv, const char *repo_path, GError **erro
char *baseurl = NULL; char *baseurl = NULL;
char *refpath = NULL; char *refpath = NULL;
char *temppath = NULL; char *temppath = NULL;
GFile *tempf = NULL;
char *remote_ref = NULL; char *remote_ref = NULL;
char *original_rev = NULL; char *original_rev = NULL;
GKeyFile *config = NULL; GKeyFile *config = NULL;
@ -349,9 +350,9 @@ ostree_builtin_pull (int argc, char **argv, const char *repo_path, GError **erro
NULL); NULL);
if (!fetch_uri (repo, soup, target_uri, &temppath, error)) if (!fetch_uri (repo, soup, target_uri, &temppath, error))
goto out; goto out;
tempf = ot_gfile_new_for_path (temppath);
rev = ot_util_get_file_contents_utf8 (temppath, error); if (!ot_gfile_load_contents_utf8 (tempf, &rev, NULL, NULL, error))
if (!rev)
goto out; goto out;
g_strchomp (rev); g_strchomp (rev);