(FILECREATED "17-Mar-87 15:14:49" {QV}<NOTECARDS>1.3K>NEXT>RHTPATCH217.;1 21110  

      changes to:  (VARS RHTPATCH217COMS))


(* Copyright (c) 1987 by Xerox Corporation. All rights reserved.)

(PRETTYCOMPRINT RHTPATCH217COMS)

(RPAQQ RHTPATCH217COMS ((* * Fixes bug #375: 3rd phase of inspector closes notefile when aborted. 
			     Can't do the RESETLST thing for 1st phase, but hopefully no breaks 
			     should occur during phase1. Also recovers Kirk's lost fix from 
			     KIRKPATCH033. Also stops removing notefiles from Notefiles hash array at 
			     the end of compacting.)
			  (FILES (FROM NOTECARDS)
				 NCREPAIR NCCOMPACT)
			  (* * Changes to NCREPAIR)
			  (FNS NC.ScavengeDatabaseFile)
			  (* * Changes to NCCOMPACT)
			  (FNS NC.CompactNoteFileToTarget)))
(* * Fixes bug #375: 3rd phase of inspector closes notefile when aborted. Can't do the 
RESETLST thing for 1st phase, but hopefully no breaks should occur during phase1. Also recovers
 Kirk's lost fix from KIRKPATCH033. Also stops removing notefiles from Notefiles hash array at 
the end of compacting.)

(FILESLOAD (FROM NOTECARDS)
	   NCREPAIR NCCOMPACT)
(* * Changes to NCREPAIR)

