builder: Use context_find_in_sources_dirs to simplify code

Instead of open-coding the search everywhere. Also some places
are changed to use flatpak_build_file to further simplify
the code.
tingping/wmclass
Alexander Larsson 2017-04-24 08:59:25 +02:00
parent b593ff04d3
commit a5157d445b
5 changed files with 51 additions and 76 deletions

View File

@ -55,23 +55,22 @@ git_get_mirror_dir (const char *url_or_path,
BuilderContext *context) BuilderContext *context)
{ {
g_autoptr(GFile) git_dir = NULL; g_autoptr(GFile) git_dir = NULL;
g_autoptr(GFile) dir = NULL;
g_autofree char *filename = NULL; g_autofree char *filename = NULL;
g_autofree char *git_dir_path = NULL; g_autofree char *git_dir_path = NULL;
GPtrArray *sources_dirs = NULL;
int i;
sources_dirs = builder_context_get_sources_dirs (context); /* Technically a path isn't a uri but if it's absolute it should still be unique. */
for (i = 0; sources_dirs != NULL && i < sources_dirs->len; i++) filename = builder_uri_to_filename (url_or_path);
dir = builder_context_find_in_sources_dirs (context,
"git",
filename,
NULL);
if (dir)
{ {
GFile* sources_root = g_ptr_array_index (sources_dirs, i); if (is_local != NULL)
g_autoptr(GFile) local_git_dir = g_file_get_child (sources_root, "git"); *is_local = TRUE;
g_autofree char *local_filename = builder_uri_to_filename (url_or_path); return g_steal_pointer (&dir);
g_autoptr(GFile) file = g_file_get_child (local_git_dir, local_filename);
if (g_file_query_exists (file, NULL)) {
if (is_local != NULL)
*is_local = TRUE;
return g_steal_pointer (&file);
}
} }
git_dir = g_file_get_child (builder_context_get_state_dir (context), git_dir = g_file_get_child (builder_context_get_state_dir (context),
@ -82,8 +81,6 @@ git_get_mirror_dir (const char *url_or_path,
if (is_local != NULL) if (is_local != NULL)
*is_local = FALSE; *is_local = FALSE;
/* Technically a path isn't a uri but if it's absolute it should still be unique. */
filename = builder_uri_to_filename (url_or_path);
return g_file_get_child (git_dir, filename); return g_file_get_child (git_dir, filename);
} }

View File

@ -217,11 +217,7 @@ get_download_location (BuilderSourceArchive *self,
g_autoptr(SoupURI) uri = NULL; g_autoptr(SoupURI) uri = NULL;
const char *path; const char *path;
g_autofree char *base_name = NULL; g_autofree char *base_name = NULL;
GFile *download_dir = NULL;
g_autoptr(GFile) sha256_dir = NULL;
g_autoptr(GFile) file = NULL; g_autoptr(GFile) file = NULL;
GPtrArray *sources_dirs = NULL;
int i;
uri = get_uri (self, error); uri = get_uri (self, error);
if (uri == NULL) if (uri == NULL)
@ -237,24 +233,21 @@ get_download_location (BuilderSourceArchive *self,
return FALSE; return FALSE;
} }
sources_dirs = builder_context_get_sources_dirs (context); file = builder_context_find_in_sources_dirs (context,
for (i = 0; sources_dirs != NULL && i < sources_dirs->len; i++) "downloads",
self->sha256,
base_name,
NULL);
if (file)
{ {
GFile* sources_root = g_ptr_array_index (sources_dirs, i); *is_local = TRUE;
g_autoptr(GFile) local_download_dir = g_file_get_child (sources_root, "downloads"); return g_steal_pointer (&file);
g_autoptr(GFile) local_sha256_dir = g_file_get_child (local_download_dir, self->sha256);
g_autoptr(GFile) local_file = g_file_get_child (local_sha256_dir, base_name);
if (g_file_query_exists (local_file, NULL)) {
*is_local = TRUE;
return g_steal_pointer (&local_file);
}
} }
download_dir = builder_context_get_download_dir (context); return flatpak_build_file (builder_context_get_download_dir (context),
sha256_dir = g_file_get_child (download_dir, self->sha256); self->sha256,
file = g_file_get_child (sha256_dir, base_name); base_name,
NULL);
return g_steal_pointer (&file);
} }
static GFile * static GFile *

View File

@ -134,21 +134,18 @@ static GFile *
get_mirror_dir (BuilderSourceBzr *self, BuilderContext *context) get_mirror_dir (BuilderSourceBzr *self, BuilderContext *context)
{ {
g_autoptr(GFile) bzr_dir = NULL; g_autoptr(GFile) bzr_dir = NULL;
g_autoptr(GFile) dir = NULL;
g_autofree char *filename = NULL; g_autofree char *filename = NULL;
g_autofree char *bzr_dir_path = NULL; g_autofree char *bzr_dir_path = NULL;
GPtrArray *sources_dirs = NULL;
int i;
sources_dirs = builder_context_get_sources_dirs (context); filename = builder_uri_to_filename (self->url);
for (i = 0; sources_dirs != NULL && i < sources_dirs->len; i++)
{ dir = builder_context_find_in_sources_dirs (context,
GFile* sources_root = g_ptr_array_index (sources_dirs, i); "bzr",
g_autoptr(GFile) local_bzr_dir = g_file_get_child (sources_root, "bzr"); filename,
g_autofree char *local_filename = builder_uri_to_filename (self->url); NULL);
g_autoptr(GFile) file = g_file_get_child (local_bzr_dir, local_filename); if (dir)
if (g_file_query_exists (file, NULL)) return g_steal_pointer (&dir);
return g_steal_pointer (&file);
}
bzr_dir = g_file_get_child (builder_context_get_state_dir (context), bzr_dir = g_file_get_child (builder_context_get_state_dir (context),
"bzr"); "bzr");
@ -156,7 +153,6 @@ get_mirror_dir (BuilderSourceBzr *self, BuilderContext *context)
bzr_dir_path = g_file_get_path (bzr_dir); bzr_dir_path = g_file_get_path (bzr_dir);
g_mkdir_with_parents (bzr_dir_path, 0755); g_mkdir_with_parents (bzr_dir_path, 0755);
filename = builder_uri_to_filename (self->url);
return g_file_get_child (bzr_dir, filename); return g_file_get_child (bzr_dir, filename);
} }

View File

@ -168,11 +168,7 @@ get_download_location (BuilderSourceFile *self,
g_autoptr(SoupURI) uri = NULL; g_autoptr(SoupURI) uri = NULL;
const char *path; const char *path;
g_autofree char *base_name = NULL; g_autofree char *base_name = NULL;
GFile *download_dir = NULL;
g_autoptr(GFile) sha256_dir = NULL;
g_autoptr(GFile) file = NULL; g_autoptr(GFile) file = NULL;
GPtrArray *sources_dirs = NULL;
int i;
uri = get_uri (self, error); uri = get_uri (self, error);
if (uri == NULL) if (uri == NULL)
@ -194,23 +190,21 @@ get_download_location (BuilderSourceFile *self,
return FALSE; return FALSE;
} }
sources_dirs = builder_context_get_sources_dirs (context); file = builder_context_find_in_sources_dirs (context,
for (i = 0; sources_dirs != NULL && i < sources_dirs->len; i++) "downloads",
self->sha256,
base_name,
NULL);
if (file != NULL)
{ {
GFile* sources_root = g_ptr_array_index (sources_dirs, i); *is_local = TRUE;
g_autoptr(GFile) local_download_dir = g_file_get_child (sources_root, "downloads"); return g_steal_pointer (&file);
g_autoptr(GFile) local_sha256_dir = g_file_get_child (local_download_dir, self->sha256);
g_autoptr(GFile) local_file = g_file_get_child (local_sha256_dir, base_name);
if (g_file_query_exists (local_file, NULL)) {
*is_local = TRUE;
return g_steal_pointer (&local_file);
}
} }
download_dir = builder_context_get_download_dir (context); file = flatpak_build_file (builder_context_get_download_dir (context),
sha256_dir = g_file_get_child (download_dir, self->sha256); self->sha256,
file = g_file_get_child (sha256_dir, base_name); base_name,
NULL);
*is_inline = FALSE; *is_inline = FALSE;
return g_steal_pointer (&file); return g_steal_pointer (&file);
} }

