From 31c93f0844f91c808033336f665f2db72f3d5b2f Mon Sep 17 00:00:00 2001 From: Mikachu2333 Date: Mon, 6 Oct 2025 00:37:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=20chsrc=5Fprepend=5Fto=5Ffil?= =?UTF-8?q?e=201.=20=E6=94=B9=E8=BF=9B=E4=BA=86=E8=8E=B7=E5=8F=96=E6=96=B0?= =?UTF-8?q?=E5=88=9B=E5=BB=BA=E6=96=87=E4=BB=B6=E5=90=8D=E7=9A=84=E6=96=B9?= =?UTF-8?q?=E5=BC=8F=202.=20=E6=94=B9=E8=BF=9B=E4=BA=86=E5=86=85=E5=AD=98?= =?UTF-8?q?=E6=B3=84=E6=BC=8F=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/framework/core.c | 55 ++++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/src/framework/core.c b/src/framework/core.c index 7ee37b2..70fec13 100644 --- a/src/framework/core.c +++ b/src/framework/core.c @@ -1759,65 +1759,66 @@ chsrc_prepend_to_file (const char *str, const char *filename) char *dir = xy_parent_dir (file); chsrc_ensure_dir (dir); - char *tmpfile_name = "prepend"; - char *tmpfile_ext = ".txt"; - char *tmpnull = ""; - FILE *tmp = chsrc_make_tmpfile (tmpfile_name, tmpfile_ext, true, &tmpnull); - fclose (tmp); - char *temp_file = xy_strcat (3, "chsrc_tmp_", tmpfile_name, tmpfile_ext); - - FILE *input = fopen (file, "r"); - FILE *output = fopen (temp_file, "w"); + char *tmpfile_path = NULL; + FILE *output = chsrc_make_tmpfile ("prepend", ".txt", false, &tmpfile_path); if (!output) { - if (input) fclose (input); - free (temp_file); char *msg = ENGLISH ? xy_2strcat ("Create temp file before Write prepend failed ", file) : xy_2strcat ("尝试在文件开头写入时创建临时文件失败:", file); chsrc_error2 (msg); exit (Exit_ExternalError); } - // 先写入要插入的行 + /* 先写入要插入的内容 */ fprintf (output, "%s", str); - // 如果原文件存在,复制其内容 + /* 如果原文件存在,复制其内容到临时文件 */ + FILE *input = fopen (file, "r"); if (input) { fseek (input, 0, SEEK_END); long file_size = ftell (input); fseek (input, 0, SEEK_SET); - char *buffer = malloc(file_size); - if (buffer == NULL) + if (file_size > 0) { - fclose (input); - return; + char *buffer = malloc (file_size); + if (buffer == NULL) + { + fclose (input); + fclose (output); + remove (tmpfile_path); + char *msg = ENGLISH ? "Memory allocation failed" : "内存分配失败"; + chsrc_error2 (msg); + exit (Exit_ExternalError); + } + + size_t bytes = fread (buffer, 1, file_size, input); + if (bytes > 0) + { + fwrite (buffer, 1, bytes, output); + } + free (buffer); } - - size_t bytes = fread(buffer, 1, file_size, input); - if (bytes > 0) fwrite(buffer, 1, bytes, output); - - free (buffer); fclose (input); } fclose (output); + + /* 删除原文件(如果存在) */ remove (file); - if (rename (temp_file, file) != 0) + /* 将临时文件重命名为目标文件 */ + if (rename (tmpfile_path, file) != 0) { - unlink (temp_file); - free (temp_file); + unlink (tmpfile_path); char *msg = ENGLISH ? xy_2strcat ("Write prepend failed to ", file) : xy_2strcat ("在文件开头写入失败: ", file); chsrc_error2 (msg); exit (Exit_ExternalError); } - free (temp_file); - log_anyway: /* 输出recipe指定的文件名 */ chsrc_log_write (filename, false);