|
1 | 1 | import os from 'os'; |
2 | 2 | import path from 'path'; |
3 | 3 | import { Uri, workspace } from 'vscode'; |
| 4 | +import { xdgData } from 'xdg-basedir'; |
4 | 5 | import { Logger } from '../../../system/logger'; |
5 | 6 | import { wait } from '../../../system/promise'; |
6 | 7 | import { getPlatform } from '../platform'; |
7 | 8 |
|
8 | | -export const sharedGKDataFolder = '.gk'; |
| 9 | +/** @deprecated prefer using XDG paths */ |
| 10 | +const legacySharedGKDataFolder = path.join(os.homedir(), '.gk'); |
9 | 11 |
|
10 | | -export async function acquireSharedFolderWriteLock(): Promise<boolean> { |
11 | | - const lockFileUri = getSharedLockFileUri(); |
| 12 | +class SharedGKDataFolderMapper { |
| 13 | + private _initPromise: Promise<void> | undefined; |
| 14 | + constructor( |
| 15 | + // do soft migration, use new folders only for new users (without existing folders) |
| 16 | + // eslint-disable-next-line @typescript-eslint/no-deprecated |
| 17 | + private sharedGKDataFolder = legacySharedGKDataFolder, |
| 18 | + private _isInitialized: boolean = false, |
| 19 | + ) {} |
12 | 20 |
|
13 | | - let stat; |
14 | | - while (true) { |
| 21 | + private async _initialize() { |
| 22 | + if (this._initPromise) { |
| 23 | + throw new Error('cannot be initialized multiple times'); |
| 24 | + } |
15 | 25 | try { |
16 | | - stat = await workspace.fs.stat(lockFileUri); |
| 26 | + await workspace.fs.stat(Uri.file(this.sharedGKDataFolder)); |
17 | 27 | } catch { |
18 | | - // File does not exist, so we can safely create it |
19 | | - break; |
| 28 | + // Path does not exist, so we can safely use xdg paths it |
| 29 | + if (xdgData) { |
| 30 | + this.sharedGKDataFolder = path.join(xdgData, 'gk'); |
| 31 | + } else { |
| 32 | + this.sharedGKDataFolder = path.join(os.homedir()); |
| 33 | + } |
| 34 | + } finally { |
| 35 | + this._isInitialized = true; |
20 | 36 | } |
| 37 | + } |
21 | 38 |
|
22 | | - const currentTime = new Date().getTime(); |
23 | | - if (currentTime - stat.ctime > 30000) { |
24 | | - // File exists, but the timestamp is older than 30 seconds, so we can safely remove it |
25 | | - break; |
| 39 | + private async waitForInitialized() { |
| 40 | + if (this._isInitialized) { |
| 41 | + return; |
26 | 42 | } |
27 | | - |
28 | | - // File exists, and the timestamp is less than 30 seconds old, so we need to wait for it to be removed |
29 | | - await wait(100); |
| 43 | + if (!this._initPromise) { |
| 44 | + this._initPromise = this._initialize(); |
| 45 | + } |
| 46 | + await this._initPromise; |
30 | 47 | } |
31 | 48 |
|
32 | | - try { |
33 | | - // write the lockfile to the shared data folder |
34 | | - await workspace.fs.writeFile(lockFileUri, new Uint8Array(0)); |
35 | | - } catch (error) { |
36 | | - Logger.error(error, 'acquireSharedFolderWriteLock'); |
37 | | - return false; |
| 49 | + private async getUri(relativeFilePath: string) { |
| 50 | + await this.waitForInitialized(); |
| 51 | + return Uri.file(path.join(this.sharedGKDataFolder, relativeFilePath)); |
38 | 52 | } |
39 | 53 |
|
40 | | - return true; |
41 | | -} |
| 54 | + async acquireSharedFolderWriteLock(): Promise<boolean> { |
| 55 | + const lockFileUri = await this.getUri('lockfile'); |
| 56 | + |
| 57 | + let stat; |
| 58 | + while (true) { |
| 59 | + try { |
| 60 | + stat = await workspace.fs.stat(lockFileUri); |
| 61 | + } catch { |
| 62 | + // File does not exist, so we can safely create it |
| 63 | + break; |
| 64 | + } |
| 65 | + |
| 66 | + const currentTime = new Date().getTime(); |
| 67 | + if (currentTime - stat.ctime > 30000) { |
| 68 | + // File exists, but the timestamp is older than 30 seconds, so we can safely remove it |
| 69 | + break; |
| 70 | + } |
| 71 | + |
| 72 | + // File exists, and the timestamp is less than 30 seconds old, so we need to wait for it to be removed |
| 73 | + await wait(100); |
| 74 | + } |
| 75 | + |
| 76 | + try { |
| 77 | + // write the lockfile to the shared data folder |
| 78 | + await workspace.fs.writeFile(lockFileUri, new Uint8Array(0)); |
| 79 | + } catch (error) { |
| 80 | + Logger.error(error, 'acquireSharedFolderWriteLock'); |
| 81 | + return false; |
| 82 | + } |
42 | 83 |
|
43 | | -export async function releaseSharedFolderWriteLock(): Promise<boolean> { |
44 | | - try { |
45 | | - const lockFileUri = getSharedLockFileUri(); |
46 | | - await workspace.fs.delete(lockFileUri); |
47 | | - } catch (error) { |
48 | | - Logger.error(error, 'releaseSharedFolderWriteLock'); |
49 | | - return false; |
| 84 | + return true; |
50 | 85 | } |
51 | 86 |
|
52 | | - return true; |
53 | | -} |
| 87 | + async releaseSharedFolderWriteLock(): Promise<boolean> { |
| 88 | + try { |
| 89 | + const lockFileUri = await this.getUri('lockfile'); |
| 90 | + await workspace.fs.delete(lockFileUri); |
| 91 | + } catch (error) { |
| 92 | + Logger.error(error, 'releaseSharedFolderWriteLock'); |
| 93 | + return false; |
| 94 | + } |
54 | 95 |
|
55 | | -function getSharedLockFileUri() { |
56 | | - return Uri.file(path.join(os.homedir(), sharedGKDataFolder, 'lockfile')); |
57 | | -} |
| 96 | + return true; |
| 97 | + } |
58 | 98 |
|
59 | | -export function getSharedRepositoryMappingFileUri() { |
60 | | - return Uri.file(path.join(os.homedir(), sharedGKDataFolder, 'repoMapping.json')); |
61 | | -} |
| 99 | + async getSharedRepositoryMappingFileUri() { |
| 100 | + return this.getUri('repoMapping.json'); |
| 101 | + } |
62 | 102 |
|
63 | | -export function getSharedCloudWorkspaceMappingFileUri() { |
64 | | - return Uri.file(path.join(os.homedir(), sharedGKDataFolder, 'cloudWorkspaces.json')); |
65 | | -} |
| 103 | + async getSharedCloudWorkspaceMappingFileUri() { |
| 104 | + return this.getUri('cloudWorkspaces.json'); |
| 105 | + } |
66 | 106 |
|
67 | | -export function getSharedLocalWorkspaceMappingFileUri() { |
68 | | - return Uri.file(path.join(os.homedir(), sharedGKDataFolder, 'localWorkspaces.json')); |
| 107 | + async getSharedLocalWorkspaceMappingFileUri() { |
| 108 | + return this.getUri('localWorkspaces.json'); |
| 109 | + } |
69 | 110 | } |
70 | 111 |
|
| 112 | +// export as a singleton |
| 113 | +// eslint-disable-next-line import-x/no-default-export |
| 114 | +export default new SharedGKDataFolderMapper(); |
| 115 | + |
71 | 116 | export function getSharedLegacyLocalWorkspaceMappingFileUri() { |
72 | 117 | return Uri.file( |
73 | 118 | path.join( |
|
0 commit comments