修复 chsrc_make_tmpfile 在win上错误处理不完善的问题

This commit is contained in:
Mikachu2333 2025-10-06 00:29:52 +08:00 committed by 曾奥然
parent 0a49c7b2d9
commit 04da1243f5

View File

@ -1396,23 +1396,46 @@ chsrc_run_directly (const char *cmd)
FILE * FILE *
chsrc_make_tmpfile (char *filename, char *postfix, bool loud, char **tmpfilepath) chsrc_make_tmpfile (char *filename, char *postfix, bool loud, char **tmpfilepath)
{ {
char *tmpfile = NULL;
FILE *f = NULL;
#ifdef XY_Build_On_Windows #ifdef XY_Build_On_Windows
/** /**
* Windows mkstemps() mkstemp() _mktemp_s() * Windows 使 GetTempPath GetTempFileName
* postfix * Windows API
* PowerShell 使 -File .ps1 *
* 使 Windows * GetTempFileName
* 1. 使 GetTempFileName
* 2. PowerShell .ps1
*/ */
char *tmpfile = xy_strcat (3, "chsrc_tmp_", filename, postfix); char temp_path[MAX_PATH];
FILE *f = fopen (tmpfile, "w+"); char temp_filename[MAX_PATH];
#else
char *tmpfile = xy_strcat (5, "/tmp/", "chsrc_tmp_", filename, "_XXXXXX", postfix);
size_t postfix_len = strlen (postfix);
/* 和 _mktemp_s() 参数不同,前者是整个缓存区大小,这里的长度是后缀长度 */ /* 获取系统临时目录 */
int fd = mkstemps (tmpfile, postfix_len); DWORD ret = GetTempPathA (MAX_PATH, temp_path);
FILE *f = fdopen (fd, "w+"); if (ret == 0 || ret > MAX_PATH)
#endif {
char *msg = CHINESE ? "无法获取系统临时目录" : "Unable to get system temp directory";
chsrc_error2 (msg);
exit (Exit_ExternalError);
}
/* 生成唯一的临时文件名 (会自动创建文件) */
ret = GetTempFileNameA (temp_path, "chsrc_", 0, temp_filename);
if (ret == 0)
{
char *msg = CHINESE ? "无法生成临时文件名" : "Unable to generate temporary filename";
chsrc_error2 (msg);
exit (Exit_ExternalError);
}
tmpfile = xy_strcat (4, temp_filename, "_", filename, postfix);
/* 删除 GetTempFileName 自动创建的文件 */
DeleteFileA (temp_filename);
/* 创建带有正确后缀的文件 */
f = fopen (tmpfile, "w+");
if (!f) if (!f)
{ {
@ -1421,7 +1444,38 @@ chsrc_make_tmpfile (char *filename, char *postfix, bool loud, char **tmpfilepath
chsrc_error2 (msg); chsrc_error2 (msg);
exit (Exit_ExternalError); exit (Exit_ExternalError);
} }
else if (loud) #else
/**
* Windows 使 mkstemps()
* POSIX
*/
tmpfile = xy_strcat (5, "/tmp/", "chsrc_tmp_", filename, "_XXXXXX", postfix);
size_t postfix_len = strlen (postfix);
/* mkstemps() 会原子性地创建文件并返回文件描述符 */
int fd = mkstemps (tmpfile, postfix_len);
if (fd == -1)
{
char *msg = CHINESE ? "无法创建临时文件: " : "Unable to create temporary file: ";
msg = xy_2strcat (msg, tmpfile);
chsrc_error2 (msg);
exit (Exit_ExternalError);
}
f = fdopen (fd, "w+");
if (!f)
{
close (fd); /* 关闭文件描述符以避免泄漏 */
char *msg = CHINESE ? "无法打开临时文件: " : "Unable to open temporary file: ";
msg = xy_2strcat (msg, tmpfile);
chsrc_error2 (msg);
exit (Exit_ExternalError);
}
#endif
if (loud)
{ {
char *msg = CHINESE ? "已创建临时文件: " : "Temporary file created: "; char *msg = CHINESE ? "已创建临时文件: " : "Temporary file created: ";
msg = xy_2strcat (msg, tmpfile); msg = xy_2strcat (msg, tmpfile);