Adding to .zip archive removes file system access rules

Open
#654 12 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
csharp
Domain
backend

Research direction

Start in src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs at the finalization code around lines 3132-3143 and the temporary and final output handling around lines 4663-4713. Reproduce the issue with the supplied AddEntryRevertingFilePermissions test, then add regression coverage showing that an access rule on the archive remains after CommitUpdate().

Written by the indexing model from the issue text.

Description

When adding to a .zip archive on disk, the update does not happen in place. Instead, a temporary .zip file with the updated contents is created in a temporary directory first, which replaces the original archive after a successful write. As a result, any file system access rules set on the .zip archive that is updated are lost.

Steps to reproduce
  1. Run the snippet below in a debugger
		[Test]
		[Category("Zip")]
		[Category("CreatesTempFile")]
		public void AddEntryRevertingFilePermissions()
		{
			const string TestValue = "0001000";
			string tempFile = "c:/temp/";
			Assert.IsNotNull(tempFile, "No permission to execute this test?");

			tempFile = Path.Combine(tempFile, "SharpZipTest.Zip");

			// create empty zip file
			using (ZipFile f = ZipFile.Create(tempFile))
			{
				f.BeginUpdate();
				f.CommitUpdate();
			}

			Console.WriteLine("break here and manually amend permissions for c:/temp/SharpZipTest.Zip by adding a rule");

			using (ZipFile f = new ZipFile(tempFile))
			{
				var m = new StringMemoryDataSource(TestValue);
				f.BeginUpdate();
				f.Add(m, "a.dat");
				f.CommitUpdate();
			}

			Console.WriteLine("permissions for c:/temp/SharpZipTest.Zip are reverted");
		}
  1. Break in the middle and amend permissions to the file by adding an access rule.
  2. Run to completion and verify that the file permission has reverted.
Expected behavior

File permissions should be retained

Actual behavior

File permissions inherited from temporary directory (e.g. C:\Users\username\AppData\Local\Temp\) are set

Version of SharpZipLib
Obtained from (only keep the relevant lines)
Further detail

The update is finalised: (directUpdate is false)
https://github.com/icsharpcode/SharpZipLib/blob/cd5310f5b7eed595110b76a2f7ae5ee013cc50f1/src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs#L3132-L3143

The temporary output (pointing to a temporary directory ) is created at https://github.com/icsharpcode/SharpZipLib/blob/cd5310f5b7eed595110b76a2f7ae5ee013cc50f1/src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs#L4663-L4669

The final output is created at
https://github.com/icsharpcode/SharpZipLib/blob/cd5310f5b7eed595110b76a2f7ae5ee013cc50f1/src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs#L4676-L4713

Dominant language
C#
Stars
3.9k
Forks
1k
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from icsharpcode/SharpZipLib

All issues in icsharpcode/SharpZipLib

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.