(DEFINEQ

(NC.ScavengeDatabaseFile
  (LAMBDA (NoteFileOrFileName BadLinkLabelsFlg ListOfBoxesToReconstruct 
			      ListOfCardsNeedingGlobalLinksReconstructed InterestedWindow)
                                                             (* rht: "17-Mar-87 15:05")

          (* Scavenge the database FileName. Essentially throw away all of the information about From and ToLinks and 
	  recreate them by retrieving the link information from the substance of each card and from the list of global links 
	  from the card.)



          (* * rht 8/9/84: Now calls NC.OpenDatabaseFile to do the file open.)



          (* * rht 7/17/85: Changed so can take a stream argument. Also handles link labels. If BadLinkLabelsFlg is non-nil, 
	  then don't try to read current link labels. Just rebuild them from what's out there. Otherwise, only rebuild if 
	  find new any new ones.)



          (* * fgh 22-Jul-85 Takes a list of bad file box cards and reconstructs the file boxes from the From pointer lists 
	  of all the cards in the NoteFile.)



          (* * fgh 30-Jul-85 Takes a list of cards with bad global links and reconstructs the global links list from the From
	  pointer lists of all the cards in the NoteFile.)



          (* * rht 11/23/85: Updated to handle new notefile and card object formats.)



          (* * rht 12/1/85: Now calls NC.GetMainCardData and NC.GetLinks instead of NC.GetNoteCard.)



          (* * rht 12/19/85: Massive overhaul for sake of speed. Should be wizzier now.)



          (* * fgh 2/4/86 Now works on open NFs. No need to error check since this function should always be called from 
	  earlier phases of the inspect & repaier.)



          (* * fgh 5/21/86 Fixed bug in handling of global links.)



          (* * rht 7/16/86: Added InterestedWindow arg.)



          (* * rht 7/16/86: Now calls NC.PutLinks passing UseOldDatesFlg.)



          (* * rht 9/5/86: Now checks that link is valid before passing it to NC.DelReferencesToCard.)



          (* * rht 10/29/86: Now closes prompt win at end if no filing info to tell.)



          (* * rht 12/10/86: No longer calls NC.GetLinks for cards appearing on the 
	  ListOfCardsNeedingGlobalLinksReconstructed list.)



          (* * rht 1/19/87: Now uses NC.CardNeedsFilingP to check for unfiled cards at the end.)



          (* * rht 1/22/87: Added yet another BLOCK call.)



          (* * rht 2/18/87: Added SPAWN.MOUSE call.)



          (* * rht 3/17/87: Changed call to OPENP to NC.OpenDatabaseFile to recover lost change from KIRKPATCH033.
	  Added RESETLST to close notefile in case we bomb out.)


    (PROG (NoteFile FileName)
	    (SPAWN.MOUSE)

          (* * First, take care of checking stream's validity, etc.)


	    (SETQ FileName (if (type? NoteFile NoteFileOrFileName)
				 then (SETQ NoteFile NoteFileOrFileName)
					(fetch (NoteFile FullFileName) of NoteFileOrFileName)
			       else NoteFileOrFileName))   (* Try to open notefile.)
	    (if (NULL (NC.NoteFileOpenP FileName))
		then (if (NULL (SETQ NoteFile
				       (NC.OpenNoteFile FileName NIL T NIL NIL NIL NIL T T NIL 
							  InterestedWindow)))
			   then (NC.PrintMsg InterestedWindow NIL "Couldn't open " FileName "."
						 (CHARACTER 13)
						 "Repair aborted.")
				  (RETURN NIL)))
	    (RESETLST
	      (RESETSAVE NIL (BQUOTE (NC.ForceDatabaseClose , NoteFile)))
	      (LET (CardTotal NoteCardNumber OldLinkLabels DiscoveredLinkLabels ReconstructLinks 
			      ReconstructGlobalLinks ToBeFiledCards)

          (* * If link labels aren't screwed up, then read them in.)


	           (OR BadLinkLabelsFlg (SETQ OldLinkLabels (NC.RetrieveLinkLabels NoteFile T)))

          (* * Mark every card that needs its global links or substance reconstructed so we don't have to search the lists so
	  much.)


	           (for Card in ListOfCardsNeedingGlobalLinksReconstructed
		      do (NC.SetUserDataProp Card (QUOTE NeedsGlobalLinksReconstructedFlg)
						 T))
	           (for Box in ListOfBoxesToReconstruct do (NC.SetUserDataProp Box
										       (QUOTE
											 
									   NeedsReconstructingFlg)
										       T))

          (* Read through all NoteCard substances to find actual pointers. Use this to create the To Links list.
	  The list collection function checks to make sure each link is valid.)


	           (SETQ CardTotal (SUB1 (fetch (NoteFile NextIndexNum)
						      NoteFile)))
	           (NC.PrintMsg InterestedWindow T "Rebuilding notefile links." (CHARACTER 13)
				  "Collecting Links for item " 1 " out of " CardTotal ".")
	           (SETQ NoteCardNumber 0)
	           (NC.MapCards
		     NoteFile
		     (FUNCTION (LAMBDA (Card)
			 (BLOCK)
			 (SETQ NoteCardNumber (ADD1 NoteCardNumber))
			 (AND (ZEROP (REMAINDER NoteCardNumber 10))
				(NC.PrintMsg InterestedWindow T "Rebuilding notefile links."
					       (CHARACTER 13)
					       "Collecting Links for item " NoteCardNumber " out of " 
					       CardTotal "."))
                                                             (* Get global links unless links are unreadable.)
			 (if (NOT (NC.FetchUserDataProp Card (QUOTE 
								 NeedsGlobalLinksReconstructedFlg)))
			     then (NC.GetLinks Card))
			 (if (NC.FetchUserDataProp Card (QUOTE NeedsReconstructingFlg))
			     then                          (* Card substance and links will be reconstructed so 
							     no need to try to read substance.)
				    (if (NOT (NC.FetchUserDataProp Card (QUOTE 
								 NeedsGlobalLinksReconstructedFlg)))
					then (NC.SetUserDataProp Card (QUOTE ScavengerToLinks)
								     (NC.FetchGlobalLinks Card))
					       (NC.SetUserDataProp Card (QUOTE 
									     ScavengerGlobalLinks)
								     (NC.FetchGlobalLinks Card)))
				    (NC.DeactivateCard Card T)
			   else
			    (NC.GetMainCardData Card)
			    (NC.ActivateCard Card)
			    (if (EQ (NC.FetchStatus Card)
					(QUOTE ACTIVE))
				then                       (* Collect links having active destinations.
							     Delete the others.)
				       (NC.SetUserDataProp
					 Card
					 (QUOTE ScavengerToLinks)
					 (NCONC (for Link in (CAR (NC.CollectReferences
									    Card))
						     eachtime (BLOCK)
						     when (if (EQ (NC.FetchStatus
									  (fetch (Link 
										  DestinationCard)
									     of Link))
									(QUOTE ACTIVE))
							      else (AND (type? Link Link)
									    (NC.DelReferencesToCard
									      Card Link))
								     NIL)
						     collect Link)
						  (if (NC.FetchUserDataProp Card (QUOTE 
								 NeedsGlobalLinksReconstructedFlg))
						    else (NC.SetUserDataProp Card (QUOTE 
									     ScavengerGlobalLinks)
										 (
									      NC.FetchGlobalLinks
										   Card))
							   (NC.FetchGlobalLinks Card))))
				       (if (NC.FetchUserDataProp Card (QUOTE 
								 NeedsGlobalLinksReconstructedFlg))
					 else (NC.SetUserDataProp Card (QUOTE 
									     ScavengerGlobalLinks)
								      (NC.FetchGlobalLinks Card)))

          (* If there are file boxes to be reconstructed, then look thru the From links to see if this card was filed in one 
	  of the to-be-reconstructed boxes)


				       (AND ListOfBoxesToReconstruct
					      (for Link in (NC.FetchFromLinks Card)
						 eachtime (BLOCK)
						 when (AND (NC.ChildLinkP Link)
							       (NC.FetchUserDataProp
								 (fetch (Link SourceCard)
								    of Link)
								 (QUOTE NeedsReconstructingFlg)))
						 do (push ReconstructLinks Link)))

          (* If there are global links to be reconstructed, then look thru the From links to see if this card had a global 
	  link from a card whose global links need reconstructing.)


				       (AND ListOfCardsNeedingGlobalLinksReconstructed
					      (for Link in (NC.FetchFromLinks Card)
						 eachtime (BLOCK)
						 when (AND (NC.GlobalLinkP Link)
							       (NC.FetchUserDataProp
								 (fetch (Link SourceCard)
								    of Link)
								 (QUOTE 
								 NeedsGlobalLinksReconstructedFlg)))
						 do (push ReconstructGlobalLinks Link)))
				       (NC.DeactivateCard Card T))))))

          (* * Reconstruct any cards as requested)


	           (for BoxToReconstruct in ListOfBoxesToReconstruct eachtime (BLOCK)
		      do                                   (* Make a new file box using the given card.)
			   (NC.MakeNoteCard (QUOTE FileBox)
					      NoteFile "Untitled: Reconstructed during repair" T NIL 
					      BoxToReconstruct)

          (* File cards whose from links indicate that they used to be filed in this file box. Also add these new links to 
	  collected ToLinks.)


			   (NC.SetUserDataProp BoxToReconstruct (QUOTE ScavengerToLinks)
						 (APPEND (NC.FetchUserDataProp BoxToReconstruct
										   (QUOTE 
										 ScavengerToLinks))
							   (for Link in ReconstructLinks
							      eachtime (BLOCK)
							      when (NC.SameCardP
								       BoxToReconstruct
								       (fetch (Link SourceCard)
									  of Link))
							      collect (NC.MakeChildLink
									  (fetch (Link 
										  DestinationCard)
									     of Link)
									  BoxToReconstruct NIL))))
                                                             (* Put the card away)
			   (NC.PutMainCardData BoxToReconstruct)
			   (NC.DeactivateCard BoxToReconstruct T))

          (* * Reconstruct any global link lists as required)


	           (for Link in ReconstructGlobalLinks bind ThisCardsToLinks 
								  ThisCardsGlobalLinks SourceCard
		      eachtime (BLOCK)
		      do (SETQ SourceCard (fetch (Link SourceCard) of Link)) 
                                                             (* Add it to the GlobalLinks list for its source card 
							     unless it's already there.)
			   (if (for GlobalLink in (SETQ ThisCardsGlobalLinks
							  (NC.FetchUserDataProp SourceCard
										  (QUOTE 
									     ScavengerGlobalLinks)))
				    eachtime (BLOCK) never (NC.SameLinkP Link GlobalLink))
			       then (NC.SetUserDataProp SourceCard (QUOTE ScavengerGlobalLinks)
							    (CONS Link ThisCardsGlobalLinks)))
                                                             (* Add it to the source card's ToLinks list unless 
							     it's already there)
			   (if (for ToLink in (SETQ ThisCardsToLinks (NC.FetchUserDataProp
							SourceCard
							(QUOTE ScavengerToLinks)))
				    eachtime (BLOCK) never (NC.SameLinkP Link ToLink))
			       then (NC.SetUserDataProp SourceCard (QUOTE ScavengerToLinks)
							    (CONS Link ThisCardsToLinks))))

          (* * Compute the From Links list by "inverting" the To Links list)


	           (NC.PrintMsg InterestedWindow T "Repairing NoteFile." (CHARACTER 13)
				  "Inverting links for item " 1 " out of " CardTotal ".")
	           (SETQ NoteCardNumber 0)
	           (NC.MapCards NoteFile
				(FUNCTION (LAMBDA (Card)
				    (SETQ NoteCardNumber (ADD1 NoteCardNumber))
				    (AND (ZEROP (REMAINDER NoteCardNumber 100))
					   (NC.PrintMsg InterestedWindow T "Repairing NoteFile."
							  (CHARACTER 13)
							  "Inverting links for item " NoteCardNumber 
							  " out of "
							  CardTotal "."))
				    (if (EQ (NC.FetchStatus Card)
						(QUOTE ACTIVE))
					then (for Link in (NC.FetchUserDataProp Card
											(QUOTE
											  
										 ScavengerToLinks))
						  bind DestinationCard LinkLabel eachtime
										    (BLOCK)
						  do       (* Add this ToLink as a FromLink for the link's 
							     destination card.)
						       (NC.SetUserDataProp
							 (SETQ DestinationCard (fetch
							     (Link DestinationCard) of Link))
							 (QUOTE ScavengerFromLinks)
							 (CONS Link (NC.FetchUserDataProp
								   DestinationCard
								   (QUOTE ScavengerFromLinks))))
                                                             (* Accumulate the link labels into a list.)
						       (if (NOT (FMEMB (SETQ LinkLabel
									       (fetch (Link Label)
										  of Link))
									     DiscoveredLinkLabels))
							   then (push DiscoveredLinkLabels 
									  LinkLabel)))))))

          (* * Reset all of the To and From Links lists in the database)


	           (NC.PrintMsg InterestedWindow T "Repairing NoteFile." (CHARACTER 13)
				  "Rewriting links for item " 1 " out of " CardTotal ".")
	           (SETQ NoteCardNumber 0)
	           (NC.MapCards NoteFile (FUNCTION (LAMBDA (Card)
				    (BLOCK)
				    (SETQ NoteCardNumber (ADD1 NoteCardNumber))
				    (AND (ZEROP (REMAINDER NoteCardNumber 10))
					   (NC.PrintMsg InterestedWindow T "Repairing NoteFile."
							  (CHARACTER 13)
							  "Rewriting links for item " NoteCardNumber 
							  " out of "
							  CardTotal "."))
				    (if (EQ (NC.FetchStatus Card)
						(QUOTE ACTIVE))
					then (NC.SetGlobalLinks Card (NC.FetchUserDataProp
								      Card
								      (QUOTE ScavengerGlobalLinks)))
					       (NC.SetToLinks Card (NC.FetchUserDataProp
								  Card
								  (QUOTE ScavengerToLinks)))
					       (NC.SetFromLinks Card (NC.FetchUserDataProp
								    Card
								    (QUOTE ScavengerFromLinks)))
                                                             (* Check whether this card isn't filed anywhere.)
					       (if (NC.CardNeedsFilingP Card)
						   then (push ToBeFiledCards Card))
					       (NC.PutLinks Card T))
                                                             (* Clean any junk off the card.)
				    (NC.DeactivateCard Card T)
				    (NC.SetUserDataPropList Card NIL))))

          (* * File any unfiled cards in the ToBeFiled box.)


	           (if ToBeFiledCards
		       then (NC.PrintMsg InterestedWindow T "Filing " (LENGTH ToBeFiledCards)
					     " cards in ToBeFiled box ...")
			      (NCP.FileCards ToBeFiledCards (fetch (NoteFile ToBeFiledCard)
								 of NoteFile)))
                                                             (* Rewrite link labels if we've found any new ones.)
	           (if (LDIFFERENCE DiscoveredLinkLabels OldLinkLabels)
		       then (NC.StoreLinkLabels NoteFile (UNION DiscoveredLinkLabels 
								      OldLinkLabels)))
                                                             (* Clean up and get out.)
	           (NC.CheckpointDatabase NoteFile T)
	           (NC.PrintMsg InterestedWindow T "Repair Completed for " (FULLNAME FileName)
				  ".")
	           (if ToBeFiledCards
		       then (NC.PrintMsg InterestedWindow NIL "Filed " (LENGTH ToBeFiledCards)
					     " cards in ToBeFiled box.")
		     else (NC.ClearMsg InterestedWindow T)))))))
)
(* * Changes to NCCOMPACT)

(DEFINEQ

(NC.CompactNoteFileToTarget
  (LAMBDA (FromNoteFile ToFileName InterestedWindow)         (* rht: "17-Mar-87 14:55")

          (* * In sorted order, copy card parts to lower addresses in the file.)



          (* * fgh 5/1/86 Now returns the ToNoteFile in order to be compatible with compact in place.)



          (* * rht 11/3/86: Now opens FromNoteFile read-only. Also now takes InterestedWindow arg.)



          (* * rht 1/22/87: Slight change to computation of new index size.)



          (* * rht 3/17/87: Added RESETLST to make sure notefiles get closed in case of bombing out.)


    (LET (ToNoteFile OperationMsg)
         (SETQ FromNoteFile
	   (NC.OpenNoteFile FromNoteFile NIL T T T NIL T T T InterestedWindow NIL NIL NIL T))
         (SETQ OperationMsg (CONCAT "Compacting " (fetch (NoteFile FullFileName) of 
										     FromNoteFile)
					(CHARACTER 13)))
         (SETQ ToNoteFile (NC.OpenNoteFile (NC.CreateDatabaseFile ToFileName
									(OR (
								   NC.ComputeNewDatabaseIndexSize
										FromNoteFile)
									      (fetch (NoteFile
											 
										    HashArraySize)
										 of FromNoteFile))
									OperationMsg T NIL T 
									InterestedWindow)
					       NIL T T T T T T T InterestedWindow))
         (RESETLST (RESETSAVE NIL (BQUOTE (NC.ForceDatabaseClose , FromNoteFile)))
		     (RESETSAVE NIL (BQUOTE (NC.ForceDatabaseClose , ToNoteFile)))
		     (LET ((OriginalStream (fetch (NoteFile Stream) of FromNoteFile))
			   (TargetStream (fetch (NoteFile Stream) of ToNoteFile))
			   FromFileLength TargetFileLength BytesRecovered)
		          (replace (NoteFile NextIndexNum) of ToNoteFile
			     with (fetch (NoteFile NextIndexNum) of FromNoteFile))
		          (SETFILEPTR TargetStream (NC.TotalIndexSize (fetch (NoteFile 
										    HashArraySize)
									     of ToNoteFile)))
                                                             (* truncate ToNoteFile after the index)
		          (if (NC.CopySortedCardParts (NC.SortIndexEntries FromNoteFile)
							  ToNoteFile NIL NIL NIL InterestedWindow 
							  OperationMsg)
			      then                         (* all useable card parts got copied)
				     (SETQ FromFileLength (GETEOFPTR OriginalStream)) 

          (* * fool NC.PutHashArray into writing out the index for the new NoteFile)


				     (replace (NoteFile Stream) of FromNoteFile with 
										     TargetStream)
				     (NCLocalDevice.PutHashArray FromNoteFile InterestedWindow T 
								   OperationMsg)
				     (replace (NoteFile Stream) of FromNoteFile with 
										   OriginalStream)

          (* * Put out the new ChkptPtr to the file.)


				     (replace (NoteFile CheckptPtr) of ToNoteFile
					with (SETQ TargetFileLength (GETEOFPTR TargetStream)))

          (* * Steal the UID from the original file so links will work. Write out the header.)


				     (replace (NoteFile UID) of ToNoteFile
					with (fetch (NoteFile UID) of FromNoteFile))
				     (NC.PutNoteFileHeader ToNoteFile)
				     (SETQ BytesRecovered (DIFFERENCE FromFileLength 
									  TargetFileLength))
				     (NC.PrintMsg NIL T (fetch (NoteFile FullFileName)
							     of FromNoteFile)
						    " compacted to "
						    (fetch (NoteFile FullFileName) of ToNoteFile)
						    (CHARACTER 13)
						    "Recovered " BytesRecovered " bytes ("
						    (FIX (TIMES 100 (FQUOTIENT BytesRecovered 
										   FromFileLength)))
						    "%%)"
						    (CHARACTER 13))
				     (NC.ClearMsg InterestedWindow T))
		      ToNoteFile)))))
)
(PUTPROPS RHTPATCH217 COPYRIGHT ("Xerox Corporation" 1987))
(DECLARE: DONTCOPY
  (FILEMAP (NIL (1186 17129 (NC.ScavengeDatabaseFile 1196 . 17127)) (17163 21028 (
NC.CompactNoteFileToTarget 17173 . 21026)))))
STOP