Skip to content

Commit

Permalink
Fix build: fix automatic shell script chmod'er (#351)
Browse files Browse the repository at this point in the history
- The buffer cannot be too large or the additional zero bytes
  will be included in the comparison.
- fs.access() does not actually return a boolean, it will throw
  if the requested access is not available.
  • Loading branch information
rix0rrr authored Jul 17, 2018
1 parent 984953b commit e021793
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions tools/cdk-build-tools/lib/os.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export async function shell(command: string[], timers?: Timers): Promise<void> {
if (code === 0) {
resolve();
} else {
reject(new Error(`${renderCommandLine(command)} exited with ${code}`));
reject(new Error(`${renderCommandLine(command)} exited with error code ${code}`));
}
});
});
Expand Down Expand Up @@ -108,19 +108,31 @@ export async function makeExecutable(javascriptFile: string): Promise<void> {
*/
async function makeShellScriptExecutable(script: string) {
try {
if (await util.promisify(fs.access)(script, fs.constants.X_OK)) { return; }
if (await canExecute(script)) { return; }
if (!await isShellScript(script)) { return; }
await util.promisify(fs.chmod)(script, 0o755);
} catch (e) {
// If it happens that this file doesn't exist, that's fine. It's
// probably a file that can be found on the $PATH.
if (e.code === 'ENOENT') { return; }
throw e;
}
}

async function canExecute(fileName: string): Promise<boolean> {
try {
await util.promisify(fs.access)(fileName, fs.constants.X_OK);
return true;
} catch (e) {
if (e.code === 'EACCES') { return false; }
throw e;
}
}

async function isShellScript(script: string): Promise<boolean> {
const f = await util.promisify(fs.open)(script, 'r');
const buffer = Buffer.alloc(10);
const buffer = Buffer.alloc(2);
await util.promisify(fs.read)(f, buffer, 0, 2, null);

return buffer.toString('utf-8') === '#!';
}
return buffer.equals(Buffer.from('#!'));
}

0 comments on commit e021793

Please sign in to comment.