Skip to content

Commit d2a3147

Browse files
authored
Merge pull request #1486 from cdr/update-backup
Back up old directory when updating
2 parents e480f65 + 9c65812 commit d2a3147

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

ci/vscode.patch

+3-7
Original file line numberDiff line numberDiff line change
@@ -512,10 +512,10 @@ index eab8591492..26668701f7 100644
512512
options.logService.error(`${logPrefix} socketFactory.connect() failed. Error:`);
513513
diff --git a/src/vs/server/browser/client.ts b/src/vs/server/browser/client.ts
514514
new file mode 100644
515-
index 0000000000..96fbd4b0bb
515+
index 0000000000..4f8543d975
516516
--- /dev/null
517517
+++ b/src/vs/server/browser/client.ts
518-
@@ -0,0 +1,270 @@
518+
@@ -0,0 +1,266 @@
519519
+import { Emitter } from 'vs/base/common/event';
520520
+import { URI } from 'vs/base/common/uri';
521521
+import { localize } from 'vs/nls';
@@ -692,14 +692,10 @@ index 0000000000..96fbd4b0bb
692692
+ headers: { "content-type": "application/json" },
693693
+ });
694694
+ if (response.status !== 200) {
695-
+ throw new Error("Unexpected response");
695+
+ throw new Error(response.statusText);
696696
+ }
697697
+
698698
+ const json = await response.json();
699-
+ if (!json.isLatest) {
700-
+ throw new Error("Update failed");
701-
+ }
702-
+
703699
+ (services.get(INotificationService) as INotificationService).info(`Updated to ${json.version}`);
704700
+ };
705701
+

src/node/app/update.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,13 @@ export class UpdateHttpProvider extends HttpProvider {
221221
targetPath = path.resolve(__dirname, "../../../")
222222
}
223223

224-
logger.debug("Replacing files", field("target", targetPath))
225-
await fs.move(directoryPath, targetPath, { overwrite: true })
224+
// Move the old directory to prevent potential data loss.
225+
const backupPath = path.resolve(targetPath, `../${path.basename(targetPath)}.${Date.now().toString()}`)
226+
logger.debug("Replacing files", field("target", targetPath), field("backup", backupPath))
227+
await fs.move(targetPath, backupPath)
228+
229+
// Move the new directory.
230+
await fs.move(directoryPath, targetPath)
226231

227232
await fs.remove(downloadPath)
228233

test/update.test.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,18 @@ describe("update", () => {
214214
await p.downloadAndApplyUpdate(update, destination)
215215
assert.equal(`console.log("UPDATED")`, await fs.readFile(entry, "utf8"))
216216

217-
// Should still work if there is no existing version somehow.
218-
await fs.remove(destination)
219-
await p.downloadAndApplyUpdate(update, destination)
220-
assert.equal(`console.log("UPDATED")`, await fs.readFile(entry, "utf8"))
217+
// There should be a backup.
218+
const dir = (await fs.readdir(path.join(tmpdir, "tests/updates"))).filter((dir) => {
219+
return dir.startsWith("code-server.")
220+
})
221+
assert.equal(dir.length, 1)
222+
assert.equal(
223+
`console.log("OLD")`,
224+
await fs.readFile(path.join(tmpdir, "tests/updates", dir[0], "code-server"), "utf8"),
225+
)
221226

222227
const archiveName = await p.getReleaseName(update)
223-
assert.deepEqual(spy, ["/latest", `/download/${version}/${archiveName}`, `/download/${version}/${archiveName}`])
228+
assert.deepEqual(spy, ["/latest", `/download/${version}/${archiveName}`])
224229
})
225230

226231
it("should not reject if unable to fetch", async () => {

0 commit comments

Comments
 (0)