fix: resolve cross-platform EPERM permissions errors

modManager.js:
- Switch from hardcoded 'junction' to dynamic symlink type based on OS (fixing Linux EPERM).
- Add retry logic for directory removal to handle file locking race conditions.
- Improve broken symlink detection during profile sync.

gameManager.js:
- Implement retry loop (3 attempts) for game directory removal in updateGameFiles to prevent EBUSY/EPERM errors on Windows.

paths.js:
- Prevent fs.mkdirSync failure in getModsPath by pre-checking for broken symbolic links.
This commit is contained in:
Fazri Gading
2026-01-26 08:19:13 +08:00
parent 4cd0539ce3
commit 615ee5cadc
3 changed files with 53 additions and 11 deletions

View File

@@ -179,9 +179,18 @@ async function getModsPath(customInstallPath = null) {
const profilesPath = path.join(userDataPath, 'Profiles'); const profilesPath = path.join(userDataPath, 'Profiles');
if (!fs.existsSync(modsPath)) { if (!fs.existsSync(modsPath)) {
// Check for broken symlink to avoid EEXIST/EPERM on mkdir
let isBrokenLink = false;
try {
const stats = fs.lstatSync(modsPath);
if (stats.isSymbolicLink()) isBrokenLink = true;
} catch (e) { /* ignore */ }
if (!isBrokenLink) {
// Ensure the Mods directory exists // Ensure the Mods directory exists
fs.mkdirSync(modsPath, { recursive: true }); fs.mkdirSync(modsPath, { recursive: true });
} }
}
if (!fs.existsSync(disabledModsPath)) { if (!fs.existsSync(disabledModsPath)) {
fs.mkdirSync(disabledModsPath, { recursive: true }); fs.mkdirSync(disabledModsPath, { recursive: true });
} }

View File

@@ -365,7 +365,21 @@ async function updateGameFiles(newVersion, progressCallback, gameDir = GAME_DIR,
if (fs.existsSync(gameDir)) { if (fs.existsSync(gameDir)) {
console.log('Removing old game files...'); console.log('Removing old game files...');
let retries = 3;
while (retries > 0) {
try {
fs.rmSync(gameDir, { recursive: true, force: true }); fs.rmSync(gameDir, { recursive: true, force: true });
break;
} catch (err) {
if ((err.code === 'EPERM' || err.code === 'EBUSY') && retries > 0) {
retries--;
console.log(`[UpdateGameFiles] Removal failed with ${err.code}, retrying in 1s... (${retries} retries left)`);
await new Promise(resolve => setTimeout(resolve, 1000));
} else {
throw err;
}
}
}
} }
fs.renameSync(tempUpdateDir, gameDir); fs.renameSync(tempUpdateDir, gameDir);

View File

@@ -2,6 +2,7 @@ const fs = require('fs');
const path = require('path'); const path = require('path');
const crypto = require('crypto'); const crypto = require('crypto');
const axios = require('axios'); const axios = require('axios');
const { getOS } = require('../utils/platformUtils');
const { getModsPath, getProfilesDir } = require('../core/paths'); const { getModsPath, getProfilesDir } = require('../core/paths');
const { saveModsToConfig, loadModsFromConfig } = require('../core/config'); const { saveModsToConfig, loadModsFromConfig } = require('../core/config');
const profileManager = require('./profileManager'); const profileManager = require('./profileManager');
@@ -307,11 +308,16 @@ async function syncModsForCurrentProfile() {
// 2. Symlink / Migration Logic // 2. Symlink / Migration Logic
let needsLink = false; let needsLink = false;
let globalStats = null;
if (fs.existsSync(globalModsPath)) { try {
const stats = fs.lstatSync(globalModsPath); globalStats = fs.lstatSync(globalModsPath);
} catch (e) {
// Path doesn't exist
}
if (stats.isSymbolicLink()) { if (globalStats) {
if (globalStats.isSymbolicLink()) {
const linkTarget = fs.readlinkSync(globalModsPath); const linkTarget = fs.readlinkSync(globalModsPath);
// Normalize paths for comparison // Normalize paths for comparison
if (path.resolve(linkTarget) !== path.resolve(profileModsPath)) { if (path.resolve(linkTarget) !== path.resolve(profileModsPath)) {
@@ -319,7 +325,7 @@ async function syncModsForCurrentProfile() {
fs.unlinkSync(globalModsPath); fs.unlinkSync(globalModsPath);
needsLink = true; needsLink = true;
} }
} else if (stats.isDirectory()) { } else if (globalStats.isDirectory()) {
// MIGRATION: It's a real directory. Move contents to profile. // MIGRATION: It's a real directory. Move contents to profile.
console.log('[ModManager] Migrating global mods folder to profile folder...'); console.log('[ModManager] Migrating global mods folder to profile folder...');
const files = fs.readdirSync(globalModsPath); const files = fs.readdirSync(globalModsPath);
@@ -348,8 +354,21 @@ async function syncModsForCurrentProfile() {
} }
// Remove the directory so we can link it // Remove the directory so we can link it
try {
let retries = 3;
while (retries > 0) {
try { try {
fs.rmSync(globalModsPath, { recursive: true, force: true }); fs.rmSync(globalModsPath, { recursive: true, force: true });
break;
} catch (err) {
if ((err.code === 'EPERM' || err.code === 'EBUSY') && retries > 0) {
retries--;
await new Promise(resolve => setTimeout(resolve, 500));
} else {
throw err;
}
}
}
needsLink = true; needsLink = true;
} catch (e) { } catch (e) {
console.error('Failed to remove global mods dir:', e); console.error('Failed to remove global mods dir:', e);
@@ -364,8 +383,8 @@ async function syncModsForCurrentProfile() {
if (needsLink) { if (needsLink) {
console.log(`[ModManager] Creating symlink: ${globalModsPath} -> ${profileModsPath}`); console.log(`[ModManager] Creating symlink: ${globalModsPath} -> ${profileModsPath}`);
try { try {
// 'junction' is key for Windows without admin const symlinkType = getOS() === 'windows' ? 'junction' : 'dir';
fs.symlinkSync(profileModsPath, globalModsPath, 'junction'); fs.symlinkSync(profileModsPath, globalModsPath, symlinkType);
} catch (err) { } catch (err) {
// If we can't create the symlink, try creating the directory first // If we can't create the symlink, try creating the directory first
console.error('[ModManager] Failed to create symlink. Falling back to direct folder mode.'); console.error('[ModManager] Failed to create symlink. Falling back to direct folder mode.');