Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PB-2050]: Feat/maintain data integrity #499

Merged
merged 4 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/apps/main/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export interface AppStore {
preferedLanguage?: string;
preferedTheme?: string;
virtualdriveWindowsLetter: string;
dataIntegrityMaintenance?: boolean;
}

const schema: Schema<AppStore> = {
Expand Down Expand Up @@ -103,6 +104,9 @@ const schema: Schema<AppStore> = {
virtualdriveWindowsLetter: {
type: 'string',
},
dataIntegrityMaintenance: {
type: 'boolean',
},
} as const;

export const defaults: AppStore = {
Expand All @@ -127,6 +131,7 @@ export const defaults: AppStore = {
preferedLanguage: '',
preferedTheme: 'system',
virtualdriveWindowsLetter: 'I',
dataIntegrityMaintenance: false,
};

const configStore = new Store({ schema, defaults });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { FilePlaceholderConverter } from '../../../../context/virtual-drive/file
import { FileSyncStatusUpdater } from '../../../../context/virtual-drive/files/application/FileSyncStatusUpdater';
import { FileCheckerStatusInRoot } from '../../../../context/virtual-drive/files/application/FileCheckerStatusInRoot';
import { FilesPlaceholderDeleter } from '../../../../context/virtual-drive/files/application/FilesPlaceholderDeleter';
import { FileIdentityUpdater } from '../../../../context/virtual-drive/files/application/FileIndetityUpdater';

export interface FilesContainer {
fileFinderByContentsId: FileFinderByContentsId;
Expand All @@ -34,4 +35,5 @@ export interface FilesContainer {
filePlaceholderConverter: FilePlaceholderConverter;
fileSyncStatusUpdater: FileSyncStatusUpdater;
filesCheckerStatusInRoot: FileCheckerStatusInRoot;
fileIdentityUpdater: FileIdentityUpdater;
}
5 changes: 5 additions & 0 deletions src/apps/sync-engine/dependency-injection/files/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { FileSyncStatusUpdater } from '../../../../context/virtual-drive/files/a
import { FileContentsUpdater } from '../../../../context/virtual-drive/files/application/FileContentsUpdater';
import { FileCheckerStatusInRoot } from '../../../../context/virtual-drive/files/application/FileCheckerStatusInRoot';
import { FilesPlaceholderDeleter } from '../../../../context/virtual-drive/files/application/FilesPlaceholderDeleter';
import { FileIdentityUpdater } from '../../../../context/virtual-drive/files/application/FileIndetityUpdater';

export async function buildFilesContainer(
folderContainer: FoldersContainer,
Expand Down Expand Up @@ -144,10 +145,13 @@ export async function buildFilesContainer(
remoteFileSystem
);

const fileIdentityUpdater = new FileIdentityUpdater(localFileSystem);

const fileSyncronizer = new FileSyncronizer(
repository,
fileSyncStatusUpdater,
filePlaceholderConverter,
fileIdentityUpdater,
fileCreator,
sharedContainer.absolutePathToRelativeConverter,
folderContainer.folderCreator,
Expand Down Expand Up @@ -176,6 +180,7 @@ export async function buildFilesContainer(
filePlaceholderConverter,
fileSyncStatusUpdater,
filesCheckerStatusInRoot,
fileIdentityUpdater,
};

return { container, subscribers: [] };
Expand Down
10 changes: 10 additions & 0 deletions src/context/virtual-drive/files/application/FileIndetityUpdater.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { File } from '../domain/File';
import { LocalFileSystem } from '../domain/file-systems/LocalFileSystem';

export class FileIdentityUpdater {
constructor(private readonly localFileSystem: LocalFileSystem) {}

async run(file: File) {
await this.localFileSystem.updateFileIdentity(file.path, file.placeholderId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { File } from '../domain/File';
import { FileSyncStatusUpdater } from './FileSyncStatusUpdater';
import { FilePlaceholderConverter } from './FIlePlaceholderConverter';
import { FileContentsUpdater } from './FileContentsUpdater';
import { FileIdentityUpdater } from './FileIndetityUpdater';

export class FileSyncronizer {
// queue of files to be uploaded
Expand All @@ -23,6 +24,7 @@ export class FileSyncronizer {
private readonly repository: FileRepository,
private readonly fileSyncStatusUpdater: FileSyncStatusUpdater,
private readonly filePlaceholderConverter: FilePlaceholderConverter,
private readonly fileIdentityUpdater: FileIdentityUpdater,
private readonly fileCreator: FileCreator,
private readonly absolutePathToRelativeConverter: AbsolutePathToRelativeConverter,
private readonly folderCreator: FolderCreator,
Expand Down Expand Up @@ -148,6 +150,7 @@ export class FileSyncronizer {
private async convertAndUpdateSyncStatus(file: File) {
await Promise.all([
this.filePlaceholderConverter.run(file),
this.fileIdentityUpdater.run(file),
this.fileSyncStatusUpdater.run(file),
]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@ export interface LocalFileSystem {
getPlaceholderStateByRelativePath(
relativePath: string
): Promise<PlaceholderState>;

updateFileIdentity(
path: File['path'],
newIdentity: string
): Promise<void>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,13 @@ export class InMemoryFileRepository implements FileRepository {
if (!this.files.has(file.contentsId)) {
throw new Error('File not found');
}

const oldContentsId = file.contentsId;
const updatedFile = file.replaceContestsAndSize(newContentsId, newSize);
// first delete the old file to be able to add the new one
this.files.delete(oldContentsId);
this.files.set(updatedFile.contentsId, updatedFile.attributes());
this.files.delete(file.contentsId);
return updatedFile;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { LocalFileSystem } from '../domain/file-systems/LocalFileSystem';
import { RelativePathToAbsoluteConverter } from '../../shared/application/RelativePathToAbsoluteConverter';
import fs from 'fs/promises';
import { PlaceholderState } from '../domain/PlaceholderState';
import Logger from 'electron-log';

export class NodeWinLocalFileSystem implements LocalFileSystem {
constructor(
Expand All @@ -15,7 +16,7 @@ export class NodeWinLocalFileSystem implements LocalFileSystem {
async getLocalFileId(file: File): Promise<`${string}-${string}`> {
const win32AbsolutePath = this.relativePathToAbsoluteConverter.run(
file.path
);
);

const { ino, dev } = await fs.stat(win32AbsolutePath);

Expand Down Expand Up @@ -72,4 +73,10 @@ export class NodeWinLocalFileSystem implements LocalFileSystem {
relativePath
) as Promise<PlaceholderState>;
}

async updateFileIdentity(path: string, newIdentity: `${string}-${string}`): Promise<void> {
Logger.info('[updateFileIdentity]: ', path, newIdentity);
const isNotDirectory = true;
return this.virtualDrive.updateFileIdentity(path, newIdentity, isNotDirectory);
}
}
Loading