Skip to content

Commit 1d4e4f0

Browse files
committed
cstrans-df-run --in-place: print more accurate diagnostic
... in case the input file cannot be opened, or a temporary file created
1 parent 7adc478 commit 1d4e4f0

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

cstrans-df-run.cc

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,14 +252,32 @@ bool DockerFileTransformer::transform(std::istream &in, std::ostream &out)
252252
return !anyError;
253253
}
254254

255+
void printOpenError(const char *msg, const std::string &fileName)
256+
{
257+
std::cerr << prog_name << ": error: "
258+
<< msg << ": " << fileName
259+
<< " (" << std::strerror(errno) << ")\n";
260+
}
261+
255262
bool transformInPlace(DockerFileTransformer &dft, const std::string &fileName)
256263
{
257264
using namespace boost::filesystem;
258265

259266
// open input file and temporary output file
260267
std::fstream fin(fileName.c_str(), std::ios::in);
261-
path tmpFileName = unique_path();
262-
std::fstream fout(tmpFileName.native().c_str(), std::ios::out);
268+
if (!fin) {
269+
printOpenError("failed to open input file", fileName);
270+
return false;
271+
}
272+
273+
const path tmpFilePath = unique_path();
274+
const std::string tmpFileName = tmpFilePath.native();
275+
std::fstream fout(tmpFileName.c_str(), std::ios::out);
276+
if (!fout) {
277+
printOpenError("failed to create temporary file", tmpFileName);
278+
fin.close();
279+
return false;
280+
}
263281

264282
// transform fin -> fout and close the streams
265283
const bool ok = dft.transform(fin, fout);

0 commit comments

Comments
 (0)