Skip to content

A modern, self-hosted backup solution for secure, encrypted backups across local and cloud storage.

fix: file owner name issue on linux in File Manager

towfiqi20a155ea7935
diff --git a/backend/__tests__/managers/BaseSystemManager.test.ts b/backend/__tests__/managers/BaseSystemManager.test.ts
index 4482dc5..38d3c3b 100644
--- a/backend/__tests__/managers/BaseSystemManager.test.ts
+++ b/backend/__tests__/managers/BaseSystemManager.test.ts
@@ -338,6 +338,7 @@ describe('BaseSystemManager', () => {
describe('getBrowsePath', () => {
it('without givenPath returns drives with stats and fallback on stat error', async () => {
const manager = new BaseSystemManager();
+ readFileMock.mockResolvedValue('root:x:0:0:root:/root:/bin/bash\nalice:x:1000:1000::/home/alice:/bin/bash\n');
const drivesResult = {
success: true,
result: [
@@ -364,12 +365,14 @@ describe('BaseSystemManager', () => {
const items = (res.result as any).items;
const etc = items.find((i: any) => i.path === '/etc');
const missing = items.find((i: any) => i.path === '/missing');
+ expect(etc.owner).toBe('alice');
expect(etc.permissions.startsWith('d')).toBe(true);
expect(missing.permissions).toBe('');
});
it('with givenPath lists directory contents sorted with directories first', async () => {
const manager = new BaseSystemManager();
+ readFileMock.mockResolvedValue('root:x:0:0:root:/root:/bin/bash\nalice:x:1000:1000::/home/alice:/bin/bash\n');
// Dirents
readdirMock.mockResolvedValue([
{ name: 'b', isDirectory: () => true },
@@ -390,6 +393,24 @@ describe('BaseSystemManager', () => {
expect(items[1].name).toBe('a.txt');
expect(items[0].isDirectory).toBe(true);
expect(items[1].isDirectory).toBe(false);
+ expect(items[0].owner).toBe('alice');
+ expect(items[1].owner).toBe('alice');
+ });
+
+ it('falls back to uid string when linux owner name is unknown', async () => {
+ const manager = new BaseSystemManager();
+ readFileMock.mockResolvedValue('root:x:0:0:root:/root:/bin/bash\n');
+ readdirMock.mockResolvedValue([{ name: 'orphaned', isDirectory: () => false }]);
+ statMock.mockResolvedValue({
+ size: 5,
+ mtime: new Date('2021-01-02'),
+ uid: 12345,
+ mode: 0o100644,
+ } as any);
+
+ const res = await manager.getBrowsePath('/tmp');
+ expect(res.success).toBe(true);
+ expect((res.result as any).items[0].owner).toBe('12345');
});
});
diff --git a/backend/__tests__/utils/executeUserScript.test.ts b/backend/__tests__/utils/executeUserScript.test.ts
index d2bb641..6536cec 100644
--- a/backend/__tests__/utils/executeUserScript.test.ts
+++ b/backend/__tests__/utils/executeUserScript.test.ts
@@ -93,13 +93,6 @@ describe('executeUserScript', () => {
).rejects.toThrow('Script path must be absolute');
});
- it('rejects when script file does not exist', async () => {
- (fs.existsSync as jest.Mock).mockReturnValue(false);
- await expect(
- executeUserScript(buildScript({ scriptPath: '/scripts/missing.sh' }))
- ).rejects.toThrow('Script file does not exist');
- });
-
it('rejects when scriptPath is a directory', async () => {
(fs.statSync as jest.Mock).mockReturnValue({ isDirectory: () => true });
await expect(executeUserScript(buildScript({ scriptPath: '/scripts/dir/' }))).rejects.toThrow(
diff --git a/backend/src/managers/BaseSystemManager.ts b/backend/src/managers/BaseSystemManager.ts
index c24b31c..022c0be 100644
--- a/backend/src/managers/BaseSystemManager.ts
+++ b/backend/src/managers/BaseSystemManager.ts
@@ -1,6 +1,6 @@
import os from 'os';
import { execSync } from 'child_process';
-import { chmod, readdir, stat, mkdtemp, rm } from 'fs/promises';
+import { chmod, readFile, readdir, stat, mkdtemp, rm } from 'fs/promises';
import { createWriteStream } from 'fs';
import path from 'path';
import si from 'systeminformation';
@@ -20,6 +20,7 @@ const execAsync = promisify(exec);
export class BaseSystemManager {
private storageManager: BaseStorageManager;
+ private linuxOwnerNames?: Promise<Map<number, string>>;
constructor() {
this.storageManager = new BaseStorageManager();
}
@@ -493,6 +494,7 @@ export class BaseSystemManager {
drives.result.map(async (drive: any) => {
try {
const stats = await stat(drive.path);
+ const owner = await this.getOwnerName(stats.uid);
return {
name: drive.name,
path: drive.path,
@@ -500,7 +502,7 @@ export class BaseSystemManager {
isDirectory: drive.isDirectory,
size: stats.isDirectory() ? 0 : stats.size,
modifiedAt: stats.mtime,
- owner: stats.uid,
+ owner,
permissions: this.formatPermissions(stats.mode),
};
} catch (error) {
@@ -540,6 +542,7 @@ export class BaseSystemManager {
try {
const fullPath = path.join(formattedPath, item.name).split(path.sep).join('/');
const stats = await stat(fullPath);
+ const owner = await this.getOwnerName(stats.uid);
return {
name: item.name,
@@ -548,7 +551,7 @@ export class BaseSystemManager {
isDirectory: item.isDirectory(),
size: stats.size,
modifiedAt: stats.mtime,
- owner: stats.uid,
+ owner,
permissions: this.formatPermissions(stats.mode),
};
} catch (error: any) {
@@ -579,6 +582,41 @@ export class BaseSystemManager {
};
}
}
+
+ private async getOwnerName(uid?: number): Promise<string | number> {
+ if (typeof uid !== 'number') {
+ return '';
+ }
+
+ if (os.platform() !== 'linux') {
+ return uid;
+ }
+
+ const ownerNames = await this.getLinuxOwnerNames();
+ return ownerNames.get(uid) || String(uid);
+ }
+
+ private async getLinuxOwnerNames(): Promise<Map<number, string>> {
+ if (!this.linuxOwnerNames) {
+ this.linuxOwnerNames = readFile('/etc/passwd', 'utf8')
+ .then(contents => {
+ const ownerNames = new Map<number, string>();
+ for (const line of contents.split('\n')) {
+ if (!line || line.startsWith('#')) continue;
+ const [name, , uidText] = line.split(':');
+ const uid = Number.parseInt(uidText, 10);
+ if (name && Number.isInteger(uid)) {
+ ownerNames.set(uid, name);
+ }
+ }
+ return ownerNames;
+ })
+ .catch(() => new Map<number, string>());
+ }
+
+ return this.linuxOwnerNames;
+ }
+
private formatPermissions(mode: number): string {
const permissions = [
mode & 0o400 ? 'r' : '-',