X-Git-Url: https://sigrok.org/gitweb/?p=libsigrok.git;a=blobdiff_plain;f=src%2Fresource.c;h=a30b4b3f744ee0d154926038e6dfce67dcb029ba;hp=ec32a4420f7755de5c5491047a756c59d56d3ba7;hb=deb7615262ac4f9cc0750a08351afa7cbf9c34d5;hpb=1503d4571b1ed2a1afb75198ca3b7ffc97c829f8 diff --git a/src/resource.c b/src/resource.c index ec32a442..a30b4b3f 100644 --- a/src/resource.c +++ b/src/resource.c @@ -35,7 +35,51 @@ * Access to resource files. */ -/** Retrieve the size of the open stream @a file. +/** + * Get a list of paths where we look for resource (e.g. firmware) files. + * + * @param res_type The type of resource to get the search paths for. + * + * @return List of strings that must be freed after use, including the strings. + * + * @since 0.6.0 + */ +SR_API GSList *sr_resourcepaths_get(int res_type) +{ + const char *subdir = NULL; + GSList *l = NULL; + const char *env; + const char *const *datadirs; + + if (res_type == SR_RESOURCE_FIRMWARE) { + subdir = "sigrok-firmware"; + + env = g_getenv("SIGROK_FIRMWARE_DIR"); + if (env) + l = g_slist_append(l, g_strdup(env)); + } + + l = g_slist_append(l, g_build_filename(g_get_user_data_dir(), subdir, NULL)); + +#ifdef FIRMWARE_DIR + if (res_type == SR_RESOURCE_FIRMWARE) { + /* + * Scan the hard-coded directory before the system directories to + * avoid picking up possibly outdated files from a system install. + */ + l = g_slist_append(l, g_strdup(FIRMWARE_DIR)); + } +#endif + + datadirs = g_get_system_data_dirs(); + while (*datadirs) + l = g_slist_append(l, g_build_filename(*datadirs++, subdir, NULL)); + + return l; +} + +/** + * Retrieve the size of the open stream @a file. * * This function only works on seekable streams. However, the set of seekable * streams is generally congruent with the set of streams that have a size. @@ -82,7 +126,11 @@ static FILE *try_open_file(const char *datadir, const char *subdir, char *filename; FILE *file; - filename = g_build_filename(datadir, subdir, name, NULL); + if (subdir) + filename = g_build_filename(datadir, subdir, name, NULL); + else + filename = g_build_filename(datadir, name, NULL); + file = g_fopen(filename, "rb"); if (file) @@ -98,50 +146,27 @@ static FILE *try_open_file(const char *datadir, const char *subdir, static int resource_open_default(struct sr_resource *res, const char *name, void *cb_data) { + GSList *paths, *p = NULL; int64_t filesize; -#ifdef FIRMWARE_DIR - const char *builtindir; -#endif - const char *subdir, *env; - const char *const *datadirs; FILE *file = NULL; (void)cb_data; - switch (res->type) { - case SR_RESOURCE_FIRMWARE: -#ifdef FIRMWARE_DIR - builtindir = FIRMWARE_DIR; -#endif - subdir = "sigrok-firmware"; - break; - default: + paths = sr_resourcepaths_get(res->type); + + /* Currently, the enum only defines SR_RESOURCE_FIRMWARE. */ + if (res->type != SR_RESOURCE_FIRMWARE) { sr_err("%s: unknown type %d.", __func__, res->type); return SR_ERR_ARG; } - env = g_getenv("SIGROK_FIRMWARE_DIR"); - if (!env) - sr_dbg("SIGROK_FIRMWARE_DIR environment variable not set, ignoring."); - else - file = try_open_file(env, "", name); - - if (!file) - file = try_open_file(g_get_user_data_dir(), subdir, name); - - /* - * Scan the hard-coded directory before the system directories to - * avoid picking up possibly outdated files from a system install. - */ -#ifdef FIRMWARE_DIR - if (!file) - file = try_open_file(builtindir, "", name); -#endif - if (!file) { - datadirs = g_get_system_data_dirs(); - while (*datadirs && !file) - file = try_open_file(*datadirs++, subdir, name); + p = paths; + while (p && !file) { + file = try_open_file((const char *)(p->data), NULL, name); + p = p->next; } + g_slist_free_full(paths, g_free); + if (!file) { sr_dbg("Failed to locate '%s'.", name); return SR_ERR; @@ -385,5 +410,3 @@ SR_PRIV void *sr_resource_load(struct sr_context *ctx, *size = res_size; return buf; } - -/** @} */