-
Notifications
You must be signed in to change notification settings - Fork 27.9k
/
userDataSyncBackupStoreService.ts
138 lines (127 loc) · 5.4 KB
/
userDataSyncBackupStoreService.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*---------------------------------------------------------------------------------------------
* 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 'vs/base/common/async';
import { VSBuffer } from 'vs/base/common/buffer';
import { toLocalISOString } from 'vs/base/common/date';
import { Disposable } from 'vs/base/common/lifecycle';
import { joinPath } from 'vs/base/common/resources';
import { URI } from 'vs/base/common/uri';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { IFileService, IFileStat } from 'vs/platform/files/common/files';
import { IUserDataProfile, IUserDataProfilesService } from 'vs/platform/userDataProfile/common/userDataProfile';
import { ALL_SYNC_RESOURCES, IResourceRefHandle, IUserDataSyncBackupStoreService, IUserDataSyncLogService, SyncResource } from 'vs/platform/userDataSync/common/userDataSync';
export class UserDataSyncBackupStoreService extends Disposable implements IUserDataSyncBackupStoreService {
_serviceBrand: any;
constructor(
@IEnvironmentService private readonly environmentService: IEnvironmentService,
@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 profile of this.userDataProfilesService.profiles) {
for (const resource of ALL_SYNC_RESOURCES) {
try {
await this.cleanUpBackup(this.getResourceBackupHome(profile, resource));
} catch (error) {
this.logService.error(error);
}
}
}
const stat = await this.fileService.resolve(this.environmentService.userDataSyncHome);
if (stat.children) {
for (const child of stat.children) {
if (child.isDirectory && !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 getAllRefs(profile: IUserDataProfile, resource: SyncResource): Promise<IResourceRefHandle[]> {
const folder = this.getResourceBackupHome(profile, resource);
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().reverse();
return all.map(stat => ({
ref: stat.name,
created: this.getCreationTime(stat)
}));
}
return [];
}
async resolveContent(profile: IUserDataProfile, resourceKey: SyncResource, ref: string): Promise<string | null> {
const folder = this.getResourceBackupHome(profile, resourceKey);
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 backup(profile: IUserDataProfile, resourceKey: SyncResource, content: string): Promise<void> {
const folder = this.getResourceBackupHome(profile, resourceKey);
const resource = joinPath(folder, `${toLocalISOString(new Date()).replace(/-|:|\.\d+Z$/g, '')}.json`);
try {
await this.fileService.writeFile(resource, VSBuffer.fromString(content));
} catch (e) {
this.logService.error(e);
}
try {
this.cleanUpBackup(folder);
} catch (e) { /* Ignore */ }
}
private getResourceBackupHome(profile: IUserDataProfile, resource: SyncResource): URI {
return joinPath(this.environmentService.userDataSyncHome, ...(profile.isDefault ? [resource] : [profile.id, resource]));
}
private async cleanUpBackup(folder: URI): Promise<void> {
try {
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);
}));
}
} catch (e) {
this.logService.error(e);
}
}
private getCreationTime(stat: IFileStat) {
return stat.ctime || 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();
}
}