ZIPFoundation icon indicating copy to clipboard operation
ZIPFoundation copied to clipboard

Unzipping fails when a symlink points to path that contains another symlink

Open JosephDuffy opened this issue 4 years ago • 5 comments

Summary

When a symlink points to a path with another symlink the unzipping fails because the path may not exist.

Steps to Reproduce

Unzip an archive that contains a Swift framework (e.g. a macOS app). I've been trying to unzip https://altstore.io/.

Expected Results

The archive should be unzipped.

Actual Results

The archive fails to be unzipped.

Regression & Version

0.9.11

Related Link

N/A

More Info

When sorting the files to be unzipped they are sorted so that directories are unzipped first, then files, then symlinks:

https://github.com/weichsel/ZIPFoundation/blob/development/Sources/ZIPFoundation/FileManager%2BZIP.swift#L103

However, if a symlink points to a path that has another symlink in it the unzip will fail. e.g.

  • [File] Foo
  • [Symlink] Resources -> Versions/Current/Resources
  • [Directory] Versions
  • [Directory] Versions/A
  • [Directory] Versions/A/Resources
  • [Symlink] Versions/Current -> Versions/A

When these are sorted it will unzip in the order:

  • [Directory] Versions
  • [Directory] Versions/A
  • [Directory] Versions/A/Resources
  • [File] Foo
  • [Symlink] Resources -> Versions/Current/Resources
  • [Symlink] Versions/Current -> Versions/A

It will fail unzipping the Resources symlink because the Versions/Current symlink does not yet exist.

Locally I have fixed this by adding:

case (.symlink, .symlink): return true

But this assumes that symlinks are in the order they should be created in the archive.

I'm not sure what a more robust fix should be for this. If I come up with a solution I will open a PR.

JosephDuffy avatar Sep 19 '20 20:09 JosephDuffy

Hi Joseph,

Thanks for reporting this and also for the detailed analysis. There is a similar issue open here: https://github.com/weichsel/ZIPFoundation/issues/62

I'm not sure what a more robust fix should be for this. If I come up with a solution I will open a PR.

A PR to fix this would be highly welcome!

weichsel avatar Sep 21 '20 15:09 weichsel

+1. I just hit this bug when using ZIPFoundation to unzip a zipped macOS app archive that contains a framework (which typically contains several symlinks) similarly as described in the original post.

jaanus avatar Jun 27 '21 08:06 jaanus

@jaanus Are you able to find a solution to this?

sandeep1995 avatar Jul 03 '22 20:07 sandeep1995

@jaanus Are you able to find a solution to this?

I have a PR with a fix: https://github.com/weichsel/ZIPFoundation/pull/187. You're welcome to use the fork until it's merged.

JosephDuffy avatar Jul 06 '22 02:07 JosephDuffy

@jaanus Are you able to find a solution to this?

Since my use case was very simple, I switched to using the unzip system command.

jaanus avatar Jul 06 '22 02:07 jaanus