Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(fslib): add ftruncate support #4477

Merged
merged 1 commit into from
May 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions .yarn/versions/39ddc77a.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
releases:
"@yarnpkg/cli": patch
"@yarnpkg/fslib": minor
"@yarnpkg/pnp": patch
"@yarnpkg/pnpify": patch

declined:
- "@yarnpkg/esbuild-plugin-pnp"
- "@yarnpkg/plugin-compat"
- "@yarnpkg/plugin-constraints"
- "@yarnpkg/plugin-dlx"
- "@yarnpkg/plugin-essentials"
- "@yarnpkg/plugin-exec"
- "@yarnpkg/plugin-file"
- "@yarnpkg/plugin-git"
- "@yarnpkg/plugin-github"
- "@yarnpkg/plugin-http"
- "@yarnpkg/plugin-init"
- "@yarnpkg/plugin-interactive-tools"
- "@yarnpkg/plugin-link"
- "@yarnpkg/plugin-nm"
- "@yarnpkg/plugin-npm"
- "@yarnpkg/plugin-npm-cli"
- "@yarnpkg/plugin-pack"
- "@yarnpkg/plugin-patch"
- "@yarnpkg/plugin-pnp"
- "@yarnpkg/plugin-pnpm"
- "@yarnpkg/plugin-stage"
- "@yarnpkg/plugin-typescript"
- "@yarnpkg/plugin-version"
- "@yarnpkg/plugin-workspace-tools"
- vscode-zipfs
- "@yarnpkg/builder"
- "@yarnpkg/core"
- "@yarnpkg/doctor"
- "@yarnpkg/json-proxy"
- "@yarnpkg/nm"
- "@yarnpkg/sdks"
- "@yarnpkg/shell"
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ The following changes only affect people writing Yarn plugins:

- The `generateLoader` function in `@yarnpkg/pnp` no longer generates the `$$SETUP_STATE` function, it now needs to be present in the `loader` passed to the function.

### Compatibility

- The patched filesystem now supports `ftruncate`.

## 3.2.1

