Implemented first half of restructuring data directories (one subdir per year)

This commit is contained in:
Stefan Ritt 2014-10-03 17:57:14 +02:00
parent 20d70dbef2
commit 5ec68e0b82

View File

@ -3712,67 +3712,102 @@ int eli_compare(const void *e1, const void *e2)
/*------------------------------------------------------------------*/ /*------------------------------------------------------------------*/
int el_build_index(LOGBOOK * lbs, BOOL rebuild) void generate_new_file_name(char *file_name, char *path, int size)
/* scan all ??????a.log files and build an index table in eli[] */
{ {
char *file_list, str[256], error_str[256], date[256], dir[256], file_name[MAX_PATH_LENGTH], *buffer, *p, char fn[MAX_PATH_LENGTH], subdir[MAX_PATH_LENGTH];
*pn, in_reply_to[80]; int status, year, month;
int index, n, length; static int first = TRUE;
int i, fh, len;
unsigned char digest[16];
if (rebuild) { // extract path from file_name
xfree(lbs->el_index); strlcpy(path, file_name, size);
xfree(lbs->n_el_index); if (strrchr(path, DIR_SEPARATOR))
*(strrchr(path, DIR_SEPARATOR)+1) = 0;
// extract file name
if (strrchr(file_name, DIR_SEPARATOR))
strlcpy(fn, strrchr(file_name, DIR_SEPARATOR)+1, sizeof(fn));
else
strlcpy(fn, file_name, sizeof(fn));
// create subdir from name
year = (fn[0]-'0')*10+(fn[1]-'0');
month = (fn[2]-'0')*10+(fn[3]-'0');
if (year < 80)
sprintf(subdir, "20%02d", year);
else
sprintf(subdir, "19%02d", year);
// create new subdir
strlcat(path, subdir, size);
#ifdef OS_WINNT
status = mkdir(path);
#else
status = mkdir(path, 0755);
#endif
if (status == 0) {
if (first) {
eprintf("\nFound old directory structure. Creating subdirectories and moving files...\n");
first = FALSE;
}
eprintf("Created directory \"%s\"\n", path);
} else {
if (errno != EEXIST) {
eprintf("generate_new_file_name: %s\n", strerror(errno));
eprintf("Cannot create directory \"%s\"\n", path);
}
} }
lbs->n_el_index = xmalloc(sizeof(int)); // assemble new path
*lbs->n_el_index = 0; strlcat(path, DIR_SEPARATOR_STR, size);
lbs->el_index = xmalloc(0); strlcat(path, fn, size);
/* get data directory */
strcpy(dir, lbs->data_dir);
if (is_verbose() > 1) {
/* show MD5 from config file */
load_config_section(lbs->name, &buffer, error_str);
if (error_str[0])
eprintf(error_str);
else {
remove_crlf(buffer);
MD5_checksum(buffer, strlen(buffer), digest);
eprintf("\n\nConfig [%s], MD5=", lbs->name);
for (i = 0; i < 16; i++)
eprintf("%02X", digest[i]);
eprintf("\n\n");
}
if (buffer)
xfree(buffer);
} }
file_list = NULL; /*------------------------------------------------------------------*/
n = ss_file_find(dir, "??????a.log", &file_list);
if (n == 0) {
if (file_list)
xfree(file_list);
file_list = NULL;
n = ss_file_find(dir, "??????.log", &file_list); int restructure_dir(char *dir)
if (n > 0) {
return EL_UPGRADE; char *file_list;
int n1, n2, index;
char file_name[MAX_PATH_LENGTH], old_path[MAX_PATH_LENGTH], new_path[MAX_PATH_LENGTH];
return EL_EMPTY; /* go through all entry files */
} n1 = ss_file_find(dir, "??????a.log", &file_list);
for (index = 0; index < n1; index++) {
if (is_verbose() > 1)
eprintf("Entries:\n");
/* go through all files */
for (index = 0; index < n; index++) {
strlcpy(file_name, dir, sizeof(file_name)); strlcpy(file_name, dir, sizeof(file_name));
strlcat(file_name, file_list + index * MAX_PATH_LENGTH, sizeof(file_name)); strlcat(file_name, file_list + index * MAX_PATH_LENGTH, sizeof(file_name));
strlcpy(old_path, file_name, sizeof(old_path));
strlcpy(new_path, old_path, sizeof(new_path));
generate_new_file_name(old_path, new_path, sizeof(new_path));
rename(old_path, new_path);
}
if (file_list)
xfree(file_list);
/* go through all attachment files */
n2 = ss_file_find(dir, "??????_??????_*", &file_list);
for (index = 0; index < n2; index++) {
strlcpy(file_name, dir, sizeof(file_name));
strlcat(file_name, file_list + index * MAX_PATH_LENGTH, sizeof(file_name));
strlcpy(old_path, file_name, sizeof(old_path));
strlcpy(new_path, old_path, sizeof(new_path));
generate_new_file_name(old_path, new_path, sizeof(new_path));
rename(old_path, new_path);
}
if (file_list)
xfree(file_list);
return n1+n2;
}
/*------------------------------------------------------------------*/
int parse_file(LOGBOOK *lbs, char *file_name)
{
char str[256], date[256], *buffer, *p, *pn, in_reply_to[80];
int length, i, fh, len;
fh = open(file_name, O_RDONLY | O_BINARY, 0644); fh = open(file_name, O_RDONLY | O_BINARY, 0644);
@ -3805,7 +3840,10 @@ int el_build_index(LOGBOOK * lbs, BOOL rebuild)
return EL_MEM_ERROR; return EL_MEM_ERROR;
} }
strcpy(str, file_list + index * MAX_PATH_LENGTH); if (strrchr(file_name, DIR_SEPARATOR))
strlcpy(str, strrchr(file_name, DIR_SEPARATOR)+1, sizeof(str));
else
strlcpy(str, file_name, sizeof(str));
strcpy(lbs->el_index[*lbs->n_el_index].file_name, str); strcpy(lbs->el_index[*lbs->n_el_index].file_name, str);
el_decode(p, "Date: ", date, sizeof(date)); el_decode(p, "Date: ", date, sizeof(date));
@ -3848,8 +3886,122 @@ int el_build_index(LOGBOOK * lbs, BOOL rebuild)
xfree(buffer); xfree(buffer);
} }
return SUCCESS;
} }
/*------------------------------------------------------------------*/
int el_build_index(LOGBOOK * lbs, BOOL rebuild)
/* scan all ??????a.log files and build an index table in eli[] */
{
char *file_list, *dir_list, error_str[256], base_dir[256], dir[256], str[256],
file_name[MAX_PATH_LENGTH], *buffer;
int dindex, index, n;
int i, status;
unsigned char digest[16];
if (rebuild) {
xfree(lbs->el_index);
xfree(lbs->n_el_index);
}
lbs->n_el_index = xmalloc(sizeof(int));
*lbs->n_el_index = 0;
lbs->el_index = xmalloc(0);
/* get data directory */
strcpy(base_dir, lbs->data_dir);
if (is_verbose() > 1) {
/* show MD5 from config file */
load_config_section(lbs->name, &buffer, error_str);
if (error_str[0])
eprintf(error_str);
else {
remove_crlf(buffer);
MD5_checksum(buffer, strlen(buffer), digest);
eprintf("\n\nConfig [%s], MD5=", lbs->name);
for (i = 0; i < 16; i++)
eprintf("%02X", digest[i]);
eprintf("\n\n");
}
if (buffer)
xfree(buffer);
}
// check for old format and upgrade if necessary
n = ss_file_find(base_dir, "??????.log", &file_list);
if (n > 0) {
if (file_list)
xfree(file_list);
return EL_UPGRADE;
}
if (is_verbose() > 1)
eprintf("Entries:\n");
// move files to directories if (new layout to reduce number of files per directory)
// ## restructure_dir(base_dir);
dir_list = NULL;
n = ss_file_find(base_dir, "????", &dir_list);
if (n == 0) {
if (dir_list)
xfree(dir_list);
dir_list = NULL;
//return EL_EMPTY;
}
/* go through all directories */
for (dindex = 0; dindex < n; dindex++) {
file_list = NULL;
strlcpy(dir, base_dir, sizeof(str));
strlcat(dir, dir_list + dindex * MAX_PATH_LENGTH, sizeof(str));
strlcat(dir, DIR_SEPARATOR_STR, sizeof(str));
n = ss_file_find(dir, "??????a.log", &file_list);
/* go through all files */
for (index = 0; index < n; index++) {
strlcpy(file_name, dir, sizeof(file_name));
strlcat(file_name, file_list + index * MAX_PATH_LENGTH, sizeof(file_name));
status = parse_file(lbs, file_name);
if (status != SUCCESS) {
if (file_list)
xfree(file_list);
return status;
}
}
if (file_list)
xfree(file_list);
}
file_list = NULL;
n = ss_file_find(base_dir, "??????a.log", &file_list);
if (n == 0) {
if (file_list)
xfree(file_list);
file_list = NULL;
return EL_EMPTY;
}
/* go through all files */
for (index = 0; index < n; index++) {
strlcpy(file_name, base_dir, sizeof(file_name));
strlcat(file_name, file_list + index * MAX_PATH_LENGTH, sizeof(file_name));
status = parse_file(lbs, file_name);
if (status != SUCCESS) {
if (file_list)
xfree(file_list);
return status;
}
}
if (file_list)
xfree(file_list); xfree(file_list);
/* sort entries according to date */ /* sort entries according to date */