Continued working on synchronization

SVN revision: 696
This commit is contained in:
Stefan Ritt 2004-01-19 16:22:28 +00:00
parent 95395f5187
commit 7bf9695573

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.200 2004/01/19 16:22:28 midas
Continued working on synchronization
Revision 1.199 2004/01/19 09:13:28 midas Revision 1.199 2004/01/19 09:13:28 midas
Removed '{n}' in quick filter and find page list Removed '{n}' in quick filter and find page list
@ -3179,7 +3182,7 @@ INT el_retrieve_attachment(LOGBOOK * lbs, int message_id, int n,
/*------------------------------------------------------------------*/ /*------------------------------------------------------------------*/
int el_submit(LOGBOOK * lbs, int message_id, int el_submit(LOGBOOK * lbs, int message_id, BOOL bedit,
char *date, char *date,
char attr_name[MAX_N_ATTR][NAME_LENGTH], char attr_name[MAX_N_ATTR][NAME_LENGTH],
char attr_value[MAX_N_ATTR][NAME_LENGTH], char attr_value[MAX_N_ATTR][NAME_LENGTH],
@ -3195,8 +3198,8 @@ int el_submit(LOGBOOK * lbs, int message_id,
Input: Input:
LOGBOOK lbs Logbook structure LOGBOOK lbs Logbook structure
int message_id Message id for existing message, 0 for int message_id Message id
new message BOOL bedit TRUE for existing message, FALSE for new message
char *date Message date char *date Message date
char attr_name[][] Name of attributes char attr_name[][] Name of attributes
char attr_value[][] Value of attributes char attr_value[][] Value of attributes
@ -3223,14 +3226,11 @@ int el_submit(LOGBOOK * lbs, int message_id,
char file_name[256], dir[256], str[NAME_LENGTH]; char file_name[256], dir[256], str[NAME_LENGTH];
time_t now, ltime; time_t now, ltime;
char message[TEXT_SIZE + 100], *p, *buffer; char message[TEXT_SIZE + 100], *p, *buffer;
BOOL bedit;
char attachment_all[64 * MAX_ATTACHMENTS]; char attachment_all[64 * MAX_ATTACHMENTS];
/* generate new file name YYMMDD.log in data directory */ /* generate new file name YYMMDD.log in data directory */
strcpy(dir, lbs->data_dir); strcpy(dir, lbs->data_dir);
bedit = (message_id > 0);
if (bedit) { if (bedit) {
/* edit existing message */ /* edit existing message */
for (index = 0; index < *lbs->n_el_index; index++) for (index = 0; index < *lbs->n_el_index; index++)
@ -3255,7 +3255,7 @@ int el_submit(LOGBOOK * lbs, int message_id,
/* file might have been edited, rebuild index */ /* file might have been edited, rebuild index */
el_build_index(lbs, TRUE); el_build_index(lbs, TRUE);
return el_submit(lbs, message_id, date, attr_name, attr_value, return el_submit(lbs, message_id, bedit, date, attr_name, attr_value,
n_attr, text, in_reply_to, reply_to, encoding, afilename, TRUE, n_attr, text, in_reply_to, reply_to, encoding, afilename, TRUE,
locked_by); locked_by);
} }
@ -3337,10 +3337,12 @@ int el_submit(LOGBOOK * lbs, int message_id,
lseek(fh, 0, SEEK_END); lseek(fh, 0, SEEK_END);
/* new message id is old plus one */ /* new message id is old plus one */
message_id = 1; if (message_id == 0) {
for (i = 0; i < *lbs->n_el_index; i++) message_id = 1;
if (lbs->el_index[i].message_id >= message_id) for (i = 0; i < *lbs->n_el_index; i++)
message_id = lbs->el_index[i].message_id + 1; if (lbs->el_index[i].message_id >= message_id)
message_id = lbs->el_index[i].message_id + 1;
}
/* enter message in index */ /* enter message in index */
index = *lbs->n_el_index; index = *lbs->n_el_index;
@ -3445,7 +3447,7 @@ int el_submit(LOGBOOK * lbs, int message_id,
sprintf(reply_to + strlen(reply_to), "%d", message_id); sprintf(reply_to + strlen(reply_to), "%d", message_id);
/* write modified message */ /* write modified message */
el_submit(lbs, reply_id, date, attr_list, attr, n_attr, message, in_reply_to, el_submit(lbs, reply_id, TRUE, date, attr_list, attr, n_attr, message, in_reply_to,
reply_to, enc, att, TRUE, lock); reply_to, enc, att, TRUE, lock);
} }
@ -3493,8 +3495,8 @@ void remove_reference(LOGBOOK * lbs, int message_id, int remove_id, BOOL reply_t
} }
/* write modified message */ /* write modified message */
el_submit(lbs, message_id, date, attr_list, attr, lbs->n_attr, message, in_reply_to, el_submit(lbs, message_id, TRUE, date, attr_list, attr, lbs->n_attr, message,
reply_to, enc, att, TRUE, lock); in_reply_to, reply_to, enc, att, TRUE, lock);
} }
/*------------------------------------------------------------------*/ /*------------------------------------------------------------------*/
@ -3740,7 +3742,7 @@ This routine corrects that. */
strcat(reply_to, ", "); strcat(reply_to, ", ");
} }
el_submit(lbs, atoi(list[i]), date, attr_list, attrib, lbs->n_attr, el_submit(lbs, atoi(list[i]), TRUE, date, attr_list, attrib, lbs->n_attr,
text, in_reply_to, reply_to, encoding, att_file, TRUE, locked_by); text, in_reply_to, reply_to, encoding, att_file, TRUE, locked_by);
} }
@ -3767,7 +3769,7 @@ This routine corrects that. */
strcat(in_reply_to, ", "); strcat(in_reply_to, ", ");
} }
el_submit(lbs, atoi(list[i]), date, attr_list, attrib, lbs->n_attr, el_submit(lbs, atoi(list[i]), TRUE, date, attr_list, attrib, lbs->n_attr,
text, in_reply_to, reply_to, encoding, att_file, TRUE, locked_by); text, in_reply_to, reply_to, encoding, att_file, TRUE, locked_by);
} }
@ -3791,7 +3793,7 @@ int el_move_message_thread(LOGBOOK * lbs, int message_id)
/* submit as new message */ /* submit as new message */
date[0] = 0; date[0] = 0;
new_id = el_submit(lbs, 0, date, attr_list, attrib, lbs->n_attr, text, new_id = el_submit(lbs, 0, FALSE, date, attr_list, attrib, lbs->n_attr, text,
in_reply_to, reply_to, encoding, att_file, FALSE, locked_by); in_reply_to, reply_to, encoding, att_file, FALSE, locked_by);
/* correct links */ /* correct links */
@ -3828,8 +3830,8 @@ int el_lock_message(LOGBOOK * lbs, int message_id, char *user)
text, &size, in_reply_to, reply_to, att_file, encoding, locked_by); text, &size, in_reply_to, reply_to, att_file, encoding, locked_by);
/* submit message, unlocked if user==NULL */ /* submit message, unlocked if user==NULL */
el_submit(lbs, message_id, date, attr_list, attrib, lbs->n_attr, text, in_reply_to, el_submit(lbs, message_id, TRUE, date, attr_list, attrib, lbs->n_attr, text,
reply_to, encoding, att_file, FALSE, user); in_reply_to, reply_to, encoding, att_file, FALSE, user);
return EL_SUCCESS; return EL_SUCCESS;
} }
@ -6639,8 +6641,7 @@ void show_find_form(LOGBOOK * lbs)
option); option);
if (comment[0]) if (comment[0])
rsprintf("<img src=\"icons/%s\" alt=\"%s\"></nobr>\n", rsprintf("<img src=\"icons/%s\" alt=\"%s\"></nobr>\n", option, comment);
option, comment);
else else
rsprintf("<img src=\"icons/%s\"></nobr>\n", option); rsprintf("<img src=\"icons/%s\"></nobr>\n", option);
} }
@ -8084,6 +8085,10 @@ int submit_message(LOGBOOK * lbs, char *url, int message_id, char *error_str)
strcpy(content, boundary); strcpy(content, boundary);
strcat(content, "\r\nContent-Disposition: form-data; name=\"cmd\"\r\n\r\nSubmit\r\n"); strcat(content, "\r\nContent-Disposition: form-data; name=\"cmd\"\r\n\r\nSubmit\r\n");
sprintf(content + strlen(content),
"%s\r\nContent-Disposition: form-data; name=\"mirror_id\"\r\n\r\n%d\r\n",
boundary, message_id);
if (isparam("unm")) if (isparam("unm"))
sprintf(content + strlen(content), sprintf(content + strlen(content),
"%s\r\nContent-Disposition: form-data; name=\"unm\"\r\n\r\n%s\r\n", "%s\r\nContent-Disposition: form-data; name=\"unm\"\r\n\r\n%s\r\n",
@ -8110,7 +8115,7 @@ int submit_message(LOGBOOK * lbs, char *url, int message_id, char *error_str)
attr_list[i], attrib[i]); attr_list[i], attrib[i]);
sprintf(content + strlen(content), sprintf(content + strlen(content),
"%s\r\nContent-Disposition: form-data; name=\"remote_date\"\r\n\r\n%s\r\n", "%s\r\nContent-Disposition: form-data; name=\"entry_date\"\r\n\r\n%s\r\n",
boundary, date); boundary, date);
sprintf(content + strlen(content), sprintf(content + strlen(content),
@ -8250,6 +8255,13 @@ int submit_message(LOGBOOK * lbs, char *url, int message_id, char *error_str)
/*------------------------------------------------------------------*/ /*------------------------------------------------------------------*/
int receive_message(LOGBOOK * lbs, char *url, int message_id, char *error_str)
{
return 1;
}
/*------------------------------------------------------------------*/
int save_md5(char *server, MD5_INDEX * md5_index, int n) int save_md5(char *server, MD5_INDEX * md5_index, int n)
{ {
char str[256], file_name[256]; char str[256], file_name[256];
@ -8288,11 +8300,80 @@ int save_md5(char *server, MD5_INDEX * md5_index, int n)
/*------------------------------------------------------------------*/ /*------------------------------------------------------------------*/
int load_md5(char *server, MD5_INDEX ** md5_index)
{
char str[256], file_name[256], *p;
int i, j;
FILE *f;
strlcpy(file_name, resource_dir, sizeof(file_name));
strcpy(str, server);
if (strstr(str, "http://"))
strcpy(str, server + 7);
for (i = 0; i < (int) strlen(str); i++)
if (strchr(":/\\", str[i]))
str[i] = '_';
while (str[strlen(str) - 1] == '_')
str[strlen(str) - 1] = 0;
strlcat(file_name, str, sizeof(file_name));
strlcat(file_name, ".md5", sizeof(file_name));
f = fopen(file_name, "rt");
if (f == NULL)
return 0;
for (i = 0; !feof(f); i++) {
str[0] = 0;
fgets(str, sizeof(str), f);
if (!str[0])
break;
if (i == 0)
*md5_index = calloc(sizeof(MD5_INDEX), 1);
else
*md5_index = realloc(md5_index, sizeof(MD5_INDEX) * (i + 1));
p = str + 2;
(*md5_index)[i].message_id = atoi(p);
while (*p && *p != ' ')
p++;
while (*p && *p == ' ')
p++;
for (j = 0; j < 16; j++)
sscanf(p + j * 2, "%02X", (*md5_index)[i].md5_digest[j]);
}
fclose(f);
return i;
}
/*------------------------------------------------------------------*/
BOOL equal_md5(unsigned char m1[16], unsigned char m2[16])
{
int i;
for (i = 0; i < 16; i++)
if (m1[i] != m2[i])
break;
return i == 16;
}
/*------------------------------------------------------------------*/
void synchronize(LOGBOOK * lbs, char *path) void synchronize(LOGBOOK * lbs, char *path)
{ {
int index, i, j, k, n, nserver, priority_remote = 0; int index, i, i_remote, i_cache, n_remote, n_cache, nserver,
remote_id, exist_remote, exist_cache, message_id;
int priority_remote = 0;
char str[2000]; char str[2000];
MD5_INDEX *md5_index; MD5_INDEX *md5_remote, *md5_cache;
char list[MAX_N_LIST][NAME_LENGTH], error_str[256]; char list[MAX_N_LIST][NAME_LENGTH], error_str[256];
if (!getcfg(lbs->name, "Mirror server", str)) { if (!getcfg(lbs->name, "Mirror server", str)) {
@ -8320,77 +8401,112 @@ void synchronize(LOGBOOK * lbs, char *path)
rsprintf("<table width=\"100%%\" cellpadding=1 cellspacing=0"); rsprintf("<table width=\"100%%\" cellpadding=1 cellspacing=0");
rsprintf("<tr><td class=\"title1\">%s <b>%s</b></td></tr>\n", rsprintf("<tr><td class=\"title1\">%s <b>%s</b></td></tr>\n",
loc("Synchronizing with"), list[index]); loc("Synchronizing with"), list[index]);
rsprintf("</table><p>\n"); rsprintf("</table><p><pre>\n");
/* send partial return buffer */ /* send partial return buffer */
flush_return_buffer(); flush_return_buffer();
sprintf(str, "%s?cmd=GetMD5", list[index]); sprintf(str, "%s?cmd=GetMD5", list[index]);
n = retrieve_remote_md5(str, &md5_index); n_remote = retrieve_remote_md5(str, &md5_remote);
if (n < 0) { if (n_remote < 0) {
free(md5_index); rsprintf("Error receiving remote message list from \"%s\"\n", list[index]);
free(md5_remote);
continue; continue;
} }
/* load local copy of remote MD5s from file */
n_cache = load_md5(list[index], &md5_cache);
/* loop through logbook entries */ /* loop through logbook entries */
for (i = 0; i < *lbs->n_el_index; i++) { for (i = 0; i < *lbs->n_el_index; i++) {
rsprintf("ID: %6d ", lbs->el_index[i].message_id);
for (j = 0; j < n; j++) message_id = lbs->el_index[i].message_id;
if (md5_index[j].message_id == lbs->el_index[i].message_id) rsprintf("ID%d: ", message_id);
/* look for message id in MD5s */
for (i_remote = 0; i_remote < n_remote; i_remote++)
if (md5_remote[i_remote].message_id == message_id)
break; break;
exist_remote = i_remote < n_remote;
if (j < n) { for (i_cache = 0; i_cache < n_cache; i_cache++)
/* message found, compare MD5 */ if (md5_cache[i_cache].message_id == message_id)
break;
exist_cache = i_cache < n_cache;
for (k = 0; k < 16; k++) /* if message exists in both lists, compare MD5s */
if (md5_index[j].md5_digest[k] != lbs->el_index[j].md5_digest[k]) if (exist_remote && exist_cache) {
break;
if (k < 16) { /* if message has been changed on this server, but not remotely, send it */
if (priority_remote) { if (!equal_md5(md5_cache[i_cache].md5_digest, lbs->el_index[index].md5_digest)
/* retrieve remote message */ && equal_md5(md5_cache[i_cache].md5_digest,
rsprintf("Retrieve remote message<br>\n"); md5_remote[i_remote].md5_digest)) {
} else {
/* submit local message */
submit_message(lbs, list[index], lbs->el_index[i].message_id,
error_str);
if (error_str[0]) /* submit local message */
rsprintf("Error sending local message: %s<br>\n", error_str); submit_message(lbs, list[index], message_id, error_str);
else
rsprintf("Local message submitted<br>\n");
}
} else {
rsprintf("%s<br>\n", loc("Message identical"));
}
} else {
/* message not found remotely */
if (priority_remote) {
/* delete local message */
rsprintf("Delete local message<br>\n");
} else {
/* submit message */
submit_message(lbs, list[index], lbs->el_index[i].message_id, error_str);
if (error_str[0]) if (error_str[0])
rsprintf("Error sending local message: %s<br>\n", error_str); rsprintf("Error sending local message: %s\n", error_str);
else else
rsprintf("Local message submitted<br>\n"); rsprintf("Local message submitted\n");
}
} else
/* if message has been changed remotely, but not on this server, receive it */
if (!equal_md5
(md5_cache[i_cache].md5_digest, md5_remote[i_remote].md5_digest)
&& equal_md5(md5_cache[i_cache].md5_digest,
lbs->el_index[index].md5_digest)) {
receive_message(lbs, list[index], message_id, error_str);
} else
/* if message has been changed remotely and on this server, show conflict */
if (!equal_md5
(md5_cache[i_cache].md5_digest, md5_remote[i_remote].md5_digest)
&& !equal_md5(md5_cache[i_cache].md5_digest,
lbs->el_index[index].md5_digest)) {
rsprintf("%s\n",
loc
("Message has been changed locally and remotely, please delete one to resolve conflict"));
} else
rsprintf("%s\n", loc("Message identical"));
} }
/* if message exists only in cache, but not remotely,
it must have been deleted remotely, so remove it locally */
if (exist_cache && !exist_remote) {
el_delete_message(lbs, message_id, TRUE, NULL, TRUE, TRUE);
rsprintf("%s\n", loc("Message has been deleted locally"));
}
/* if message does not exist in cache and remotely,
it must be new, so send it */
if (!exist_cache && !exist_remote) {
remote_id =
submit_message(lbs, list[index], lbs->el_index[i].message_id, error_str);
if (error_str[0])
rsprintf("%s: %s\n", loc("Error sending local message"), error_str);
else
rsprintf("%s\n", loc("Local message submitted"));
}
flush_return_buffer();
} }
free(md5_index); free(md5_remote);
free(md5_cache);
/* save remote MD5s in file */ /* save remote MD5s in file */
sprintf(str, "%s?cmd=GetMD5", list[index]); sprintf(str, "%s?cmd=GetMD5", list[index]);
n = retrieve_remote_md5(str, &md5_index); n_remote = retrieve_remote_md5(str, &md5_remote);
save_md5(list[index], md5_index, n); save_md5(list[index], md5_remote, n_remote);
free(md5_index); free(md5_remote);
} }
rsprintf("</body></html>\n"); rsprintf("</pre></body></html>\n");
flush_return_buffer(); flush_return_buffer();
keep_alive = 0; keep_alive = 0;
} }
@ -9053,6 +9169,9 @@ BOOL is_command_allowed(LOGBOOK * lbs, char *command)
strcat(menu_str, "Admin, "); strcat(menu_str, "Admin, ");
strcat(menu_str, "Change elogd.cfg, "); strcat(menu_str, "Change elogd.cfg, ");
if (getcfg(lbs->name, "Mirror server", str))
strcat(menu_str, "Synchronize, ");
if (!getcfg("global", "Admin user", str) if (!getcfg("global", "Admin user", str)
|| strstr(str, getparam("unm")) != 0) { || strstr(str, getparam("unm")) != 0) {
@ -9072,6 +9191,8 @@ BOOL is_command_allowed(LOGBOOK * lbs, char *command)
} }
strcat(menu_str, "Config, Logout, "); strcat(menu_str, "Config, Logout, ");
} else { } else {
if (getcfg(lbs->name, "Mirror server", str))
strcat(menu_str, "Synchronize, ");
strcat(menu_str, "Config, "); strcat(menu_str, "Config, ");
strcat(menu_str, "Change [global]"); strcat(menu_str, "Change [global]");
strcat(menu_str, ", "); strcat(menu_str, ", ");
@ -9348,11 +9469,9 @@ void show_page_filters(LOGBOOK * lbs, int n_msg, int page_n, int n_page, BOOL to
if (isparam(attr_list[i]) if (isparam(attr_list[i])
&& equal_ustring(option, getparam(attr_list[i]))) && equal_ustring(option, getparam(attr_list[i])))
rsprintf("<option selected value=\"%s\">%s\n", option, rsprintf("<option selected value=\"%s\">%s\n", option, comment);
comment);
else else
rsprintf("<option value=\"%s\">%s\n", option, rsprintf("<option value=\"%s\">%s\n", option, comment);
comment);
} }
} }
@ -10147,6 +10266,9 @@ void show_elog_list(LOGBOOK * lbs, INT past_n, INT last_n, INT page_n)
else else
strcat(menu_str, "Config, "); strcat(menu_str, "Config, ");
if (getcfg(lbs->name, "Mirror server", str))
strcat(menu_str, "Synchronize, ");
strcat(menu_str, "Last x, Help"); strcat(menu_str, "Last x, Help");
} }
@ -10855,6 +10977,7 @@ void submit_elog(LOGBOOK * lbs)
char slist[MAX_N_ATTR + 10][NAME_LENGTH], svalue[MAX_N_ATTR + 10][NAME_LENGTH]; char slist[MAX_N_ATTR + 10][NAME_LENGTH], svalue[MAX_N_ATTR + 10][NAME_LENGTH];
int i, j, n, missing, first, index, mindex, suppress, message_id, resubmit_orig, int i, j, n, missing, first, index, mindex, suppress, message_id, resubmit_orig,
mail_to_size; mail_to_size;
BOOL bedit;
/* check for required attributs */ /* check for required attributs */
missing = 0; missing = 0;
@ -10977,9 +11100,23 @@ void submit_elog(LOGBOOK * lbs)
in_reply_to[0] = 0; in_reply_to[0] = 0;
date[0] = 0; date[0] = 0;
resubmit_orig = 0; resubmit_orig = 0;
bedit = FALSE;
if (isparam("remote_date")) /* evaluate parameters from mirror submission */
strcpy(date, getparam("remote_date")); //## test!! if (isparam("mirror_id")) {
message_id = atoi(getparam("mirror_id"));
/* check if message already exists */
for (i = 0; i < *lbs->n_el_index; i++)
if (lbs->el_index[i].message_id == message_id) {
bedit = TRUE;
break;
}
if (isparam("entry_date"))
strcpy(date, getparam("entry_date"));
}
if (*getparam("edit_id") && *getparam("resubmit") && atoi(getparam("resubmit")) == 1) { if (*getparam("edit_id") && *getparam("resubmit") && atoi(getparam("resubmit")) == 1) {
resubmit_orig = atoi(getparam("edit_id")); resubmit_orig = atoi(getparam("edit_id"));
@ -11007,6 +11144,7 @@ void submit_elog(LOGBOOK * lbs)
date[0] = 0; date[0] = 0;
} else { } else {
if (*getparam("edit_id")) { if (*getparam("edit_id")) {
bedit = TRUE;
message_id = atoi(getparam("edit_id")); message_id = atoi(getparam("edit_id"));
strcpy(in_reply_to, "<keep>"); strcpy(in_reply_to, "<keep>");
strcpy(reply_to, "<keep>"); strcpy(reply_to, "<keep>");
@ -11019,7 +11157,7 @@ void submit_elog(LOGBOOK * lbs)
logf(lbs, "EDIT message #%d", message_id); logf(lbs, "EDIT message #%d", message_id);
message_id = message_id =
el_submit(lbs, message_id, date, attr_list, attrib, lbs->n_attr, el_submit(lbs, message_id, bedit, date, attr_list, attrib, lbs->n_attr,
getparam("text"), in_reply_to, reply_to, getparam("text"), in_reply_to, reply_to,
*getparam("html") ? "HTML" : "plain", att_file, TRUE, NULL); *getparam("html") ? "HTML" : "plain", att_file, TRUE, NULL);
@ -11285,8 +11423,8 @@ void copy_to(LOGBOOK * lbs, int src_id, char *dest_logbook, int move, int orig_i
the source logbook even if the destination has a differnt number of attributes */ the source logbook even if the destination has a differnt number of attributes */
message_id = message_id =
el_submit(lbs_dest, 0, date, attr_list, attrib, lbs->n_attr, text, str, "", el_submit(lbs_dest, 0, FALSE, date, attr_list, attrib, lbs->n_attr, text, str,
encoding, attachment, TRUE, NULL); "", encoding, attachment, TRUE, NULL);
if (message_id <= 0) { if (message_id <= 0) {
sprintf(str, loc("New message cannot be written to directory \"%s\""), sprintf(str, loc("New message cannot be written to directory \"%s\""),