### Installs
Expand Down
2 changes: 1 addition & 1 deletion packages/yarnpkg-core/sources/worker-zip/index.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions packages/yarnpkg-fslib/sources/FakeFS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@ export abstract class FakeFS<P extends Path> {
abstract readlinkPromise(p: P): Promise<P>;
abstract readlinkSync(p: P): P;

abstract ftruncatePromise(fd: number, len?: number): Promise<void>;
abstract ftruncateSync(fd: number, len?: number): void;

abstract truncatePromise(p: P, len?: number): Promise<void>;
abstract truncateSync(p: P, len?: number): void;

Expand Down
8 changes: 8 additions & 0 deletions packages/yarnpkg-fslib/sources/NoFS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,14 @@ export class NoFS extends FakeFS<PortablePath> {
throw makeError();
}

async ftruncatePromise(fd: number, len?: number): Promise<never> {
throw makeError();
}

ftruncateSync(fd: number, len?: number): never {
throw makeError();
}

watch(): never {
throw makeError();
}
Expand Down
10 changes: 10 additions & 0 deletions packages/yarnpkg-fslib/sources/NodeFS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,16 @@ export class NodeFS extends BasePortableFakeFS {
return this.realFs.truncateSync(npath.fromPortablePath(p), len);
}

async ftruncatePromise(fd: number, len?: number): Promise<void> {
return await new Promise<void>((resolve, reject) => {
this.realFs.ftruncate(fd, len, this.makeCallback(resolve, reject));
});
}

ftruncateSync(fd: number, len?: number): void {
return this.realFs.ftruncateSync(fd, len);
}

watch(p: PortablePath, cb?: WatchCallback): Watcher;
watch(p: PortablePath, opts: WatchOptions, cb?: WatchCallback): Watcher;
watch(p: PortablePath, a?: WatchOptions | WatchCallback, b?: WatchCallback) {
Expand Down
8 changes: 8 additions & 0 deletions packages/yarnpkg-fslib/sources/ProxiedFS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,14 @@ export abstract class ProxiedFS<P extends Path, IP extends Path> extends FakeFS<
return this.baseFs.truncateSync(this.mapToBase(p), len);
}

async ftruncatePromise(fd: number, len?: number): Promise<void> {
return this.baseFs.ftruncatePromise(fd, len);
}

ftruncateSync(fd: number, len?: number): void {
return this.baseFs.ftruncateSync(fd, len);
}

watch(p: P, cb?: WatchCallback): Watcher;
watch(p: P, opts: WatchOptions, cb?: WatchCallback): Watcher;
watch(p: P, a?: WatchOptions | WatchCallback, b?: WatchCallback) {
Expand Down
8 changes: 8 additions & 0 deletions packages/yarnpkg-fslib/sources/ZipFS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1476,6 +1476,14 @@ export class ZipFS extends BasePortableFakeFS {
return this.writeFileSync(p, truncated);
}

async ftruncatePromise(fd: number, len?: number): Promise<void> {
return this.truncatePromise(this.fdToPath(fd, `ftruncate`), len);
}

ftruncateSync(fd: number, len?: number): void {
return this.truncateSync(this.fdToPath(fd, `ftruncateSync`), len);
}

watch(p: PortablePath, cb?: WatchCallback): Watcher;
watch(p: PortablePath, opts: WatchOptions, cb?: WatchCallback): Watcher;
watch(p: PortablePath, a?: WatchOptions | WatchCallback, b?: WatchCallback) {
Expand Down
24 changes: 24 additions & 0 deletions packages/yarnpkg-fslib/sources/ZipOpenFS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,30 @@ export class ZipOpenFS extends BasePortableFakeFS {
});
}

async ftruncatePromise(fd: number, len?: number): Promise<void> {
if ((fd & ZIP_FD) === 0)
return this.baseFs.ftruncatePromise(fd, len);

const entry = this.fdMap.get(fd);
if (typeof entry === `undefined`)
throw errors.EBADF(`ftruncate`);

const [zipFs, realFd] = entry;
return zipFs.ftruncatePromise(realFd, len);
}

ftruncateSync(fd: number, len?: number): void {
if ((fd & ZIP_FD) === 0)
return this.baseFs.ftruncateSync(fd, len);

const entry = this.fdMap.get(fd);
if (typeof entry === `undefined`)
throw errors.EBADF(`ftruncateSync`);

const [zipFs, realFd] = entry;
return zipFs.ftruncateSync(realFd, len);
}

watch(p: PortablePath, cb?: WatchCallback): Watcher;
watch(p: PortablePath, opts: WatchOptions, cb?: WatchCallback): Watcher;
watch(p: PortablePath, a?: WatchOptions | WatchCallback, b?: WatchCallback) {
Expand Down
2 changes: 2 additions & 0 deletions packages/yarnpkg-fslib/sources/patchFs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const SYNC_IMPLEMENTATIONS = new Set([
`statSync`,
`symlinkSync`,
`truncateSync`,
`ftruncateSync`,
`unlinkSync`,
`unwatchFile`,
`utimesSync`,
Expand Down Expand Up @@ -64,6 +65,7 @@ const ASYNC_IMPLEMENTATIONS = new Set([
`statPromise`,
`symlinkPromise`,
`truncatePromise`,
`ftruncatePromise`,
`unlinkPromise`,
`utimesPromise`,
`writeFilePromise`,
Expand Down
26 changes: 26 additions & 0 deletions packages/yarnpkg-fslib/tests/NodeFS.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,31 @@ describe(`NodeFS`, () => {
await expect(nodeFs.readFilePromise(source, `utf8`)).resolves.toStrictEqual(sourceContent);
await expect(nodeFs.readFilePromise(destination, `utf8`)).resolves.toStrictEqual(sourceContent);
});

it(`should support ftruncatePromise`, async () => {
await xfs.mktempPromise(async dir => {
const p = `${dir}/foo.txt` as PortablePath;
await nodeFs.writeFilePromise(p, `foo`);

const fd = await nodeFs.openPromise(p, `r+`);
await nodeFs.ftruncatePromise(fd, 2);
await nodeFs.closePromise(fd);

await expect(nodeFs.readFilePromise(p, `utf8`)).resolves.toEqual(`fo`);
});
});

it(`should support ftruncateSync`, () => {
xfs.mktempSync(async dir => {
const p = `${dir}/foo.txt` as PortablePath;
nodeFs.writeFileSync(p, `foo`);

const fd = nodeFs.openSync(p, `r+`);
nodeFs.ftruncateSync(fd, 2);
nodeFs.closeSync(fd);

expect(nodeFs.readFileSync(p, `utf8`)).toEqual(`fo`);
});
});
});
});
18 changes: 18 additions & 0 deletions packages/yarnpkg-fslib/tests/ZipFS.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,24 @@ describe(`ZipFS`, () => {
zipFs.discardAndClose();
});

it(`should support ftruncate`, async () => {
const libzip = getLibzipSync();
const zipFs = new ZipFS(null, {libzip});

const fd = zipFs.openSync(`/foo.txt` as PortablePath, `r+`);

zipFs.writeFileSync(fd, `1234567890`);

zipFs.ftruncateSync(fd, 5);
expect(zipFs.readFileSync(fd, `utf8`)).toStrictEqual(`12345`);

await zipFs.ftruncatePromise(fd, 4);
expect(zipFs.readFileSync(fd, `utf8`)).toStrictEqual(`1234`);

zipFs.closeSync(fd);
zipFs.discardAndClose();
});

it(`should support watchFile and unwatchFile`, () => {
const libzip = getLibzipSync();
const zipFs = new ZipFS(null, {libzip});
Expand Down
2 changes: 1 addition & 1 deletion packages/yarnpkg-pnp/sources/hook.js

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions packages/yarnpkg-pnpify/sources/NodeModulesFS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,14 @@ export class PortableNodeModulesFS extends FakeFS<PortablePath> {
return this.baseFs.truncateSync(this.resolveDirOrFilePath(p), len);
}

async ftruncatePromise(fd: number, len?: number): Promise<void> {
return await this.baseFs.ftruncatePromise(fd, len);
}

ftruncateSync(fd: number, len?: number): void {
return this.baseFs.ftruncateSync(fd, len);
}

watch(p: PortablePath, cb?: WatchCallback): Watcher;
watch(p: PortablePath, opts: WatchOptions, cb?: WatchCallback): Watcher;
watch(p: PortablePath, a?: WatchOptions | WatchCallback, b?: WatchCallback): Watcher {
Expand Down