Verify that output file can be written before rendering

This commit is contained in:
josh committed 2026-11-01 20:38:25 -05:00
1 parent 6b77b1a666
commit c8f6d36bdc
1 file changed
+21
+21
View File
@@ -1,4 +1,7 @@
#include <fart/Engine.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
Engine::Engine(const RenderParams & params)
: params(params)
@@ -28,5 +31,23 @@ int Engine::run(string_view input_file, opt<string_view> output_file)
return 1;
}
/* Verify the output file can be written before rendering. */
FILE * fh = fopen(output_path.c_str(), "wb");
if (fh == nullptr)
{
fprintf(stderr, "Error opening output file '%s': %s\n", output_path.c_str(), strerror(errno));
return 1;
}
if (fclose(fh) != 0)
{
fprintf(stderr, "Error closing output file '%s': %s\n", output_path.c_str(), strerror(errno));
return 1;
}
if (remove(output_path.c_str()) != 0)
{
fprintf(stderr, "Error removing output file '%s': %s\n", output_path.c_str(), strerror(errno));
return 1;
}
return 0;
}