Skip to content

Commit

Permalink
Fix: chmod +w files after copying them (#1086)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmercm authored Apr 12, 2024
1 parent 81cb28c commit 30a5f26
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/polyfill/fsPoly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ export default class FsPoly {
static async copyFile(src: string, dest: string): Promise<void> {
const previouslyExisted = await this.exists(src);
await util.promisify(fs.copyFile)(src, dest);

// Ensure the destination file is writable
const stat = await this.stat(dest);
const chmodOwnerWrite = 0o200;
if (!(stat.mode & chmodOwnerWrite)) {
await fs.promises.chmod(dest, stat.mode | chmodOwnerWrite);
}

if (previouslyExisted) {
// Windows doesn't update mtime on overwrite?
await this.touch(dest);
Expand Down
9 changes: 7 additions & 2 deletions test/igir.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import fs from 'node:fs';
import path from 'node:path';
import util from 'node:util';

import Logger from '../src/console/logger.js';
import LogLevel from '../src/console/logLevel.js';
Expand Down Expand Up @@ -83,7 +85,7 @@ async function runIgir(optionsProps: OptionsProps): Promise<TestOutput> {
.reduce(ArrayPoly.reduceUnique(), []);
const outputFilesBefore = await fsPoly.walk(options.getOutputDirRoot());

await new Igir(options, new Logger(LogLevel.NEVER)).main();
await new Igir(options, new Logger(LogLevel.ALWAYS)).main();

const outputFilesAndCrcs = (await Promise.all(options.getInputPaths()
.map(async (inputPath) => walkWithCrc(inputPath, options.getOutputDirRoot()))))
Expand Down Expand Up @@ -229,7 +231,7 @@ describe('with explicit DATs', () => {
});
});

it('should copy and clean', async () => {
it('should copy and clean read-only files', async () => {
await copyFixturesToTemp(async (inputTemp, outputTemp) => {
// Given some existing files in the output directory
const junkFiles = [
Expand All @@ -243,6 +245,9 @@ describe('with explicit DATs', () => {
expect(await fsPoly.exists(junkFile)).toEqual(true);
}));

const inputFiles = await fsPoly.walk(inputTemp);
await Promise.all(inputFiles.map(async (inputFile) => util.promisify(fs.chmod)(inputFile, '0444')));

// When running igir with the clean command
const result = await runIgir({
commands: ['copy', 'clean'],
Expand Down

0 comments on commit 30a5f26

Please sign in to comment.