@@ -615,46 +615,94 @@ StringBuffer & EclAgent::getTempfileBase(StringBuffer & buff)
615615 return buff.append (queryTempfilePath ()).append (PATHSEPCHAR).appendLower (wuid);
616616}
617617
618- const char *EclAgent::queryTemporaryFile (const char *fname)
618+ const char *EclAgent::queryTemporaryFile (const char * fname)
619619{
620- StringBuffer tempfilename ;
621- getTempfileBase (tempfilename). append (PATHSEPCHAR). append (fname);
620+ dbgassertex (! isEmptyString (fname)) ;
621+
622622 CriticalBlock crit (tfsect);
623- ForEachItemIn (idx, tempFiles)
624- {
625- if (strcmp (tempFiles. item (idx), tempfilename. str ())== 0 )
626- return tempFiles. item (idx );
627- }
623+
624+ Linked<CTempFileInfo> tempFileInfo = tempFileInfoMap. getValue (fname);
625+ if (tempFileInfo )
626+ return tempFileInfo-> tempFileName . str ( );
627+
628628 StringBuffer errmsg;
629- errmsg.append (" Attempt to read temp file that has not yet been registered: " ).append (tempfilename );
629+ errmsg.append (" Attempt to read temp file that has not yet been registered: " ).append (tempFileInfo-> tempFileName . str () );
630630 fail (0 , errmsg.str ());
631- return 0 ;
631+ return nullptr ;
632632}
633633
634- const char *EclAgent::noteTemporaryFile (const char *fname)
634+ const char *EclAgent::noteTemporaryFile (const char * fname)
635635{
636- StringBuffer tempfilename;
637- getTempfileBase (tempfilename).append (PATHSEPCHAR).append (fname);
636+ if (isEmptyString (fname))
637+ {
638+ fail (0 , " Attempt to register empty temp file name" );
639+ return nullptr ;
640+ }
641+
638642 CriticalBlock crit (tfsect);
639- tempFiles.append (tempfilename.str ());
640- return tempFiles.item (tempFiles.length ()-1 );
643+
644+ Linked<CTempFileInfo> tempFileInfo = tempFileInfoMap.getValue (fname);
645+ if (tempFileInfo)
646+ {
647+ WARNLOG (" Warning: temp file already registered %s" , fname);
648+ return tempFileInfo->tempFileName .str ();
649+ }
650+
651+ /* tempOwnerId is used at the end of the temporary file name to ensure
652+ * that in the event of a crash, we can use the pid and start time to
653+ * determine if temp files are from a currently running process or not,
654+ * and therefore whether they can be safely deleted.
655+ */
656+ if (tempOwnerId.isEmpty ())
657+ {
658+ unsigned pid = (unsigned )GetCurrentProcessId ();
659+ unsigned __int64 startEpoch = (unsigned __int64)time (nullptr );
660+ tempOwnerId.append (' .' ).append (pid).append (' .' ).append (startEpoch);
661+ }
662+
663+ tempFileInfo.setown (new CTempFileInfo);
664+ tempFileInfoMap.setValue (fname, tempFileInfo.getLink ());
665+
666+ StringBuffer base;
667+ getTempfileBase (base);
668+ base.append (PATHSEPCHAR);
669+ StringBuffer tail;
670+ splitFilename (fname, nullptr , nullptr , &tail, nullptr );
671+ tempFileInfo->tempFileName .append (base).append (tail).append (tempOwnerId);
672+
673+ return tempFileInfo->tempFileName .str ();
641674}
642675
643- const char * EclAgent::noteTemporaryFilespec (const char *fspec )
676+ void EclAgent::removeTemporaryFile (const char * fname )
644677{
645- CriticalBlock crit (tfsect);
646- tempFiles.append (fspec);
647- return tempFiles.item (tempFiles.length ()-1 );
678+ dbgassertex (!isEmptyString (fname));
679+
680+ const char *tempFileName = queryTemporaryFile (fname);
681+ if (tempFileName)
682+ {
683+ Owned<IFile> file = createIFile (tempFileName);
684+ if (file->isFile () != fileBool::foundYes)
685+ return ;
686+
687+ file->remove ();
688+ }
648689}
649690
650691void EclAgent::deleteTempFiles ()
651692{
652693 CriticalBlock crit (tfsect);
653- ForEachItemIn (idx, tempFiles)
694+
695+ if (!tempOwnerId.isEmpty ())
654696 {
655- remove (tempFiles.item (idx));
697+ HashIterator it (tempFileInfoMap);
698+ ForEach (it)
699+ {
700+ const char *originalId = static_cast <const char *>(it.query ().getKey ());
701+ CTempFileInfo *tempFileInfo = tempFileInfoMap.getValue (originalId);
702+ if (tempFileInfo)
703+ removeTemporaryFile (originalId);
704+ }
656705 }
657- tempFiles.kill ();
658706}
659707
660708const char *EclAgent::loadResource (unsigned id)
0 commit comments