改进 chsrc_prepend_to_file

1. 改进了获取新创建文件名的方式
2. 改进了内存泄漏问题
This commit is contained in:
Mikachu2333
2025-10-06 00:37:07 +08:00
committed by 曾奥然
parent 04da1243f5
commit 31c93f0844

View File

@@ -1759,65 +1759,66 @@ chsrc_prepend_to_file (const char *str, const char *filename)
char *dir = xy_parent_dir (file); char *dir = xy_parent_dir (file);
chsrc_ensure_dir (dir); chsrc_ensure_dir (dir);
char *tmpfile_name = "prepend"; char *tmpfile_path = NULL;
char *tmpfile_ext = ".txt"; FILE *output = chsrc_make_tmpfile ("prepend", ".txt", false, &tmpfile_path);
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");
if (!output) if (!output)
{ {
if (input) fclose (input);
free (temp_file);
char *msg = ENGLISH ? xy_2strcat ("Create temp file before Write prepend failed ", file) char *msg = ENGLISH ? xy_2strcat ("Create temp file before Write prepend failed ", file)
: xy_2strcat ("尝试在文件开头写入时创建临时文件失败:", file); : xy_2strcat ("尝试在文件开头写入时创建临时文件失败:", file);
chsrc_error2 (msg); chsrc_error2 (msg);
exit (Exit_ExternalError); exit (Exit_ExternalError);
} }
// 先写入要插入的 /* 先写入要插入的内容 */
fprintf (output, "%s", str); fprintf (output, "%s", str);
// 如果原文件存在,复制其内容 /* 如果原文件存在,复制其内容到临时文件 */
FILE *input = fopen (file, "r");
if (input) if (input)
{ {
fseek (input, 0, SEEK_END); fseek (input, 0, SEEK_END);
long file_size = ftell (input); long file_size = ftell (input);
fseek (input, 0, SEEK_SET); fseek (input, 0, SEEK_SET);
char *buffer = malloc(file_size); if (file_size > 0)
{
char *buffer = malloc (file_size);
if (buffer == NULL) if (buffer == NULL)
{ {
fclose (input); fclose (input);
return; 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); size_t bytes = fread (buffer, 1, file_size, input);
if (bytes > 0) fwrite(buffer, 1, bytes, output); if (bytes > 0)
{
fwrite (buffer, 1, bytes, output);
}
free (buffer); free (buffer);
}
fclose (input); fclose (input);
} }
fclose (output); fclose (output);
/* 删除原文件(如果存在) */
remove (file); remove (file);
if (rename (temp_file, file) != 0) /* 将临时文件重命名为目标文件 */
if (rename (tmpfile_path, file) != 0)
{ {
unlink (temp_file); unlink (tmpfile_path);
free (temp_file);
char *msg = ENGLISH ? xy_2strcat ("Write prepend failed to ", file) char *msg = ENGLISH ? xy_2strcat ("Write prepend failed to ", file)
: xy_2strcat ("在文件开头写入失败: ", file); : xy_2strcat ("在文件开头写入失败: ", file);
chsrc_error2 (msg); chsrc_error2 (msg);
exit (Exit_ExternalError); exit (Exit_ExternalError);
} }
free (temp_file);
log_anyway: log_anyway:
/* 输出recipe指定的文件名 */ /* 输出recipe指定的文件名 */
chsrc_log_write (filename, false); chsrc_log_write (filename, false);