Skip to content

Commit

Permalink
EES-2236 Fix missing zip file permissions in Unix environments
Browse files Browse the repository at this point in the history
Currently, .NET doesn't automatically add Unix file permissions to our
zip's file entries. This means that all of the files cannot be opened
without setting the permissions manually in the OS.

To fix this, we manually add the Unix file permissions ourselves by
converting a permission string to an octal literal on the zip entry.

I believe that in .NET 6, this should be done automatically for us due
to the following change: dotnet/runtime#55531
  • Loading branch information
ntsim committed Aug 17, 2021
1 parent ef8eae8 commit 46cde52
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#nullable enable
using System;
using System.IO.Compression;

namespace GovUk.Education.ExploreEducationStatistics.Common.Extensions
{
public static class ZipArchiveExtensions
{
/// <summary>
/// Adds UNIX file permissions to the zip entry.
/// </summary>
/// <remarks>
/// We need this due to .NET not adding these permissions for us.
/// See: https://github.com/dotnet/runtime/issues/1548
/// This should be fixed in .NET 6 as part of the following PR:
/// https://github.com/dotnet/runtime/pull/55531
/// </remarks>
public static ZipArchiveEntry SetUnixPermissions(this ZipArchiveEntry entry, string permissions)
{
entry.ExternalAttributes |= Convert.ToInt32(permissions, 8) << 16;
return entry;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ private async Task DoZipFilesToStream(
continue;
}

var entry = archive.CreateEntry(releaseFile.File.ZipFileEntryName());
var entry = archive
.CreateEntry(releaseFile.File.ZipFileEntryName())
.SetUnixPermissions("664");

await using var entryStream = entry.Open();

await _blobStorageService.DownloadToStream(
Expand All @@ -144,7 +147,10 @@ await _blobStorageService.DownloadToStream(

if (subjectIds.Any())
{
var entry = archive.CreateEntry(FileType.DataGuidance.GetEnumLabel() + "/data-guidance.txt");
var entry = archive
.CreateEntry(FileType.DataGuidance.GetEnumLabel() + "/data-guidance.txt")
.SetUnixPermissions("664");

await using var entryStream = entry.Open();

await _dataGuidanceFileWriter.WriteToStream(entryStream, release, subjectIds);
Expand Down

0 comments on commit 46cde52

Please sign in to comment.