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.

1 > /*--------------------------------------------------------------------------------------------- abstractSynchronizer.ts ×49
2 > * Copyright (c) Microsoft Corporation. All rights reserved.
3 > * Licensed under the MIT License. See License.txt in the project root for license information.
4 > *--------------------------------------------------------------------------------------------*/
5 >
6 > import { Promises } from '../../../base/common/async.js';
7 > import { VSBuffer } from '../../../base/common/buffer.js';
8 > import { toLocalISOString } from '../../../base/common/date.js';
9 > import { Disposable } from '../../../base/common/lifecycle.js';
10 > import { joinPath } from '../../../base/common/resources.js';
11 > import { URI } from '../../../base/common/uri.js';
12 > import { IConfigurationService } from '../../configuration/common/configuration.js';
13 > import { IEnvironmentService } from '../../environment/common/environment.js';
14 > import { FileOperationResult, IFileService, IFileStat, toFileOperationResult } from '../../files/common/files.js';
15 > import { IUserDataProfilesService } from '../../userDataProfile/common/userDataProfile.js';
16 > import { ALL_SYNC_RESOURCES, IResourceRefHandle, IUserDataSyncLocalStoreService, IUserDataSyncLogService, SyncResource } from './userDataSync.js';
17 >
18 > export class UserDataSyncLocalStoreService extends Disposable implements IUserDataSyncLocalStoreService {
19 >
20 > _serviceBrand: undefined;
21 >
22 > constructor(
23 > @IEnvironmentService private readonly environmentService: IEnvironmentService, userDataSyncStoreService.ts ×36
24 > @IFileService private readonly fileService: IFileService,
25 > @IConfigurationService private readonly configurationService: IConfigurationService,
26 > @IUserDataSyncLogService private readonly logService: IUserDataSyncLogService,
27 > @IUserDataProfilesService private readonly userDataProfilesService: IUserDataProfilesService,
28 > ) {
29 > super();
30 > this.cleanUp();
31 > }
33 > private async cleanUp(): Promise<void> {
34 > for (const profile of this.userDataProfilesService.profiles) { userDataSyncStoreService.ts ×36
35 > for (const resource of ALL_SYNC_RESOURCES) {
36 > try {
37 > await this.cleanUpBackup(this.getResourceBackupHome(resource, profile.isDefault ? undefined : profile.id));
38 > } catch (error) {
39 this.logService.error(error);
40 }
42 > }
43 >
44 > let stat: IFileStat;
45 > try {
46 > stat = await this.fileService.resolve(this.environmentService.userDataSyncHome);
47 > } catch (error) {
48 > if (toFileOperationResult(error) !== FileOperationResult.FILE_NOT_FOUND) {
49 this.logService.error(error);
50 }
52 > }
53
54 if (stat.children) {
55 for (const child of stat.children) {
56 if (child.isDirectory && !ALL_SYNC_RESOURCES.includes(<SyncResource>child.name) && !this.userDataProfilesService.profiles.some(profile => profile.id === child.name)) {
57 try {
58 this.logService.info('Deleting non existing profile from backup', child.resource.path);
59 await this.fileService.del(child.resource, { recursive: true });
60 } catch (error) {
61 this.logService.error(error);
62 }
63 }
64 }
65 }
68 > async getAllResourceRefs(resource: SyncResource, collection?: string, root?: URI): Promise<IResourceRefHandle[]> {
69 const folder = this.getResourceBackupHome(resource, collection, root);
70 try {
71 const stat = await this.fileService.resolve(folder);
72 if (stat.children) {
73 const all = stat.children.filter(stat => stat.isFile && !stat.name.startsWith('lastSync')).sort().reverse();
74 return all.map(stat => ({
75 ref: stat.name,
76 created: this.getCreationTime(stat)
77 }));
78 }
79 } catch (error) {
80 if (toFileOperationResult(error) !== FileOperationResult.FILE_NOT_FOUND) {
81 throw error;
82 }
83 }
84 return [];
85 }
87 > async resolveResourceContent(resourceKey: SyncResource, ref: string, collection?: string, root?: URI): Promise<string | null> {
88 const folder = this.getResourceBackupHome(resourceKey, collection, root);
89 const file = joinPath(folder, ref);
90 try {
91 const content = await this.fileService.readFile(file);
92 return content.value.toString();
93 } catch (error) {
94 this.logService.error(error);
95 return null;
96 }
97 }
99 > async writeResource(resourceKey: SyncResource, content: string, cTime: Date, collection?: string, root?: URI): Promise<void> {
100 > const folder = this.getResourceBackupHome(resourceKey, collection, root); userDataSyncLocalStoreService.ts ×2
101 > const resource = joinPath(folder, `${toLocalISOString(cTime).replace(/-|:|\.\d+Z$/g, '')}.json`);
102 > try {
103 > await this.fileService.writeFile(resource, VSBuffer.fromString(content));
104 > } catch (e) {
105 this.logService.error(e);
106 }
109 > private getResourceBackupHome(resource: SyncResource, collection?: string, root: URI = this.environmentService.userDataSyncHome): URI {
110 > return joinPath(root, ...(collection ? [collection, resource] : [resource])); userDataSyncStoreService.ts ×36
111 > }
113 > private async cleanUpBackup(folder: URI): Promise<void> {
115 > try {
116 > if (!(await this.fileService.exists(folder))) {
117 > return;
118 > }
119 > } catch (e) {
120 return;
121 }
122 const stat = await this.fileService.resolve(folder);
123 if (stat.children) {
124 const all = stat.children.filter(stat => stat.isFile && /^\d{8}T\d{6}(\.json)?$/.test(stat.name)).sort();
125 const backUpMaxAge = 1000 * 60 * 60 * 24 * (this.configurationService.getValue<number>('sync.localBackupDuration') || 30 /* Default 30 days */);
126 let toDelete = all.filter(stat => Date.now() - this.getCreationTime(stat) > backUpMaxAge);
127 const remaining = all.length - toDelete.length;
128 if (remaining < 10) {
129 toDelete = toDelete.slice(10 - remaining);
130 }
131 await Promises.settled(toDelete.map(async stat => {
132 this.logService.info('Deleting from backup', stat.resource.path);
133 await this.fileService.del(stat.resource);
134 }));
135 }
136 > } catch (e) { userDataSyncStoreService.ts ×36
137 this.logService.error(e);
138 }
141 > private getCreationTime(stat: IFileStat) {
142 return new Date(
143 parseInt(stat.name.substring(0, 4)),
144 parseInt(stat.name.substring(4, 6)) - 1,
145 parseInt(stat.name.substring(6, 8)),
146 parseInt(stat.name.substring(9, 11)),
147 parseInt(stat.name.substring(11, 13)),
148 parseInt(stat.name.substring(13, 15))
149 ).getTime();
150 }