SModelImpl.mesa
Copyright © 1984, 1985 by Xerox Corporation. All rights reserved.
Levin on December 19, 1983 4:20 pm
Bob Hagmann on May 18, 1984 9:04:25 am PDT
Russ Atkinson (RRA) August 16, 1985 3:05:58 pm PDT
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, ExpandName, FileInfo, GetInfo, GetName, OpenFile, OpenFileFromStream, Rename, StreamFromOpenFile, StreamOpen],
FSPseudoServers USING [TranslateForWrite],
IO USING [Close, GetIndex, PutFR, PutFR1, STREAM],
Rope USING [Concat, Equal, Length, Match, Replace, ROPE, Run, SkipTo, Substr];
SModelImpl: CEDAR MONITOR
IMPORTS BasicTime, DFInternal, DFUtilities, FS, FSPseudoServers, IO, Rope
EXPORTS DFOperations = BEGIN
ROPE: TYPE = Rope.ROPE;
STREAM: TYPE = IO.STREAM;
Global (monitored) variables
nSModels: NAT ← 0;
Exported to DFOperations
SModel: PUBLIC PROC [dfFile: ROPE, action: DFOperations.SModelAction ← [], interact: DFOperations.InteractionProc ← NIL, clientData: REF ANYNIL, log: STREAMNIL] RETURNS [errors, warnings, filesActedUpon: INT ← 0] = {
client: DFInternal.Client = NEW[DFInternal.ClientDescriptor ← [
(interact ← IF interact = NIL THEN DFInternal.DefaultInteractionProc ELSE interact),
clientData,
log
]];
tempDF: ROPE = "SModelTemporaryDF$";
NewTemporaryDF: ENTRY PROC [pages: INT] RETURNS [out: 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 DFInternal.LocalFileInfo ← NEW[DFInternal.LocalFileInfo ← [name: tempDF]];
DFInternal.ReportFSError[error, localInfo, client, $abort];
}
];
RETURN[FS.StreamFromOpenFile[outFile, $write]]
};
CloseTemporaryDF: PROC [out: STREAM] RETURNS [tempName: ROPE] = {
outFile: FS.OpenFile = FS.OpenFileFromStream[out];
tempNameoutFile.GetName[].fullFName; -- includes version number
out.Close[];
};
FinishWithTemporaryDF: ENTRY PROC [
tempName: ROPE, localName: ROPE, commit: BOOLTRUE] = {
IF commit THEN FS.Rename[from: tempName, to: localName]
ELSE FS.Delete[tempName];
nSModels ← nSModels.PRED;
};
UserConfirms: PROC [localInfo: REF DFInternal.LocalFileInfo, remoteInfo: REF DFInternal.RemoteFileInfo] RETURNS [BOOL] = {
RETURN[DFInternal.YesOrNo[
client: client,
message:
IO.PutFR[
"%g {%g} to %g ?",
[rope[localInfo.name]],
[rope[DFUtilities.DateToRope[localInfo.date]]],
[rope[DFUtilities.RemoveVersionNumber[remoteInfo.name]]],
],
default: TRUE
]]
};
SModelInner: PROC [dfFile: ROPE, date: DFUtilities.Date] RETURNS [REF DFInternal.RemoteFileInfo] = {
directoryPath: ROPENIL;
writeDirectoryPath: ROPENIL;
directoryLen: INT ← 0;
somethingChanged: BOOLFALSE;
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[info: localInfo, remoteCheck: FALSE
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.PutFR1["'%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 => {
Get the directory in angle-bracket format
directoryPath ← directory.path1 ← FS.ExpandName[directory.path1].fullFName;
directoryLen ← Rope.Length[directoryPath];
writeDirectoryPath ← TranslateHost[directoryPath];
};
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: FALSE
! 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). We do the checking with the name suitably translated for the write server, since that gets around extra checking on the read servers, which are allowed to lag the write server anyway.
CheckWriteInfo[Rope.Concat[writeDirectoryPath, file.name], localInfo, remoteInfo
! FS.Error => DFInternal.ReportFSError[error, remoteInfo, client, $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]];
CheckWriteInfo[TranslateHost[remoteDFInfo.name], localInfo, remoteDFInfo
! FS.Error => DFInternal.ReportFSError[error, remoteDFInfo, client, $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, "Obsolete self-reference; remote version is older than date in DF file."];
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 ← FS.Copy[
to: remoteInfo.name, from: localInfo.name, attach: TRUE
! FS.Error => DFInternal.ReportFSError[error, remoteInfo, client, $abort]];
IF Rope.Run[file.name, 0, directoryPath, 0, FALSE] = directoryLen
THEN {
Factor out the directory path
file.name ← Rope.Substr[file.name, directoryLen];
}
ELSE {
DFInternal.DoAbort[client.log, IO.PutFR1["Bogus file name (%g).", [rope[file.name]] ]];
};
};
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;
};
EXITS
skip => NULL;
};
include: REF DFUtilities.IncludeItem => {
path: ROPE ← include.path1 ← FS.ExpandName[include.path1].fullFName;
remoteInfo: REF DFInternal.RemoteFileInfo = SModelInner[path, include.date];
IF ~Rope.Equal[remoteInfo.name, path, 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: FALSE
! FS.Error =>
IF error.group = $user THEN {ReportMissing[localInfo]; GO TO skip}
ELSE DFInternal.ReportFSError[error, localInfo, client, $abort]
];
SELECT TRUE FROM
imports.date.gmt = BasicTime.nullGMT => {
warnings ← warnings.SUCC;
DFInternal.SimpleInteraction[
client,
NEW[DFOperations.InfoInteraction ← [
class: $warning,
message: IO.PutFR["Local import %g not found.",
[rope[localInfo.name]], [rope[DFUtilities.DateToRope[localInfo.date]]]
]
]]
];
};
imports.date.format = $explicit AND imports.date.gmt # localInfo.date.gmt => {
warnings ← warnings.SUCC;
DFInternal.SimpleInteraction[
client,
NEW[DFOperations.InfoInteraction ← [
class: $warning,
message: IO.PutFR["Local import %g does not have requested date.",
[rope[localInfo.name]], [rope[DFUtilities.DateToRope[localInfo.date]]]
]
]]
];
};
ENDCASE;
IF action.remoteCheck THEN {
CheckWriteInfo[TranslateHost[remoteInfo.name], localInfo, remoteInfo
! FS.Error => DFInternal.ReportFSError[error, remoteInfo, client, $abort];
];
IF localInfo.date # remoteInfo.date 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];
EXITS
skip => NULL;
};
ENDCASE;
DFUtilities.WriteItemToStream[out, item];
};
in: STREAM = FS.StreamOpen[dfLocalInfo.name, $read
! FS.Error => DFInternal.ReportFSError[error, dfLocalInfo, client, $abort]
];
inFile: FS.OpenFile = FS.OpenFileFromStream[in];
out: 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
]]
];
[] ← FS.Copy[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
};
];
};
CheckWriteInfo: PROC [path: ROPE, localInfo: REF DFInternal.LocalFileInfo, remoteInfo: REF DFInternal.RemoteFileInfo] = {
remoteInfo.date.gmt ← FS.FileInfo[
name: path,
wantedCreatedTime: localInfo.date.gmt
! FS.Error =>
SELECT error.code FROM
$unknownCreatedTime, $unknownFile => {
File not found, so just continue with updated info
remoteInfo.date.gmt ← BasicTime.nullGMT;
remoteInfo.date.format ← $omitted;
CONTINUE;
};
ENDCASE;
].created;
};
TranslateHost: PROC [path: ROPE] RETURNS [ROPE] = {
IF Rope.Match["[*]*", path] THEN {
rPos: INT ← Rope.SkipTo[path, 1, "]"];
host: ROPE ← Rope.Substr[path, 1, rPos-1];
writeHost: ROPE ← FSPseudoServers.TranslateForWrite[host];
IF NOT Rope.Equal[host, writeHost, FALSE] THEN
RETURN [Rope.Replace[path, 1, rPos-1, writeHost]];
};
RETURN [path];
};
END.