Skip to content

Commit 31c93f0

Browse files
Mikachu2333ccmywish
authored andcommitted
改进 chsrc_prepend_to_file
1. 改进了获取新创建文件名的方式 2. 改进了内存泄漏问题
1 parent 04da124 commit 31c93f0

File tree

1 file changed

+28
-27
lines changed

1 file changed

+28
-27
lines changed

src/framework/core.c

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1759,65 +1759,66 @@ chsrc_prepend_to_file (const char *str, const char *filename)
17591759
char *dir = xy_parent_dir (file);
17601760
chsrc_ensure_dir (dir);
17611761

1762-
char *tmpfile_name = "prepend";
1763-
char *tmpfile_ext = ".txt";
1764-
char *tmpnull = "";
1765-
FILE *tmp = chsrc_make_tmpfile (tmpfile_name, tmpfile_ext, true, &tmpnull);
1766-
fclose (tmp);
1767-
char *temp_file = xy_strcat (3, "chsrc_tmp_", tmpfile_name, tmpfile_ext);
1768-
1769-
FILE *input = fopen (file, "r");
1770-
FILE *output = fopen (temp_file, "w");
1762+
char *tmpfile_path = NULL;
1763+
FILE *output = chsrc_make_tmpfile ("prepend", ".txt", false, &tmpfile_path);
17711764

17721765
if (!output)
17731766
{
1774-
if (input) fclose (input);
1775-
free (temp_file);
17761767
char *msg = ENGLISH ? xy_2strcat ("Create temp file before Write prepend failed ", file)
17771768
: xy_2strcat ("尝试在文件开头写入时创建临时文件失败:", file);
17781769
chsrc_error2 (msg);
17791770
exit (Exit_ExternalError);
17801771
}
17811772

1782-
// 先写入要插入的行
1773+
/* 先写入要插入的内容 */
17831774
fprintf (output, "%s", str);
17841775

1785-
// 如果原文件存在,复制其内容
1776+
/* 如果原文件存在,复制其内容到临时文件 */
1777+
FILE *input = fopen (file, "r");
17861778
if (input)
17871779
{
17881780
fseek (input, 0, SEEK_END);
17891781
long file_size = ftell (input);
17901782
fseek (input, 0, SEEK_SET);
17911783

1792-
char *buffer = malloc(file_size);
1793-
if (buffer == NULL)
1784+
if (file_size > 0)
17941785
{
1795-
fclose (input);
1796-
return;
1797-
}
1798-
1799-
size_t bytes = fread(buffer, 1, file_size, input);
1800-
if (bytes > 0) fwrite(buffer, 1, bytes, output);
1786+
char *buffer = malloc (file_size);
1787+
if (buffer == NULL)
1788+
{
1789+
fclose (input);
1790+
fclose (output);
1791+
remove (tmpfile_path);
1792+
char *msg = ENGLISH ? "Memory allocation failed" : "内存分配失败";
1793+
chsrc_error2 (msg);
1794+
exit (Exit_ExternalError);
1795+
}
18011796

1802-
free (buffer);
1797+
size_t bytes = fread (buffer, 1, file_size, input);
1798+
if (bytes > 0)
1799+
{
1800+
fwrite (buffer, 1, bytes, output);
1801+
}
1802+
free (buffer);
1803+
}
18031804
fclose (input);
18041805
}
18051806

18061807
fclose (output);
1808+
1809+
/* 删除原文件(如果存在) */
18071810
remove (file);
18081811

1809-
if (rename (temp_file, file) != 0)
1812+
/* 将临时文件重命名为目标文件 */
1813+
if (rename (tmpfile_path, file) != 0)
18101814
{
1811-
unlink (temp_file);
1812-
free (temp_file);
1815+
unlink (tmpfile_path);
18131816
char *msg = ENGLISH ? xy_2strcat ("Write prepend failed to ", file)
18141817
: xy_2strcat ("在文件开头写入失败: ", file);
18151818
chsrc_error2 (msg);
18161819
exit (Exit_ExternalError);
18171820
}
18181821

1819-
free (temp_file);
1820-
18211822
log_anyway:
18221823
/* 输出recipe指定的文件名 */
18231824
chsrc_log_write (filename, false);

0 commit comments

Comments
 (0)