Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zip Archive created under Linux is missing file permissions #4022

Closed
marcwittke opened this issue Jun 20, 2020 · 2 comments
Closed

Zip Archive created under Linux is missing file permissions #4022

marcwittke opened this issue Jun 20, 2020 · 2 comments
Labels
feature file-archiving Issues with archiving with the file target file-target

Comments

@marcwittke
Copy link

NLog version: 4.5.11

Platform: .NET Core 3

Current NLog config (xml or C#, if relevant)

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      autoReload="true" internalLogFile="logs/nlog.log"
      internalLogLevel="Warn">

  <targets async="true">
    <target name="rollingLogFile" xsi:type="File" layout="${level:uppercase=true:padding=-5} ${mdlc:item=app-Activity:padding=5} ${mdlc:item=app-Identity:padding=5} ${date} ${time} ${logger} ${message} ${exception}"
            keepFileOpen="true"
            concurrentWrites="false"
            fileName="logs/Application.log"
            archiveFileName="logs/Application.{#}.zip"
            archiveAboveSize="10240000"
            archiveNumbering="Rolling"
            maxArchiveFiles="20"
            enableArchiveFileCompression="true" />
    
  </targets>

  <rules>
    <logger name="*" minlevel="Debug" writeTo="rollingLogFile" />
  </rules>

</nlog>

Running the application on linux, everything seems to work. Opening a archived log file shows a problem: the files are archived with permission 0000 - no one can read them. The Linux unzippers do respect these permission masks (unlike windows tools) which results in a file that no one can read and a sudo operation is required to fix the file permissions before opening the file.

I recently stumbled upon the same problem and I think I know how to fix it. Unfortunately I don't have a windows machine at hand to compile the complete code, therefore the proposed fix via comment:

.net core now exposes the ExternalAttributes property of ZipArchiveEntry that allows you to set permissions, like this (664):

entry.ExternalAttributes = entry.ExternalAttributes | (Convert.ToInt32("664", 8) << 16);

note that the string is an octal representation of the permission bit mask, therefore the convert to base 8. The line should probably embedded in some compiler-if checking for netstandard.

@snakefoot
Copy link
Contributor

snakefoot commented Jun 21, 2020

The preferred goal is to refactor and extract the zip-compression logic out of FileTarget into its own nuget-package. See #2732 (Maybe with help from #1249).

The NLog FileTarget is already a gnarly old class, that have taken on many warts (options). And doing a lot more than just write to a file :)

But thank you for the great input. When a champion arrives that is ready to take on the task, then this option would be great to include.

Notice that ExternalAttributes is not part of NetStandard2 (Requires NetStandard2.1). See also dotnet/runtime#1548 and dotnet/corefx#18565 (comment)

ExternalAttributes appears to be available in .NET Core 2.x but it is not available in .NET Standard 2

Strange that Microsoft have not included good default values for ExternalAttributes out-of-box on the Linux-platform. Kind of ruin the illusion of NetCore.

@snakefoot snakefoot added feature file-target up-for-grabs file-archiving Issues with archiving with the file target labels Jun 21, 2020
@snakefoot
Copy link
Contributor

Closing as not top-priority. Ofcouse pull-request are most welcome. See also #3711 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature file-archiving Issues with archiving with the file target file-target
Projects
None yet
Development

No branches or pull requests

2 participants