client: DFInternal.Client =
NEW[DFInternal.ClientDescriptor ← [
(interact ← IF interact = NIL THEN DFInternal.DefaultInteractionProc ELSE interact),
clientData,
log
]];
FinishWithTemporaryDF:
ENTRY
PROC [
tempName: ROPE, localName: ROPE, commit: BOOL ← TRUE] = {
IF commit THEN FS.Rename[from: tempName, to: localName]
ELSE FS.Delete[tempName];
nSModels ← nSModels.PRED;
};
SModelInner:
PROC [dfFile:
ROPE, date: DFUtilities.Date]
RETURNS [
REF DFInternal.RemoteFileInfo] = {
directoryPath: ROPE ← NIL;
somethingChanged: BOOL ← FALSE;
dfRemoteInfo: REF DFInternal.RemoteFileInfo = NEW[DFInternal.RemoteFileInfo ← [name: dfFile, date: date]];
dfLocalInfo: REF DFInternal.LocalFileInfo = NEW[DFInternal.LocalFileInfo ← [name: DFInternal.ShortName[dfFile]]];
DoOneItem: DFUtilities.ProcessItemProc = {
StoreThisFile:
PROC [
localInfo: REF DFInternal.LocalFileInfo, remoteInfo: REF DFInternal.RemoteFileInfo,
formatFromDF: DFUtilities.DateFormat]
RETURNS [store: BOOL] = {
This procedure returns TRUE if, in principle, the file should be stored. It does not alter either 'localInfo' or 'remoteInfo' and does not let the user influence its decision.
lGMT: BasicTime.GMT = localInfo.date.gmt; -- can't be nullGMT
rGMT: BasicTime.GMT = remoteInfo.date.gmt; -- may be nullGMT
If the local file is not attached, we store it anyway, so that the attachment will be made.
IF localInfo.attachedTo.Length[] = 0 THEN RETURN[TRUE];
If the local file is attached to a remote file whose name differs from the one specified in the DF, we store it even if the dates match, so that the proper attachment will be made.
IF ~Rope.Equal[
DFUtilities.RemoveVersionNumber[remoteInfo.name],
DFUtilities.RemoveVersionNumber[localInfo.attachedTo],
FALSE]
THEN {
If the short names of the two files are the same and they have the same date, we assume that the remote file is good enough. We need to make an attachment "the other way", however, and prevent the main loop from doing anything.
IF remoteInfo.date = localInfo.date
AND Rope.Equal[
DFInternal.ShortName[remoteInfo.name],
DFInternal.ShortName[localInfo.attachedTo],
FALSE]
THEN {
FS.Copy[
to: DFUtilities.RemoveVersionNumber[localInfo.name],
from: remoteInfo.name, wantedCreatedTime: remoteInfo.date.gmt,
remoteCheck: action.remoteCheck,
attach: TRUE
! FS.Error => GO TO storeAnyway
];
localInfo.date.format ← $omitted ; -- force GetFileInfo to get the highest number version (that we just wrote)
DFInternal.GetFileInfo[localInfo
-- update attachedTo
! FS.Error => DFInternal.ReportFSError[error, localInfo, client, $abort]
];
RETURN[FALSE];
EXITS
storeAnyway => NULL;
};
RETURN[TRUE]
};
The following tests need to be done delicately, since we may not know the actual truth about the remote file, but only what the DF file claims is true. In particular, it is quite possible that remoteDate.gmt = nullGMT when a remote file actually exists. (This case arises if formatFromDF ~= $explicit and ~action.remoteCheck.)
SELECT formatFromDF
FROM
$explicit, $omitted => RETURN[lGMT ~= rGMT];
$notEqual => RETURN[action.remoteCheck AND lGMT ~= rGMT];
$greaterThan =>
SELECT
TRUE
FROM
~action.remoteCheck => RETURN[FALSE];
remoteInfo.date.format ~= $explicit => RETURN[TRUE]; -- no extant remote file
ENDCASE =>
SELECT BasicTime.Period[from: lGMT, to: rGMT]
FROM
< 0 => RETURN[TRUE];
= 0 => RETURN[FALSE];
> 0 => {
warnings ← warnings.SUCC;
DFInternal.SimpleInteraction[
client,
NEW[DFOperations.InfoInteraction ← [
class: $warning,
message:
IO.PutFR["%g {%g} is referenced with '>' in the DF file but is older than the remote version %g {%g}.",
[rope[localInfo.name]],
[rope[DFUtilities.DateToRope[localInfo.date]]],
[rope[remoteInfo.name]],
[rope[DFUtilities.DateToRope[remoteInfo.date]]]
]
]]
];
RETURN[FALSE]
};
ENDCASE;
ENDCASE;
};
ReportMissing:
PROC [localInfo:
REF DFInternal.LocalFileInfo] = {
warnings ← warnings.SUCC;
DFInternal.SimpleInteraction[
client,
NEW[DFOperations.InfoInteraction ← [
class: $warning,
message: IO.PutFR["'%g' doesn't exist.", [rope[localInfo.name]]]
]]
];
};
SelfReference:
PROC [shortName:
ROPE]
RETURNS [
BOOL] = {
RETURN[shortName.Equal[dfLocalInfo.name, FALSE]]
};
DFInternal.CheckAbort[client];
WITH item
SELECT
FROM
directory:
REF DFUtilities.DirectoryItem =>
directoryPath ← directory.path1;
file:
REF DFUtilities.FileItem => {
shortName: ROPE = DFUtilities.RemoveVersionNumber[file.name];
localInfo: REF DFInternal.LocalFileInfo ← NEW[DFInternal.LocalFileInfo ← [shortName]];
remoteInfo:
REF DFInternal.RemoteFileInfo ←
NEW[DFInternal.RemoteFileInfo ← [
name: directoryPath.Concat[file.name],
date: file.date
]];
DFInternal.GetFileInfo[info: localInfo, remoteCheck:
TRUE
!
FS.Error =>
IF error.group = $user THEN {ReportMissing[localInfo]; GO TO skip}
ELSE DFInternal.ReportFSError[error, localInfo, client, $abort]
];
IF action.remoteCheck
THEN {
The local file is known to exist. Any remote file with whose date matches will be satisfactory. Note that remoteInfo.name will typically have a version (hint).
remoteInfo.date ← localInfo.date;
DFInternal.GetFileInfo[info: remoteInfo, notFoundOK: TRUE, client: client, errorLevel: $abort];
};
remoteInfo.name ← DFUtilities.RemoveVersionNumber[remoteInfo.name];
At this point, remoteInfo.date.format will be $explicit if we are supposed to believe that there is a remote file with create date remoteInfo.date.gmt, and remoteInfo.date.format will be $omitted if we are supposed to believe no such file exists. Furthermore, remoteInfo.name has no version number.
SELECT
TRUE
FROM
SelfReference[shortName] => {
This file item is a reference to the DF file we are updating. We check to see if the !H version on the remote server is newer than the input version of the DF file. If so, the user is warned, since he is probably using an obsolete DF file.
remoteDFInfo:
REF DFInternal.RemoteFileInfo =
NEW[DFInternal.RemoteFileInfo ← [name: remoteInfo.name]];
DFInternal.GetFileInfo[
info: remoteDFInfo, notFoundOK: TRUE, client: client, errorLevel: $abort];
IF remoteDFInfo.date.gmt ~= BasicTime.nullGMT
AND
file.date.format = $explicit AND
BasicTime.Period[from: file.date.gmt, to: remoteDFInfo.date.gmt] > 0 AND
~DFInternal.YesOrNo[
client: client,
message:
IO.PutFR[
"%g may be obsolete, since the remote version from which it is derived {%g} is older than %g {%g} (the present newest remote one); continue anyway?",
[rope[shortName]],
[rope[DFUtilities.DateToRope[file.date]]],
[rope[remoteDFInfo.name]],
[rope[DFUtilities.DateToRope[remoteDFInfo.date]]]
],
default: FALSE,
blunder: TRUE
] THEN DFInternal.DoAbort[client.log];
IF StoreThisFile[localInfo, remoteInfo, file.date.format]
THEN
somethingChanged ← TRUE; -- we'll actually store it later, if user approves.
dfRemoteInfo.name ← remoteInfo.name; -- redundant unless top-level DF
file.name ← shortName; -- ensure no version number
SELECT file.date.format
FROM
$explicit, $omitted => file.date ← [$explicit, outFile.GetInfo[].created];
ENDCASE;
};
StoreThisFile[localInfo, remoteInfo, file.date.format] AND
UserConfirms[localInfo, remoteInfo] => {
somethingChanged ← TRUE;
IF action.storeChanged
THEN {
DFInternal.SimpleInteraction[
client,
NEW[DFOperations.FileInteraction ← [
localFile: localInfo.name,
remoteFile: remoteInfo.name,
dateFormat:
SELECT file.date.format
FROM
$explicit => $explicit,
$greaterThan => $greaterThan,
ENDCASE => $notEqual,
date: localInfo.date.gmt,
action: store
]]
];
file.name ← DFInternal.ShortName[
file: FSExtras.NewCopy[
to: remoteInfo.name, from: localInfo.name, attach: TRUE
! FS.Error => DFInternal.ReportFSError[error, remoteInfo, client, $abort]],
keepVersion: TRUE
];
};
filesActedUpon ← filesActedUpon.SUCC;
SELECT file.date.format
FROM
$explicit, $omitted => file.date ← localInfo.date;
ENDCASE => file.name ← shortName;
};
ENDCASE =>
If the file's date and/or (remote) version number is missing or incorrect, we may wish to update them/it. Because of the logic on the earlier call of GetFileInfo, the local file is known to exist. However, it may or may not be attached to a remote file, since the user may have explicitly prohibited a Copy in the preceding arm of the SELECT. If it isn't attached, we don't want to disturb the DF file entry.
IF localInfo.attachedTo.Length ~= 0
THEN {
remoteVersion: ROPE = DFUtilities.GetVersionNumber[localInfo.attachedTo];
IF ~remoteVersion.Equal[DFUtilities.GetVersionNumber[file.name]]
THEN {
file.name ← shortName.Concat[remoteVersion];
somethingChanged ← TRUE;
};
SELECT file.date.format
FROM
$omitted => {
file.date ← localInfo.date;
somethingChanged ← TRUE;
};
$explicit =>
IF file.date ~= localInfo.date
THEN {
file.date ← localInfo.date;
somethingChanged ← TRUE;
};
ENDCASE;
};
};
include:
REF DFUtilities.IncludeItem => {
remoteInfo: REF DFInternal.RemoteFileInfo = SModelInner[include.path1, include.date];
IF ~Rope.Equal[remoteInfo.name, include.path1,
FALSE]
THEN {
include.path1 ← remoteInfo.name; somethingChanged ← TRUE};
SELECT include.date.format
FROM
$explicit, $omitted => {include.date ← remoteInfo.date; somethingChanged ← TRUE};
ENDCASE => include.path1 ← DFUtilities.RemoveVersionNumber[include.path1];
};
imports:
REF DFUtilities.ImportsItem => {
localInfo:
REF DFInternal.LocalFileInfo =
NEW[DFInternal.LocalFileInfo ← [name: DFInternal.ShortName[imports.path1]]];
remoteInfo:
REF DFInternal.RemoteFileInfo =
NEW[DFInternal.RemoteFileInfo ← [name: imports.path1, date: imports.date]];
DFInternal.GetFileInfo[info: localInfo, remoteCheck:
TRUE
!
FS.Error =>
IF error.group = $user THEN {ReportMissing[localInfo]; GO TO skip}
ELSE DFInternal.ReportFSError[error, localInfo, client, $abort]
];
IF action.remoteCheck
THEN {
DFInternal.GetFileInfo[info: remoteInfo, notFoundOK: TRUE, client: client, errorLevel: $abort];
IF StoreThisFile[localInfo, remoteInfo, imports.date.format]
THEN {
warnings ← warnings.SUCC;
DFInternal.SimpleInteraction[
client,
NEW[DFOperations.InfoInteraction ← [
class: $warning,
message:
IO.PutFR["%g {%g} doesn't correspond to %g {%g}.",
[rope[localInfo.name]], [rope[DFUtilities.DateToRope[localInfo.date]]],
[rope[remoteInfo.name]], [rope[DFUtilities.DateToRope[remoteInfo.date]]]
]
]]
];
};
imports.path1 ← remoteInfo.name;
};
SELECT imports.date.format
FROM
$explicit => NULL;
$omitted => {imports.date ← localInfo.date; somethingChanged ← TRUE};
ENDCASE => imports.path1 ← DFUtilities.RemoveVersionNumber[imports.path1];
};
ENDCASE;
DFUtilities.WriteItemToStream[out, item];
};
in:
IO.
STREAM =
FS.StreamOpen[dfLocalInfo.name, $read
! FS.Error => DFInternal.ReportFSError[error, dfLocalInfo, client, $abort]
];
inFile: FS.OpenFile = FS.OpenFileFromStream[in];
out: IO.STREAM = NewTemporaryDF[inFile.GetInfo[].pages];
outFile: FS.OpenFile = FS.OpenFileFromStream[out];
dfLocalInfo.date ← [$explicit, inFile.GetInfo[].created];
DFInternal.SimpleInteraction[
client,
NEW[DFOperations.DFInfoInteraction ← [action: start, dfFile: dfLocalInfo.name]]
];
DFUtilities.ParseFromStream[in, DoOneItem, DFUtilities.Filter[comments:
TRUE] !
DFUtilities.SyntaxError
-- [reason: ROPE]-- =>
DFInternal.DoAbort[
log,
IO.PutFR["Syntax error in '%g'[%d]: %g",
[rope[dfLocalInfo.name]], [cardinal[in.GetIndex[]]], [rope[reason]]
],
interact,
clientData
];
DFInternal.AbortDF => {
in.Close[];
FinishWithTemporaryDF[CloseTemporaryDF[out], NIL, FALSE];
};
];
in.Close[];
dfLocalInfo.date.gmt ← outFile.GetInfo[].created;
IF somethingChanged
AND UserConfirms[dfLocalInfo, dfRemoteInfo]
THEN {
tempName: ROPE = CloseTemporaryDF[out];
dfRemoteDateFormat: DFUtilities.DateFormat = dfRemoteInfo.date.format;
dfRemoteInfo.date ← dfLocalInfo.date;
IF action.storeChanged
THEN {
DFInternal.SimpleInteraction[
client,
NEW[DFOperations.FileInteraction ← [
localFile: dfLocalInfo.name,
remoteFile: dfRemoteInfo.name,
dateFormat:
SELECT dfRemoteDateFormat
FROM
$explicit => $explicit,
$greaterThan => $greaterThan,
ENDCASE => $notEqual,
date: dfLocalInfo.date.gmt,
action: $store
]]
];
[] ← FSExtras.NewCopy[to: dfRemoteInfo.name, from: tempName, attach:
TRUE
! FS.Error => DFInternal.ReportFSError[error, dfRemoteInfo, client, $abort]];
FinishWithTemporaryDF[tempName, dfLocalInfo.name];
};
filesActedUpon ← filesActedUpon.SUCC;
}
ELSE FinishWithTemporaryDF[CloseTemporaryDF[out], NIL, FALSE];
DFInternal.SimpleInteraction[
client,
NEW[DFOperations.DFInfoInteraction ← [
action: end,
dfFile: dfLocalInfo.name,
message: IF somethingChanged THEN NIL ELSE "DF file is unchanged"
]]
];
RETURN[dfRemoteInfo]
};
[] ← SModelInner[dfFile, [] !
ABORTED => {
DFInternal.SimpleInteraction[client, NEW[DFOperations.AbortInteraction ← [TRUE]]];
We would like to RESUME at this point, but until ABORTED is redeclared as a SIGNAL, it won't work.
};
DFInternal.AbortDF => {
errors ← errors.SUCC;
DFInternal.SimpleInteraction[client, NEW[DFOperations.DFInfoInteraction ← [action: $abort, dfFile: dfFile]]];
CONTINUE
};
];