-
Notifications
You must be signed in to change notification settings - Fork 311
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
Conversation
ファイル移動に失敗した場合一時ファイルを削除する
🚀 プレビュー用ページを作成しました 🚀 更新時点でのコミットハッシュ: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ほぼLGTMです!!
もうほとんど完成なので、こちらでコード書き換えてマージさせていただこうと思います!!
src/backend/electron/fileHelper.ts
Outdated
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; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
結構ややこしさが上がってしまうので、重ならないランダムであるuuid4を使っちゃいますか!!
こうなるはず!
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桁数字を提案したのは僕なのですみません 🙇 )
src/backend/electron/fileHelper.ts
Outdated
}); | ||
} catch (error) { | ||
fs.promises.unlink(tmpPath).catch((reason) => { | ||
log.warn("Fail to remove %s\n %o", tmpPath, reason); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
こうかも
log.warn("Fail to remove %s\n %o", tmpPath, reason); | |
log.warn("Failed to remove %s\n %o", tmpPath, reason); |
src/backend/electron/fileHelper.ts
Outdated
overwrite: true, | ||
}); | ||
} catch (error) { | ||
fs.promises.unlink(tmpPath).catch((reason) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
「ファイルが存在していたら」も足しても良いかも?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!!
変更に変なとこあったら後からご指摘いただければ 🙇
内容
writeFileSafely
の一時ファイルが既存のファイルを上書きしないようにします。また、ファイル移動に失敗した場合一時ファイルを削除するようにします。
関連 Issue
その他
一時ファイル名のランダム文字列部には
Math.rand()
を使っています。これでも十分なはず?
とりあえず一時ファイルの作成試行回数は16回にしてあります。
あまり回数を増やし過ぎるとメインスレッドを長時間ブロックしてしまいます。