@@ -163,45 +163,41 @@ int Job::Execute()
163163 LOG_DEBUG
164164 );
165165
166- // Get a temporary filename, then reuse it to create an empty directory.
167- std::string sDirectory = getTemporaryDirectoryPath ();
168- std::string sFilesName = std::string (" " );
169- std::string prefix = std::string (" pga_" );
170-
171- // Generate random string of 6 characters long to make unique dir name
172- std::string result = generateRandomString (7 );
173- sFilesName = prefix + m_jobid + std::string (" _" ) + stepid + std::string (" _" ) + result;
166+ namespace fs = boost::filesystem;
167+
168+ // Generate unique temporary directory
169+ std::string prefix = (
170+ boost::format (" pga_%s_%s_" ) % m_jobid % stepid
171+ ).str ();
172+
173+ fs::path jobDir;
174+ fs::path filepath ((
175+ boost::format (" %s_%s.%s" ) %
176+ m_jobid % stepid %
174177#if BOOST_OS_WINDOWS
175- std::string sModel = ( boost::format ( " %s \\ %s " ) % sDirectory % sFilesName ). str ();
178+ " .bat "
176179#else
177- std::string sModel = ( boost::format ( " %s/%s " ) % sDirectory % sFilesName ). str ();
180+ " .scr "
178181#endif
179- std::string dirname = sModel ;
182+ ).str ());
183+ fs::path errorFilePath (
184+ (boost::format (" %s_%s_error.txt" ) % m_jobid % stepid).str ()
185+ );
180186
181- if (dirname == " " )
187+ if (! createUniqueTemporaryDirectory (prefix, jobDir) )
182188 {
183189 output = " Couldn't get a temporary filename!" ;
184190 LogMessage (output, LOG_WARNING );
185191 rc = -1 ;
186- break ;
187- }
188192
189- if (!boost::filesystem::create_directory (boost::filesystem::path (dirname)))
190- {
191- LogMessage (
192- " Couldn't create temporary directory: " + dirname, LOG_WARNING
193- );
194- rc = -1 ;
195193 break ;
196194 }
197195
198- #if BOOST_OS_WINDOWS
199- std::string filename = dirname + " \\ " + m_jobid + " _" + stepid + " .bat" ;
200- std::string errorFile = dirname + " \\ " + m_jobid + " _" + stepid + " _error.txt" ;
201- #else
202- std::string filename = dirname + " /" + m_jobid + " _" + stepid + " .scr" ;
203- std::string errorFile = dirname + " /" + m_jobid + " _" + stepid + " _error.txt" ;
204- #endif
196+ filepath = jobDir / filepath;
197+ errorFilePath = jobDir / errorFilePath;
198+
199+ std::string filename = filepath.string ();
200+ std::string errorFile = errorFilePath.string ();
205201
206202 std::string code = steps->GetString (" jstcode" );
207203
@@ -222,8 +218,8 @@ int Job::Execute()
222218 LOG_WARNING
223219 );
224220
225- if (boost::filesystem::exists (dirname ))
226- boost::filesystem::remove_all (dirname );
221+ if (boost::filesystem::exists (jobDir ))
222+ boost::filesystem::remove_all (jobDir );
227223
228224 rc = -1 ;
229225 break ;
@@ -234,14 +230,17 @@ int Job::Execute()
234230 out_file.close ();
235231
236232#if !BOOST_OS_WINDOWS
237- // change file permission to 700 for executable in linux
238- int ret = chmod ((const char *)filename.c_str (), S_IRWXU );
239-
240- if (ret != 0 )
233+ // Change file permission to 700 for executable in linux
234+ try {
235+ boost::filesystem::permissions (
236+ filepath, boost::filesystem::owner_all
237+ );
238+ } catch (const fs::filesystem_error &ex) {
241239 LogMessage (
242- " Error setting executable permission to file: " + filename,
243- LOG_DEBUG
240+ " Error setting executable permission to file: " +
241+ filename, LOG_DEBUG
244242 );
243+ }
245244#endif
246245 }
247246
@@ -368,10 +367,8 @@ int Job::Execute()
368367 // output in the log, just throw warnings.
369368 try
370369 {
371- boost::filesystem::path dir_path (dirname);
372-
373- if (boost::filesystem::exists (dir_path))
374- boost::filesystem::remove_all (dir_path);
370+ if (boost::filesystem::exists (jobDir))
371+ boost::filesystem::remove_all (jobDir);
375372 }
376373 catch (boost::filesystem::filesystem_error const & e)
377374 {
0 commit comments