2
0
mirror of https://gitlab.com/apparmor/apparmor synced 2025-08-31 06:16:03 +00:00

parser: Move policy cache initialization code into its own function

This patch moves the logic that sets up the policy into a new function
in policy_cache.c

Signed-off-by: Tyler Hicks <tyhicks@canonical.com>
Acked-by: John Johansen <john.johansen@canonical.com>
This commit is contained in:
Tyler Hicks
2015-03-25 17:09:26 -05:00
parent 2a082ee543
commit 0f12effabf
4 changed files with 54 additions and 47 deletions

View File

@@ -30,9 +30,12 @@
#define _(s) gettext(s)
#include "lib.h"
#include "features.h"
#include "parser.h"
#include "policy_cache.h"
char *cacheloc = NULL;
#define le16_to_cpu(x) ((uint16_t)(le16toh (*(uint16_t *) x)))
const char header_string[] = "\004\010\000version\000\002";
@@ -226,3 +229,49 @@ void install_cache(const char *cachetmpname, const char *cachename)
}
}
}
void setup_cache(void)
{
autofree char *cache_features_path = NULL;
autofree char *cache_flags = NULL;
/* create the cacheloc once and use it everywhere */
if (!cacheloc) {
if (asprintf(&cacheloc, "%s/cache", basedir) == -1) {
PERROR(_("Memory allocation error."));
exit(1);
}
}
if (force_clear_cache)
exit(clear_cache_files(cacheloc));
/*
* Deal with cache directory versioning:
* - If cache/.features is missing, create it if --write-cache.
* - If cache/.features exists, and does not match features_string,
* force cache reading/writing off.
*/
if (asprintf(&cache_features_path, "%s/.features", cacheloc) == -1) {
PERROR(_("Memory allocation error."));
exit(1);
}
cache_flags = load_features_file(cache_features_path);
if (cache_flags) {
if (strcmp(features_string, cache_flags) != 0) {
if (write_cache && cond_clear_cache) {
if (create_cache(cacheloc, cache_features_path,
features_string))
skip_read_cache = 1;
} else {
if (show_cache)
PERROR("Cache read/write disabled: %s does not match %s\n", FEATURES_FILE, cache_features_path);
write_cache = 0;
skip_read_cache = 1;
}
}
} else if (write_cache) {
create_cache(cacheloc, cache_features_path, features_string);
}
}