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.

This commit is contained in:
Roy Marples 2008-07-03 13:11:47 +00:00
parent d61f831896
commit a88a177f99
7 changed files with 50 additions and 16 deletions

View File

@ -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;

View File

@ -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)

View File

@ -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.

View File

@ -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;

View File

@ -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'))

View File

@ -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

View File

@ -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