diff options
| author | Lucas De Marchi <lucas.demarchi@profusion.mobi> | 2011-12-31 19:28:31 -0200 |
|---|---|---|
| committer | Lucas De Marchi <lucas.demarchi@profusion.mobi> | 2012-01-01 06:18:16 -0200 |
| commit | c4dc3ca8a22d96fde95c34ba0cd7e0b3aa11032d (patch) | |
| tree | 696fbcea1d45740c784b09db94138937cd211a87 /libkmod/libkmod.c | |
| parent | 9fd58f30bfad2b5f996210d887885489bba85a83 (diff) | |
| download | kmod-c4dc3ca8a22d96fde95c34ba0cd7e0b3aa11032d.tar.gz | |
Add call to check if resources are valid
Diffstat (limited to 'libkmod/libkmod.c')
| -rw-r--r-- | libkmod/libkmod.c | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/libkmod/libkmod.c b/libkmod/libkmod.c index 794cc54..7acd8d8 100644 --- a/libkmod/libkmod.c +++ b/libkmod/libkmod.c @@ -29,6 +29,7 @@ #include <string.h> #include <ctype.h> #include <sys/utsname.h> +#include <sys/stat.h> #include "libkmod.h" #include "libkmod-private.h" @@ -611,6 +612,62 @@ int kmod_lookup_alias_from_commands(struct kmod_ctx *ctx, const char *name, return nmatch; } +static bool is_cache_invalid(const char *path, unsigned long long stamp) +{ + struct stat st; + + if (stat(path, &st) < 0) + return true; + + if (stamp != ts_usec(&st.st_mtim)) + return true; + + return false; +} + +/** + * kmod_validate_resources: + * @ctx: kmod library context + * + * Check if indexes and configuration files changed on disk and the current + * context is not valid anymore. + * + * Returns KMOD_RESOURCES_OK if resources are still valid, + * KMOD_RESOURCES_MUST_RELOAD if it's sufficient to call + * kmod_unload_resources() and kmod_load_resources() or + * KMOD_RESOURCES_MUST_RECREATE if @ctx must be re-created. + */ +KMOD_EXPORT int kmod_validate_resources(struct kmod_ctx *ctx) +{ + struct kmod_list *l; + size_t i; + + if (ctx == NULL || ctx->config == NULL) + return KMOD_RESOURCES_MUST_RECREATE; + + kmod_list_foreach(l, ctx->config->paths) { + struct kmod_config_path *cf = l->data; + + if (is_cache_invalid(cf->path, cf->stamp)) + return KMOD_RESOURCES_MUST_RECREATE; + } + + for (i = 0; i < _KMOD_INDEX_LAST; i++) { + char path[PATH_MAX]; + + if (ctx->indexes[i] == NULL) + continue; + + snprintf(path, sizeof(path), "%s/%s.bin", ctx->dirname, + index_files[i]); + + if (is_cache_invalid(path, ctx->indexes_stamp[i])) + return KMOD_RESOURCES_MUST_RELOAD; + } + + return KMOD_RESOURCES_OK; +} + /** * kmod_load_resources: * @ctx: kmod library context |
