parser: Create aa_features function to write features to a file

This patch removes the final dependency on callers needing access to the
features string so aa_features_get_string() can go away.

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 4d1fa49c37
commit 5b85282e9f
3 changed files with 33 additions and 17 deletions

View file

@ -450,14 +450,36 @@ void aa_features_unref(aa_features *features)
}
/**
* aa_features_get_string - provides immutable string representation of features
* aa_features_write_to_file - write a string representation to a file
* @features: the features
* @path: the path to write to
*
* Returns: an immutable string representation of features
* Returns: 0 on success, -1 on error with errno set
*/
const char *aa_features_get_string(aa_features *features)
int aa_features_write_to_file(aa_features *features, const char *path)
{
return features->string;
autoclose int fd = -1;
size_t size;
ssize_t retval;
char *string;
fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC | O_CLOEXEC,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (fd == -1)
return -1;
string = features->string;
size = strlen(string);
do {
retval = write(fd, string, size);
if (retval == -1)
return -1;
size -= retval;
string += retval;
} while (size);
return 0;
}
/**

View file

@ -27,7 +27,7 @@ int aa_features_new_from_string(aa_features **features,
int aa_features_new_from_kernel(aa_features **features);
aa_features *aa_features_ref(aa_features *features);
void aa_features_unref(aa_features *features);
const char *aa_features_get_string(aa_features *features);
int aa_features_write_to_file(aa_features *features, const char *path);
bool aa_features_is_equal(aa_features *features1, aa_features *features2);
bool aa_features_supports(aa_features *features, const char *str);

View file

@ -107,7 +107,7 @@ int clear_cache_files(const char *path)
return dirat_for_each(NULL, path, NULL, clear_cache_cb);
}
int create_cache(const char *cachedir, const char *path, const char *features)
int create_cache(const char *cachedir, const char *path, aa_features *features)
{
struct stat stat_file;
autofclose FILE * f = NULL;
@ -116,13 +116,10 @@ int create_cache(const char *cachedir, const char *path, const char *features)
goto error;
create_file:
f = fopen(path, "w");
if (f) {
if (fwrite(features, strlen(features), 1, f) != 1 )
goto error;
if (aa_features_write_to_file(features, path) == -1)
goto error;
return 0;
}
return 0;
error:
/* does the dir exist? */
@ -231,7 +228,6 @@ int setup_cache(aa_features *kernel_features, const char *cacheloc)
{
autofree char *cache_features_path = NULL;
aa_features *cache_features;
const char *kernel_features_string;
if (!cacheloc) {
errno = EINVAL;
@ -250,12 +246,11 @@ int setup_cache(aa_features *kernel_features, const char *cacheloc)
return -1;
}
kernel_features_string = aa_features_get_string(kernel_features);
if (!aa_features_new(&cache_features, cache_features_path)) {
if (!aa_features_is_equal(kernel_features, cache_features)) {
if (write_cache && cond_clear_cache) {
if (create_cache(cacheloc, cache_features_path,
kernel_features_string))
kernel_features))
skip_read_cache = 1;
} else {
if (show_cache)
@ -266,8 +261,7 @@ int setup_cache(aa_features *kernel_features, const char *cacheloc)
}
aa_features_unref(cache_features);
} else if (write_cache) {
create_cache(cacheloc, cache_features_path,
kernel_features_string);
create_cache(cacheloc, cache_features_path, kernel_features);
}
return 0;