From 03ae6a672d3296aa585867bab3a04b879f8a9a46 Mon Sep 17 00:00:00 2001 From: Dietrich Epp Date: Wed, 24 Mar 2021 03:38:26 -0400 Subject: [PATCH] Refactor: Pass conversion mode to file converter This is preparation for the resource fork converter. GitOrigin-RevId: b50299047964e2d6d4a8b6ce3178639ac23f8d50 --- defs.h | 17 ++++++++--------- file.c | 14 ++++++++++++-- sync.c | 17 +++-------------- util.c | 4 ---- 4 files changed, 23 insertions(+), 29 deletions(-) diff --git a/defs.h b/defs.h index d5f0d3e..20ec063 100644 --- a/defs.h +++ b/defs.h @@ -37,11 +37,6 @@ void print_errcode(OSErr err, const char *msg, ...); // Print an out-of-memory error. void print_memerr(unsigned long size); -// Print an abort message. -void print_abort_func(const char *file, int line); - -#define print_abort() print_abort_func(__FILE__, __LINE__) - // Log the error result of a function call. void log_call(OSErr err, const char *function); @@ -51,13 +46,17 @@ int c2pstr(unsigned char *ostr, const char *istr); // Convert a Pascall string (maximum 31 characters) to a C string. void p2cstr(char *ostr, const unsigned char *istr); +// Global operation mode +typedef enum { + kModeUnknown, + kModePush, + kModePull, +} operation_mode; + // ============================================================================= // file.c // ============================================================================= -// Text file conversion function. -typedef int (*convert_func)(short src, short dest, void *srcBuf, void *destBuf); - enum { kSrcDir, kDestDir, @@ -90,6 +89,6 @@ struct file_info { // Synchronize a file according to the action in the action field. The temporary // directory must be a valid directory on the destination volume. -int sync_file(struct file_info *file, convert_func func, short srcVol, +int sync_file(struct file_info *file, operation_mode mode, short srcVol, long srcDir, short destVol, long destDir, short tempVol, long tempDir); diff --git a/file.c b/file.c index 623cbb2..e6d6aee 100644 --- a/file.c +++ b/file.c @@ -152,7 +152,7 @@ static int replace_file(FSSpec *temp, FSSpec *dest, file_action action) { static Ptr gSrcBuffer; static Ptr gDestBuffer; -int sync_file(struct file_info *file, convert_func func, short srcVol, +int sync_file(struct file_info *file, operation_mode mode, short srcVol, long srcDir, short destVol, long destDir, short tempVol, long tempDir) { OSType creator = 'MPS ', fileType = 'TEXT'; @@ -241,7 +241,17 @@ int sync_file(struct file_info *file, convert_func func, short srcVol, } // Convert data. - r = func(srcRef, destRef, gSrcBuffer, gDestBuffer); + switch (mode) { + case kModePush: + r = mac_to_unix(srcRef, destRef, gSrcBuffer, gDestBuffer); + break; + case kModePull: + r = mac_from_unix(srcRef, destRef, gSrcBuffer, gDestBuffer); + break; + default: + print_err("invalid mode"); + goto error; + } if (r != 0) { goto error; } diff --git a/sync.c b/sync.c index 2e68bc7..a3ee57d 100644 --- a/sync.c +++ b/sync.c @@ -1,7 +1,5 @@ #include "defs.h" -#include "convert.h" - #include #include #include @@ -23,13 +21,6 @@ static const char *kActionName[] = { "delete", }; -// Global operation mode -enum { - kModeUnknown, - kModePush, - kModePull, -}; - // Array of metadata entries for files. static Handle gFiles; static unsigned gFileCount; @@ -212,7 +203,8 @@ static int list_dir(short vRefNum, long dirID, int which) { return 0; } -static int command_main(char *localPath, char *remotePath, int mode) { +static int command_main(char *localPath, char *remotePath, + operation_mode mode) { short localVol, remoteVol, srcVol, destVol, tempVol; long localDir, remoteDir, srcDir, destDir, tempDir; struct file_info *array, *file; @@ -220,7 +212,6 @@ static int command_main(char *localPath, char *remotePath, int mode) { int r, i, n; char name[32]; bool hasAction; - convert_func func; // Get handles to local and remote directories. if (localPath == NULL) { @@ -319,7 +310,6 @@ static int command_main(char *localPath, char *remotePath, int mode) { // Synchronize the files. InitCursorCtl(NULL); if (mode == kModePull) { - func = mac_from_unix; err = FindFolder(destVol, kTemporaryFolderType, true, &tempVol, &tempDir); if (err != 0) { @@ -327,7 +317,6 @@ static int command_main(char *localPath, char *remotePath, int mode) { return 1; } } else { - func = mac_to_unix; // When pushing, we use the destination directory as the temporary // folder, to avoid crossing filesystem boundaries on the host. tempVol = destVol; @@ -340,7 +329,7 @@ static int command_main(char *localPath, char *remotePath, int mode) { fprintf(stderr, "## %s: %s\n", kActionName[file->action], name); } SpinCursor(32); - r = sync_file(&array[i], func, srcVol, srcDir, destVol, destDir, + r = sync_file(&array[i], mode, srcVol, srcDir, destVol, destDir, tempVol, tempDir); if (r != 0) { print_err("failed to synchronize file: %s", name); diff --git a/util.c b/util.c index eafaccd..894c5a2 100644 --- a/util.c +++ b/util.c @@ -86,10 +86,6 @@ void print_memerr(unsigned long size) { print_errcode(err, "out of memory; size=%lu", size); } -void print_abort_func(const char *file, int line) { - print_err("assertion failed: %s:d", file, line); -} - void log_call(OSErr err, const char *function) { const char *emsg;