Skip to content

Commit

Permalink
Set execute bit for downloaded executables
Browse files Browse the repository at this point in the history
  • Loading branch information
SKyletoft committed Mar 20, 2022
1 parent cc49220 commit 74e6b66
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion vsc_gui/src/download_native.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { stringify } from 'querystring';
import * as vscode from 'vscode';
const fs = require('fs');
const https = require('https');
Expand Down Expand Up @@ -52,9 +53,38 @@ export function download() {
vscode.window.showInformationMessage("Download complete");
const zip = new unzip(TMP_FILE);
zip.extractAllTo(NATIVE_FOLDER, true);
fs.createWriteStream(DONE).close(); // Create file to mark that we don't need to redownload
console.log(`File extracted!`);
vscode.window.showInformationMessage("File extracted");

mark_executables(NATIVE_FOLDER);

fs.createWriteStream(DONE).close(); // Create file to mark that we don't need to redownload
vscode.window.showInformationMessage("Setup complete");
})
.pipe(fs.createWriteStream(TMP_FILE));
}

/// Returns true if the name ends with ".exe" or doesn't contain a "." at all
function is_executable(file: string) {
if (file.endsWith(".exe") || file.endsWith(".elf")) { return true; }
const name_starts_at = Math.max(file.lastIndexOf("/"), file.lastIndexOf("\\"));
const name = file.substring(name_starts_at);
return name.indexOf(".") === -1;
}

/// Recursively finds executables and sets the executable bit (relevant for Unix systems)
function mark_executables(path: string) {
const files = fs.readdirSync(path);

for (const file of files) {
const full_file = `${path}/${file}`;
if (fs.lstatSync(full_file).isDirectory()) {
mark_executables(full_file);
} else if (is_executable(full_file)) {
console.log(`modding ${file}`);
fs.chmodSync(full_file, 0o777);
} else {
console.log(`leaving ${file} alone`);
}
}
}

0 comments on commit 74e6b66

Please sign in to comment.