From a88a177f991527e545e75588013afd16c647e656 Mon Sep 17 00:00:00 2001 From: Roy Marples Date: Thu, 3 Jul 2008 13:11:47 +0000 Subject: Add older_than function to complement newer_than function. Also make the userland instance reversed to be compatable with current baselayout, which truely does suck. --- src/librc/librc-depend.c | 42 +++++++++++++++++++++++++++++----------- src/librc/librc.h | 1 + src/librc/rc.h.in | 5 +++++ src/librc/rc.map | 1 + src/rc/rc-applets.c | 14 +++++++++----- src/test/librc.funcs.hidden.list | 1 + src/test/rc.funcs.list | 2 ++ 7 files changed, 50 insertions(+), 16 deletions(-) diff --git a/src/librc/librc-depend.c b/src/librc/librc-depend.c index 5b7de424..145bdde6 100644 --- a/src/librc/librc-depend.c +++ b/src/librc/librc-depend.c @@ -553,12 +553,12 @@ rc_deptree_order(const RC_DEPTREE *deptree, const char *runlevel, int options) } librc_hidden_def(rc_deptree_order) -bool -rc_newer_than(const char *source, const char *target) +static bool +mtime_check(const char *source, const char *target, bool newer) { struct stat buf; time_t mtime; - bool newer = true; + bool retval = true; DIR *dp; struct dirent *d; char path[PATH_MAX]; @@ -569,32 +569,52 @@ rc_newer_than(const char *source, const char *target) return false; mtime = buf.st_mtime; - /* Of course we are newer than targets that don't exist - such as broken symlinks */ + /* If target does not exist, return true to mimic shell test */ if (stat(target, &buf) != 0) return true; - if (mtime < buf.st_mtime) - return false; + + if (newer) { + if (mtime < buf.st_mtime) + return false; + } else { + if (mtime > buf.st_mtime) + return false; + } + /* If not a dir then reset errno */ if (!(dp = opendir(target))) { errno = serrno; return true; } - /* Check if we're newer than all the entries in the dir */ + /* Check all the entries in the dir */ while ((d = readdir(dp))) { if (d->d_name[0] == '.') continue; snprintf(path, sizeof(path), "%s/%s", target, d->d_name); - newer = rc_newer_than(source, path); - if (! newer) + retval = mtime_check(source, path, newer); + if (!retval) break; } closedir(dp); - return newer; + return retval; +} + +bool +rc_newer_than(const char *source, const char *target) +{ + + return mtime_check(source, target, true); } librc_hidden_def(rc_newer_than) +bool +rc_older_than(const char *source, const char *target) +{ + return mtime_check(source, target, false); +} +librc_hidden_def(rc_older_than) + typedef struct deppair { const char *depend; diff --git a/src/librc/librc.h b/src/librc/librc.h index 353b5923..e75ebec1 100644 --- a/src/librc/librc.h +++ b/src/librc/librc.h @@ -86,6 +86,7 @@ librc_hidden_proto(rc_deptree_update_needed) librc_hidden_proto(rc_find_pids) librc_hidden_proto(rc_getline) librc_hidden_proto(rc_newer_than) +librc_hidden_proto(rc_older_than) librc_hidden_proto(rc_runlevel_exists) librc_hidden_proto(rc_runlevel_get) librc_hidden_proto(rc_runlevel_list) diff --git a/src/librc/rc.h.in b/src/librc/rc.h.in index 7c94c3eb..30fbfd0e 100644 --- a/src/librc/rc.h.in +++ b/src/librc/rc.h.in @@ -306,6 +306,11 @@ typedef void *RC_DEPTREE; * @return true if source is newer than target, otherwise false */ bool rc_newer_than(const char *, const char *); +/*! Check to see if source is newer than target. + * If target is a directory then we traverse it and it's children. +* @return true if source is newer than target, otherwise false */ +bool rc_older_than(const char *, const char *); + /*! Update the cached dependency tree if it's older than any init script, * its configuration file or an external configuration file the init script * has specified. diff --git a/src/librc/rc.map b/src/librc/rc.map index 8a803f3d..e1dd8437 100644 --- a/src/librc/rc.map +++ b/src/librc/rc.map @@ -13,6 +13,7 @@ global: rc_environ_fd; rc_find_pids; rc_newer_than; + rc_older_than; rc_runlevel_exists; rc_runlevel_get; rc_runlevel_list; diff --git a/src/rc/rc-applets.c b/src/rc/rc-applets.c index cbcbae4d..77790e90 100644 --- a/src/rc/rc-applets.c +++ b/src/rc/rc-applets.c @@ -408,7 +408,7 @@ static int do_shell_var(int argc, char **argv) void run_applets(int argc, char **argv) { int i = 2; - bool match = false; + bool (*is_than) (const char *, const char *); char *p; pid_t pid = 0; @@ -437,18 +437,22 @@ void run_applets(int argc, char **argv) if (strcmp(applet, "shell_var") == 0) exit(do_shell_var(argc, argv)); + /* This test is perverted - historically the baselayout function + * returns 0 on *failure*, which is plain wrong */ if (strcmp(applet, "is_newer_than") == 0 || strcmp(applet, "is_older_than") == 0) { if (argc < 3) exit (EXIT_FAILURE); if (strcmp(applet, "is_newer_than") == 0) - match = true; + is_than = &rc_older_than; + else + is_than = &rc_newer_than; while (i < argc) { - if (rc_newer_than(argv[1], argv[i++]) != match) - exit (EXIT_FAILURE); + if (!is_than(argv[1], argv[i++])) + exit(EXIT_SUCCESS); } - exit(EXIT_SUCCESS); + exit(EXIT_FAILURE); }; if (applet[0] == 'e' || (applet[0] == 'v' && applet[1] == 'e')) diff --git a/src/test/librc.funcs.hidden.list b/src/test/librc.funcs.hidden.list index 2ece066d..15249add 100644 --- a/src/test/librc.funcs.hidden.list +++ b/src/test/librc.funcs.hidden.list @@ -11,6 +11,7 @@ rc_deptree_update_needed rc_find_pids rc_getline rc_newer_than +rc_older_than rc_runlevel_exists rc_runlevel_get rc_runlevel_list diff --git a/src/test/rc.funcs.list b/src/test/rc.funcs.list index a10afc04..85cf9362 100644 --- a/src/test/rc.funcs.list +++ b/src/test/rc.funcs.list @@ -22,6 +22,8 @@ rc_find_pids rc_find_pids@@RC_1.0 rc_newer_than rc_newer_than@@RC_1.0 +rc_older_than +rc_older_than@@RC_1.0 rc_runlevel_exists rc_runlevel_exists@@RC_1.0 rc_runlevel_get -- cgit v1.2.3