Skip to content

Commit af10cc7

Browse files
committed
Only the active version is kept on disk + fixes for switching between active versions
1 parent 2f11db6 commit af10cc7

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed

src/vm/byond.service.ts

+28-21
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ export enum VersionStatus {
99
Fetching,
1010
Fetched,
1111
Loading,
12-
Extracting,
1312
Loaded,
1413
}
1514

@@ -31,7 +30,7 @@ export class ByondService {
3130
this._versions.delete(version.toString());
3231
await this.commandQueueService.runToCompletion(
3332
'/bin/rm',
34-
`-rf\0/var/lib/byond/${version}.zip\0/var/lib/byond/${version}`,
33+
`-rf\0/var/lib/byond/${version}.zip\0/mnt/host/byond/${version}`,
3534
);
3635
if (this._activeVersion === version) this._activeVersion = null;
3736
});
@@ -41,38 +40,51 @@ export class ByondService {
4140
public get versions(): ReadonlyMap<string, VersionStatus> {
4241
return this._versions;
4342
}
44-
public setActive = this.lock.wrap(async (version: string) => {
43+
44+
public load = this.lock.wrap(async (version: string, setActive?: boolean) => {
45+
setActive ??= true;
46+
4547
const status = this._versions.get(version);
4648
if (status == null || status < VersionStatus.Fetched) return;
4749

50+
let destination = `/mnt/host/byond/${version}`;
4851
if (status < VersionStatus.Loaded) {
52+
if (setActive) {
53+
destination = '/var/lib/byond_staging';
54+
}
4955
try {
5056
this._versions.set(version, VersionStatus.Loading);
5157
const zipFile = await this.getVersion(version, true);
5258
await this.emulatorService.sendFile(
5359
`byond/${version}.zip`,
5460
new Uint8Array(await zipFile.arrayBuffer()),
5561
);
56-
this._versions.set(version, VersionStatus.Extracting);
57-
await this.commandQueueService.runToSuccess(
58-
'/bin/unzip',
59-
`/mnt/host/byond/${version}.zip\0byond/bin*\0-j\0-d\0/var/lib/byond/${version}`,
60-
);
61-
await this.commandQueueService.runToSuccess(
62-
'/bin/rm',
63-
`/mnt/host/byond/${version}.zip`,
64-
);
62+
await this.commandQueueService.runToSuccess([
63+
`/bin/unzip /mnt/host/byond/${version}.zip 'byond/bin*' -j -d ${destination}`,
64+
`/bin/rm /mnt/host/byond/${version}.zip`,
65+
]);
6566
this._versions.set(version, VersionStatus.Loaded);
66-
this._activeVersion = version;
6767
} catch (e) {
6868
this._versions.set(version, VersionStatus.Fetched);
6969
await this.commandQueueService.runToCompletion(
7070
'/bin/rm',
71-
`-rf\0/var/lib/byond/${version}.zip\0/var/lib/byond/${version}`,
71+
`-rf\0${destination}`,
7272
);
7373
throw e;
7474
}
7575
}
76+
if (setActive) {
77+
if (this._activeVersion)
78+
await this.commandQueueService.runToSuccess(
79+
'/bin/mv',
80+
`/var/lib/byond\0/mnt/host/byond/${this._activeVersion}`,
81+
);
82+
await this.commandQueueService.runToSuccess(
83+
'/bin/mv',
84+
`${destination}\0/var/lib/byond`,
85+
);
86+
}
87+
this._activeVersion = version;
7688
});
7789
public getVersion = this.lock.wrap(async (version: string) => {
7890
try {
@@ -122,10 +134,7 @@ export class ByondService {
122134
}
123135
});
124136
void this.lock.run(() =>
125-
commandQueueService.runToSuccess(
126-
'/bin/mkdir',
127-
'-p\0/mnt/host/byond\0/var/lib/byond',
128-
),
137+
commandQueueService.runToSuccess('/bin/mkdir', '-p\0/mnt/host/byond'),
129138
);
130139
}
131140

@@ -135,8 +144,6 @@ export class ByondService {
135144
}
136145

137146
public useActive<T extends (path: string | null) => any>(fn: T) {
138-
this.lock.run(() =>
139-
fn(this._activeVersion ? `/var/lib/byond/${this._activeVersion}/` : null),
140-
);
147+
this.lock.run(() => fn(this._activeVersion ? `/var/lib/byond/` : null));
141148
}
142149
}

0 commit comments

Comments
 (0)