From: Rafal Krypa Date: Wed, 4 Dec 2013 15:58:53 +0000 (+0100) Subject: utils: use common code for apply_rules and apply_cipso X-Git-Tag: v1.0.3~1 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=68e38ff3936597a3189d29f57a68dae5ac08db1e;p=platform%2Fupstream%2Fsmack.git utils: use common code for apply_rules and apply_cipso Code for apply_rules() has been rewritten to use opendir() and readdir(), but apply_cipso() remained implemented with nftw(). This patch implements both applying functions with opendir() and readdir() using a common internal function apply_dir(). The resulting code is 45 lines shorter and keeps directory traversal logic in single place. Signed-off-by: Rafal Krypa --- diff --git a/utils/common.c b/utils/common.c index 1b6efdd..c429001 100644 --- a/utils/common.c +++ b/utils/common.c @@ -22,16 +22,10 @@ #include #include #include -#include #include #include #include -#define SMACK_MAGIC 0x43415d53 - -static int apply_cipso_cb(const char *fpath, const struct stat *sb, - int typeflag, struct FTW *ftwbuf); - int clear(void) { int fd; @@ -56,7 +50,8 @@ int clear(void) return ret; } -int apply_rules(const char *path, int clear) +static int apply_dir(const char *path, + int (*func)(const char *path, int fd, int arg), int arg) { struct smack_accesses *rules = NULL; DIR *dir; @@ -99,7 +94,7 @@ int apply_rules(const char *path, int clear) break; } - ret = smack_accesses_add_from_file(rules, fd); + ret = func(dent->d_name, fd, arg); close(fd); if (ret < 0) { fprintf(stderr, "Reading rules from '%s' failed.\n", @@ -136,36 +131,19 @@ int apply_rules(const char *path, int clear) return -1; } - ret = apply_rules_file(path, fd, clear); + ret = func(path, fd, arg); close(fd); return ret; } -int apply_cipso(const char *path) +int apply_rules(const char *path, int clear) { - struct stat sbuf; - int fd; - int ret; - - if (stat(path, &sbuf)) { - fprintf(stderr, "stat() failed for '%s' : %s\n", path, - strerror(errno)); - return -1; - } - - if (S_ISDIR(sbuf.st_mode)) - return nftw(path, apply_cipso_cb, 1, FTW_PHYS|FTW_ACTIONRETVAL); - - fd = open(path, O_RDONLY); - if (fd < 0) { - fprintf(stderr, "open() failed for '%s' : %s\n", path, - strerror(errno)); - return -1; - } + return apply_dir(path, apply_rules_file, 0); +} - ret = apply_cipso_file(path, fd); - close(fd); - return ret; +int apply_cipso(const char *path) +{ + return apply_dir(path, apply_cipso_file, 0); } int apply_rules_file(const char *path, int fd, int clear) @@ -200,7 +178,7 @@ int apply_rules_file(const char *path, int fd, int clear) return ret; } -int apply_cipso_file(const char *path, int fd) +int apply_cipso_file(const char *path, int fd, int arg /* not used */) { struct smack_cipso *cipso = NULL; int ret; @@ -230,26 +208,3 @@ int apply_cipso_file(const char *path, int fd) return 0; } - -static int apply_cipso_cb(const char *fpath, const struct stat *sb, - int typeflag, struct FTW *ftwbuf) -{ - int fd; - int ret; - - if (typeflag == FTW_D) - return ftwbuf->level ? FTW_SKIP_SUBTREE : FTW_CONTINUE; - else if (typeflag != FTW_F) - return FTW_STOP; - - fd = open(fpath, O_RDONLY); - if (fd < 0) { - fprintf(stderr, "open() failed for '%s' : %s\n", fpath, - strerror(errno)); - return -1; - } - - ret = apply_cipso_file(fpath, fd) ? FTW_STOP : FTW_CONTINUE; - close(fd); - return ret; -} diff --git a/utils/common.h b/utils/common.h index 1eab142..2249361 100644 --- a/utils/common.h +++ b/utils/common.h @@ -28,6 +28,6 @@ int clear(void); int apply_rules(const char *path, int clear); int apply_cipso(const char *path); int apply_rules_file(const char *path, int fd, int clear); -int apply_cipso_file(const char *path, int fd); +int apply_cipso_file(const char *path, int fd, int arg); #endif // COMMON_H diff --git a/utils/smackcipso.c b/utils/smackcipso.c index 643f080..3e2d9fc 100644 --- a/utils/smackcipso.c +++ b/utils/smackcipso.c @@ -36,7 +36,7 @@ int main(int argc, char **argv) } if (argc == 1) { - if (apply_cipso_file(NULL, STDIN_FILENO)) + if (apply_cipso_file(NULL, STDIN_FILENO, 0)) exit(1); } else { if (apply_cipso(argv[1]))