Skip to content

Commit

Permalink
fix(fs): Fixes the Zip magic fd bits (#4737)
Browse files Browse the repository at this point in the history
* Fixes the Zip magic fd bits

* Reverts changes to the readdir signatures

* Reverts extra changes

* Updates the hook

* Feedback
  • Loading branch information
arcanis committed Aug 12, 2022
1 parent 3741be5 commit 380b32b
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 37 deletions.
67 changes: 48 additions & 19 deletions .pnp.cjs

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

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

declined:
- "@yarnpkg/builder"
- "@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-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/core"
- "@yarnpkg/doctor"
- "@yarnpkg/nm"
- "@yarnpkg/pnpify"
- "@yarnpkg/sdks"
- "@yarnpkg/shell"
36 changes: 22 additions & 14 deletions packages/yarnpkg-fslib/sources/ZipOpenFS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@ import {watchFile, unwatchFile, unwatchAllFiles}
import * as errors from './errors';
import {Filename, FSPath, npath, PortablePath, ppath} from './path';

const ZIP_FD = 0x80000000;
// Only file descriptors prefixed by those values will be forwarded to the ZipFS
// instances. Note that the highest ZIP_MAGIC bit MUST NOT be set, otherwise the
// resulting fd becomes a negative integer, which isn't supposed to happen per
// the unix rules (caused problems w/ Go).
//
// Those values must be synced with packages/yarnpkg-pnp/sources/esm-loader/fspatch.ts
//
const ZIP_MASK = 0xff000000;
const ZIP_MAGIC = 0x2a000000;

/**
* Extracts the archive part (ending in the first instance of `extension`) from a path.
Expand Down Expand Up @@ -155,7 +163,7 @@ export class ZipOpenFS extends BasePortableFakeFS {
}

private remapFd(zipFs: ZipFS, fd: number) {
const remappedFd = this.nextFd++ | ZIP_FD;
const remappedFd = this.nextFd++ | ZIP_MAGIC;
this.fdMap.set(remappedFd, [zipFs, fd]);
return remappedFd;
}
Expand Down Expand Up @@ -197,7 +205,7 @@ export class ZipOpenFS extends BasePortableFakeFS {
}

async readPromise(fd: number, buffer: Buffer, offset: number, length: number, position: number) {
if ((fd & ZIP_FD) === 0)
if ((fd & ZIP_MASK) !== ZIP_MAGIC)
return await this.baseFs.readPromise(fd, buffer, offset, length, position);

const entry = this.fdMap.get(fd);
Expand All @@ -209,7 +217,7 @@ export class ZipOpenFS extends BasePortableFakeFS {
}

readSync(fd: number, buffer: Buffer, offset: number, length: number, position: number) {
if ((fd & ZIP_FD) === 0)
if ((fd & ZIP_MASK) !== ZIP_MAGIC)
return this.baseFs.readSync(fd, buffer, offset, length, position);

const entry = this.fdMap.get(fd);
Expand All @@ -223,7 +231,7 @@ export class ZipOpenFS extends BasePortableFakeFS {
writePromise(fd: number, buffer: Buffer, offset?: number, length?: number, position?: number): Promise<number>;
writePromise(fd: number, buffer: string, position?: number): Promise<number>;
async writePromise(fd: number, buffer: Buffer | string, offset?: number, length?: number, position?: number): Promise<number> {
if ((fd & ZIP_FD) === 0) {
if ((fd & ZIP_MASK) !== ZIP_MAGIC) {
if (typeof buffer === `string`) {
return await this.baseFs.writePromise(fd, buffer, offset);
} else {
Expand All @@ -247,7 +255,7 @@ export class ZipOpenFS extends BasePortableFakeFS {
writeSync(fd: number, buffer: Buffer, offset?: number, length?: number, position?: number): number;
writeSync(fd: number, buffer: string, position?: number): number;
writeSync(fd: number, buffer: Buffer | string, offset?: number, length?: number, position?: number): number {
if ((fd & ZIP_FD) === 0) {
if ((fd & ZIP_MASK) !== ZIP_MAGIC) {
if (typeof buffer === `string`) {
return this.baseFs.writeSync(fd, buffer, offset);
} else {
Expand All @@ -269,7 +277,7 @@ export class ZipOpenFS extends BasePortableFakeFS {
}

async closePromise(fd: number) {
if ((fd & ZIP_FD) === 0)
if ((fd & ZIP_MASK) !== ZIP_MAGIC)
return await this.baseFs.closePromise(fd);

const entry = this.fdMap.get(fd);
Expand All @@ -283,7 +291,7 @@ export class ZipOpenFS extends BasePortableFakeFS {
}

closeSync(fd: number) {
if ((fd & ZIP_FD) === 0)
if ((fd & ZIP_MASK) !== ZIP_MAGIC)
return this.baseFs.closeSync(fd);

const entry = this.fdMap.get(fd);
Expand Down Expand Up @@ -415,7 +423,7 @@ export class ZipOpenFS extends BasePortableFakeFS {
async fstatPromise(fd: number, opts: {bigint: true}): Promise<BigIntStats>
async fstatPromise(fd: number, opts?: {bigint: boolean}): Promise<BigIntStats | Stats>
async fstatPromise(fd: number, opts?: { bigint: boolean }) {
if ((fd & ZIP_FD) === 0)
if ((fd & ZIP_MASK) !== ZIP_MAGIC)
return this.baseFs.fstatPromise(fd, opts);

const entry = this.fdMap.get(fd);
Expand All @@ -430,7 +438,7 @@ export class ZipOpenFS extends BasePortableFakeFS {
fstatSync(fd: number, opts: {bigint: true}): BigIntStats
fstatSync(fd: number, opts?: {bigint: boolean}): BigIntStats | Stats
fstatSync(fd: number, opts?: { bigint: boolean }) {
if ((fd & ZIP_FD) === 0)
if ((fd & ZIP_MASK) !== ZIP_MAGIC)
return this.baseFs.fstatSync(fd, opts);

const entry = this.fdMap.get(fd);
Expand Down Expand Up @@ -469,7 +477,7 @@ export class ZipOpenFS extends BasePortableFakeFS {
}

async fchmodPromise(fd: number, mask: number): Promise<void> {
if ((fd & ZIP_FD) === 0)
if ((fd & ZIP_MASK) !== ZIP_MAGIC)
return this.baseFs.fchmodPromise(fd, mask);

const entry = this.fdMap.get(fd);
Expand All @@ -481,7 +489,7 @@ export class ZipOpenFS extends BasePortableFakeFS {
}

fchmodSync(fd: number, mask: number): void {
if ((fd & ZIP_FD) === 0)
if ((fd & ZIP_MASK) !== ZIP_MAGIC)
return this.baseFs.fchmodSync(fd, mask);

const entry = this.fdMap.get(fd);
Expand Down Expand Up @@ -857,7 +865,7 @@ export class ZipOpenFS extends BasePortableFakeFS {
}

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

const entry = this.fdMap.get(fd);
Expand All @@ -869,7 +877,7 @@ export class ZipOpenFS extends BasePortableFakeFS {
}

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

const entry = this.fdMap.get(fd);
Expand Down

0 comments on commit 380b32b

Please sign in to comment.