summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/cli_def.l7
-rw-r--r--src/cli_new.c2
-rw-r--r--src/cli_parse.y27
-rw-r--r--src/cli_val.h1
-rw-r--r--src/cli_val_engine.c11
-rw-r--r--src/commit.c136
-rw-r--r--src/delete.c92
-rw-r--r--src/set.c22
8 files changed, 274 insertions, 24 deletions
diff --git a/src/cli_def.l b/src/cli_def.l
index 07c9c68..d73f1c4 100644
--- a/src/cli_def.l
+++ b/src/cli_def.l
@@ -19,8 +19,9 @@ static char str_delim = 0;
static int eof_seen = 0;
static int pre_str_state = 0;
-static char *reg_fields[] = { "default", "tag", "type", "multi", NULL };
-static int reg_fields_t[] = { DEFAULT, TAG, TYPE, MULTI, 0 };
+static char *reg_fields[] = { "default", "tag", "type", "multi", "priority",
+ NULL };
+static int reg_fields_t[] = { DEFAULT, TAG, TYPE, MULTI, PRIORITY, 0 };
static char *act_fields[] = { "help", "syntax", "commit",
"delete", "update", "activate", "create",
"begin", "end",
@@ -262,7 +263,7 @@ RE_OP_COND (==|!=|<|>|<=|>=|in)
RE_OP_OTHER (pattern|exec|,|\|\||&&|=|!|\(|\)|;)
/* template fields */
-RE_REG_FIELD (default|tag|type|multi)
+RE_REG_FIELD (default|tag|type|multi|priority)
RE_ACT_FIELD (help|syntax|commit|delete|update|activate|create|begin|end|comp_help|allowed)
%%
diff --git a/src/cli_new.c b/src/cli_new.c
index 1ecbe83..26cc719 100644
--- a/src/cli_new.c
+++ b/src/cli_new.c
@@ -1435,7 +1435,7 @@ static int expand_string(char *stringp)
if(clind_config_engine_apply_command_path(n_cfg_path,
n_tmpl_path,
n_cmd_path,
- FALSE,
+ TRUE,
&cv,
get_cdirp(),
get_tdirp(),
diff --git a/src/cli_parse.y b/src/cli_parse.y
index 6378bc9..dd3d7d2 100644
--- a/src/cli_parse.y
+++ b/src/cli_parse.y
@@ -1,8 +1,12 @@
%{
-
#include <assert.h>
#include <stdio.h>
#include <string.h>
+#include <errno.h>
+#include <stdlib.h>
+
+#define __USE_ISOC99
+#include <limits.h>
#include "cli_val.h"
@@ -30,6 +34,7 @@ static void cli_deferror(const char *);
%token TYPE
%token HELP
%token DEFAULT
+%token PRIORITY
%token PATTERN
%token EXEC
%token SYNTAX
@@ -101,6 +106,7 @@ type: TYPE TYPE_DEF
cause: help_cause
| default_cause
+ | priority_stmt
| syntax_cause
| ACTION action { append(parse_defp->actions + $1, $2, 0);}
| dummy_stmt
@@ -126,6 +132,25 @@ default_cause: DEFAULT STRING
parse_defp->def_default = $2;
}
+priority_stmt: PRIORITY VALUE
+ {
+ char *tmp = $2.val;
+ long long int cval = 0;
+ char *endp = NULL;
+ errno = 0;
+ cval = strtoll(tmp, &endp, 10);
+ if (($2.val_type != INT_TYPE)
+ || (errno == ERANGE
+ && (cval == LLONG_MAX || cval == LLONG_MIN))
+ || (errno != 0 && cval == 0)
+ || (*endp != '\0') || (cval < 0) || (cval > UINT_MAX)) {
+ yy_cli_parse_error((const char *)
+ "Priority must be <u32>\n");
+ } else {
+ parse_defp->def_priority = cval;
+ }
+ }
+
syntax_cause: SYNTAX exp {append(parse_defp->actions + syntax_act, $2, 0);}
;
diff --git a/src/cli_val.h b/src/cli_val.h
index edbda41..520fa8e 100644
--- a/src/cli_val.h
+++ b/src/cli_val.h
@@ -101,6 +101,7 @@ typedef struct {
char *def_type_help;
char *def_node_help;
char *def_default;
+ unsigned int def_priority;
boolean tag;
boolean multi;
vtw_list actions[top_act];
diff --git a/src/cli_val_engine.c b/src/cli_val_engine.c
index adf40a4..a81f413 100644
--- a/src/cli_val_engine.c
+++ b/src/cli_val_engine.c
@@ -56,6 +56,9 @@
#include "cli_val_engine.h"
+
+static int is_multi_node(clind_path_ref tmpl_path);
+
/*********************
* Data definitions
*
@@ -129,6 +132,13 @@ static char** clind_get_current_value(clind_path_ref cfg_path,
int value_ref = 0;
*ret_size=0;
+
+ if (check_existence) {
+ if (is_multi_node(tmpl_path)) {
+ check_existence = FALSE;
+ }
+ }
+
DPRINT("get_current_value cfg[%s] tmpl[%s] chkexist=%d\n",
clind_path_get_path_string(cfg_path),
clind_path_get_path_string(tmpl_path),
@@ -217,6 +227,7 @@ static char** clind_get_current_value(clind_path_ref cfg_path,
struct stat statbuf;
/* Directory reference: */
+
if(!check_existence || (lstat(cfg_path_string, &statbuf) == 0)) {
ret=(char**)realloc(ret,sizeof(char*)*1);
ret[0]=clind_unescape(cfg_end);
diff --git a/src/commit.c b/src/commit.c
index 534757c..3b4d9d3 100644
--- a/src/commit.c
+++ b/src/commit.c
@@ -15,6 +15,18 @@
#include "cli_parse.h"
#include "cli_path_utils.h"
+struct DirIndex {
+ int dirname_index;
+ int dirname_ct;
+ struct DirSort** dirname;
+};
+
+struct DirSort {
+ char name[255];
+ unsigned long priority;
+};
+
+
static char def_name[] = DEF_NAME;
static char tag_name[] = TAG_NAME;
static char opaque_name[] = OPQ_NAME;
@@ -53,21 +65,101 @@ static void make_dir()
}
#endif
-static struct dirent *
-get_next_filtered_dirent(DIR *dp, int exclude_wh)
+static int
+compare_dirname(const void *p, const void *q)
+{
+ const struct DirSort *a = (const struct DirSort*)*(struct Dirsort **)p;
+ const struct DirSort *b = (const struct DirSort*)*(struct Dirsort **)q;
+ if (a->priority == b->priority) {
+ return strcmp(a->name,b->name);
+ }
+ return ((long)b->priority - (long)a->priority);
+}
+
+static int
+compare_dirname_reverse_priority(const void *p, const void *q)
{
- struct dirent *dirp = NULL;
+ const struct DirSort *a = (const struct DirSort*)*(struct Dirsort **)p;
+ const struct DirSort *b = (const struct DirSort*)*(struct Dirsort **)q;
+ if (a->priority == b->priority) {
+ return strcmp(a->name,b->name);
+ }
+ return ((long)a->priority - (long)b->priority);
+}
+
+struct DirIndex*
+init_next_filtered_dirname(DIR *dp, int sort_order, int exclude_wh)
+{
+ struct DirIndex *di = malloc(sizeof(struct DirIndex));
+ di->dirname = malloc(1024 * sizeof(char*));
+ di->dirname_ct = 0;
+ di->dirname_index = 0;
+
+ struct dirent *dirp;
while ((dirp = readdir(dp)) != NULL) {
if (strcmp(dirp->d_name, ".") == 0 || strcmp(dirp->d_name, "..") == 0
|| strcmp(dirp->d_name, MOD_NAME) == 0
|| strcmp(dirp->d_name, opaque_name) == 0
|| (exclude_wh && strncmp(dirp->d_name, ".wh.", 4) == 0)) {
continue;
- } else {
- return dirp;
+ }
+ else {
+ struct DirSort *d = malloc(sizeof(struct DirSort));
+ if (strlen(dirp->d_name) >= 255) {
+ bye("configuration value exceeds 255 chars\n");
+ }
+ strcpy(d->name,dirp->d_name);
+ d->priority = (unsigned long)0;
+ vtw_def def;
+ char *path;
+ path = malloc(strlen(t_path.path)+strlen(dirp->d_name)+2+8+1);
+ sprintf(path,"%s/%s/node.def",t_path.path,dirp->d_name);
+ struct stat s;
+ if ((lstat(path,&s) >= 0) &&
+ ((s.st_mode & S_IFMT) == S_IFREG)) {
+ memset(&def, 0, sizeof(def));
+ if (parse_def(&def,path,FALSE) == 0) {
+ d->priority = def.def_priority;
+ }
+ }
+ free(path);
+ di->dirname[di->dirname_ct++] = d;
+ if (di->dirname_ct % 1024 == 0) {
+ di->dirname = realloc(di->dirname, (di->dirname_ct+1024)*sizeof(char*));
+ }
}
}
- return dirp;
+ if (sort_order == 0) {
+ qsort(di->dirname, di->dirname_ct, sizeof(char*), compare_dirname);
+ }
+ else {
+ qsort(di->dirname, di->dirname_ct, sizeof(char*), compare_dirname_reverse_priority);
+ }
+ return di;
+}
+
+static char*
+get_next_filtered_dirname(struct DirIndex *di)
+{
+ if (di == NULL || di->dirname_index == di->dirname_ct) {
+ return NULL;
+ }
+ return di->dirname[di->dirname_index++]->name;
+}
+
+void
+release_dir_index(struct DirIndex *di)
+{
+ if (di != NULL) {
+ int i;
+ for (i = 0; i < di->dirname_ct; ++i) {
+ if (di->dirname[i] != NULL) {
+ free((struct DirSort*)di->dirname[i]);
+ }
+ }
+ free(di);
+ di = NULL;
+ }
}
/*************************************************
@@ -88,7 +180,7 @@ static boolean validate_dir_for_commit()
boolean value_present=FALSE;
int subdirs_number=0;
DIR *dp=NULL;
- struct dirent *dirp=NULL;
+ char *dirname = NULL;
char *cp=NULL;
boolean ret=TRUE;
char *uename = NULL;
@@ -130,14 +222,14 @@ static boolean validate_dir_for_commit()
if (def_present && def.tag) {
push_path(&t_path, tag_name); /* PUSH 2a */
}
-
- while ((dirp = get_next_filtered_dirent(dp, 1)) != NULL) {
+ struct DirIndex *di = init_next_filtered_dirname(dp,0,1);
+ while ((dirname = get_next_filtered_dirname(di)) != NULL) {
subdirs_number++;
if(uename)
my_free(uename);
- uename = clind_unescape(dirp->d_name);
+ uename = clind_unescape(dirname);
if (strcmp(uename, VAL_NAME) == 0) {
@@ -197,6 +289,11 @@ static boolean validate_dir_for_commit()
continue;
}
+ if (strcmp(uename,"def") == 0) {
+ ret = TRUE;
+ continue;
+ }
+
push_path(&m_path, uename); /* PUSH 3 */
if (lstat(m_path.path, &statbuf) < 0) {
printf("Can't read directory %s\n",
@@ -251,6 +348,7 @@ static boolean validate_dir_for_commit()
pop_path(&t_path); /* for PUSH 2b */
} // while
+ release_dir_index(di);
status = closedir(dp);
if (status)
bye("Cannot close dir %s\n", m_path.path);
@@ -960,16 +1058,18 @@ valstruct_append(valstruct *mvals, char *cp, vtw_type_e type)
*/
static void
get_filtered_directory_listing(DIR *dp, valstruct *mvals, vtw_type_e type,
- int exclude_wh)
+ int exclude_wh, int sort_order)
{
- struct dirent *dirp = NULL;
+ char *dname = NULL;
char *cp = NULL;
memset(mvals, 0, sizeof (valstruct));
- while ((dirp = get_next_filtered_dirent(dp, exclude_wh)) != NULL) {
- cp = clind_unescape(dirp->d_name);
+ struct DirIndex *di = init_next_filtered_dirname(dp, sort_order, exclude_wh);
+ while ((dname = get_next_filtered_dirname(di)) != NULL) {
+ cp = clind_unescape(dname);
valstruct_append(mvals, cp, type);
}
+ release_dir_index(di);
}
/* check if a value is one of those in a valstruct.
@@ -1073,19 +1173,19 @@ static boolean commit_delete_children(vtw_def *defp, boolean deleting,
memset(&cur_sorted, 0, sizeof(vtw_sorted));
/* changes directory */
- get_filtered_directory_listing(dp, &mvals, type, 0);
+ get_filtered_directory_listing(dp, &mvals, type, 0, 1);
if (closedir(dp) != 0) {
INTERNAL;
}
if (adp && mdp) {
/* active directory */
- get_filtered_directory_listing(adp, &valsA, type, 0);
+ get_filtered_directory_listing(adp, &valsA, type, 0, 1);
if (closedir(adp) != 0) {
INTERNAL;
}
/* modified directory */
- get_filtered_directory_listing(mdp, &valsM, type, 0);
+ get_filtered_directory_listing(mdp, &valsM, type, 0, 1);
if (closedir(mdp) != 0) {
INTERNAL;
}
@@ -1164,7 +1264,7 @@ static boolean commit_update_children(vtw_def *defp, boolean creating,
if (type == ERROR_TYPE)
type = TEXT_TYPE;
- get_filtered_directory_listing(dp, &mvals, type, 0);
+ get_filtered_directory_listing(dp, &mvals, type, 0, 0);
if (closedir(dp) != 0) {
INTERNAL;
}
diff --git a/src/delete.c b/src/delete.c
index bb36fd1..9bdb3ed 100644
--- a/src/delete.c
+++ b/src/delete.c
@@ -34,6 +34,53 @@ static void remove_rf(boolean do_umount)
free(command);
}
}
+static boolean has_default(char **def, int size)
+{
+ char *buf;
+ buf = malloc(1025);
+ char *buf_ptr;
+ FILE *fp = fopen(t_path.path, "r");
+ if (fp) {
+ while (fgets(buf, 1024, fp)) {
+ if (strncmp(buf, "default:", 8) == 0) {
+ buf_ptr = index(buf,':');
+ if (buf_ptr == NULL) {
+ break;
+ }
+ buf_ptr++;
+ if (size < strlen(buf_ptr)-1) {
+ bye("default buffer size is too small\n");
+ }
+ memcpy(*def, buf_ptr, strlen(buf_ptr)-1);
+ fclose(fp);
+ free(buf);
+ return 0;
+ }
+ }
+ fclose(fp);
+ }
+ free(buf);
+ return 1;
+}
+static void reset_default(char *def_val)
+{
+ char *command,*def_cmd;
+ boolean has_default = 1;
+ if (def_val == NULL) {
+ return;
+ }
+ if (has_default) {
+ touch();
+ command = my_malloc(strlen(m_path.path) + 100, "set");
+ sprintf(command, "echo %s > %s/node.val", def_val, m_path.path);
+ system(command);
+ def_cmd = malloc(strlen(m_path.path) + 12);
+ sprintf(def_cmd, "touch %s/def",m_path.path);
+ system(def_cmd);
+ free(command);
+ free(def_cmd);
+ }
+}
/***************************************************
set_validate:
validate value against definition
@@ -139,6 +186,7 @@ int main(int argc, char **argv)
fprintf(out_stream, "Nothing to delete\n");
bye("Nothing to delete at %s", m_path.path);
}
+
remove_rf(FALSE);
pop_path(&m_path);
if ((dp = opendir(m_path.path)) == NULL){
@@ -187,7 +235,17 @@ int main(int argc, char **argv)
}
}
/* else no defnition, remove it also */
- remove_rf(FALSE);
+ char *def_val;
+ def_val = malloc(1025);
+ if (has_default(&def_val,1024) == 0) {
+ reset_default(def_val);
+ free(def_val);
+ }
+ else {
+ remove_rf(FALSE);
+ }
+
+ // remove_rf(FALSE);
exit(0);
}
if(ai < argc -1 || last_tag) {
@@ -267,6 +325,38 @@ int main(int argc, char **argv)
remove_rf(FALSE);
return 0;
}
+
+ /*
+ let's do a new check here:
+ -> if this is a leaf and there is a value look for a match of the value
+ -> make sure to check existing configuration as well as uncommitted config
+ */
+ if (ai+1 == argc) {
+ //does this work up until the last value
+ pop_path(&m_path);
+ if(lstat(m_path.path, &statbuf) == 0) {
+ //now compare last value with that in the node.def file to determine whether to accept this delete
+ status = get_value(&cp, &m_path);
+ if (status != VTWERR_OK) {
+ bye("Cannot read old value %s\n", m_path.path);
+ }
+ if (!strcmp(cp,argv[argc - 1])) {
+ /* Also need to handle the case where the value is not specified. */
+ char *def_val;
+ def_val = malloc(1025);
+ if (has_default(&def_val,1024) == 0) {
+ reset_default(def_val);
+ free(def_val);
+ }
+ else {
+ remove_rf(FALSE);
+ }
+ return 0;
+ }
+ }
+ }
+
+
fprintf(out_stream, "The specified configuration node is not valid\n");
bye("There is no appropriate template for %s",
m_path.path + strlen(get_mdirp()));
diff --git a/src/set.c b/src/set.c
index d961fb8..ed43267 100644
--- a/src/set.c
+++ b/src/set.c
@@ -245,6 +245,16 @@ int main(int argc, char **argv)
pop_path(&m_path);
}
make_dir();
+
+
+ if (!def.multi) {
+ char *path;
+ path= malloc(strlen(m_path.path)+5);
+ sprintf(path, "%s/def",m_path.path);
+ unlink(path);
+ free(path);
+ }
+
push_path(&m_path, VAL_NAME);
if(not_new && !def.multi) {
/* it is not multi and seen from M */
@@ -341,11 +351,22 @@ handle_default(vtw_path *mpath, vtw_path *tpath, char *exclude)
}
if (def.def_default) {
push_path(mpath, uename);
+
push_path(mpath, VAL_NAME);
if (lstat(mpath->path, &statbuf) < 0) {
/* no value. add the default */
pop_path(mpath);
touch_dir(mpath->path); /* make sure directory exist */
+
+ //create def marker
+ char *def_file;
+ def_file = malloc(strlen(mpath->path)+22);
+ sprintf(def_file,"touch %s/def",mpath->path);
+ system(def_file);
+ sprintf(def_file,"echo 'empty' > %s/def",mpath->path);
+ system(def_file);
+ free(def_file);
+
push_path(mpath, VAL_NAME);
fp = fopen(mpath->path, "w");
if (fp == NULL) {
@@ -356,6 +377,7 @@ handle_default(vtw_path *mpath, vtw_path *tpath, char *exclude)
bye("Error writing file %s", mpath->path);
}
fclose(fp);
+
}
pop_path(mpath); /* value */
pop_path(mpath); /* child */