Refactor: Pass conversion mode to file converter
This is preparation for the resource fork converter. GitOrigin-RevId: b50299047964e2d6d4a8b6ce3178639ac23f8d50
This commit is contained in:
parent
910039b77a
commit
03ae6a672d
17
defs.h
17
defs.h
|
@ -37,11 +37,6 @@ void print_errcode(OSErr err, const char *msg, ...);
|
||||||
// Print an out-of-memory error.
|
// Print an out-of-memory error.
|
||||||
void print_memerr(unsigned long size);
|
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.
|
// Log the error result of a function call.
|
||||||
void log_call(OSErr err, const char *function);
|
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.
|
// Convert a Pascall string (maximum 31 characters) to a C string.
|
||||||
void p2cstr(char *ostr, const unsigned char *istr);
|
void p2cstr(char *ostr, const unsigned char *istr);
|
||||||
|
|
||||||
|
// Global operation mode
|
||||||
|
typedef enum {
|
||||||
|
kModeUnknown,
|
||||||
|
kModePush,
|
||||||
|
kModePull,
|
||||||
|
} operation_mode;
|
||||||
|
|
||||||
// =============================================================================
|
// =============================================================================
|
||||||
// file.c
|
// file.c
|
||||||
// =============================================================================
|
// =============================================================================
|
||||||
|
|
||||||
// Text file conversion function.
|
|
||||||
typedef int (*convert_func)(short src, short dest, void *srcBuf, void *destBuf);
|
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
kSrcDir,
|
kSrcDir,
|
||||||
kDestDir,
|
kDestDir,
|
||||||
|
@ -90,6 +89,6 @@ struct file_info {
|
||||||
|
|
||||||
// Synchronize a file according to the action in the action field. The temporary
|
// Synchronize a file according to the action in the action field. The temporary
|
||||||
// directory must be a valid directory on the destination volume.
|
// 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 srcDir, short destVol, long destDir, short tempVol,
|
||||||
long tempDir);
|
long tempDir);
|
||||||
|
|
14
file.c
14
file.c
|
@ -152,7 +152,7 @@ static int replace_file(FSSpec *temp, FSSpec *dest, file_action action) {
|
||||||
static Ptr gSrcBuffer;
|
static Ptr gSrcBuffer;
|
||||||
static Ptr gDestBuffer;
|
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 srcDir, short destVol, long destDir, short tempVol,
|
||||||
long tempDir) {
|
long tempDir) {
|
||||||
OSType creator = 'MPS ', fileType = 'TEXT';
|
OSType creator = 'MPS ', fileType = 'TEXT';
|
||||||
|
@ -241,7 +241,17 @@ int sync_file(struct file_info *file, convert_func func, short srcVol,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert data.
|
// 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) {
|
if (r != 0) {
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
|
17
sync.c
17
sync.c
|
@ -1,7 +1,5 @@
|
||||||
#include "defs.h"
|
#include "defs.h"
|
||||||
|
|
||||||
#include "convert.h"
|
|
||||||
|
|
||||||
#include <CursorCtl.h>
|
#include <CursorCtl.h>
|
||||||
#include <Files.h>
|
#include <Files.h>
|
||||||
#include <Folders.h>
|
#include <Folders.h>
|
||||||
|
@ -23,13 +21,6 @@ static const char *kActionName[] = {
|
||||||
"delete",
|
"delete",
|
||||||
};
|
};
|
||||||
|
|
||||||
// Global operation mode
|
|
||||||
enum {
|
|
||||||
kModeUnknown,
|
|
||||||
kModePush,
|
|
||||||
kModePull,
|
|
||||||
};
|
|
||||||
|
|
||||||
// Array of metadata entries for files.
|
// Array of metadata entries for files.
|
||||||
static Handle gFiles;
|
static Handle gFiles;
|
||||||
static unsigned gFileCount;
|
static unsigned gFileCount;
|
||||||
|
@ -212,7 +203,8 @@ static int list_dir(short vRefNum, long dirID, int which) {
|
||||||
return 0;
|
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;
|
short localVol, remoteVol, srcVol, destVol, tempVol;
|
||||||
long localDir, remoteDir, srcDir, destDir, tempDir;
|
long localDir, remoteDir, srcDir, destDir, tempDir;
|
||||||
struct file_info *array, *file;
|
struct file_info *array, *file;
|
||||||
|
@ -220,7 +212,6 @@ static int command_main(char *localPath, char *remotePath, int mode) {
|
||||||
int r, i, n;
|
int r, i, n;
|
||||||
char name[32];
|
char name[32];
|
||||||
bool hasAction;
|
bool hasAction;
|
||||||
convert_func func;
|
|
||||||
|
|
||||||
// Get handles to local and remote directories.
|
// Get handles to local and remote directories.
|
||||||
if (localPath == NULL) {
|
if (localPath == NULL) {
|
||||||
|
@ -319,7 +310,6 @@ static int command_main(char *localPath, char *remotePath, int mode) {
|
||||||
// Synchronize the files.
|
// Synchronize the files.
|
||||||
InitCursorCtl(NULL);
|
InitCursorCtl(NULL);
|
||||||
if (mode == kModePull) {
|
if (mode == kModePull) {
|
||||||
func = mac_from_unix;
|
|
||||||
err =
|
err =
|
||||||
FindFolder(destVol, kTemporaryFolderType, true, &tempVol, &tempDir);
|
FindFolder(destVol, kTemporaryFolderType, true, &tempVol, &tempDir);
|
||||||
if (err != 0) {
|
if (err != 0) {
|
||||||
|
@ -327,7 +317,6 @@ static int command_main(char *localPath, char *remotePath, int mode) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
func = mac_to_unix;
|
|
||||||
// When pushing, we use the destination directory as the temporary
|
// When pushing, we use the destination directory as the temporary
|
||||||
// folder, to avoid crossing filesystem boundaries on the host.
|
// folder, to avoid crossing filesystem boundaries on the host.
|
||||||
tempVol = destVol;
|
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);
|
fprintf(stderr, "## %s: %s\n", kActionName[file->action], name);
|
||||||
}
|
}
|
||||||
SpinCursor(32);
|
SpinCursor(32);
|
||||||
r = sync_file(&array[i], func, srcVol, srcDir, destVol, destDir,
|
r = sync_file(&array[i], mode, srcVol, srcDir, destVol, destDir,
|
||||||
tempVol, tempDir);
|
tempVol, tempDir);
|
||||||
if (r != 0) {
|
if (r != 0) {
|
||||||
print_err("failed to synchronize file: %s", name);
|
print_err("failed to synchronize file: %s", name);
|
||||||
|
|
4
util.c
4
util.c
|
@ -86,10 +86,6 @@ void print_memerr(unsigned long size) {
|
||||||
print_errcode(err, "out of memory; size=%lu", 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) {
|
void log_call(OSErr err, const char *function) {
|
||||||
const char *emsg;
|
const char *emsg;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue