Skip to content

Commit

Permalink
Merge pull request #249 from milaiwi/windows_bug
Browse files Browse the repository at this point in the history
Fixed operation not permitted when renaming directory with children in windows
  • Loading branch information
samlhuillier committed May 11, 2024
2 parents 794d21f + e6fd035 commit 6bd4133
Showing 1 changed file with 31 additions and 9 deletions.
40 changes: 31 additions & 9 deletions electron/main/Files/registerFilesHandler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ipcMain } from "electron";
import { ipcMain, BrowserWindow } from "electron";
import * as path from "path";
import {
FileInfoTree,
Expand All @@ -14,6 +14,8 @@ import {
GetFilesInfoListForListOfPaths,
GetFilesInfoList,
markdownExtensions,
startWatchingDirectory,
updateFileListForRenderer,
} from "./Filesystem";
import * as fs from "fs";
import {
Expand Down Expand Up @@ -140,23 +142,43 @@ export const registerFileHandlers = (
"rename-file-recursive",
async (event, renameFileProps: RenameFileProps) => {
const windowInfo = windowsManager.getWindowInfoForContents(event.sender);

if (!windowInfo) {
throw new Error("Window info not found.");
}

windowsManager.watcher?.unwatch(windowInfo?.vaultDirectoryForWindow);

fs.rename(
renameFileProps.oldFilePath,
renameFileProps.newFilePath,
(err) => {
if (process.platform == 'win32') {
windowsManager.watcher?.close().then(() => {
fs.rename(renameFileProps.oldFilePath, renameFileProps.newFilePath, (err) => {
if (err) {
throw err;
}

// Re-start watching all paths in array
const win = BrowserWindow.fromWebContents(event.sender);
if (win) {
windowsManager.watcher = startWatchingDirectory(
win,
windowInfo.vaultDirectoryForWindow,
);
updateFileListForRenderer(win, windowInfo.vaultDirectoryForWindow);
}
})
});
} else {
// On non-Windows platforms, directly perform the rename operation
fs.rename(renameFileProps.oldFilePath, renameFileProps.newFilePath, (err) => {
if (err) {
throw err;
}
// Re-watch the vault directory after renaming
windowsManager.watcher?.add(windowInfo?.vaultDirectoryForWindow);
}
);

});
}

console.log("reindexing folder");
// then need to trigger reindexing of folder
windowInfo.dbTableClient.updateDBItemsWithNewFilePath(
renameFileProps.oldFilePath,
Expand Down

0 comments on commit 6bd4133

Please sign in to comment.