+
+static char *
+amindexd_nicedate(
+ char * datestamp)
+{
+ static char nice[20];
+ int year, month, day;
+ int hours, minutes, seconds;
+ char date[9], atime[7];
+ int numdate, numtime;
+
+ strncpy(date, datestamp, 8);
+ date[8] = '\0';
+ numdate = atoi(date);
+ year = numdate / 10000;
+ month = (numdate / 100) % 100;
+ day = numdate % 100;
+
+ if(strlen(datestamp) <= 8) {
+ g_snprintf(nice, SIZEOF(nice), "%4d-%02d-%02d",
+ year, month, day);
+ }
+ else {
+ strncpy(atime, &(datestamp[8]), 6);
+ atime[6] = '\0';
+ numtime = atoi(atime);
+ hours = numtime / 10000;
+ minutes = (numtime / 100) % 100;
+ seconds = numtime % 100;
+
+ g_snprintf(nice, SIZEOF(nice), "%4d-%02d-%02d-%02d-%02d-%02d",
+ year, month, day, hours, minutes, seconds);
+ }
+
+ return nice;
+}
+
+static int
+cmp_date(
+ const char * date1,
+ const char * date2)
+{
+ return strncmp(date1, date2, strlen(date2));
+}
+
+static int
+get_index_dir(
+ char *dump_hostname,
+ char *hostname,
+ char *diskname)
+{
+ struct stat dir_stat;
+ char *fn;
+ char *s;
+ char *lower_hostname;
+
+ lower_hostname = stralloc(dump_hostname);
+ for(s=lower_hostname; *s != '\0'; s++)
+ *s = tolower(*s);
+
+ fn = getindexfname(dump_hostname, diskname, NULL, 0);
+ if (stat(fn, &dir_stat) == 0 && S_ISDIR(dir_stat.st_mode)) {
+ amfree(lower_hostname);
+ amfree(fn);
+ return 1;
+ }
+ amfree(fn);
+ if (hostname != NULL) {
+ fn = getindexfname(hostname, diskname, NULL, 0);
+ if (stat(fn, &dir_stat) == 0 && S_ISDIR(dir_stat.st_mode)) {
+ amfree(lower_hostname);
+ amfree(fn);
+ return 1;
+ }
+ }
+ amfree(fn);
+ fn = getindexfname(lower_hostname, diskname, NULL, 0);
+ if (stat(fn, &dir_stat) == 0 && S_ISDIR(dir_stat.st_mode)) {
+ amfree(lower_hostname);
+ amfree(fn);
+ return 1;
+ }
+ amfree(fn);
+ if(diskname != NULL) {
+ fn = getoldindexfname(dump_hostname, diskname, NULL, 0);
+ if (stat(fn, &dir_stat) == 0 && S_ISDIR(dir_stat.st_mode)) {
+ amfree(lower_hostname);
+ amfree(fn);
+ return 1;
+ }
+ amfree(fn);
+ if (hostname != NULL) {
+ fn = getoldindexfname(hostname, diskname, NULL, 0);
+ if (stat(fn, &dir_stat) == 0 && S_ISDIR(dir_stat.st_mode)) {
+ amfree(lower_hostname);
+ amfree(fn);
+ return 1;
+ }
+ }
+ amfree(fn);
+ fn = getoldindexfname(lower_hostname, diskname, NULL, 0);
+ if (stat(fn, &dir_stat) == 0 && S_ISDIR(dir_stat.st_mode)) {
+ amfree(lower_hostname);
+ amfree(fn);
+ return 1;
+ }
+ amfree(fn);
+ }
+ amfree(lower_hostname);
+ return -1;
+}
+
+static char *
+get_index_name(
+ char *dump_hostname,
+ char *hostname,
+ char *diskname,
+ char *timestamps,
+ int level)
+{
+ struct stat dir_stat;
+ char *fn;
+ char *s;
+ char *lower_hostname;
+
+ lower_hostname = stralloc(dump_hostname);
+ for(s=lower_hostname; *s != '\0'; s++)
+ *s = tolower(*s);
+
+ fn = getindexfname(dump_hostname, diskname, timestamps, level);
+ if (stat(fn, &dir_stat) == 0 && S_ISREG(dir_stat.st_mode)) {
+ amfree(lower_hostname);
+ return fn;
+ }
+ amfree(fn);
+ if(hostname != NULL) {
+ fn = getindexfname(hostname, diskname, timestamps, level);
+ if (stat(fn, &dir_stat) == 0 && S_ISREG(dir_stat.st_mode)) {
+ amfree(lower_hostname);
+ return fn;
+ }
+ }
+ amfree(fn);
+ fn = getindexfname(lower_hostname, diskname, timestamps, level);
+ if (stat(fn, &dir_stat) == 0 && S_ISREG(dir_stat.st_mode)) {
+ amfree(lower_hostname);
+ return fn;
+ }
+ amfree(fn);
+ if(diskname != NULL) {
+ fn = getoldindexfname(dump_hostname, diskname, timestamps, level);
+ if (stat(fn, &dir_stat) == 0 && S_ISREG(dir_stat.st_mode)) {
+ amfree(lower_hostname);
+ return fn;
+ }
+ amfree(fn);
+ if(hostname != NULL) {
+ fn = getoldindexfname(hostname, diskname, timestamps, level);
+ if (stat(fn, &dir_stat) == 0 && S_ISREG(dir_stat.st_mode)) {
+ amfree(lower_hostname);
+ return fn;
+ }
+ }
+ amfree(fn);
+ fn = getoldindexfname(lower_hostname, diskname, timestamps, level);
+ if (stat(fn, &dir_stat) == 0 && S_ISREG(dir_stat.st_mode)) {
+ amfree(lower_hostname);
+ return fn;
+ }
+ }
+ amfree(lower_hostname);
+ return NULL;
+}