Made rename/create logbook work with groups

SVN revision: 929
This commit is contained in:
Stefan Ritt 2004-06-29 18:57:23 +00:00
parent d62528e792
commit 42ff4ea3a3

View File

@ -6,6 +6,9 @@
Contents: Web server program for Electronic Logbook ELOG Contents: Web server program for Electronic Logbook ELOG
$Log$ $Log$
Revision 1.362 2004/06/29 18:57:23 midas
Made rename/create logbook work with groups
Revision 1.361 2004/06/28 20:29:49 midas Revision 1.361 2004/06/28 20:29:49 midas
Rename logbook in groups on logbook rename Rename logbook in groups on logbook rename
@ -1740,10 +1743,18 @@ time_t cfgfile_mtime = 0;
/*-------------------------------------------------------------------*/ /*-------------------------------------------------------------------*/
void check_config_file() void check_config_file(BOOL force)
{ {
struct stat cfg_stat; struct stat cfg_stat;
if (force) {
if (cfgbuffer) {
free(cfgbuffer);
cfgbuffer = NULL;
}
return;
}
/* force re-read configuration file if changed */ /* force re-read configuration file if changed */
if (stat(config_file, &cfg_stat) == 0) { if (stat(config_file, &cfg_stat) == 0) {
if (cfgfile_mtime < cfg_stat.st_mtime) { if (cfgfile_mtime < cfg_stat.st_mtime) {
@ -2420,7 +2431,7 @@ time_t date_to_ltime(char *date)
void check_config() void check_config()
{ {
check_config_file(); check_config_file(FALSE);
check_language(); check_language();
} }
@ -5385,11 +5396,45 @@ void change_logbook_in_group(LOGBOOK *lbs, char *new_name)
n = strbreak(grpmembers, grplist, MAX_N_LIST, ","); n = strbreak(grpmembers, grplist, MAX_N_LIST, ",");
for (j = 0; j < n; j++) { for (j = 0; j < n; j++) {
if (strieq(lbs->name, grplist[j])) { if (strieq(lbs->name, grplist[j])) {
if (new_name[0]) { /* rename or remove logbook */
/* rename logbook */ change_config_line(lbs, grpname, lbs->name, new_name);
change_config_line(lbs, grpname, lbs->name, new_name); break;
break; }
} }
}
}
}
/*------------------------------------------------------------------*/
void add_logbook_to_group(LOGBOOK *lbs, char *new_name)
{
int i, j, n, flag;
char str[1000], grpname[256], grpmembers[1000];
char grplist[MAX_N_LIST][NAME_LENGTH];
/* enumerate groups */
for (i = 0;; i++) {
if (!enumcfg("global", grpname, grpmembers, i))
break;
flag = 0;
strlcpy(str, grpname, sizeof(str));
str[9] = 0;
if (strieq(str, "top group"))
flag = 2;
str[5] = 0;
if (strieq(str, "group"))
flag = 1;
if (flag) {
n = strbreak(grpmembers, grplist, MAX_N_LIST, ",");
for (j = 0; j < n; j++) {
if (strieq(lbs->name, grplist[j])) {
/* rename or remove logbook */
change_config_line(lbs, grpname, "", new_name);
break;
} }
} }
} }
@ -8003,7 +8048,7 @@ int save_admin_config(char *section, char *buffer, char *error)
free(buf); free(buf);
/* force re-read of config file */ /* force re-read of config file */
check_config(); check_config_file(TRUE);
return 1; return 1;
} }
@ -8128,14 +8173,14 @@ int change_config_line(LOGBOOK * lbs, char *option, char *old_value, char *new_v
free(buf); free(buf);
/* force re-read of config file */ /* force re-read of config file */
check_config(); check_config_file(TRUE);
return 1; return 1;
} }
/*------------------------------------------------------------------*/ /*------------------------------------------------------------------*/
int delete_logbook(char *logbook, char *error) int delete_logbook(LOGBOOK *lbs, char *error)
{ {
int fh, i, length; int fh, i, length;
char *buf, *p1, *p2; char *buf, *p1, *p2;
@ -8150,6 +8195,9 @@ int delete_logbook(char *logbook, char *error)
return 0; return 0;
} }
/* remove logbook name in groups */
change_logbook_in_group(lbs, "");
/* read previous contents */ /* read previous contents */
length = lseek(fh, 0, SEEK_END); length = lseek(fh, 0, SEEK_END);
lseek(fh, 0, SEEK_SET); lseek(fh, 0, SEEK_SET);
@ -8159,7 +8207,7 @@ int delete_logbook(char *logbook, char *error)
buf[length] = 0; buf[length] = 0;
/* find logbook config */ /* find logbook config */
p1 = (char *) find_section(buf, logbook); p1 = (char *) find_section(buf, lbs->name);
p2 = (char *) find_next_section(p1 + 1); p2 = (char *) find_next_section(p1 + 1);
if (p2) if (p2)
@ -8188,7 +8236,7 @@ int delete_logbook(char *logbook, char *error)
free(buf); free(buf);
/* force re-read of config file */ /* force re-read of config file */
check_config(); check_config_file(TRUE);
el_index_logbooks(TRUE); el_index_logbooks(TRUE);
return 1; return 1;
@ -8196,19 +8244,18 @@ int delete_logbook(char *logbook, char *error)
/*------------------------------------------------------------------*/ /*------------------------------------------------------------------*/
int rename_logbook(LOGBOOK *lbs, char *new_name, char *error) int rename_logbook(LOGBOOK *lbs, char *new_name)
{ {
int fh, i, length; int fh, i, length;
char *buf, *buf2, *p1, *p2; char *buf, *buf2, *p1, *p2;
char str[256], lb_dir[256], old_dir[256], new_dir[256]; char str[256], lb_dir[256], old_dir[256], new_dir[256];
error[0] = 0;
fh = open(config_file, O_RDWR | O_BINARY, 644); fh = open(config_file, O_RDWR | O_BINARY, 644);
if (fh < 0) { if (fh < 0) {
sprintf(error, loc("Cannot open file <b>%s</b>"), config_file); sprintf(str, loc("Cannot open file <b>%s</b>"), config_file);
strcat(error, ": "); strcat(str, ": ");
strcat(error, strerror(errno)); strcat(str, strerror(errno));
show_error(str);
return 0; return 0;
} }
@ -8240,7 +8287,7 @@ int rename_logbook(LOGBOOK *lbs, char *new_name, char *error)
if (p2 == NULL) { if (p2 == NULL) {
close(fh); close(fh);
free(buf); free(buf);
strcpy(error, loc("Syntax error in config file")); show_error(loc("Syntax error in config file"));
return 0; return 0;
} }
p2 ++; p2 ++;
@ -8266,9 +8313,10 @@ int rename_logbook(LOGBOOK *lbs, char *new_name, char *error)
lseek(fh, 0, SEEK_SET); lseek(fh, 0, SEEK_SET);
i = write(fh, buf, strlen(buf)); i = write(fh, buf, strlen(buf));
if (i < (int) strlen(buf)) { if (i < (int) strlen(buf)) {
sprintf(error, loc("Cannot write to <b>%s</b>"), config_file); sprintf(str, loc("Cannot write to <b>%s</b>"), config_file);
strcat(error, ": "); strcat(str, ": ");
strcat(error, strerror(errno)); strcat(str, strerror(errno));
show_error(str);
close(fh); close(fh);
free(buf); free(buf);
return 0; return 0;
@ -8282,9 +8330,8 @@ int rename_logbook(LOGBOOK *lbs, char *new_name, char *error)
close(fh); close(fh);
free(buf); free(buf);
/* force re-read of config file */ /* force re-read of config file */
check_config(); check_config_file(TRUE);
el_index_logbooks(TRUE); el_index_logbooks(TRUE);
return 1; return 1;
@ -8292,21 +8339,23 @@ int rename_logbook(LOGBOOK *lbs, char *new_name, char *error)
/*------------------------------------------------------------------*/ /*------------------------------------------------------------------*/
int create_logbook(char *logbook, char *templ, char *error) int create_logbook(LOGBOOK *oldlbs, char *logbook, char *templ)
{ {
int fh, i, length, templ_length; int fh, i, length, templ_length;
char *buf, *p1, *p2; char *buf, *p1, *p2, str[256];
error[0] = 0;
fh = open(config_file, O_RDWR | O_BINARY, 644); fh = open(config_file, O_RDWR | O_BINARY, 644);
if (fh < 0) { if (fh < 0) {
sprintf(error, loc("Cannot open file <b>%s</b>"), config_file); sprintf(str, loc("Cannot open file <b>%s</b>"), config_file);
strcat(error, ": "); strcat(str, ": ");
strcat(error, strerror(errno)); strcat(str, strerror(errno));
show_error(str);
return 0; return 0;
} }
/* add logbook to current group */
add_logbook_to_group(oldlbs, logbook);
/* read previous contents */ /* read previous contents */
length = lseek(fh, 0, SEEK_END); length = lseek(fh, 0, SEEK_END);
lseek(fh, 0, SEEK_SET); lseek(fh, 0, SEEK_SET);
@ -8363,9 +8412,10 @@ int create_logbook(char *logbook, char *templ, char *error)
lseek(fh, 0, SEEK_SET); lseek(fh, 0, SEEK_SET);
i = write(fh, buf, strlen(buf)); i = write(fh, buf, strlen(buf));
if (i < (int) strlen(buf)) { if (i < (int) strlen(buf)) {
sprintf(error, loc("Cannot write to <b>%s</b>"), config_file); sprintf(str, loc("Cannot write to <b>%s</b>"), config_file);
strcat(error, ": "); strcat(str, ": ");
strcat(error, strerror(errno)); strcat(str, strerror(errno));
show_error(str);
close(fh); close(fh);
free(buf); free(buf);
return 0; return 0;
@ -8381,7 +8431,7 @@ int create_logbook(char *logbook, char *templ, char *error)
free(buf); free(buf);
/* force re-read of config file */ /* force re-read of config file */
check_config(); check_config_file(TRUE);
el_index_logbooks(TRUE); el_index_logbooks(TRUE);
return 1; return 1;
@ -8428,7 +8478,7 @@ int save_config(char *buffer, char *error)
close(fh); close(fh);
/* force re-read of config file */ /* force re-read of config file */
check_config(); check_config_file(TRUE);
return 1; return 1;
} }
@ -9324,7 +9374,7 @@ void show_logbook_delete(LOGBOOK * lbs)
/* delete logbook */ /* delete logbook */
str[0] = 0; str[0] = 0;
delete_logbook(lbs->name, str); delete_logbook(lbs, str);
if (str[0]) if (str[0])
show_error(str); show_error(str);
else else
@ -9362,18 +9412,25 @@ void show_logbook_delete(LOGBOOK * lbs)
void show_logbook_rename(LOGBOOK * lbs) void show_logbook_rename(LOGBOOK * lbs)
{ {
char str[256]; int i;
char str[256], lbn[256];
/* redirect if confirm = NO */
if (getparam("lbname") && *getparam("lbname")) { if (getparam("lbname") && *getparam("lbname")) {
rename_logbook(lbs, getparam("lbname"), str); /* check if logbook name exists already */
if (str[0]) strcpy(lbn, getparam("lbname"));
show_error(str); for (i = 0; lb_list[i].name[0]; i++)
else { if (strieq(lbn, lb_list[i].name)) {
sprintf(str, "../%s/?cmd=Config", getparam("lbname")); sprintf(str, loc("Logbook \"%s\" exists already, please choose different name"), lbn);
redirect(NULL, str); show_error(str);
return;
} }
if (!rename_logbook(lbs, getparam("lbname")))
return;
sprintf(str, "../%s/?cmd=Config", getparam("lbname"));
redirect(NULL, str);
return; return;
} else { } else {
@ -9411,6 +9468,7 @@ void show_logbook_new(LOGBOOK * lbs)
if (getparam("lbname") && *getparam("lbname")) { if (getparam("lbname") && *getparam("lbname")) {
/* check if logbook name exists already */
strcpy(lbn, getparam("lbname")); strcpy(lbn, getparam("lbname"));
for (i = 0; lb_list[i].name[0]; i++) for (i = 0; lb_list[i].name[0]; i++)
if (strieq(lbn, lb_list[i].name)) { if (strieq(lbn, lb_list[i].name)) {
@ -9420,15 +9478,13 @@ void show_logbook_new(LOGBOOK * lbs)
} }
/* create new logbook */ /* create new logbook */
create_logbook(getparam("lbname"), getparam("template"), str); if (!create_logbook(lbs, getparam("lbname"), getparam("template")))
if (str[0]) return;
show_error(str);
else { strcpy(lbn, getparam("lbname"));
strcpy(lbn, getparam("lbname")); url_encode(lbn, sizeof(lbn));
url_encode(lbn, sizeof(lbn)); sprintf(str, "../%s/?cmd=Config", lbn);
sprintf(str, "../%s/?cmd=Config", lbn); redirect(NULL, str);
redirect(NULL, str);
}
return; return;
} }
@ -14431,7 +14487,7 @@ int add_attribute_option(LOGBOOK * lbs, char *attrname, char *attrvalue)
free(buf); free(buf);
/* force re-read of config file */ /* force re-read of config file */
check_config(); check_config_file(TRUE);
return 1; return 1;
} }
@ -14525,7 +14581,7 @@ int set_attributes(LOGBOOK * lbs, char attributes[][NAME_LENGTH], int n)
free(buf); free(buf);
/* force re-read of config file */ /* force re-read of config file */
check_config(); check_config_file(TRUE);
return 1; return 1;
} }