Skip to content

Commit

Permalink
fix(ZipFS): support empty archives resulting from an unlink after wri…
Browse files Browse the repository at this point in the history
…te in `getBufferAndClose` (#5179)

* fix(ZipFS): support empty archives resulting from an unlink after write in `getBufferAndClose`

* chore: update artifacts
paul-soporan authored and merceyz committed Jan 6, 2023
1 parent 8bf48bb commit f6ecf9d
Showing 7 changed files with 69 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .pnp.cjs

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

36 changes: 36 additions & 0 deletions .yarn/versions/1f698bb0.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
releases:
"@yarnpkg/fslib": patch

declined:
- "@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-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-version"
- "@yarnpkg/plugin-workspace-tools"
- vscode-zipfs
- "@yarnpkg/builder"
- "@yarnpkg/cli"
- "@yarnpkg/core"
- "@yarnpkg/doctor"
- "@yarnpkg/json-proxy"
- "@yarnpkg/nm"
- "@yarnpkg/pnp"
- "@yarnpkg/pnpify"
- "@yarnpkg/sdks"
- "@yarnpkg/shell"
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -8,6 +8,12 @@ Yarn now accepts sponsorships! Please give a look at our [OpenCollective](https:

## Master

## TBD

### Bugfixes

- `ZipFS.prototype.getBufferAndClose` will not error on empty archives resulting from an unlink after write.

## 3.3.1

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

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions packages/yarnpkg-fslib/sources/ZipFS.ts
Original file line number Diff line number Diff line change
@@ -238,6 +238,12 @@ export class ZipFS extends BasePortableFakeFS {
if (!this.lzSource)
throw new Error(`ZipFS was not created from a Buffer`);

// zip_source_open on an unlink-after-write empty archive fails with "Entry has been deleted"
if (this.entries.size === 0) {
this.discardAndClose();
return makeEmptyArchive();
}

try {
// Prevent close from cleaning up the source
this.libzip.source.keep(this.lzSource);
15 changes: 15 additions & 0 deletions packages/yarnpkg-fslib/tests/ZipFS.test.ts
Original file line number Diff line number Diff line change
@@ -705,6 +705,21 @@ describe(`ZipFS`, () => {
expect(new ZipFS(buffer, {libzip}).readdirSync(PortablePath.root)).toHaveLength(0);
});

it(`should support getting the buffer from an empty in-memory zip archive (unlink after write)`, () => {
const libzip = getLibzipSync();

const zipFs = new ZipFS(null, {libzip});

zipFs.writeFileSync(`/foo.txt` as PortablePath, `foo`);
zipFs.unlinkSync(`/foo.txt` as PortablePath);

const buffer = zipFs.getBufferAndClose();

expect(buffer).toStrictEqual(makeEmptyArchive());

expect(new ZipFS(buffer, {libzip}).readdirSync(PortablePath.root)).toHaveLength(0);
});

ifNotWin32It(`should preserve the umask`, async () => {
const tmpdir = xfs.mktempSync();
const archive = `${tmpdir}/archive.zip` as PortablePath;
2 changes: 1 addition & 1 deletion packages/yarnpkg-pnp/sources/hook.js

Large diffs are not rendered by default.

0 comments on commit f6ecf9d

Please sign in to comment.