Skip to content

Commit 22472b5

Browse files
committed
Check if remote server components actually needs an update
Signed-off-by: Seb Julliand <[email protected]>
1 parent 00428ed commit 22472b5

File tree

1 file changed

+58
-20
lines changed

1 file changed

+58
-20
lines changed

src/connection/serverComponent.ts

Lines changed: 58 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import { getInstance } from "../base";
22

3-
import { Config } from "../config";
43
import path from "path";
54
import { OutputChannel, extensions, window } from "vscode";
5+
import { Config } from "../config";
66

77
import { stat } from "fs/promises";
88
import { SERVER_VERSION_FILE } from "./SCVersion";
99

10+
import IBMi from "@halcyontech/vscode-ibmi-types/api/IBMi";
11+
import Crypto from 'crypto';
12+
import { readFileSync } from "fs";
13+
1014
const ExecutablePathDir = `$HOME/.vscode/`;
1115

1216
export enum UpdateStatus {
@@ -33,7 +37,7 @@ export class ServerComponent {
3337
if (show) {
3438
this.outputChannel.show();
3539
}
36-
40+
3741
if (this.outputChannel) {
3842
this.outputChannel.appendLine(jsonString);
3943
}
@@ -43,15 +47,15 @@ export class ServerComponent {
4347
return this.installed;
4448
}
4549

46-
static getInitCommand(): string|undefined {
50+
static getInitCommand(): string | undefined {
4751
const path = this.getComponentPath();
4852

4953
if (path) {
5054
return `/QOpenSys/QIBM/ProdData/JavaVM/jdk80/64bit/bin/java -Dos400.stdio.convert=N -jar ${path} --single`
5155
}
5256
}
5357

54-
static getComponentPath(): string|undefined {
58+
static getComponentPath(): string | undefined {
5559
if (Config.ready) {
5660
const installedVersion = Config.getServerComponentName();
5761

@@ -107,13 +111,13 @@ export class ServerComponent {
107111
const assetPath = path.join(extensionPath, `dist`, SERVER_VERSION_FILE);
108112
const assetExistsLocally = await exists(assetPath);
109113

110-
ServerComponent.writeOutput(JSON.stringify({assetPath, assetExists: assetExistsLocally}));
114+
ServerComponent.writeOutput(JSON.stringify({ assetPath, assetExists: assetExistsLocally }));
111115

112116
if (assetExistsLocally) {
113117
const basename = SERVER_VERSION_FILE;
114118
const lastInstalledName = Config.getServerComponentName();
115119

116-
ServerComponent.writeOutput(JSON.stringify({basename, lastInstalledName}));
120+
ServerComponent.writeOutput(JSON.stringify({ basename, lastInstalledName }));
117121

118122
await this.initialise();
119123

@@ -132,26 +136,44 @@ export class ServerComponent {
132136
const stuffInStderr = commandResult.stderr.length > 0;
133137
const remotePath = path.posix.join(commandResult.stdout, basename);
134138

135-
ServerComponent.writeOutput(JSON.stringify({remotePath, ExecutablePathDir}));
139+
ServerComponent.writeOutput(JSON.stringify({ remotePath, ExecutablePathDir }));
140+
141+
const remoteExists = await connection.content.testStreamFile(remotePath, "f");
142+
const md5IsEqual = remoteExists && await compareMD5Hash(connection, assetPath, remotePath);
143+
if (!remoteExists || !md5IsEqual) {
144+
if (remoteExists) {
145+
const allowWrite = await connection.sendCommand({
146+
command: `chmod 600 ${remotePath}`
147+
});
148+
if (allowWrite.code !== 0) {
149+
this.writeOutput(JSON.stringify(allowWrite));
150+
window.showErrorMessage(`Remote file ${remotePath} cannot be written; try to delete it and reconnect.`, 'Show')
151+
.then(show => {
152+
if (show) {
153+
this.outputChannel.show();
154+
}
155+
});
156+
return UpdateStatus.FAILED;
157+
}
158+
}
159+
await connection.uploadFiles([{ local: assetPath, remote: remotePath }]);
136160

137-
await connection.uploadFiles([{local: assetPath, remote: remotePath}]);
161+
const scAuth = await connection.sendCommand({
162+
command: `chmod 400 ${remotePath}`
163+
});
138164

139-
const scAuth = await connection.sendCommand({
140-
command: `chmod 400 ${remotePath}`
141-
});
165+
this.writeOutput(JSON.stringify(scAuth));
142166

143-
this.writeOutput(JSON.stringify(scAuth));
167+
await Config.setServerComponentName(basename);
144168

145-
await Config.setServerComponentName(basename);
169+
if (stuffInStderr) {
170+
ServerComponent.writeOutput(`Server component was uploaded to ${remotePath} but there was something in stderr, which is not right. It might be worth seeing your user profile startup scripts.`);
171+
}
146172

147-
if (stuffInStderr) {
148-
ServerComponent.writeOutput(`Server component was uploaded to ${remotePath} but there was something in stderr, which is not right. It might be worth seeing your user profile startup scripts.`);
173+
window.showInformationMessage(`Db2 for IBM i extension server component has been updated!`);
174+
this.installed = true;
175+
updateResult = UpdateStatus.JUST_UPDATED;
149176
}
150-
151-
window.showInformationMessage(`Db2 for IBM i extension server component has been updated!`);
152-
this.installed = true;
153-
updateResult = UpdateStatus.JUST_UPDATED;
154-
155177
} else {
156178
updateResult = UpdateStatus.FAILED;
157179

@@ -196,4 +218,20 @@ async function exists(path: string) {
196218
} catch (e) {
197219
return false;
198220
}
221+
}
222+
223+
async function compareMD5Hash(connection: IBMi, local: string, remote: string) {
224+
const localMD5 = Crypto.createHash("md5")
225+
.update(readFileSync(local))
226+
.digest("hex")
227+
.toLowerCase();
228+
229+
const remoteMD5Result = (await connection.sendCommand({ command: `${connection.remoteFeatures.md5sum} ${remote}` }));
230+
if (remoteMD5Result.code === 0) {
231+
return localMD5 === remoteMD5Result.stdout.split(/\s+/).at(0);
232+
}
233+
else {
234+
ServerComponent.writeOutput(JSON.stringify(remoteMD5Result));
235+
return false;
236+
}
199237
}

0 commit comments

Comments
 (0)