libsepol/cil: Refactor filecon file type handling

Prepare for the addition of an optional file type in genfscon rules
by refactoring filecon file type handling.

Make the "any" file type be the first value in enum cil_filecon_types
because it will be the most common file type.

Signed-off-by: James Carter <jwcart2@gmail.com>
This commit is contained in:
James Carter 2021-11-10 09:08:37 -05:00
parent 55e674894b
commit c9ed552129
4 changed files with 35 additions and 22 deletions

View File

@ -1765,6 +1765,9 @@ int cil_filecons_to_string(struct cil_db *db, char **out, size_t *size)
str_tmp += buf_pos;
switch(filecon->type) {
case CIL_FILECON_ANY:
str_type = "";
break;
case CIL_FILECON_FILE:
str_type = "\t--";
break;
@ -2530,7 +2533,7 @@ void cil_filecon_init(struct cil_filecon **filecon)
*filecon = cil_malloc(sizeof(**filecon));
(*filecon)->path_str = NULL;
(*filecon)->type = 0;
(*filecon)->type = CIL_FILECON_ANY;
(*filecon)->context_str = NULL;
(*filecon)->context = NULL;
}

View File

@ -4229,7 +4229,9 @@ int cil_gen_filecon(struct cil_db *db, struct cil_tree_node *parse_current, stru
filecon->path_str = parse_current->next->data;
if (type == CIL_KEY_FILE) {
if (type == CIL_KEY_ANY) {
filecon->type = CIL_FILECON_ANY;
} else if (type == CIL_KEY_FILE) {
filecon->type = CIL_FILECON_FILE;
} else if (type == CIL_KEY_DIR) {
filecon->type = CIL_FILECON_DIR;
@ -4243,8 +4245,6 @@ int cil_gen_filecon(struct cil_db *db, struct cil_tree_node *parse_current, stru
filecon->type = CIL_FILECON_PIPE;
} else if (type == CIL_KEY_SYMLINK) {
filecon->type = CIL_FILECON_SYMLINK;
} else if (type == CIL_KEY_ANY) {
filecon->type = CIL_FILECON_ANY;
} else {
cil_log(CIL_ERR, "Invalid file type\n");
rc = SEPOL_ERR;

View File

@ -730,14 +730,14 @@ struct cil_context {
};
enum cil_filecon_types {
CIL_FILECON_FILE = 1,
CIL_FILECON_ANY = 0,
CIL_FILECON_FILE,
CIL_FILECON_DIR,
CIL_FILECON_CHAR,
CIL_FILECON_BLOCK,
CIL_FILECON_SOCKET,
CIL_FILECON_PIPE,
CIL_FILECON_SYMLINK,
CIL_FILECON_ANY
};
struct cil_filecon {

View File

@ -1232,24 +1232,34 @@ void cil_write_ast_node(FILE *out, struct cil_tree_node *node)
struct cil_filecon *filecon = node->data;
fprintf(out, "(filecon ");
fprintf(out, "\"%s\" ", filecon->path_str);
if (filecon->type == CIL_FILECON_FILE)
fprintf(out, "%s ", CIL_KEY_FILE);
else if (filecon->type == CIL_FILECON_DIR)
fprintf(out, "%s ", CIL_KEY_DIR);
else if (filecon->type == CIL_FILECON_CHAR)
fprintf(out, "%s ", CIL_KEY_CHAR);
else if (filecon->type == CIL_FILECON_BLOCK)
fprintf(out, "%s ", CIL_KEY_BLOCK);
else if (filecon->type == CIL_FILECON_SOCKET)
fprintf(out, "%s ", CIL_KEY_SOCKET);
else if (filecon->type == CIL_FILECON_PIPE)
fprintf(out, "%s ", CIL_KEY_PIPE);
else if (filecon->type == CIL_FILECON_SYMLINK)
fprintf(out, "%s ", CIL_KEY_SYMLINK);
else if (filecon->type == CIL_FILECON_ANY)
switch (filecon->type) {
case CIL_FILECON_ANY:
fprintf(out, "%s ", CIL_KEY_ANY);
else
break;
case CIL_FILECON_FILE:
fprintf(out, "%s ", CIL_KEY_FILE);
break;
case CIL_FILECON_DIR:
fprintf(out, "%s ", CIL_KEY_DIR);
break;
case CIL_FILECON_CHAR:
fprintf(out, "%s ", CIL_KEY_CHAR);
break;
case CIL_FILECON_BLOCK:
fprintf(out, "%s ", CIL_KEY_BLOCK);
break;
case CIL_FILECON_SOCKET:
fprintf(out, "%s ", CIL_KEY_SOCKET);
break;
case CIL_FILECON_PIPE:
fprintf(out, "%s ", CIL_KEY_PIPE);
break;
case CIL_FILECON_SYMLINK:
fprintf(out, "%s ", CIL_KEY_SYMLINK);
break;
default:
fprintf(out, "<?FILETYPE> ");
}
if (filecon->context)
write_context(out, filecon->context, CIL_TRUE);
else if (filecon->context_str)