src/vs/platform/userDataSync/common/userDataSyncLocalStoreService.ts
151 LOC · 78 covered · 73 uncovered · 20 ranges · 585 concepts · 3 introducers · 348 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
/*---------------------------------------------------------------------------------------------
abstractSynchronizer.ts ×49
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Promises } from '../../../base/common/async.js';
import { VSBuffer } from '../../../base/common/buffer.js';
import { toLocalISOString } from '../../../base/common/date.js';
import { Disposable } from '../../../base/common/lifecycle.js';
import { joinPath } from '../../../base/common/resources.js';
import { URI } from '../../../base/common/uri.js';
import { IConfigurationService } from '../../configuration/common/configuration.js';
import { IEnvironmentService } from '../../environment/common/environment.js';
import { FileOperationResult, IFileService, IFileStat, toFileOperationResult } from '../../files/common/files.js';
import { IUserDataProfilesService } from '../../userDataProfile/common/userDataProfile.js';
import { ALL_SYNC_RESOURCES, IResourceRefHandle, IUserDataSyncLocalStoreService, IUserDataSyncLogService, SyncResource } from './userDataSync.js';
export class UserDataSyncLocalStoreService extends Disposable implements IUserDataSyncLocalStoreService {
_serviceBrand: undefined;
constructor(
@IEnvironmentService private readonly environmentService: IEnvironmentService,
userDataSyncStoreService.ts ×36
@IFileService private readonly fileService: IFileService,
@IConfigurationService private readonly configurationService: IConfigurationService,
@IUserDataSyncLogService private readonly logService: IUserDataSyncLogService,
@IUserDataProfilesService private readonly userDataProfilesService: IUserDataProfilesService,
) {
super();
this.cleanUp();
}
private async cleanUp(): Promise<void> {
for (const resource of ALL_SYNC_RESOURCES) {
try {
await this.cleanUpBackup(this.getResourceBackupHome(resource, profile.isDefault ? undefined : profile.id));
} catch (error) {
this.logService.error(error);
}
}
let stat: IFileStat;
try {
stat = await this.fileService.resolve(this.environmentService.userDataSyncHome);
} catch (error) {
if (toFileOperationResult(error) !== FileOperationResult.FILE_NOT_FOUND) {
this.logService.error(error);
}
}
if (stat.children) {
for (const child of stat.children) {
if (child.isDirectory && !ALL_SYNC_RESOURCES.includes(<SyncResource>child.name) && !this.userDataProfilesService.profiles.some(profile => profile.id === child.name)) {
try {
this.logService.info('Deleting non existing profile from backup', child.resource.path);
await this.fileService.del(child.resource, { recursive: true });
} catch (error) {
this.logService.error(error);
}
}
}
}
async getAllResourceRefs(resource: SyncResource, collection?: string, root?: URI): Promise<IResourceRefHandle[]> {
const folder = this.getResourceBackupHome(resource, collection, root);
try {
const stat = await this.fileService.resolve(folder);
if (stat.children) {
const all = stat.children.filter(stat => stat.isFile && !stat.name.startsWith('lastSync')).sort().reverse();
return all.map(stat => ({
ref: stat.name,
created: this.getCreationTime(stat)
}));
}
} catch (error) {
if (toFileOperationResult(error) !== FileOperationResult.FILE_NOT_FOUND) {
throw error;
}
}
return [];
}
async resolveResourceContent(resourceKey: SyncResource, ref: string, collection?: string, root?: URI): Promise<string | null> {
const folder = this.getResourceBackupHome(resourceKey, collection, root);
const file = joinPath(folder, ref);
try {
const content = await this.fileService.readFile(file);
return content.value.toString();
} catch (error) {
this.logService.error(error);
return null;
}
}
async writeResource(resourceKey: SyncResource, content: string, cTime: Date, collection?: string, root?: URI): Promise<void> {
const folder = this.getResourceBackupHome(resourceKey, collection, root);
userDataSyncLocalStoreService.ts ×2
const resource = joinPath(folder, `${toLocalISOString(cTime).replace(/-|:|\.\d+Z$/g, '')}.json`);
try {
await this.fileService.writeFile(resource, VSBuffer.fromString(content));
} catch (e) {
this.logService.error(e);
}
private getResourceBackupHome(resource: SyncResource, collection?: string, root: URI = this.environmentService.userDataSyncHome): URI {
return joinPath(root, ...(collection ? [collection, resource] : [resource]));
userDataSyncStoreService.ts ×36
}
private async cleanUpBackup(folder: URI): Promise<void> {
try {
if (!(await this.fileService.exists(folder))) {
return;
}
} catch (e) {
return;
}
const stat = await this.fileService.resolve(folder);
if (stat.children) {
const all = stat.children.filter(stat => stat.isFile && /^\d{8}T\d{6}(\.json)?$/.test(stat.name)).sort();
const backUpMaxAge = 1000 * 60 * 60 * 24 * (this.configurationService.getValue<number>('sync.localBackupDuration') || 30 /* Default 30 days */);
let toDelete = all.filter(stat => Date.now() - this.getCreationTime(stat) > backUpMaxAge);
const remaining = all.length - toDelete.length;
if (remaining < 10) {
toDelete = toDelete.slice(10 - remaining);
}
await Promises.settled(toDelete.map(async stat => {
this.logService.info('Deleting from backup', stat.resource.path);
await this.fileService.del(stat.resource);
}));
}
this.logService.error(e);
}
private getCreationTime(stat: IFileStat) {
return new Date(
parseInt(stat.name.substring(0, 4)),
parseInt(stat.name.substring(4, 6)) - 1,
parseInt(stat.name.substring(6, 8)),
parseInt(stat.name.substring(9, 11)),
parseInt(stat.name.substring(11, 13)),
parseInt(stat.name.substring(13, 15))
).getTime();
}