Skip to content

Commit

Permalink
Merge branch 'master-erb' into linux
Browse files Browse the repository at this point in the history
  • Loading branch information
JoanVicens committed Mar 28, 2024
2 parents 5d835cd + 82e23d6 commit bbc10ea
Show file tree
Hide file tree
Showing 104 changed files with 1,570 additions and 318 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -305,9 +305,12 @@
"uuid": "^8.3.2"
},
"devEngines": {
"node": ">=14.x",
"node": ">=18.0.0 <19.0.0",
"npm": ">=7.x"
},
"engines": {
"node": ">=18.0.0 <19.0.0"
},
"browserslist": [],
"prettier": {
"overrides": [
Expand Down
25 changes: 25 additions & 0 deletions src/apps/fuse/AsyncFunctionQueue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export class AsyncFunctionQueue {
private readonly queue = new Map<string, Promise<void>>();

constructor(
private readonly asyncFunction: (...params: any[]) => Promise<void>
) {}

async enqueue(...params: any[]): Promise<void> {
const key = params[0];

if (this.queue.has(key)) {
const promise = this.queue.get(key);
await promise;
}

const added = this.asyncFunction(...params);
this.queue.set(key, added);

try {
await added;
} finally {
this.queue.delete(key);
}
}
}
64 changes: 39 additions & 25 deletions src/apps/fuse/FuseApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { WriteCallback } from './callbacks/WriteCallback';
import { ReleaseCallback } from './callbacks/ReleaseCallback';
import { FuseDependencyContainer } from './dependency-injection/FuseDependencyContainer';
import { ensureFolderExists } from './../shared/fs/ensure-folder-exists';
import { mountPromise, unmountFusedDirectory, unmountPromise } from './helpers';

// eslint-disable-next-line @typescript-eslint/no-var-requires
const fuse = require('@gcas/fuse');
Expand All @@ -30,30 +31,40 @@ export class FuseApp {

private async getOpt() {
const readdir = new ReaddirCallback(
this.fuseContainer.virtualDriveContainer
this.fuseContainer.virtualDriveContainer,
this.fuseContainer.offlineDriveContainer
);
const getattr = new GetAttributesCallback(
this.fuseContainer.virtualDriveContainer,
this.fuseContainer.offlineDriveContainer
);
const open = new OpenCallback(this.fuseContainer.virtualDriveContainer);
const read = new ReadCallback(this.fuseContainer.virtualDriveContainer);
const open = new OpenCallback(
this.fuseContainer.virtualDriveContainer,
this.fuseContainer.offlineDriveContainer
);
const read = new ReadCallback(
this.fuseContainer.virtualDriveContainer,
this.fuseContainer.offlineDriveContainer
);
const renameOrMove = new RenameOrMoveCallback(
this.fuseContainer.virtualDriveContainer
this.fuseContainer.virtualDriveContainer,
this.fuseContainer.offlineDriveContainer
);
const create = new CreateCallback(this.fuseContainer.offlineDriveContainer);
const makeDirectory = new MakeDirectoryCallback(
this.fuseContainer.virtualDriveContainer
);
const trashFile = new TrashFileCallback(
this.fuseContainer.virtualDriveContainer
this.fuseContainer.virtualDriveContainer,
this.fuseContainer.offlineDriveContainer
);
const trashFolder = new TrashFolderCallback(
this.fuseContainer.virtualDriveContainer
);
const write = new WriteCallback(this.fuseContainer.offlineDriveContainer);
const release = new ReleaseCallback(
this.fuseContainer.offlineDriveContainer
this.fuseContainer.offlineDriveContainer,
this.fuseContainer.virtualDriveContainer
);

return {
Expand Down Expand Up @@ -81,39 +92,42 @@ export class FuseApp {
maxRead: FuseApp.MAX_INT_32,
});

this._fuse.mount((err: any) => {
if (err) {
try {
await mountPromise(this._fuse);
} catch {
try {
await unmountFusedDirectory(this.paths.root);
await mountPromise(this._fuse);
} catch (err) {
Logger.error(`[FUSE] mount error: ${err}`);
}
});
}
}

async stop(): Promise<void> {
this._fuse?.unmount((err: any) => {
if (err) {
Logger.error(`[FUSE] unmount error: ${err}`);
}
});
await unmountPromise(this._fuse);
}

async clearCache(): Promise<void> {
await this.fuseContainer.virtualDriveContainer.allLocalContentsDeleter.run();
}

async update(): Promise<void> {
Logger.info('[FUSE] Updating tree');

const tree =
await this.fuseContainer.virtualDriveContainer.existingNodesTreeBuilder.run();
try {
const tree =
await this.fuseContainer.virtualDriveContainer.existingNodesTreeBuilder.run();

await this.fuseContainer.virtualDriveContainer.repositoryPopulator.run(
tree.files
);
await this.fuseContainer.virtualDriveContainer.repositoryPopulator.run(
tree.files
);

await this.fuseContainer.virtualDriveContainer.folderRepositoryInitiator.run(
tree.folders
);
await this.fuseContainer.virtualDriveContainer.folderRepositoryInitiator.run(
tree.folders
);

Logger.info('[FUSE] Tree updated successfully');
Logger.info('[FUSE] Tree updated successfully');
} catch (err) {
Logger.error('[FUSE] Updating the tree ', err);
}
}
}
11 changes: 11 additions & 0 deletions src/apps/fuse/callbacks/AccessCallback.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { NotifyFuseCallback } from './FuseCallback';

export class AccessCallback extends NotifyFuseCallback {
constructor() {
super('Access', { input: true, output: true });
}

async execute() {
return this.right();
}
}
11 changes: 11 additions & 0 deletions src/apps/fuse/callbacks/ChownCallback.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { NotifyFuseCallback } from './FuseCallback';

export class ChownCallback extends NotifyFuseCallback {
constructor() {
super('Chown', { input: true, output: true });
}

async execute(_path: string, _uid: number, _gid: number) {
return this.right();
}
}
7 changes: 6 additions & 1 deletion src/apps/fuse/callbacks/FuseCallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export abstract class FuseCallback<T> {

protected left(error: FuseError | unknown): Either<FuseError, T> {
if (error instanceof FuseError) {
Logger.error(`${this.name} ${error}`);
return left(error);
}

Expand Down Expand Up @@ -120,11 +121,15 @@ export abstract class NotifyFuseCallback extends FuseCallback<undefined> {
if (result.isLeft()) {
const error = result.getLeft();

if (this.debug.output) {
Logger.debug(`${this.name} ${error}`);
}

return callback(error.code);
}

if (this.debug.output) {
Logger.debug(`${this.name} completed successfully`);
Logger.debug(`${this.name} completed successfully ${params[0]}`);
}

callback(NotifyFuseCallback.OK);
Expand Down
4 changes: 2 additions & 2 deletions src/apps/fuse/callbacks/FuseErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export class FuseError extends Error {
}

export class FuseNoSuchFileOrDirectoryError extends FuseError {
constructor() {
super(FuseCodes.ENOENT, 'No such file or directory');
constructor(readonly path: string) {
super(FuseCodes.ENOENT, `No such file or directory <${path}>`);
}
}

Expand Down
16 changes: 12 additions & 4 deletions src/apps/fuse/callbacks/GetAttributesCallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import { OfflineDriveDependencyContainer } from '../dependency-injection/offline
import { VirtualDriveDependencyContainer } from '../dependency-injection/virtual-drive/VirtualDriveDependencyContainer';
import { FuseCallback } from './FuseCallback';
import { FuseError, FuseNoSuchFileOrDirectoryError } from './FuseErrors';
import Logger from 'electron-log';

type GetAttributesCallbackData = {
mode: number;
size: number;
mtime: Date;
ctime: Date;
atime?: Date;
uid: number;
gid: number;
};

export class GetAttributesCallback extends FuseCallback<GetAttributesCallbackData> {
Expand All @@ -26,11 +27,10 @@ export class GetAttributesCallback extends FuseCallback<GetAttributesCallbackDat
}

protected left(
error: FuseError
error: FuseNoSuchFileOrDirectoryError
): Either<FuseError, GetAttributesCallbackData> {
// When the OS wants to check if a node exists will try to get the attributes of it
// so not founding them is not an error
Logger.info(`Attributes of ${this.name}:.`);
return left(error);
}

Expand All @@ -42,6 +42,8 @@ export class GetAttributesCallback extends FuseCallback<GetAttributesCallbackDat
mtime: new Date(),
ctime: new Date(),
atime: undefined,
uid: process.getuid(),
gid: process.getgid(),
});
}

Expand All @@ -57,6 +59,8 @@ export class GetAttributesCallback extends FuseCallback<GetAttributesCallbackDat
ctime: file.createdAt,
mtime: file.updatedAt,
atime: new Date(),
uid: process.getuid(),
gid: process.getgid(),
});
}

Expand All @@ -72,6 +76,8 @@ export class GetAttributesCallback extends FuseCallback<GetAttributesCallbackDat
ctime: folder.createdAt,
mtime: folder.updatedAt,
atime: folder.createdAt,
uid: process.getuid(),
gid: process.getgid(),
});
}