View File

@ -143,8 +143,6 @@ get_source_file (BuilderSourcePatch *self,
g_autoptr(GFile) patch = NULL; g_autoptr(GFile) patch = NULL;
g_autoptr(GFile) file = NULL; g_autoptr(GFile) file = NULL;
g_autofree char *base_name = NULL; g_autofree char *base_name = NULL;
GPtrArray *sources_dirs = NULL;
int i;
GFile *base_dir = BUILDER_SOURCE (self)->base_dir; GFile *base_dir = BUILDER_SOURCE (self)->base_dir;
@ -157,15 +155,12 @@ get_source_file (BuilderSourcePatch *self,
patch = g_file_new_for_path (self->path); patch = g_file_new_for_path (self->path);
base_name = g_file_get_basename (patch); base_name = g_file_get_basename (patch);
sources_dirs = builder_context_get_sources_dirs (context); file = builder_context_find_in_sources_dirs (context,
for (i = 0; sources_dirs != NULL && i < sources_dirs->len; i++) "patches",
{ base_name,
GFile* sources_root = g_ptr_array_index (sources_dirs, i); NULL);
g_autoptr(GFile) patches_dir = g_file_get_child (sources_root, "patches"); if (file)
g_autoptr(GFile) file = g_file_get_child (patches_dir, base_name); return g_steal_pointer (&file);
if (g_file_query_exists (file, NULL))
return g_steal_pointer (&file);
}
return g_file_resolve_relative_path (base_dir, self->path); return g_file_resolve_relative_path (base_dir, self->path);
} }