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

ファイル保存全般をアトミックにする #2308

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions src/backend/electron/electronConfig.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { join } from "path";
import fs from "fs";
import { app } from "electron";
import { moveFile } from "move-file";
import { BaseConfigManager, Metadata } from "@/backend/common/ConfigManager";
import { ConfigType } from "@/type/preload";
import { writeFileSafely } from "@/helpers/fileHelper";

export class ElectronConfigManager extends BaseConfigManager {
protected getAppVersion() {
Expand All @@ -23,16 +23,10 @@ export class ElectronConfigManager extends BaseConfigManager {
}

protected async save(config: ConfigType & Metadata) {
// ファイル書き込みに失敗したときに設定が消えないように、tempファイル書き込み後上書き移動する
const temp_path = `${this.configPath}.tmp`;
await fs.promises.writeFile(
temp_path,
await writeFileSafely(
this.configPath,
JSON.stringify(config, undefined, 2),
);

await moveFile(temp_path, this.configPath, {
overwrite: true,
});
}

private get configPath(): string {
Expand Down
5 changes: 3 additions & 2 deletions src/backend/electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
TextAsset,
} from "@/type/preload";
import { themes } from "@/domain/theme";
import { writeFileSafely } from "@/helpers/fileHelper";

type SingleInstanceLockData = {
filePath: string | undefined;
Expand Down Expand Up @@ -742,9 +743,9 @@ registerIpcMainHandle<IpcMainHandle>({
win.show();
},

WRITE_FILE: (_, { filePath, buffer }) => {
WRITE_FILE: async (_, { filePath, buffer }) => {
try {
fs.writeFileSync(
await writeFileSafely(
filePath,
new DataView(buffer instanceof Uint8Array ? buffer.buffer : buffer),
);
Expand Down
4 changes: 2 additions & 2 deletions src/backend/electron/manager/RuntimeInfoManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
* ランタイム情報には起動しているエンジンのURLなどが含まれる。
*/

import fs from "fs";
import AsyncLock from "async-lock";
import log from "electron-log/main";
import type { AltPortInfos } from "@/store/type";
import { EngineId, EngineInfo } from "@/type/preload";
import { writeFileSafely } from "@/helpers/fileHelper";

/**
* ランタイム情報書き出しに必要なEngineInfo
Expand Down Expand Up @@ -99,7 +99,7 @@ export class RuntimeInfoManager {

// ファイル書き出し
try {
await fs.promises.writeFile(
await writeFileSafely(
this.runtimeInfoPath,
JSON.stringify(runtimeInfoFormatFor3rdParty), // FIXME: zod化する
);
Expand Down
15 changes: 15 additions & 0 deletions src/helpers/fileHelper.ts
Copy link
Contributor

Choose a reason for hiding this comment

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

fileHelper.tsはレンダラープロセスで使用されています。
そのためnodeAPI(fsmove-file)をインポートするとバンドルに失敗してビルドが失敗しているのではないかと思います。

Copy link
Author

Choose a reason for hiding this comment

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

webアプリ全般に詳しくなく、見当がついていない状況だったので、レビュー大変助かります!

fileHelper.tsはレンダラープロセスで使用されています。
そのためnodeAPI(fsやmove-file)をインポートするとバンドルに失敗してビルドが失敗しているのではないかと思います。

なるほどです。であれば、追加するヘルパー関数はsrc/backend/electron以下に別途ファイルを作るなどして、メインプロセスからのみ使用するファイルに記載する必要がありそうですね。

メインプロセス、レンダラープロセスについて理解していない部分があるので、調べつつ修正してみます!

Copy link
Member

@Hiroshiba Hiroshiba Oct 22, 2024

Choose a reason for hiding this comment

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

あっ!!! なるほど、すみませんでした!!!!

とりあえず一旦の解決策としては、electron用のディレクトリにヘルパー関数を置くのが良さそうだと思います!!
(と言おうとしたら既にそう提案されてますね!!! 非常に良いと思います!!!)

ちなみに取っ掛かりとしてelectronの仕組みをちょっとだけご説明すると。
electronはchromiumブラウザでGUIを表示するプロセス(レンダラー側)と、あとはそのGUIを起動したり、ブラウザではできないことをする用のメインプロセスに分かれていて、ファイル操作はメインプロセスじゃないとできないようになっている感じです!
なのでレンダラー側からファイル操作の処理をimportしたときに、そんなものがないのでエラーになる感じかなと・・・!!

Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import fs from "fs";
import { moveFile } from "move-file";
import { ResultError } from "@/type/result";

export async function writeFileSafely(
path: string,
data: string | NodeJS.ArrayBufferView,
) {
// ファイル書き込みに失敗したときに設定が消えないように、tempファイル書き込み後上書き移動する
const temp_path = `${path}.tmp`;
await fs.promises.writeFile(temp_path, data);

await moveFile(temp_path, path, {
overwrite: true,
});
}

/** ファイル書き込み時のエラーメッセージを生成する */
// instanceof ResultErrorで生まれるResultError<any>を受け取れるようにするため、anyを許容する
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
Loading