Expand All @@ -85,9 +91,11 @@ export class GetAttributesCallback extends FuseCallback<GetAttributesCallbackDat
mtime: new Date(),
ctime: offlineFile.createdAt,
atime: offlineFile.createdAt,
uid: process.getuid(),
gid: process.getgid(),
});
}

return this.left(new FuseNoSuchFileOrDirectoryError());
return this.left(new FuseNoSuchFileOrDirectoryError(path));
}
}
20 changes: 14 additions & 6 deletions src/apps/fuse/callbacks/OpenCallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,31 @@ import { VirtualDriveDependencyContainer } from '../dependency-injection/virtual
import Logger from 'electron-log';
import { FuseCallback } from './FuseCallback';
import { FuseIOError, FuseNoSuchFileOrDirectoryError } from './FuseErrors';
import { OfflineDriveDependencyContainer } from '../dependency-injection/offline/OfflineDriveDependencyContainer';

export class OpenCallback extends FuseCallback<number> {
constructor(private readonly container: VirtualDriveDependencyContainer) {
constructor(
private readonly virtual: VirtualDriveDependencyContainer,
private readonly offline: OfflineDriveDependencyContainer
) {
super('Open');
}

async execute(path: string, _flags: Array<any>) {
const file = await this.container.filesSearcher.run({ path });
const virtual = await this.virtual.filesSearcher.run({ path });

if (!file) {
return this.left(new FuseNoSuchFileOrDirectoryError());
if (!virtual) {
const offline = await this.offline.offlineFileSearcher.run({ path });
if (offline) {
return this.right(0);
}
return this.left(new FuseNoSuchFileOrDirectoryError(path));
}

try {
await this.container.downloadContentsToPlainFile.run(file);
await this.virtual.downloadContentsToPlainFile.run(virtual);

return this.right(file.id);
return this.right(0);
} catch (err: unknown) {
Logger.error('Error downloading file: ', err);
if (err instanceof Error) {
Expand Down
Loading

0 comments on commit bbc10ea

Please sign in to comment.