DIRECTORY
BasicTime USING [GMT, nullGMT, Period],
DFInternal
USING [
AbortDF, CheckAbort, Client, ClientDescriptor, DefaultInteractionProc, DoAbort, GetFileInfo, LocalFileInfo, RemoteFileInfo, ReportFSError, ShortName, SimpleInteraction, YesOrNo],
DFOperations
USING [
AbortInteraction, DFInfoInteraction, FileAction, FileInteraction, InfoInteraction, InteractionProc, SModelAction],
DFUtilities
USING [
Date, DateFormat, DateToRope, DirectoryItem, FileItem, Filter, GetVersionNumber, ImportsItem, IncludeItem, ParseFromStream, ProcessItemProc, RemoveVersionNumber, SyntaxError, WriteItemToStream],
FS
USING [
Copy, Create, Delete, Error, GetInfo, GetName, OpenFile, OpenFileFromStream, Rename, StreamFromOpenFile, StreamOpen],
FSExtras USING [NewCopy],
IO USING [card, Close, GetIndex, PutFR, rope, STREAM],
Rope USING [Concat, Equal, Length, ROPE];
SModelImpl:
CEDAR
MONITOR
IMPORTS BasicTime, DFInternal, DFUtilities, FS, FSExtras, IO, Rope
EXPORTS DFOperations =
SModel:
PUBLIC
PROC [
dfFile: ROPE, action: Ops.SModelAction ← [],
interact: Ops.InteractionProc ← NIL, clientData: REF ANY ← NIL, log: IO.STREAM ← NIL]
RETURNS [errors, warnings, filesActedUpon: INT ← 0] = {
client: Int.Client = NEW[Int.ClientDescriptor ← [interact, clientData, log]];
tempDF: ROPE = "SModelTemporaryDF$";
NewTemporaryDF:
ENTRY
PROC [pages:
INT]
RETURNS [out:
IO.
STREAM] = {
outFile: FS.OpenFile;
nSModels ← nSModels.SUCC;
outFile ←
FS.Create[
name: tempDF, pages: pages,
setKeep: TRUE, keep: IF nSModels = 1 THEN 1 ELSE CARDINAL.LAST
!
FS.Error => {
localInfo: REF Int.LocalFileInfo ← NEW[Int.LocalFileInfo ← [name: tempDF]];
Int.ReportFSError[error, localInfo, client, $abort];
}
];
RETURN[FS.StreamFromOpenFile[outFile, $write]]
};
CloseTemporaryDF:
PROC [out:
IO.
STREAM]
RETURNS [tempName:
ROPE] = {
outFile: FS.OpenFile = FS.OpenFileFromStream[out];
tempName ← outFile.GetName[].fullFName; -- includes version number
out.Close[];
};
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;
};
UserConfirms:
PROC [localInfo:
REF Int.LocalFileInfo, remoteInfo:
REF Int.RemoteFileInfo]
RETURNS [BOOL] = {
RETURN[Int.YesOrNo[
client: client,
message:
IO.PutFR[
"%g {%g} to %g ?",
IO.rope[localInfo.name],
IO.rope[Utils.DateToRope[localInfo.date]],
IO.rope[Utils.RemoveVersionNumber[remoteInfo.name]],
],
default: TRUE
]]
};
SModelInner:
PROC [dfFile:
ROPE, date: Utils.Date]
RETURNS [
REF Int.RemoteFileInfo] = {
directoryPath: ROPE ← NIL;
somethingChanged: BOOL ← FALSE;
dfRemoteInfo: REF Int.RemoteFileInfo = NEW[Int.RemoteFileInfo ← [name: dfFile, date: date]];
dfLocalInfo: REF Int.LocalFileInfo = NEW[Int.LocalFileInfo ← [name: Int.ShortName[dfFile]]];
DoOneItem: Utils.ProcessItemProc = {
StoreThisFile:
PROC [
localInfo: REF Int.LocalFileInfo, remoteInfo: REF Int.RemoteFileInfo,
formatFromDF: Utils.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[
Utils.RemoveVersionNumber[remoteInfo.name],
Utils.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[
Int.ShortName[remoteInfo.name],
Int.ShortName[localInfo.attachedTo],
FALSE]
THEN {
FS.Copy[
to: Utils.RemoveVersionNumber[localInfo.name],
from: remoteInfo.name, wantedCreatedTime: remoteInfo.date.gmt,
remoteCheck: action.remoteCheck,
attach: TRUE
! FS.Error => GO TO storeAnyway
];
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;
Int.SimpleInteraction[
client,
NEW[Ops.InfoInteraction ← [
class: $warning,
message:
IO.PutFR["%g {%g} is referenced with '>' in the DF file but is older than the remote version %g {%g}.",
IO.rope[localInfo.name],
IO.rope[Utils.DateToRope[localInfo.date]],
IO.rope[remoteInfo.name],
IO.rope[Utils.DateToRope[remoteInfo.date]]
]
]]
];
RETURN[FALSE]
};
ENDCASE;
ENDCASE;
};
ReportMissing:
PROC [localInfo:
REF Int.LocalFileInfo] = {
warnings ← warnings.SUCC;
Int.SimpleInteraction[
client,
NEW[Ops.InfoInteraction ← [
class: $warning,
message: IO.PutFR["'%g' doesn't exist.", IO.rope[localInfo.name]]
]]
];
};
SelfReference:
PROC [shortName:
ROPE]
RETURNS [
BOOL] = {
RETURN[shortName.Equal[dfLocalInfo.name, FALSE]]
};
Int.CheckAbort[client];
WITH item
SELECT
FROM
directory:
REF Utils.DirectoryItem =>
directoryPath ← directory.path1;
file:
REF Utils.FileItem => {
shortName: ROPE = Utils.RemoveVersionNumber[file.name];
localInfo: REF Int.LocalFileInfo ← NEW[Int.LocalFileInfo ← [shortName]];
remoteInfo:
REF Int.RemoteFileInfo ←
NEW[Int.RemoteFileInfo ← [
name: directoryPath.Concat[file.name],
date: file.date
]];
Int.GetFileInfo[localInfo
!
FS.Error =>
IF error.group = $user THEN {ReportMissing[localInfo]; GO TO skip}
ELSE Int.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;
Int.GetFileInfo[info: remoteInfo, notFoundOK: TRUE, client: client, errorLevel: $abort];
};
remoteInfo.name ← Utils.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 Int.RemoteFileInfo =
NEW[Int.RemoteFileInfo ← [name: remoteInfo.name]];
Int.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
~Int.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?",
IO.rope[shortName],
IO.rope[Utils.DateToRope[file.date]],
IO.rope[remoteDFInfo.name],
IO.rope[Utils.DateToRope[remoteDFInfo.date]]
],
default: FALSE
] THEN Int.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 {
Int.SimpleInteraction[
client,
NEW[Ops.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 ← Int.ShortName[
file: FSExtras.NewCopy[
to: remoteInfo.name, from: localInfo.name, attach: TRUE
! FS.Error => Int.ReportFSError[error, remoteInfo, client, $abort]],
keepVersion: TRUE
];
};
filesActedUpon ← filesActedUpon.SUCC;
SELECT file.date.format
FROM
$explicit, $omitted => file.date ← localInfo.date;
ENDCASE => file.name ← Utils.RemoveVersionNumber[file.name];
};
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 = Utils.GetVersionNumber[localInfo.attachedTo];
IF ~remoteVersion.Equal[Utils.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 Utils.IncludeItem => {
remoteInfo: REF Int.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 ← Utils.RemoveVersionNumber[include.path1];
};
imports:
REF Utils.ImportsItem => {
localInfo:
REF Int.LocalFileInfo =
NEW[Int.LocalFileInfo ← [name: Int.ShortName[imports.path1]]];
remoteInfo:
REF Int.RemoteFileInfo =
NEW[Int.RemoteFileInfo ← [name: imports.path1, date: imports.date]];
Int.GetFileInfo[localInfo
!
FS.Error =>
IF error.group = $user THEN {ReportMissing[localInfo]; GO TO skip}
ELSE Int.ReportFSError[error, localInfo, client, $abort]
];
IF action.remoteCheck
THEN {
Int.GetFileInfo[info: remoteInfo, notFoundOK: TRUE, client: client, errorLevel: $abort];
IF StoreThisFile[localInfo, remoteInfo, imports.date.format]
THEN {
warnings ← warnings.SUCC;
Int.SimpleInteraction[
client,
NEW[Ops.InfoInteraction ← [
class: $warning,
message:
IO.PutFR["%g {%g} doesn't correspond to %g {%g}.",
IO.rope[localInfo.name], IO.rope[Utils.DateToRope[localInfo.date]],
IO.rope[remoteInfo.name], IO.rope[Utils.DateToRope[remoteInfo.date]]
]
]]
];
};
imports.path1 ← remoteInfo.name;
};
SELECT imports.date.format
FROM
$explicit => NULL;
$omitted => {imports.date ← localInfo.date; somethingChanged ← TRUE};
ENDCASE => imports.path1 ← Utils.RemoveVersionNumber[imports.path1];
};
ENDCASE;
Utils.WriteItemToStream[out, item];
};
in:
IO.
STREAM =
FS.StreamOpen[dfLocalInfo.name, $read
! FS.Error => Int.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];
Int.SimpleInteraction[
client,
NEW[Ops.DFInfoInteraction ← [action: start, dfFile: dfLocalInfo.name]]
];
Utils.ParseFromStream[in, DoOneItem, Utils.Filter[comments:
TRUE] !
Utils.SyntaxError
-- [reason: ROPE]-- =>
Int.DoAbort[
log,
IO.PutFR["Syntax error in '%g'[%d]: %g",
IO.rope[dfLocalInfo.name], IO.card[in.GetIndex[]], IO.rope[reason]
],
interact,
clientData
];
Int.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: Utils.DateFormat = dfRemoteInfo.date.format;
dfRemoteInfo.date ← dfLocalInfo.date;
IF action.storeChanged
THEN {
Int.SimpleInteraction[
client,
NEW[Ops.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 => Int.ReportFSError[error, dfRemoteInfo, client, $abort]];
FinishWithTemporaryDF[tempName, dfLocalInfo.name];
};
filesActedUpon ← filesActedUpon.SUCC;
}
ELSE FinishWithTemporaryDF[CloseTemporaryDF[out], NIL, FALSE];
Int.SimpleInteraction[
client,
NEW[Ops.DFInfoInteraction ← [
action: end,
dfFile: dfLocalInfo.name,
message: IF somethingChanged THEN NIL ELSE "DF file is unchanged"
]]
];
RETURN[dfRemoteInfo]
};
IF interact = NIL THEN interact ← Int.DefaultInteractionProc;
[] ← SModelInner[dfFile, [] !
ABORTED => {
Int.SimpleInteraction[client, NEW[Ops.AbortInteraction ← [TRUE]]];
We would like to RESUME at this point, but until ABORTED is redeclared as a SIGNAL, it won't work.
};
Int.AbortDF => {
errors ← errors.SUCC;
Int.SimpleInteraction[client, NEW[Ops.DFInfoInteraction ← [action: $abort, dfFile: dfFile]]];
CONTINUE
};
];
};