DIRECTORY
BasicTime USING [Period],
DFInternal
USING [
AbortDF, CheckAbort, Client, ClientDescriptor, DefaultInteractionProc, GetFileInfo, LocalFile, LocalFileInfo, RemoteFileInfo, ReportFSError, ShortName, SimpleInteraction, YesOrNo],
DFOperations
USING [
AbortInteraction, BringOverAction, BringOverFilter, DFInfoInteraction, FileAction, FileInteraction, InfoInteraction, InteractionProc],
DFUtilities
USING [
ClassifyFileExtension, Date, DateToRope, DifferenceOfUsingLists, DirectoryItem, FileItem, Filter, ImportsItem, IncludeItem, ParseFromStream, ProcessItemProc, RemoveVersionNumber, SortUsingList, SyntaxError, UsingEntry, UsingList],
FS USING [Close, Copy, Error, Open, StreamOpen],
IO USING [card, Close, GetIndex, PutFR, rope, STREAM],
Rope USING [Cat, Concat, Equal, ROPE];
BringOverImpl:
CEDAR
PROGRAM
IMPORTS BasicTime, DFInternal, DFUtilities, FS, IO, Rope
EXPORTS DFOperations =
BringOver:
PUBLIC
PROC [
dfFile: ROPE, filter: Ops.BringOverFilter ← [], action: Ops.BringOverAction ← $enter,
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]];
retrievedFiles: REF Utils.UsingList ← NIL; -- for error reporting only
ConvertFilter:
PROC [f: Ops.BringOverFilter]
RETURNS [filter: Utils.Filter] = {
The following is a cheat; it assumes that Ops.BringOverFilterN and Utils.FilterN are in the same order, for N = A, B, C. LOOPHOLE would also work.
filter ← [filterA: VAL[f.filterA.ORD], filterB: VAL[f.filterB.ORD], filterC: VAL[f.filterC.ORD]];
IF f.list ~=
NIL
THEN {
nEntries: NAT ← 0;
i: NAT ← 0;
FOR l:
LIST
OF
ROPE ← f.list, l.rest
UNTIL l =
NIL
DO
nEntries ← nEntries.SUCC;
ENDLOOP;
filter.list ← NEW[Utils.UsingList[nEntries]];
FOR l:
LIST
OF
ROPE ← f.list, l.rest
UNTIL l =
NIL
DO
file: ROPE = Utils.RemoveVersionNumber[l.first];
IF ~(filter.filterA = $all
OR Utils.ClassifyFileExtension[file] = filter.filterA)
THEN {
warnings ← warnings.SUCC;
Int.SimpleInteraction[
client,
NEW[Ops.InfoInteraction ← [
class: $warning,
message: Rope.Cat["'", file, "' could not be found in any nested DF file."]
]]
];
};
filter.list.u[i] ← Utils.UsingEntry[name: file];
i ← i.SUCC;
ENDLOOP;
filter.list.nEntries ← nEntries;
Utils.SortUsingList[usingList: filter.list, nearlySorted: FALSE];
};
};
BringOverInner:
PROC [dfFile:
ROPE, date: Utils.Date, filter: Utils.Filter]
RETURNS [retrieved: NAT ← 0] = {
Note: 'retrieved' is maintained, and therefore meaningful, only if filter.list ~= NIL.
requested: NAT = IF filter.list ~= NIL THEN filter.list.nEntries ELSE NAT.LAST;
Note: 'iAllocatedRetrievedFiles' is TRUE iff this instance of 'BringOverInner' is the outermost one for which filter.list ~= NIL. Thus, when recursion below this instance is complete, all of filter.list should have been retrieved.
iAllocatedRetrievedFiles: BOOL ← FALSE;
directoryPath: ROPE ← NIL;
Localize:
PROC [remote:
ROPE, date: Utils.Date, action: Ops.BringOverAction]
RETURNS [localInfo: REF Int.LocalFileInfo, dontProcess: BOOL ← FALSE] = {
remoteInfo: REF Int.RemoteFileInfo ← NEW[Int.RemoteFileInfo ← [remote, date]];
AttachNeeded:
PROC
RETURNS [action: {alreadyLocal, doAttach, userSaidNo}] = {
attach: BOOL ← TRUE;
sameDate: BOOL = (localInfo.date.gmt = remoteInfo.date.gmt);
IF Int.LocalFile[remoteInfo.name] THEN RETURN[$alreadyLocal];
Assert: remoteInfo.date.gmt ~= BasicTime.nullGMT
Assert: (localInfo.date.format = $explicit and localInfo.date.gmt ~= BasicTime.nullGMT) iff local file exists.
IF localInfo.date.format = $explicit
AND Rope.Equal[
Utils.RemoveVersionNumber[localInfo.attachedTo],
Utils.RemoveVersionNumber[remoteInfo.name],
FALSE] THEN
Both a local and a remote file exist and the local file is attached to a file whose name (without version) matches the remote file's name (without version). Check to see if they have the correct relationship in time.
SELECT date.format
FROM
$greaterThan =>
attach ← BasicTime.Period[from: localInfo.date.gmt, to: remoteInfo.date.gmt] > 0;
ENDCASE => attach ← ~sameDate;
IF ~attach THEN RETURN[$alreadyLocal];
IF ~sameDate
AND ~Int.YesOrNo[
client: client,
message:
IO.PutFR[
"%g {%g} to %g%g?",
IO.rope[remoteInfo.name],
IO.rope[Utils.DateToRope[remoteInfo.date]],
IO.rope[localInfo.name],
IO.rope[
IF localInfo.date.format = $explicit
THEN
IO.PutFR[
" (previously: %g, keep: %d) ",
IO.rope[Utils.DateToRope[localInfo.date]],
IO.card[localInfo.keep]
]
ELSE NIL
]
],
default: TRUE
] THEN RETURN[$userSaidNo];
IF localInfo.date.format = $explicit
AND localInfo.keep = 1
AND
BasicTime.Period[from: remoteInfo.date.gmt, to: localInfo.date.gmt] > 0 AND
Utils.ClassifyFileExtension[localInfo.name] = $source AND
~Int.YesOrNo[
client: client,
message:
IO.PutFR[
"Are you sure? (%g {%g} is older than %g {%g} and will overwrite it)",
IO.rope[remoteInfo.name],
IO.rope[Utils.DateToRope[remoteInfo.date]],
IO.rope[localInfo.name],
IO.rope[Utils.DateToRope[localInfo.date]]
],
default: TRUE
] THEN RETURN[$userSaidNo];
RETURN[$doAttach]
};
localInfo ←
NEW[Int.LocalFileInfo ← [name: Int.ShortName[remote
! FS.Error => {Int.ReportFSError[error, remoteInfo, client]; GO TO quit}
]]];
Int.GetFileInfo[info: localInfo, notFoundOK: TRUE, client: client ! FS.Error => GO TO quit];
localInfo.name ← Utils.RemoveVersionNumber[localInfo.name];
IF ~(date.format = $explicit
AND action = $enter)
THEN
Int.GetFileInfo[info: remoteInfo, client: client ! FS.Error => GO TO quit];
SELECT AttachNeeded[]
FROM
$alreadyLocal => NULL;
$doAttach => {
Int.SimpleInteraction[
client,
NEW[Ops.FileInteraction ← [
remoteFile: remoteInfo.name,
localFile: localInfo.name,
dateFormat:
SELECT remoteInfo.date.format
FROM
$explicit => $explicit,
$greaterThan => $greaterThan,
ENDCASE => $notEqual,
date: remoteInfo.date.gmt,
action: IF action = $check THEN $check ELSE $fetch
]]
];
IF action ~= $check
THEN
FS.Copy[
to: localInfo.name,
from: remoteInfo.name, wantedCreatedTime: remoteInfo.date.gmt,
setKeep: FALSE,
keep: IF Utils.ClassifyFileExtension[localInfo.name] = $source THEN 2 ELSE 1,
remoteCheck: action ~= $enter, attach: TRUE
! FS.Error => {Int.ReportFSError[error, localInfo, client]; GO TO quit}
];
filesActedUpon ← filesActedUpon.SUCC;
};
$userSaidNo => dontProcess ← TRUE;
ENDCASE;
IF ~ dontProcess
AND action = $fetch
THEN {
ENABLE FS.Error => {Int.ReportFSError[error, remoteInfo, client]; GO TO quit};
FS.Close[FS.Open[localInfo.name]];
};
EXITS
quit => {errors ← errors.SUCC; dontProcess ← TRUE};
};
DoOneItem: Utils.ProcessItemProc = {
Int.CheckAbort[client];
IF retrievedFiles ~=
NIL
AND retrievedFiles.nEntries = retrievedFiles.length
THEN
RETURN[TRUE];
WITH item
SELECT
FROM
directory:
REF Utils.DirectoryItem =>
directoryPath ← directory.path1;
file:
REF Utils.FileItem => {
remoteName: ROPE = Rope.Concat[directoryPath, file.name];
localInfo: REF Int.LocalFileInfo = Localize[remoteName, file.date, action].localInfo;
IF retrievedFiles ~=
NIL
THEN {
Some enclosing call of BringOverInner presented a filter with an explicit list, so the number of files to be retrieved at that point was known then ('retrievedFiles.u.length'). We maintain the names of the files actually retrieved to enable intelligent error reporting at the end if not all of the files in the original filter.list are found.
retrievedFiles.u[retrievedFiles.nEntries] ← [name: Int.ShortName[localInfo.name]];
retrievedFiles.nEntries ← retrievedFiles.nEntries.SUCC;
};
IF filter.list ~=
NIL
AND (retrieved ← retrieved.
SUCC) = filter.list.nEntries
THEN
RETURN[TRUE];
};
imports:
REF Utils.ImportsItem => {
newFilter: Utils.Filter = [
comments: filter.comments, -- comments processing is unaffected by imports
filterA: filter.filterA, -- source/derived distinction is unaffected by imports
filterB: IF imports.form = $exports THEN $public ELSE filter.filterB,
filterC: $all, -- if the top level passes imports, they can come from anywhere
list: IF imports.form = $list THEN imports.list ELSE filter.list
];
SELECT
TRUE
FROM
newFilter.list =
NIL =>
In this case, both filter.list and imports.list are NIL. There are no optimizations available to permit early termination of BringOver, so we need not maintain 'retrieved'.
[] ← BringOverInner[imports.path1, imports.date, newFilter];
newFilter.list.nEntries = 0 =>
We should never get here with filter.list.nEntries = 0 (see next case, below), so it must be the case that imports.list.nEntries = 0. That is, the imports item contained an explicit list, but it had an empty intersection with our filter.list. Accordingly, there is no point in recurring.
NULL;
ENDCASE => {
We are passing a non-NIL, non-empty filter.list to the next lower level. It will therefore return a meaningful count of the number of files it retrieves.
retrievedBelow: NAT = BringOverInner[imports.path1, imports.date, newFilter];
IF filter.list ~=
NIL
THEN {
The following statement and a similar one in the 'file' case, above, are the only ones that alter 'retrieved', and are executed iff filter.list ~= NIL. Note that 'requested' is initialized to a meaningful value under the same precondition. The test succeeds iff we have retrieved everything that was requested by our invoker's filter.list.
IF requested = (retrieved ← retrieved + retrievedBelow) THEN RETURN[TRUE];
IF imports.form = $list
THEN {
In this case, we were invoked with an explicit filter.list and the imports item had one as well. Utils.ParseFromStream intersected the two and handed it to us as 'imports.list', which we passed down as newFilter.list in the nested call above. As a result of that call, newFilter.list became sorted, and filter.list was sorted by the recursion above us (i.e., by the level that invoked this instance of BringOverInner). Therefore, the precondition (sorted input) for Utils.DifferenceOfUsingLists is met.
filter.list ← Utils.DifferenceOfUsingLists[filter.list, newFilter.list];
If filter.list is now empty, we should abandon this level of BringOver. In the absence of errors, however, the test above (requested = retrieved) will have been TRUE and control wouldn't get here. However, if some file in newFilter.list was not found (e.g., the Imports item mentioned a file that didn't appear in the lower-level DF, or some other part of the filter prevented a match), the test above will fail (`requested' will still exceed `retrieved'), yet filter.list will be empty.
RETURN[filter.list.nEntries = 0]
};
};
};
};
include:
REF Utils.IncludeItem =>
retrieved ← BringOverInner[include.path1, include.date, filter] + retrieved;
ENDCASE;
};
dfLocalInfo: REF Int.LocalFileInfo;
dontProcess: BOOL;
[dfLocalInfo, dontProcess] ← Localize[dfFile, date, $fetch];
IF
~dontProcess
THEN {
dfStream:
IO.
STREAM =
FS.StreamOpen[dfLocalInfo.name
! FS.Error => {Int.ReportFSError[error, dfLocalInfo, client]; GO TO skip};
];
Int.SimpleInteraction[
client,
NEW[Ops.DFInfoInteraction ← [action: $start, dfFile: dfFile]]
];
IF filter.list ~=
NIL
AND retrievedFiles =
NIL
THEN {
retrievedFiles ← NEW[Utils.UsingList[filter.list.nEntries]];
retrievedFiles.nEntries ← 0;
iAllocatedRetrievedFiles ← TRUE;
};
Utils.ParseFromStream[dfStream, DoOneItem, filter !
Utils.SyntaxError
-- [reason: ROPE]-- => {
errors ← errors.SUCC;
Int.SimpleInteraction[
client,
NEW[Ops.InfoInteraction ← [
class: $error,
message:
IO.PutFR[
"Syntax error in '%g'[%d]: %g\NProcessing of this DF file aborted.",
IO.rope[dfLocalInfo.name], IO.card[dfStream.GetIndex[]], IO.rope[reason]
]
]]
];
CONTINUE
};
Int.AbortDF => dfStream.Close[];
];
dfStream.Close[];
IF iAllocatedRetrievedFiles
THEN {
IF retrievedFiles.nEntries ~= filter.list.nEntries
THEN {
There were some files specified in the explicit filter list that weren't retrieved.
diff: REF Utils.UsingList;
Utils.SortUsingList[retrievedFiles, FALSE];
diff ← Utils.DifferenceOfUsingLists[filter.list, retrievedFiles];
FOR i:
NAT
IN [0..diff.nEntries)
DO
IF filter.filterA = $all
OR Utils.ClassifyFileExtension[diff.u[i].name] = filter.filterA
THEN {
warnings ← warnings.SUCC;
Int.SimpleInteraction[
client,
NEW[Ops.InfoInteraction ← [
class: $warning,
message: Rope.Cat["'", diff.u[i].name, "' could not be found in any nested DF file."]
]]
];
};
ENDLOOP;
};
retrievedFiles ← NIL;
};
Int.SimpleInteraction[
client,
NEW[Ops.DFInfoInteraction ← [action: $end, dfFile: dfFile]]
];
EXITS
skip => errors ← errors.SUCC;
};
};
uFilter: Utils.Filter = ConvertFilter[filter];
IF interact = NIL THEN interact ← Int.DefaultInteractionProc;
[] ← BringOverInner[dfFile, [], uFilter !
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
};
];
};