Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: writeFileSafelyの一時ファイルが既存のファイルを上書きしないようにする #2454

Merged
merged 3 commits into from
Jan 1, 2025

Conversation

sabonerune
Copy link
Contributor

内容

writeFileSafelyの一時ファイルが既存のファイルを上書きしないようにします。
また、ファイル移動に失敗した場合一時ファイルを削除するようにします。

関連 Issue

その他

一時ファイル名のランダム文字列部にはMath.rand()を使っています。
これでも十分なはず?

とりあえず一時ファイルの作成試行回数は16回にしてあります。
あまり回数を増やし過ぎるとメインスレッドを長時間ブロックしてしまいます。

ファイル移動に失敗した場合一時ファイルを削除する
@sabonerune sabonerune requested a review from a team as a code owner December 30, 2024 10:09
@sabonerune sabonerune requested review from Hiroshiba and removed request for a team December 30, 2024 10:09
@voicevox-preview-pages
Copy link

voicevox-preview-pages bot commented Dec 30, 2024

🚀 プレビュー用ページを作成しました 🚀

更新時点でのコミットハッシュ:ae34263

Copy link
Member

@Hiroshiba Hiroshiba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ほぼLGTMです!!

もうほとんど完成なので、こちらでコード書き換えてマージさせていただこうと思います!!

Comment on lines 13 to 31
let tmpPath: string;
let maxRetries = 16;
while (true) {
maxRetries--;
try {
// ランダムな文字列を8文字生成
const randStr = Math.floor(Math.random() * 36 ** 8)
.toString(36)
.padStart(8, "0");
tmpPath = `${path}-${randStr}.tmp`;
fs.writeFileSync(tmpPath, data, { flag: "wx" });
break;
} catch (error) {
const e = error as NodeJS.ErrnoException;
if (e.code !== "EEXIST" || maxRetries <= 0) {
throw e;
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

結構ややこしさが上がってしまうので、重ならないランダムであるuuid4を使っちゃいますか!!
こうなるはず!

Suggested change
let tmpPath: string;
let maxRetries = 16;
while (true) {
maxRetries--;
try {
// ランダムな文字列を8文字生成
const randStr = Math.floor(Math.random() * 36 ** 8)
.toString(36)
.padStart(8, "0");
tmpPath = `${path}-${randStr}.tmp`;
fs.writeFileSync(tmpPath, data, { flag: "wx" });
break;
} catch (error) {
const e = error as NodeJS.ErrnoException;
if (e.code !== "EEXIST" || maxRetries <= 0) {
throw e;
}
}
}
const tmpPath = `${path}-${uuid4()}.tmp`;
fs.writeFileSync(tmpPath, data, { flag: "wx" });

(8桁数字を提案したのは僕なのですみません 🙇 )

});
} catch (error) {
fs.promises.unlink(tmpPath).catch((reason) => {
log.warn("Fail to remove %s\n %o", tmpPath, reason);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

こうかも

Suggested change
log.warn("Fail to remove %s\n %o", tmpPath, reason);
log.warn("Failed to remove %s\n %o", tmpPath, reason);

overwrite: true,
});
} catch (error) {
fs.promises.unlink(tmpPath).catch((reason) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

「ファイルが存在していたら」も足しても良いかも?

Copy link
Member

@Hiroshiba Hiroshiba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!!

変更に変なとこあったら後からご指摘いただければ 🙇

@Hiroshiba Hiroshiba merged commit a52114a into VOICEVOX:main Jan 1, 2025
10 checks passed
@sabonerune sabonerune deleted the fix/file-helper-safety branch January 2, 2025 03:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants