#include "replace.h"
#include "system/filesys.h"
#include "system/dir.h"
-#include "system/locale.h"
+#include "system/glob.h"
#include "system/wait.h"
#include <talloc.h>
static int script_filter(const struct dirent *de)
{
- size_t namelen = strlen(de->d_name);
- char *ptr;
-
- /* Ignore . and .. */
- if (namelen < 3) {
- return 0;
- }
-
- /* Skip filenames with ~ */
- ptr = strchr(de->d_name, '~');
- if (ptr != NULL) {
- return 0;
- }
-
- /* Filename should start with [0-9][0-9]. */
- if ((! isdigit(de->d_name[0])) ||
- (! isdigit(de->d_name[1])) ||
- (de->d_name[2] != '.')) {
- return 0;
- }
+ int ret;
- /* Ignore filenames with multiple '.'s */
- ptr = index(&de->d_name[3], '.');
- if (ptr != NULL) {
- return 0;
+ /* Match a script pattern */
+ ret = fnmatch("[0-9][0-9].*.script", de->d_name, 0);
+ if (ret == 0) {
+ return 1;
}
- return 1;
+ return 0;
}
static int get_script_list(TALLOC_CTX *mem_ctx,
{
struct dirent **namelist = NULL;
struct run_event_script_list *script_list;
+ size_t ls;
int count, ret;
int i;
goto done;
}
+ ls = strlen(".script");
for (i=0; i<count; i++) {
struct run_event_script *s = &script_list->script[i];
- s->name = talloc_strdup(script_list, namelist[i]->d_name);
+ s->name = talloc_strndup(script_list,
+ namelist[i]->d_name,
+ strlen(namelist[i]->d_name) - ls);
if (s->name == NULL) {
ret = ENOMEM;
talloc_free(script_list);
{
DIR *dirp;
struct dirent *de;
+ char script_file[PATH_MAX];
int ret, new_mode;
char *filename;
struct stat st;
bool found;
int fd = -1;
+ ret = snprintf(script_file,
+ sizeof(script_file),
+ "%s.script",
+ script_name);
+ if (ret >= sizeof(script_file)) {
+ return ENAMETOOLONG;
+ }
+
dirp = opendir(script_dir);
if (dirp == NULL) {
return errno;
found = false;
while ((de = readdir(dirp)) != NULL) {
- if (strcmp(de->d_name, script_name) == 0) {
+ if (strcmp(de->d_name, script_file) == 0) {
/* check for valid script names */
ret = script_filter(de);
return ENOENT;
}
- filename = talloc_asprintf(mem_ctx, "%s/%s", script_dir, script_name);
+ filename = talloc_asprintf(mem_ctx, "%s/%s", script_dir, script_file);
if (filename == NULL) {
return ENOMEM;
}
struct stat st;
char *path = NULL;
- path = talloc_asprintf(mem_ctx, "%s/%s",
+ path = talloc_asprintf(mem_ctx, "%s/%s.script",
run_event_script_dir(run_ctx),
script->name);
if (path == NULL) {
script = &state->script_list->script[state->index];
- path = talloc_asprintf(state, "%s/%s",
+ path = talloc_asprintf(state, "%s/%s.script",
run_event_script_dir(state->run_ctx),
script->name);
if (path == NULL) {