2021-03-16 16:08:52 +00:00
|
|
|
#include "defs.h"
|
|
|
|
|
2021-03-24 07:31:00 +00:00
|
|
|
#include "convert.h"
|
|
|
|
|
2021-03-16 16:08:52 +00:00
|
|
|
#include <Files.h>
|
|
|
|
#include <Script.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
// Make an FSSpec for a temporary file.
|
|
|
|
static int make_temp(FSSpec *temp, short vRefNum, long dirID,
|
|
|
|
const unsigned char *name) {
|
|
|
|
Str31 tname;
|
|
|
|
unsigned pfxlen, maxpfx = 31 - 4;
|
|
|
|
OSErr err;
|
|
|
|
|
|
|
|
pfxlen = name[0];
|
|
|
|
if (pfxlen > maxpfx) {
|
|
|
|
pfxlen = maxpfx;
|
|
|
|
}
|
|
|
|
memcpy(tname + 1, name + 1, pfxlen);
|
|
|
|
memcpy(tname + 1 + pfxlen, ".tmp", 4);
|
|
|
|
tname[0] = pfxlen + 4;
|
|
|
|
err = FSMakeFSSpec(vRefNum, dirID, tname, temp);
|
2021-03-16 16:24:56 +00:00
|
|
|
if (err != 0 && err != fnfErr) {
|
2021-03-16 16:08:52 +00:00
|
|
|
print_errcode(err, "could not create temp file spec");
|
|
|
|
return 1;
|
|
|
|
}
|
2021-03-16 16:24:56 +00:00
|
|
|
return 0;
|
2021-03-16 16:08:52 +00:00
|
|
|
}
|
|
|
|
|
2021-03-24 07:31:00 +00:00
|
|
|
// Set the modification time for a file.
|
|
|
|
static int set_modtime(FSSpec *spec, long modTime) {
|
2021-03-16 16:08:52 +00:00
|
|
|
CInfoPBRec ci;
|
|
|
|
Str31 name;
|
|
|
|
OSErr err;
|
|
|
|
|
|
|
|
memset(&ci, 0, sizeof(ci));
|
2021-03-24 07:31:00 +00:00
|
|
|
memcpy(name, spec->name, spec->name[0] + 1);
|
2021-03-16 16:08:52 +00:00
|
|
|
ci.hFileInfo.ioNamePtr = name;
|
2021-03-24 07:31:00 +00:00
|
|
|
ci.hFileInfo.ioVRefNum = spec->vRefNum;
|
|
|
|
ci.hFileInfo.ioDirID = spec->parID;
|
2021-03-16 16:08:52 +00:00
|
|
|
err = PBGetCatInfoSync(&ci);
|
|
|
|
if (err != 0) {
|
|
|
|
print_errcode(err, "could not get temp file info");
|
2021-03-24 07:31:00 +00:00
|
|
|
return 1;
|
2021-03-16 16:08:52 +00:00
|
|
|
}
|
2021-03-24 07:31:00 +00:00
|
|
|
memcpy(name, spec->name, spec->name[0] + 1);
|
2021-03-16 16:08:52 +00:00
|
|
|
ci.hFileInfo.ioNamePtr = name;
|
2021-03-24 07:31:00 +00:00
|
|
|
ci.hFileInfo.ioVRefNum = spec->vRefNum;
|
|
|
|
ci.hFileInfo.ioDirID = spec->parID;
|
2021-03-16 16:08:52 +00:00
|
|
|
ci.hFileInfo.ioFlMdDat = modTime;
|
|
|
|
err = PBSetCatInfoSync(&ci);
|
|
|
|
if (err != 0) {
|
|
|
|
print_errcode(err, "could not set temp file info");
|
2021-03-24 07:31:00 +00:00
|
|
|
return 1;
|
2021-03-16 16:08:52 +00:00
|
|
|
}
|
2021-03-24 07:31:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move a temp file over a destination file. This may modify the temp file spec
|
|
|
|
// if it moves in multiple stages.
|
|
|
|
static int replace_file(FSSpec *temp, FSSpec *dest, file_action action) {
|
|
|
|
HParamBlockRec pb;
|
|
|
|
CMovePBRec cm;
|
|
|
|
OSErr err;
|
|
|
|
bool mustMove, mustRename;
|
2021-03-16 16:08:52 +00:00
|
|
|
|
|
|
|
// First, try to exchange files if destination exists.
|
|
|
|
if (action == kActionReplace) {
|
2021-03-24 07:31:00 +00:00
|
|
|
err = FSpExchangeFiles(temp, dest);
|
2021-03-16 16:08:52 +00:00
|
|
|
if (gLogLevel >= kLogVerbose) {
|
|
|
|
log_call(err, "FSpExchangeFiles");
|
|
|
|
}
|
|
|
|
if (err == 0) {
|
2021-03-24 07:31:00 +00:00
|
|
|
err = FSpDelete(temp);
|
2021-03-16 16:08:52 +00:00
|
|
|
if (err != 0) {
|
|
|
|
print_errcode(err, "could not remove temporary file");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
// paramErr: function not supported by volume.
|
|
|
|
if (err != paramErr) {
|
|
|
|
print_errcode(err, "could not exchange files");
|
2021-03-24 07:31:00 +00:00
|
|
|
return 1;
|
2021-03-16 16:08:52 +00:00
|
|
|
}
|
|
|
|
// Otherwise, delete destination and move temp file over.
|
|
|
|
err = FSpDelete(dest);
|
|
|
|
if (err != 0) {
|
|
|
|
print_errcode(err, "could not remove destination file");
|
2021-03-24 07:31:00 +00:00
|
|
|
return 1;
|
2021-03-16 16:08:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-24 07:31:00 +00:00
|
|
|
mustMove = dest->parID != temp->parID;
|
|
|
|
mustRename = memcmp(dest->name, temp->name, dest->name[0] + 1) != 0;
|
2021-03-16 16:27:17 +00:00
|
|
|
|
2021-03-16 16:08:52 +00:00
|
|
|
// Next, try MoveRename.
|
2021-03-16 16:27:17 +00:00
|
|
|
if (mustMove && mustRename) {
|
|
|
|
memset(&pb, 0, sizeof(pb));
|
2021-03-24 07:31:00 +00:00
|
|
|
pb.copyParam.ioNamePtr = temp->name;
|
|
|
|
pb.copyParam.ioVRefNum = temp->vRefNum;
|
2021-03-16 16:27:17 +00:00
|
|
|
pb.copyParam.ioNewName = dest->name;
|
|
|
|
pb.copyParam.ioNewDirID = dest->parID;
|
2021-03-24 07:31:00 +00:00
|
|
|
pb.copyParam.ioDirID = temp->parID;
|
2021-03-16 16:27:17 +00:00
|
|
|
err = PBHMoveRenameSync(&pb);
|
|
|
|
if (gLogLevel >= kLogVerbose) {
|
|
|
|
log_call(err, "PBHMoveRename");
|
|
|
|
}
|
|
|
|
if (err == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
// paramErr: function not supported by volume.
|
|
|
|
if (err != paramErr) {
|
|
|
|
print_errcode(err, "could not rename temporary file");
|
2021-03-24 07:31:00 +00:00
|
|
|
return 1;
|
2021-03-16 16:27:17 +00:00
|
|
|
}
|
2021-03-16 16:08:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, try move and then rename.
|
2021-03-16 16:27:17 +00:00
|
|
|
if (mustMove) {
|
2021-03-16 16:08:52 +00:00
|
|
|
memset(&cm, 0, sizeof(cm));
|
2021-03-24 07:31:00 +00:00
|
|
|
cm.ioNamePtr = temp->name;
|
|
|
|
cm.ioVRefNum = temp->vRefNum;
|
2021-03-16 16:08:52 +00:00
|
|
|
cm.ioNewDirID = dest->parID;
|
2021-03-24 07:31:00 +00:00
|
|
|
cm.ioDirID = temp->parID;
|
2021-03-16 16:08:52 +00:00
|
|
|
err = PBCatMoveSync(&cm);
|
|
|
|
if (gLogLevel >= kLogVerbose) {
|
|
|
|
log_call(err, "PBCatMove");
|
|
|
|
}
|
|
|
|
if (err != 0) {
|
|
|
|
print_errcode(err, "could not move temporary file");
|
2021-03-24 07:31:00 +00:00
|
|
|
return 1;
|
2021-03-16 16:08:52 +00:00
|
|
|
}
|
2021-03-24 07:31:00 +00:00
|
|
|
temp->parID = dest->parID;
|
2021-03-16 16:08:52 +00:00
|
|
|
}
|
2021-03-16 16:27:17 +00:00
|
|
|
if (mustRename) {
|
2021-03-24 07:31:00 +00:00
|
|
|
err = FSpRename(temp, dest->name);
|
2021-03-16 16:08:52 +00:00
|
|
|
if (gLogLevel >= kLogVerbose) {
|
|
|
|
log_call(err, "FSpRename");
|
|
|
|
}
|
|
|
|
if (err != 0) {
|
|
|
|
print_errcode(err, "could not rename temporary file");
|
2021-03-24 07:31:00 +00:00
|
|
|
return 1;
|
2021-03-16 16:08:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-03-24 07:31:00 +00:00
|
|
|
static Ptr gSrcBuffer;
|
|
|
|
static Ptr gDestBuffer;
|
|
|
|
|
2021-03-24 07:38:26 +00:00
|
|
|
int sync_file(struct file_info *file, operation_mode mode, short srcVol,
|
2021-03-16 16:39:45 +00:00
|
|
|
long srcDir, short destVol, long destDir, short tempVol,
|
|
|
|
long tempDir) {
|
2021-03-26 07:35:00 +00:00
|
|
|
OSType creator, fileType;
|
2021-03-24 07:31:00 +00:00
|
|
|
FSSpec src, dest, temp;
|
|
|
|
short srcRef = 0, destRef = 0;
|
|
|
|
bool has_temp = false;
|
|
|
|
int r;
|
2021-03-16 16:08:52 +00:00
|
|
|
OSErr err;
|
|
|
|
|
|
|
|
// Handle actions which don't involve conversion.
|
|
|
|
if (file->action == kActionNone) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (file->action == kActionDelete) {
|
|
|
|
err = FSMakeFSSpec(destVol, destDir, file->name, &dest);
|
|
|
|
if (err != 0) {
|
|
|
|
print_errcode(err, "could not create destination spec");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
err = FSpDelete(&dest);
|
|
|
|
if (err != 0) {
|
|
|
|
print_errcode(err, "could not delete destination file");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create file specs.
|
|
|
|
err = FSMakeFSSpec(srcVol, srcDir, file->name, &src);
|
|
|
|
if (err != 0) {
|
|
|
|
print_errcode(err, "could not create source spec");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
err = FSMakeFSSpec(destVol, destDir, file->name, &dest);
|
|
|
|
if (err != 0 && err != fnfErr) {
|
|
|
|
print_errcode(err, "could not create destination spec");
|
|
|
|
return 1;
|
|
|
|
}
|
2021-03-24 07:31:00 +00:00
|
|
|
r = make_temp(&temp, tempVol, tempDir, dest.name);
|
2021-03-16 16:08:52 +00:00
|
|
|
if (r != 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-03-26 07:35:00 +00:00
|
|
|
// Create the temporary file.
|
|
|
|
switch (file->type) {
|
|
|
|
case kTypeText:
|
2021-03-26 07:47:15 +00:00
|
|
|
case kTypeTextUTF8:
|
2021-03-26 07:35:00 +00:00
|
|
|
creator = 'MPS ';
|
|
|
|
fileType = 'TEXT';
|
|
|
|
break;
|
|
|
|
case kTypeResource:
|
|
|
|
creator = 'RSED';
|
|
|
|
fileType = 'rsrc';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
print_err("invalid type");
|
|
|
|
return 1;
|
2021-03-24 07:31:00 +00:00
|
|
|
}
|
|
|
|
err = FSpCreate(&temp, creator, fileType, smSystemScript);
|
|
|
|
if (err == dupFNErr) {
|
|
|
|
err = FSpDelete(&temp);
|
|
|
|
if (err != 0) {
|
|
|
|
print_errcode(err, "could not delete existing temp file");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
err = FSpCreate(&temp, creator, fileType, smSystemScript);
|
|
|
|
}
|
|
|
|
if (err != 0) {
|
|
|
|
print_errcode(err, "could not create file");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
has_temp = true;
|
|
|
|
|
|
|
|
// Get buffers for conversion.
|
|
|
|
if (gSrcBuffer == NULL) {
|
|
|
|
gSrcBuffer = NewPtr(kBufferTotalSize);
|
|
|
|
if (gSrcBuffer == NULL) {
|
|
|
|
print_memerr(kBufferTotalSize);
|
|
|
|
goto error;
|
2021-03-16 16:08:52 +00:00
|
|
|
}
|
2021-03-24 07:31:00 +00:00
|
|
|
}
|
|
|
|
if (gDestBuffer == NULL) {
|
|
|
|
gDestBuffer = NewPtr(kBufferTotalSize);
|
|
|
|
if (gDestBuffer == NULL) {
|
|
|
|
print_memerr(kBufferTotalSize);
|
|
|
|
goto error;
|
2021-03-16 16:08:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-26 07:35:00 +00:00
|
|
|
// Open the source file for reading.
|
|
|
|
if (file->type == kTypeResource && mode == kModePush) {
|
|
|
|
err = FSpOpenRF(&src, fsRdPerm, &srcRef);
|
|
|
|
} else {
|
|
|
|
err = FSpOpenDF(&src, fsRdPerm, &srcRef);
|
|
|
|
}
|
|
|
|
if (err != 0) {
|
|
|
|
print_errcode(err, "could not open file");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (file->type == kTypeResource && mode == kModePull) {
|
|
|
|
err = FSpOpenRF(&temp, fsRdWrPerm, &destRef);
|
|
|
|
} else {
|
|
|
|
err = FSpOpenDF(&temp, fsRdWrPerm, &destRef);
|
|
|
|
}
|
|
|
|
if (err != 0) {
|
|
|
|
print_errcode(err, "could not open temp file");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2021-03-24 07:31:00 +00:00
|
|
|
// Convert data.
|
2021-03-26 07:35:00 +00:00
|
|
|
switch (file->type) {
|
|
|
|
case kTypeText:
|
|
|
|
if (mode == kModePush) {
|
|
|
|
r = mac_to_unix(srcRef, destRef, gSrcBuffer, gDestBuffer);
|
|
|
|
} else {
|
|
|
|
r = mac_from_unix(srcRef, destRef, gSrcBuffer, gDestBuffer);
|
|
|
|
}
|
2021-03-24 07:38:26 +00:00
|
|
|
break;
|
2021-03-26 07:47:15 +00:00
|
|
|
case kTypeTextUTF8: {
|
|
|
|
unsigned char srcEnding, destEnding;
|
|
|
|
if (mode == kModePush) {
|
|
|
|
srcEnding = 0x0d;
|
|
|
|
destEnding = 0x0a;
|
|
|
|
} else {
|
|
|
|
srcEnding = 0x0a;
|
|
|
|
destEnding = 0x0d;
|
|
|
|
}
|
|
|
|
r = convert_line_endings(srcRef, destRef, gSrcBuffer, srcEnding,
|
|
|
|
destEnding);
|
|
|
|
} break;
|
2021-03-26 07:35:00 +00:00
|
|
|
case kTypeResource:
|
|
|
|
r = copy_data(srcRef, destRef, gSrcBuffer);
|
2021-03-24 07:38:26 +00:00
|
|
|
break;
|
|
|
|
default:
|
2021-03-26 07:35:00 +00:00
|
|
|
print_err("invalid type");
|
2021-03-24 07:38:26 +00:00
|
|
|
goto error;
|
|
|
|
}
|
2021-03-16 16:08:52 +00:00
|
|
|
if (r != 0) {
|
2021-03-24 07:31:00 +00:00
|
|
|
goto error;
|
2021-03-16 16:08:52 +00:00
|
|
|
}
|
|
|
|
|
2021-03-24 07:31:00 +00:00
|
|
|
// Close files.
|
|
|
|
err = FSClose(srcRef);
|
|
|
|
srcRef = 0;
|
|
|
|
if (err != 0) {
|
|
|
|
print_errcode(err, "could not close source file");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
err = FSClose(destRef);
|
|
|
|
destRef = 0;
|
|
|
|
if (err != 0) {
|
|
|
|
print_errcode(err, "could not close temp file");
|
|
|
|
goto error;
|
|
|
|
}
|
2021-03-16 16:08:52 +00:00
|
|
|
|
2021-03-24 07:31:00 +00:00
|
|
|
// Set modification time.
|
|
|
|
r = set_modtime(&temp, file->meta[kSrcDir].modTime);
|
|
|
|
if (r != 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Overwrite destination.
|
|
|
|
r = replace_file(&temp, &dest, file->action);
|
|
|
|
if (r != 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
error:
|
2021-03-16 16:08:52 +00:00
|
|
|
// Clean up.
|
2021-03-24 07:31:00 +00:00
|
|
|
if (srcRef != 0) {
|
|
|
|
err = FSClose(srcRef);
|
|
|
|
if (err != 0) {
|
|
|
|
print_errcode(err, "could not close source file");
|
|
|
|
}
|
2021-03-16 16:08:52 +00:00
|
|
|
}
|
2021-03-24 07:31:00 +00:00
|
|
|
if (destRef != 0) {
|
|
|
|
err = FSClose(destRef);
|
|
|
|
if (err != 0) {
|
|
|
|
print_errcode(err, "could not close destination file");
|
|
|
|
}
|
2021-03-16 16:08:52 +00:00
|
|
|
}
|
2021-03-24 07:31:00 +00:00
|
|
|
if (has_temp) {
|
|
|
|
err = FSpDelete(&temp);
|
|
|
|
if (err != 0) {
|
|
|
|
print_errcode(err, "could not delete temp file");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
2021-03-16 16:08:52 +00:00
|
|
|
}
|