jcs
/subtext
/amendments
/456
binkp: Use common binkp: dir, move bad zips to bad subdir
jcs made amendment 456 about 1 year ago
--- binkp.c Sat Mar 25 21:47:37 2023
+++ binkp.c Mon Mar 27 21:30:29 2023
@@ -34,12 +34,13 @@ time_t binkp_next_poll = 0;
static struct fidopkt_address binkp_our_address = { 0 };
static struct binkp_connection *binkpc = NULL;
-static Str255 binkp_inbox_dir, binkp_done_dir, binkp_outbox_dir;
+static Str255 binkp_inbox_dir, binkp_done_dir, binkp_outbox_dir,
+ binkp_bad_dir;
static short binkp_fidopkt_skipped, binkp_fidopkt_imported;
static bool binkp_temp_fail = false;
struct uthread *binkp_thread = NULL;
-void binkp_mkdir(Str255 *dir_str, char *suffix);
+void binkp_mkdir(Str255 dir_str, char *suffix);
bool binkp_connect(void);
void binkp_free(void);
size_t binkp_send_frame(short command, char *data, size_t data_size);
@@ -56,7 +57,7 @@ void binkp_deliver_outbox(void);
void
binkp_init(void)
{
- short newdirid, error;
+ Str255 base_dir;
if (db->config.binkp_hostname[0] == '\0')
return;
@@ -68,30 +69,35 @@ binkp_init(void)
return;
}
- binkp_mkdir(&binkp_inbox_dir, "inbox");
- if (!db->config.binkp_delete_done)
- binkp_mkdir(&binkp_done_dir, "processed");
- binkp_mkdir(&binkp_outbox_dir, "outbox");
-
+ binkp_mkdir(base_dir, NULL);
+ binkp_mkdir(binkp_inbox_dir, "inbox");
+ binkp_mkdir(binkp_done_dir, "processed");
+ binkp_mkdir(binkp_outbox_dir, "outbox");
+ binkp_mkdir(binkp_bad_dir, "bad");
+
binkp_ready = true;
}
void
-binkp_mkdir(Str255 *dir_str, char *suffix)
+binkp_mkdir(Str255 dir_str, char *suffix)
{
- short error, id;
+ short error;
+ long id;
if (getpath(db->bile->vrefnum, db->bile->filename, dir_str, false) != 0)
panic("getpath failed on %s", PtoCstr(db->bile->filename));
- PtoCstr(*dir_str);
- snprintf((char *)*dir_str, sizeof(Str255), "%s:binkp-%s", *dir_str,
- suffix);
- CtoPstr(*dir_str);
+ PtoCstr(dir_str);
+ strlcat((char *)dir_str, ":binkp", sizeof(Str255));
+ if (suffix != NULL) {
+ strlcat((char *)dir_str, ":", sizeof(Str255));
+ strlcat((char *)dir_str, suffix, sizeof(Str255));
+ }
+ CtoPstr(dir_str);
- if (!FIsDir(*dir_str)) {
- error = DirCreate(db->bile->vrefnum, 0, *dir_str, &id);
+ if (!FIsDir(dir_str)) {
+ error = DirCreate(db->bile->vrefnum, 0, dir_str, &id);
if (error)
- panic("Failed creating %s: %d", PtoCstr(*dir_str), error);
+ panic("Failed creating %s: %d", PtoCstr(dir_str), error);
}
}
@@ -634,10 +640,11 @@ binkp_toss_inbox(void)
DirInfo *dpb = (DirInfo *)&cipbr;
CMovePBRec cmpbr = { 0 };
Str255 file_name_c, path, done_path;
- short dir_id, error;
+ short dir_id, error, zret;
unsigned long started = Time, elapsed;
size_t data_size;
char *data;
+ bool bad;
binkp_fidopkt_skipped = 0;
binkp_fidopkt_imported = 0;
@@ -678,11 +685,18 @@ binkp_toss_inbox(void)
binkp_fidopkt_skipped = 0;
binkp_fidopkt_imported = 0;
+ bad = false;
if (zip_is_zip_file(path)) {
- if (!zip_read_file(path, binkp_zip_decider,
- binkp_fidopkt_processor))
+ zret = zip_read_file(path, binkp_zip_decider,
+ binkp_fidopkt_processor);
+ if (zret == ZIP_NO_MEMORY)
binkp_temp_fail = true;
+ else if (zret != ZIP_OK) {
+ bad = true;
+ logger_printf("[binkp] Failed processing ZIP file %s: %d",
+ file_name_c, zret);
+ }
} else if (strstr((char *)file_name_c, ".pkt")) {
data_size = binkp_buffer_file(path, &data);
if (data_size == 0)
@@ -705,22 +719,33 @@ binkp_toss_inbox(void)
(char *)file_name_c, binkp_fidopkt_imported,
binkp_fidopkt_skipped);
- PtoCstr(binkp_done_dir);
- snprintf((char *)done_path, sizeof(done_path),
- "%s:%s", binkp_done_dir, file_name_c);
- CtoPstr(done_path);
- CtoPstr(binkp_done_dir);
-
- if (db->config.binkp_delete_done) {
+ if (db->config.binkp_delete_done && !bad) {
if ((error = FSDelete(path, 0)) != 0) {
logger_printf("[binkp] Failed deleting inbox file %s: %d",
PtoCstr(path), error);
break;
}
} else {
+ if (bad) {
+ PtoCstr(binkp_bad_dir);
+ snprintf((char *)done_path, sizeof(done_path),
+ "%s:%s", binkp_bad_dir, file_name_c);
+ CtoPstr(done_path);
+ CtoPstr(binkp_bad_dir);
+ } else {
+ PtoCstr(binkp_done_dir);
+ snprintf((char *)done_path, sizeof(done_path),
+ "%s:%s", binkp_done_dir, file_name_c);
+ CtoPstr(done_path);
+ CtoPstr(binkp_done_dir);
+ }
+
try_rename:
cmpbr.ioNamePtr = path;
- cmpbr.ioNewName = binkp_done_dir;
+ if (bad)
+ cmpbr.ioNewName = binkp_bad_dir;
+ else
+ cmpbr.ioNewName = binkp_done_dir;
cmpbr.ioDirID = 0;
cmpbr.ioNewDirID = 0;
cmpbr.ioVRefNum = 0;
@@ -734,8 +759,8 @@ try_rename:
goto try_rename;
} else if (error) {
logger_printf("[binkp] Failed moving %s to %s: %d",
- PtoCstr(path), PtoCstr(binkp_done_dir), error);
- CtoPstr(binkp_done_dir);
+ PtoCstr(path), PtoCstr(done_path), error);
+ CtoPstr(done_path);
break;
}
}