4 * @brief Implementation file for TempFile
13 #include "Environment.h"
14 #include "Constants.h"
17 static bool WMrunning(int iPI);
20 * @brief Delete the temp file when instance is deleted.
29 * @brief Create a temporary file with given prefix.
30 * @param [in] prefix A prefix for temp file name.
31 * @param [in] ext extension for temp file name.
32 * @return Created temp file path.
34 String TempFile::Create(const String& prefix, const String& ext)
36 String temp = env::GetTemporaryPath();
46 temp = env::GetTemporaryFileName(temp, pref, nullptr);
51 String tempext = temp + ext;
52 if (MoveFile(temp.c_str(), tempext.c_str()))
62 * @brief Delete the temporary file, if it exists.
63 * @return true if there was no error.
65 bool TempFile::Delete()
69 success = !!DeleteFile(m_path.c_str());
75 * @brief Cleanup tempfiles created by WinMerge.
76 * This function finds temp folders which don't have WinMerge instance using
83 String filepattern(TempFolderPrefix);
84 filepattern += _T("*.*");
85 String pattern = paths::GetParentPath(env::GetTemporaryPath());
86 pattern = paths::ConcatPath(pattern, filepattern);
92 h = FindFirstFile (TFile(pattern).wpath().c_str(), &ff);
93 if (h == INVALID_HANDLE_VALUE)
98 foldername = ff.cFileName;
99 if (ff.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
101 // Remove leading "WM_" from filename to get the ProcessID
102 tempfolderPID = foldername.substr(_tcslen(TempFolderPrefix));
104 // Check if this instance of WM is still running
107 int pid = atoi(ucr::toUTF8(tempfolderPID).c_str());
110 tempfolderPID = paths::ConcatPath(paths::GetParentPath(pattern), ff.cFileName);
111 res = ClearTempfolder(tempfolderPID);
113 bok = !!FindNextFile(h, &ff) ;
121 bok = !!FindNextFile(h, &ff) ;
123 if (h != INVALID_HANDLE_VALUE)
128 * @brief Is WinMerge with given processID running?
129 * @param [in] processIDs List of WinMerge processes.
130 * @param [in] iPI ProcessID to check.
131 * @return true if processID was found from the list, `false` otherwise.
133 static bool WMrunning(int iPI)
135 HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, false, iPI);
137 return (GetLastError() == ERROR_ACCESS_DENIED);
138 CloseHandle(hProcess);
143 * @brief Remove the temp folder.
144 * @param [in] pathName Folder to remove.
145 * @return true if removal succeeds, `false` if fails.
147 bool ClearTempfolder(const String &pathName)
151 TFile(pathName).remove(true);