Refactor: Pass conversion mode to file converter

This is preparation for the resource fork converter.

GitOrigin-RevId: b50299047964e2d6d4a8b6ce3178639ac23f8d50
This commit is contained in:
Dietrich Epp 2021-03-24 03:38:26 -04:00
parent 910039b77a
commit 03ae6a672d
4 changed files with 23 additions and 29 deletions

17
defs.h
View File

@ -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);

14
file.c
View File

@ -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;
}

17
sync.c
View File

@ -1,7 +1,5 @@
#include "defs.h"
#include "convert.h"
#include <CursorCtl.h>
#include <Files.h>
#include <Folders.h>
@ -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);

4
util.c
View File

@ -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;