138 lines
4.2 KiB
JavaScript
138 lines
4.2 KiB
JavaScript
const { app } = require('electron');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const { execSync } = require('child_process');
|
|
|
|
console.log('开始测试锁文件清理功能...');
|
|
|
|
// 测试安全删除文件功能
|
|
function testSafeDeleteFile(filePath) {
|
|
console.log(`测试删除文件: ${filePath}`);
|
|
|
|
try {
|
|
// 首先尝试直接删除
|
|
fs.unlinkSync(filePath);
|
|
console.log(`成功删除文件: ${filePath}`);
|
|
} catch (error) {
|
|
if (error.code === 'EBUSY' || error.code === 'EACCES') {
|
|
console.log(`文件被占用,无法删除: ${filePath}`);
|
|
console.log(`错误信息: ${error.message}`);
|
|
|
|
// 在 Windows 上尝试使用 del 命令
|
|
if (process.platform === 'win32') {
|
|
try {
|
|
execSync(`del /F /Q "${filePath}"`, { stdio: 'ignore' });
|
|
console.log(`使用 del 命令删除文件: ${filePath}`);
|
|
} catch (delError) {
|
|
console.error(`del 命令删除失败: ${filePath}`, delError);
|
|
}
|
|
}
|
|
} else {
|
|
console.error(`删除文件失败: ${filePath}`, error);
|
|
}
|
|
}
|
|
}
|
|
|
|
// 测试创建和删除锁文件
|
|
function testLockFileOperations() {
|
|
const userDataPath = app.getPath('userData');
|
|
const testLockFile = path.join(userDataPath, 'test-lockfile');
|
|
|
|
console.log('用户数据路径:', userDataPath);
|
|
console.log('测试锁文件路径:', testLockFile);
|
|
|
|
try {
|
|
// 创建测试锁文件
|
|
fs.writeFileSync(testLockFile, process.pid.toString());
|
|
console.log(`创建测试锁文件: ${testLockFile}, PID: ${process.pid}`);
|
|
|
|
// 等待一段时间
|
|
setTimeout(() => {
|
|
console.log('开始测试删除锁文件...');
|
|
testSafeDeleteFile(testLockFile);
|
|
|
|
// 检查文件是否还存在
|
|
if (fs.existsSync(testLockFile)) {
|
|
console.log('文件仍然存在,尝试强制删除...');
|
|
if (process.platform === 'win32') {
|
|
try {
|
|
execSync(`del /F /Q "${testLockFile}"`, { stdio: 'ignore' });
|
|
console.log('强制删除成功');
|
|
} catch (error) {
|
|
console.error('强制删除失败:', error);
|
|
}
|
|
}
|
|
} else {
|
|
console.log('文件已成功删除');
|
|
}
|
|
|
|
process.exit(0);
|
|
}, 2000);
|
|
|
|
} catch (error) {
|
|
console.error('创建测试锁文件失败:', error);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
// 测试检查现有锁文件
|
|
function testExistingLockFiles() {
|
|
const userDataPath = app.getPath('userData');
|
|
const possibleLockFiles = [
|
|
path.join(userDataPath, 'single-instance-lock'),
|
|
path.join(userDataPath, 'lockfile'),
|
|
path.join(userDataPath, '.lock')
|
|
];
|
|
|
|
console.log('检查现有锁文件...');
|
|
|
|
for (const lockFile of possibleLockFiles) {
|
|
if (fs.existsSync(lockFile)) {
|
|
console.log(`发现锁文件: ${lockFile}`);
|
|
try {
|
|
const content = fs.readFileSync(lockFile, 'utf8');
|
|
console.log(`锁文件内容: ${content}`);
|
|
|
|
const pid = parseInt(content.trim());
|
|
if (pid) {
|
|
console.log(`锁文件中的 PID: ${pid}`);
|
|
|
|
// 检查进程是否还在运行
|
|
if (process.platform === 'win32') {
|
|
try {
|
|
const result = execSync(`tasklist /FI "PID eq ${pid}" /FO CSV /NH`, { encoding: 'utf8' });
|
|
if (result.includes(pid.toString())) {
|
|
console.log(`进程 ${pid} 仍在运行`);
|
|
} else {
|
|
console.log(`进程 ${pid} 不存在,可以删除锁文件`);
|
|
testSafeDeleteFile(lockFile);
|
|
}
|
|
} catch (error) {
|
|
console.log(`进程 ${pid} 不存在,删除锁文件`);
|
|
testSafeDeleteFile(lockFile);
|
|
}
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error(`读取锁文件失败: ${lockFile}`, error);
|
|
testSafeDeleteFile(lockFile);
|
|
}
|
|
} else {
|
|
console.log(`没有找到锁文件: ${lockFile}`);
|
|
}
|
|
}
|
|
}
|
|
|
|
// 运行测试
|
|
console.log('当前进程 PID:', process.pid);
|
|
|
|
// 检查现有锁文件
|
|
testExistingLockFiles();
|
|
|
|
// 如果直接运行此脚本
|
|
if (require.main === module) {
|
|
// 测试锁文件操作
|
|
testLockFileOperations();
|
|
}
|
|
|
|
module.exports = { testSafeDeleteFile, testExistingLockFiles, testLockFileOperations };
|