9
9
#include " ../misc/parseerror.h"
10
10
11
11
#include < cerrno>
12
+ #include < cstdio>
12
13
#include < cstdlib>
13
14
#include < cstring>
14
15
#include < fstream>
@@ -293,10 +294,11 @@ string TestApplication::workingCopyPathAs(
293
294
const std::string &relativeTestFilePath, const std::string &relativeWorkingCopyPath, WorkingCopyMode mode) const
294
295
{
295
296
// ensure working directory is present
297
+ auto workingCopyPath = std::string ();
296
298
if (!dirExists (m_workingDir) && !makeDir (m_workingDir)) {
297
299
cerr << Phrases::Error << " Unable to create working copy for \" " << relativeTestFilePath << " \" : can't create working directory \" "
298
300
<< m_workingDir << " \" ." << Phrases::EndFlush;
299
- return string () ;
301
+ return workingCopyPath ;
300
302
}
301
303
302
304
// ensure subdirectory exists
@@ -319,26 +321,37 @@ string TestApplication::workingCopyPathAs(
319
321
// fail otherwise
320
322
cerr << Phrases::Error << " Unable to create working copy for \" " << relativeWorkingCopyPath << " \" : can't create directory \" "
321
323
<< currentLevel << " \" (inside working directory)." << Phrases::EndFlush;
322
- return string () ;
324
+ return workingCopyPath ;
323
325
}
324
326
}
325
327
326
- // just return the path if we don't want to actually create a copy
327
- if (mode == WorkingCopyMode::NoCopy) {
328
- return m_workingDir + relativeWorkingCopyPath;
328
+ workingCopyPath = m_workingDir + relativeWorkingCopyPath;
329
+ switch (mode) {
330
+ case WorkingCopyMode::NoCopy:
331
+ // just return the path if we don't want to actually create a copy
332
+ return workingCopyPath;
333
+ case WorkingCopyMode::Cleanup:
334
+ // ensure the file does not exist in cleanup mode
335
+ if (std::remove (workingCopyPath.data ()) != 0 && errno != ENOENT) {
336
+ const auto error = std::strerror (errno);
337
+ cerr << Phrases::Error << " Unable to delete \" " << workingCopyPath << " \" : " << error << Phrases::EndFlush;
338
+ workingCopyPath.clear ();
339
+ }
340
+ return workingCopyPath;
341
+ default :;
329
342
}
330
343
331
344
// copy the file
332
- const auto origFilePath (testFilePath (relativeTestFilePath));
333
- auto workingCopyPath (m_workingDir + relativeWorkingCopyPath);
345
+ const auto origFilePath = testFilePath (relativeTestFilePath);
334
346
size_t workingCopyPathAttempt = 0 ;
335
347
NativeFileStream origFile, workingCopy;
336
348
origFile.open (origFilePath, ios_base::in | ios_base::binary);
337
349
if (origFile.fail ()) {
338
350
cerr << Phrases::Error << " Unable to create working copy for \" " << relativeTestFilePath
339
351
<< " \" : an IO error occurred when opening original file \" " << origFilePath << " \" ." << Phrases::EndFlush;
340
- cerr << " error: " << strerror (errno) << endl;
341
- return string ();
352
+ cerr << " error: " << std::strerror (errno) << endl;
353
+ workingCopyPath.clear ();
354
+ return workingCopyPath;
342
355
}
343
356
workingCopy.open (workingCopyPath, ios_base::out | ios_base::binary | ios_base::trunc );
344
357
while (workingCopy.fail () && fileSystemItemExists (workingCopyPath)) {
@@ -351,7 +364,8 @@ string TestApplication::workingCopyPathAs(
351
364
cerr << Phrases::Error << " Unable to create working copy for \" " << relativeTestFilePath
352
365
<< " \" : an IO error occurred when opening target file \" " << workingCopyPath << " \" ." << Phrases::EndFlush;
353
366
cerr << " error: " << strerror (errno) << endl;
354
- return string ();
367
+ workingCopyPath.clear ();
368
+ return workingCopyPath;
355
369
}
356
370
workingCopy << origFile.rdbuf ();
357
371
workingCopy.close ();
@@ -362,7 +376,8 @@ string TestApplication::workingCopyPathAs(
362
376
cerr << Phrases::Error << " Unable to create working copy for \" " << relativeTestFilePath << " \" : " ;
363
377
if (origFile.fail ()) {
364
378
cerr << " an IO error occurred when reading original file \" " << origFilePath << " \" " ;
365
- return string ();
379
+ workingCopyPath.clear ();
380
+ return workingCopyPath;
366
381
}
367
382
if (workingCopy.fail ()) {
368
383
if (origFile.fail ()) {
@@ -371,7 +386,8 @@ string TestApplication::workingCopyPathAs(
371
386
cerr << " an IO error occurred when writing to target file \" " << workingCopyPath << " \" ." ;
372
387
}
373
388
cerr << " error: " << strerror (errno) << endl;
374
- return string ();
389
+ workingCopyPath.clear ();
390
+ return workingCopyPath;
375
391
}
376
392
377
393
#ifdef PLATFORM_UNIX
0 commit comments