Skip to content
Snippets Groups Projects
Commit 0f0a8a1a authored by Rob Swindell's avatar Rob Swindell :speech_balloon:
Browse files

Refactor the function: unpack_bundle()

1. When a 0-length bundle file was encountered or an unpacking error occurred,
   any remaining bundles for the current search day-of-week (e.g. *.SU*) would
   be skipped/ignored. This bug (issue #764, regarding the 0-length file part),
   is fixed by not incrementing the day-of-week index in the main loop, but
   rather only incremeting the index when all bundles for the current
   day-of-week have been processed.
2. The age calculation for 0-byte/length bundle files was incorrect, so all
   0-length  bundle files would always be considered "less than 24-hours old"
   (and thus, never auto-deleted).
   This exacerbated the problem of issue #764 since it would persist until the
   0-length files were manually deleted. Fixed the file age calculation and
   now logging the date/timestamp of the 0-length file as well.
3. Don't do the switch/case/sprintf dance when we're not re-running a glob()
   search.
4. Replace the switch/case statement with an array of week day names/patterns.
5. Ignore (with a warning log message) any sub-directories of the inbound
   directory that happen to match the bundle file search pattern.
6. Use better variable naming.
7. Refer to files with a length of 0 as "0-length" instead of "0-byte" in log
   messages.
parent 3a45c021
No related branches found
No related tags found
No related merge requests found
...@@ -2661,83 +2661,54 @@ bool pack_bundle(const char *tmp_pkt, fidoaddr_t orig, fidoaddr_t dest) ...@@ -2661,83 +2661,54 @@ bool pack_bundle(const char *tmp_pkt, fidoaddr_t orig, fidoaddr_t dest)
/****************************************************************************** /******************************************************************************
This function checks the inbound directory for the first bundle it finds, it This function checks the inbound directory for the first bundle it finds, it
will then unpack and delete the bundle. If no bundles exist this function will then unpack and delete the bundle. If no more bundles exist, this function
returns a false, otherwise a true is returned. returns false, otherwise true is returned.
******************************************************************************/ ******************************************************************************/
bool unpack_bundle(const char* inbound) bool unpack_bundle(const char* inbound)
{ {
char* p; char path[MAX_PATH+1];
char str[MAX_PATH+1];
char fname[MAX_PATH+1]; char fname[MAX_PATH+1];
int i; int day_of_week = 0;
static glob_t g; static glob_t g;
static size_t gi; static size_t gi;
for(i=0;i<7 && !terminated;i++) { while (!terminated) {
if(gi >= g.gl_pathc) {
#if defined(__unix__) /* support upper or lower case */ #if defined(__unix__) /* support upper or lower case */
switch(i) { const char* week[] = { "[Ss][Uu]", "[Mm][Oo]", "[Tt][Uu]", "[Ww][Ee]", "[Tt][Hh]", "[Ff][Rr]", "[Ss][Aa]" };
case 0: #else // case insensitive file system
p="[Ss][Uu]"; const char* week[] = { "SU", "MO", "TU", "WE", "TH", "FR", "SA" };
break;
case 1:
p="[Mm][Oo]";
break;
case 2:
p="[Tt][Uu]";
break;
case 3:
p="[Ww][Ee]";
break;
case 4:
p="[Tt][Hh]";
break;
case 5:
p="[Ff][Rr]";
break;
default:
p="[Ss][Aa]";
break;
}
#else
switch(i) {
case 0:
p="su";
break;
case 1:
p="mo";
break;
case 2:
p="tu";
break;
case 3:
p="we";
break;
case 4:
p="th";
break;
case 5:
p="fr";
break;
default:
p="sa";
break;
}
#endif #endif
SAFEPRINTF2(str,"%s*.%s?",inbound,p); if(day_of_week >= sizeof week / sizeof week[0])
if(gi>=g.gl_pathc) { break;
SAFEPRINTF2(path, "%s*.%s?", inbound, week[day_of_week]);
day_of_week++;
gi=0; gi=0;
globfree(&g); globfree(&g);
glob(str,0,NULL,&g); glob(path,0,NULL,&g);
if(g.gl_pathc < 1)
continue;
} }
if(gi<g.gl_pathc) {
SAFECOPY(fname,g.gl_pathv[gi]); SAFECOPY(fname,g.gl_pathv[gi]);
gi++; gi++;
if(isdir(fname)) {
lprintf(LOG_WARNING, "Ignoring directory matching bundle pattern: %s", fname);
continue;
}
off_t length = flength(fname); off_t length = flength(fname);
if(length < 1) { if(length < 1) {
if(fdate(fname) < time(NULL) + (24*60*60)) time_t ftime = fdate(fname);
lprintf(LOG_DEBUG, "Ignoring %ld-byte file (less than 24-hours old): %s", (long)length, fname); time_t age = time(NULL) - ftime;
float hours_old = age / (60.0F * 60.0F);
char* tp = ctime(&ftime);
if(tp == NULL)
tp = "<an invalid date/time>";
else
tp += 4;
if(hours_old < 24.0)
lprintf(LOG_DEBUG, "Ignoring %ld-length file from %.20s (less than 24-hours old): %s", (long)length, tp, fname);
else { else {
lprintf(LOG_INFO, "Deleting %ld-byte file (more than 24-hours old): %s", (long)length, fname); lprintf(LOG_INFO, "Deleting %ld-length file from %.20s (at least 24-hours old): %s", (long)length, tp, fname);
delfile(fname, __LINE__); /* Delete it if it's a 0-byte file */ delfile(fname, __LINE__); /* Delete it if it's a 0-byte file */
} }
continue; continue;
...@@ -2746,22 +2717,21 @@ bool unpack_bundle(const char* inbound) ...@@ -2746,22 +2717,21 @@ bool unpack_bundle(const char* inbound)
if(unpack(fname, inbound) != 0) { /* failure */ if(unpack(fname, inbound) != 0) { /* failure */
lprintf(LOG_ERR, "!Unpack failure: %s", fname); lprintf(LOG_ERR, "!Unpack failure: %s", fname);
/* rename to "*.?_?" or (if it exists) "*.?-?" */ /* rename to "*.?_?" or (if it exists) "*.?-?" */
SAFECOPY(str,fname); SAFECOPY(path,fname);
str[strlen(str)-2]='_'; path[strlen(path)-2]='_';
if(fexistcase(str)) if(fexistcase(path))
str[strlen(str)-2]='-'; path[strlen(path)-2]='-';
if(fexistcase(str)) if(fexistcase(path))
delfile(str, __LINE__); delfile(path, __LINE__);
if(rename(fname,str)) if(rename(fname,path))
lprintf(LOG_ERR,"ERROR line %d renaming %s to %s" lprintf(LOG_ERR,"ERROR line %d renaming %s to %s"
,__LINE__,fname,str); ,__LINE__,fname,path);
continue; continue;
} }
delfile(fname, __LINE__); /* successful, so delete bundle */ delfile(fname, __LINE__); /* successful, so delete bundle */
bundles_unpacked++; bundles_unpacked++;
return(true); return(true);
} }
}
return(false); return(false);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment