8 #include "fwTools/System.hpp" 10 #include <fwCore/base.hpp> 12 #include <fwRuntime/Runtime.hpp> 15 #include <boost/filesystem/operations.hpp> 16 #include <boost/filesystem/fstream.hpp> 17 #include <boost/regex.hpp> 18 #include <boost/lexical_cast.hpp> 26 #include <sys/types.h> 34 #define F4S_TMP_EXT "fw4spl-tmp" 39 std::string System::s_tempPrefix;
43 typedef std::shared_ptr< RemoveTemporaryFolder > sptr;
51 ::boost::system::error_code er;
52 ::boost::filesystem::remove_all(m_path, er);
53 OSLM_ERROR_IF(
"Failed to remove " << m_path <<
" : " << er.message(), er.value() != 0);
55 ::boost::filesystem::path m_path;
57 static RemoveTemporaryFolder::sptr autoRemoveTempFolder;
61 static struct CleanZombies
88 namespace fs = ::boost::filesystem;
89 static fs::path sysTmp;
97 if(!fs::exists(sysTmp))
99 bool res = fs::create_directories(sysTmp);
100 SLM_ASSERT(
" Failed to create '"+sysTmp.string()+
"' path", res);
104 ::boost::system::error_code err;
105 sysTmp = fs::temp_directory_path(err);
110 fs::path fallback(
"C:\\");
112 fs::path fallback(
"/tmp");
114 OSLM_ERROR(
"Temporary Path Error : " << err.message() <<
". " <<
"Falling back to " << fallback );
123 const ::boost::filesystem::path createUniqueFolder(const ::boost::filesystem::path& folderUniquePath)
125 namespace fs = ::boost::filesystem;
126 bool created =
false;
131 tmpDir = fs::unique_path(folderUniquePath);
133 if(!fs::exists(tmpDir))
135 fs::create_directories(tmpDir);
150 namespace fs = ::boost::filesystem;
151 static fs::path tmpDirPath;
153 if(!tmpDirPath.empty() && fs::exists(tmpDirPath))
155 if(!subFolderPrefix.empty())
157 const std::string subDirName = subFolderPrefix +
"-" +
"%%%%%%%%%%%%";
158 fs::path tmpSubDir = createUniqueFolder(tmpDirPath/subDirName);
165 const fs::path &sysTmp = getTempPath();
167 const std::string tmpDirName = s_tempPrefix + (s_tempPrefix.empty() ?
"" :
"-") +
"%%%%%%%%%%%%." F4S_TMP_EXT;
168 fs::path tmpDir = createUniqueFolder(sysTmp/tmpDirName);
171 fs::path pidFile = tmpDir / (::boost::lexical_cast<std::string>(getPID()) +
".pid");
172 fs::fstream( pidFile, std::ios::out ).close();
174 autoRemoveTempFolder = std::make_shared<RemoveTemporaryFolder>(tmpDirPath);
176 if(!subFolderPrefix.empty())
178 const std::string subDirName = subFolderPrefix +
"-" +
"%%%%%%%%%%%%";
179 tmpDir = createUniqueFolder(tmpDir/subDirName);
182 OSLM_INFO(
"Temporary folder is : " << tmpDirPath);
191 HANDLE hProcess = OpenProcess(SYNCHRONIZE, FALSE, pid);
198 CloseHandle(hProcess);
202 return kill(pid,0) == 0;
212 namespace fs = ::boost::filesystem;
214 const ::boost::regex pidFilter(
"([[:digit:]]+)\\.pid" );
216 fs::directory_iterator i( dir );
217 fs::directory_iterator endIter;
221 for(; i != endIter; ++i )
224 if( !fs::is_regular_file( i->status() ) )
229 ::boost::smatch what;
232 std::string s = i->path().filename().string();
233 if( !::boost::regex_match( s, what, pidFilter ) )
240 pid = ::boost::lexical_cast<
int >(what.str(1));
243 catch (boost::bad_lexical_cast&)
255 namespace fs = ::boost::filesystem;
257 const ::boost::regex tmpFolderFilter(
".*\\." F4S_TMP_EXT );
259 std::vector< fs::path > allTempFolders;
261 fs::directory_iterator i( dir );
262 fs::directory_iterator endIter;
264 for(; i != endIter; ++i )
267 if( !fs::is_directory( i->status() ) )
272 ::boost::smatch what;
275 if( !::boost::regex_match( i->path().filename().string(), what, tmpFolderFilter ) )
280 allTempFolders.push_back( i->path() );
284 for(
const fs::path &foundTmpDir : allTempFolders)
286 int pid = tempFolderPID(foundTmpDir);
288 if(pid && !isProcessRunning(pid))
290 OSLM_INFO(
"Removing old temp dir : " << foundTmpDir);
292 ::boost::system::error_code er;
293 fs::remove_all(foundTmpDir, er);
295 OSLM_INFO_IF(
"Failed to remove " << foundTmpDir <<
" : " << er.message(), er.value() != 0);
static FWRUNTIME_API Runtime * getDefault()
Retrieves the default runtime instance.
#define OSLM_INFO_IF(message, cond)
#define OSLM_INFO(message)
#define OSLM_ERROR(message)
#define SLM_ASSERT(message, cond)
work like 'assert' from 'cassert', with in addition a message logged by spylog (with FATAL loglevel) ...
#define OSLM_ERROR_IF(message, cond)
FWRUNTIME_API::boost::filesystem::path getWorkingPath() const
Get the path where Bundles and share folder are located.