-- DirImpl.Mesa, last edit 22-Dec-81 13:01:15
-- Pilot 6.0/ Mesa 7.0


DIRECTORY
  CWF: TYPE USING [WF1],
  Dir: TYPE USING [ADepRecord, DepSeq, FileInfo],
  Directory: TYPE USING [ignore, Lookup],
  File: TYPE USING [Capability, nullCapability],
  MDModel: TYPE USING [ResetFileEntries];

DirImpl: PROGRAM
IMPORTS CWF, Directory, MDModel
EXPORTS Dir = {

-- no MDS usage

-- may raise Directory.Error!!!
NewVersion: PUBLIC PROC[fi: Dir.FileInfo, src: BOOL] RETURNS[newVersion: BOOL] = {
IF src THEN {
	fi.srcCap ← File.nullCapability;
	fi.srcPresent ← FALSE;
	fi.srcDepSeq ← NIL;
	fi.srcCreate ← 0;		-- will be recomputed
	fi.srcCap ← Directory.Lookup[fileName: fi.srcFileName, permissions: Directory.ignore];
	fi.srcPresent ← TRUE;
	newVersion ← TRUE;
	}
ELSE {
	oldCap: File.Capability ← fi.bcdCap;
	fi.bcdCap ← File.nullCapability;
	fi.bcdPresent ← FALSE;
	fi.bcdDepSeq ← NIL;
	fi.bcdCreate ← 0;		-- will be recomputed
	fi.bcdCap ← Directory.Lookup[fileName: fi.bcdFileName, permissions: Directory.ignore];
	fi.bcdPresent ← TRUE;
	newVersion ← TRUE;
	IF oldCap ~= File.nullCapability THEN MDModel.ResetFileEntries[oldCap, fi];
	};
};

AddToDep: PUBLIC PROC[depseq: Dir.DepSeq, 
	padeprecord: POINTER TO Dir.ADepRecord] = {
IF padeprecord↑.relation = errortype THEN ERROR;
IF depseq.size + 1 >= depseq.maxsize THEN 
	CWF.WF1["Error - too many dependencies for %s.\n"L, 
		IF depseq.bcdFileName ~= NIL THEN depseq.bcdFileName 
		ELSE depseq.srcFileName]
ELSE {
	depseq[depseq.size] ← padeprecord↑;
	depseq.size ← depseq.size + 1;
	};
};

}.




-- uses the diskseq
ReadInUnsavedFiles: PUBLIC ENTRY PROC[window: Window.Handle] = {
ENABLE UNWIND => NULL;
log: Stream.Handle;
target: STRING ← [100];
disk: Dir.Disk;

	WriteP: PROC[ch: CHAR] = {
	Put.Char[window, ch];
	};

log ← Subr.NewStream["NonPermanentFiles"L, Subr.Read
	! Subr.FileError => GOTO out];
WHILE Subr.GetLine[log, target] DO
	disk ← InternalLookup[target];
	IF disk ~= NIL THEN {
		disk.notsaved ← TRUE;
		CWF.FWF1[WriteP, "%s will be stored on the next Permanent.\n"L, target];
		};
	ENDLOOP;
Stream.Delete[log];
EXITS
out => NULL;
};

WriteOutUnsavedFiles: PUBLIC ENTRY PROC = {
ENABLE UNWIND => NULL;
log: Stream.Handle ← NIL;
disk: Dir.Disk;

	LogProc: INTERNAL PROC[ch: CHAR] = {
	Stream.PutChar[log, ch];
	};
	
FOR i: CARDINAL IN [0 .. diskseq.size) DO
	disk ← @diskseq[i];
	IF disk.notsaved THEN {
		IF log = NIL THEN
			log ← Subr.NewStream["NonPermanentFiles"L, Subr.Write];
		CWF.FWF1[LogProc, "%s\n"L, disk.filename];
		};
	ENDLOOP;
IF log ~= NIL THEN Stream.Delete[log];
};