commit 5379234e9b50541180b4a90a222af2d5fee174b2
parent be7a176e9c4a3ab442622adc2d68bf38e5eb5422
Author: nibo <nibo@relim.de>
Date: Sat, 12 Jul 2025 17:06:52 +0200
Stop overwriting output pdf filepath in case of error
Diffstat:
3 files changed, 47 insertions(+), 4 deletions(-)
diff --git a/src/core.c b/src/core.c
@@ -521,6 +521,31 @@ filepath_resolve_tilde(const char *path)
}
}
+char *
+filepath_as_dot_file(const char *path)
+{
+ const char *c, *last_slash;
+ char *dot_path, *dc;
+
+ last_slash = NULL;
+ for (c = path; *c; c++) {
+ if (*c == '/') {
+ last_slash = c;
+ }
+ }
+ dot_path = emalloc(c - path + 2 * sizeof(char));
+ for (c = path, dc = dot_path; c<=last_slash; c++, dc++) {
+ *dc = *c;
+ }
+ *dc = '.';
+ dc++;
+ for (; *c; c++, dc++) {
+ *dc = *c;
+ }
+ *dc = '\0';
+ return dot_path;
+}
+
static const char *
size_type_to_string(enum SizeType type)
{
diff --git a/src/core.h b/src/core.h
@@ -411,6 +411,7 @@ char *filepath_add_ending_slash_if_missing(const char *path);
char *filepath_basename(const char *path);
char *filepath_dirname(const char *path);
char *filepath_resolve_tilde(const char *path);
+char *filepath_as_dot_file(const char *path);
struct Size *size_create(const char *str);
struct Size *size_copy(struct Size *size);
diff --git a/src/out_pdf.c b/src/out_pdf.c
@@ -501,6 +501,7 @@ pdf_filepath_create(
char *pdf_filepath = NULL;
char *pdf_filename, *tmp;
enum FileType type;
+
if (cho_filepath) {
pdf_filepath = file_extension_replace_or_add(cho_filepath, "pdf");
pdf_filename = filepath_basename(pdf_filepath);
@@ -2852,7 +2853,7 @@ out_pdf_create(
struct PDFContext ctx;
pdfio_rect_t media_box_a4 = { 0.0, 0.0, MEDIABOX_WIDTH, MEDIABOX_HEIGHT };
pdfio_rect_t crop_box = { 0.0, 0.0, MEDIABOX_WIDTH, MEDIABOX_HEIGHT };
- char *dirpath, *pdf_filepath;
+ char *dirpath, *pdf_filepath, *pdf_dot_filepath;
ctx.diagram_font_is_base_font = false;
ctx.fonts = NULL;
@@ -2874,9 +2875,10 @@ out_pdf_create(
LOG_DEBUG("pdf_filepath_create failed.");
return NULL;
}
- ctx.pdf_file = pdfioFileCreate(pdf_filepath, "2.0", &media_box_a4, &crop_box, NULL, NULL);
+ pdf_dot_filepath = filepath_as_dot_file(pdf_filepath);
+ ctx.pdf_file = pdfioFileCreate(pdf_dot_filepath, "2.0", &media_box_a4, &crop_box, NULL, NULL);
if (!ctx.pdf_file) {
- LOG_DEBUG("pdfioFileCreate failed.");
+ LOG_DEBUG("pdfioFileCreateTemporary failed.");
return NULL;
}
if (!pdf_set_title(&ctx, songs)) {
@@ -2919,10 +2921,25 @@ out_pdf_create(
goto CLEAN;
}
objs_free(ctx.fonts);
+ if (rename(pdf_dot_filepath, pdf_filepath)) {
+ LOG_DEBUG("rename failed.");
+ util_log(
+ NULL,
+ 0,
+ LOG_ERR,
+ "Moving temporary created pdf file from '%s' to '%s' failed: %s",
+ pdf_dot_filepath,
+ pdf_filepath,
+ strerror(errno)
+ );
+ }
+ free(pdf_dot_filepath);
return pdf_filepath;
CLEAN:
- if (unlink(pdf_filepath)) {
+ if (unlink(pdf_dot_filepath)) {
LOG_DEBUG("unlink failed.");
}
+ free(pdf_dot_filepath);
+ free(pdf_filepath);
return NULL;
}