Skip to content

Commit

Permalink
Fixed Issue #41
Browse files Browse the repository at this point in the history
Added code to set permissions on the final file, respecting umask.
Also fixed potentially similar problem with exports, although the file
is opened differently, and I think it was already respecting umask.
  • Loading branch information
Tom Francis committed Oct 2, 2019
1 parent 33c5732 commit b57c1fe
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Ketarin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
<Reference Include="ScintillaNET, Version=3.6.3.0, Culture=neutral, processorArchitecture=MSIL" Condition=" ! $(Configuration.Contains('Mono'))">
<HintPath>packages\jacobslusser.ScintillaNET.3.6.3\lib\net40\ScintillaNET.dll</HintPath>
</Reference>
<Reference Include="Mono.Posix" Condition="$(Configuration.Contains('Mono'))" />
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Data.SQLite, Version=1.0.109.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=MSIL" Condition=" ! $(Configuration.Contains('Mono'))">
Expand Down Expand Up @@ -403,6 +404,7 @@
<Compile Include="MainForm.Designer.cs">
<DependentUpon>MainForm.cs</DependentUpon>
</Compile>
<Compile Include="MaskedPermissions.cs" Condition=" $(Configuration.Contains('Mono'))" />
<Compile Include="NonBinaryFileException.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
4 changes: 4 additions & 0 deletions MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1214,6 +1214,10 @@ private void ExportJobs(IEnumerable<ApplicationJob> objects)
try
{
File.WriteAllText(dialog.FileName, ApplicationJob.GetXml(objects, dialog.FilterIndex == 2, Encoding.UTF8), Encoding.UTF8);
#if MONO
MaskedPermissions.setMaskedPermissions(dialog.FileName,
Mono.Unix.Native.FilePermissions.DEFFILEMODE);
#endif
}
catch (Exception ex)
{
Expand Down
71 changes: 71 additions & 0 deletions MaskedPermissions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;
using System.Runtime.InteropServices;
using Mono.Unix;
using Mono.Unix.Native;

namespace Ketarin
{
public class MaskedPermissions
{
private static uint umask_value = getUmask();

public static uint getUmask()
{
uint _umask_value = umask(0);
umask(_umask_value);
return _umask_value;
}

public static uint setMaskedPermissions(string path, FilePermissions mode)
{
uint _mode = NativeConvert.FromFilePermissions(mode);
_mode = _mode & ~(umask_value);
return chmod(path, _mode);
}

[System.Runtime.InteropServices.DllImport("libc", SetLastError=false, EntryPoint="umask")]
private extern static uint umask(uint mask);

[System.Runtime.InteropServices.DllImport("libc", SetLastError=true, EntryPoint="chmod")]
private extern static uint chmod([MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))] string path, uint mode);
}

class FileNameMarshaler : ICustomMarshaler
{
private static FileNameMarshaler instance = new FileNameMarshaler();
public static ICustomMarshaler GetInstance(string s)
{
return instance;
}

public void CleanUpManagedData(Object obj)
{
// do nothing
}

public void CleanUpNativeData(IntPtr pNativeData)
{
UnixMarshal.FreeHeap(pNativeData);
}

public int GetNativeDataSize()
{
return IntPtr.Size;
}

public IntPtr MarshalManagedToNative(object obj)
{
String str = obj as string;
if (str == null)
{
return IntPtr.Zero;
}
return UnixMarshal.StringToHeap(str, UnixEncoding.Instance);
}

public object MarshalNativeToManaged(IntPtr pNativeData)
{
return UnixMarshal.PtrToString(pNativeData, UnixEncoding.Instance);
}
}
}
6 changes: 5 additions & 1 deletion Updater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,10 @@ protected Status DoDownload(ApplicationJob job, Uri urlToRequest)
// However, we cannot rely on functions to clean up the path, since they
// might actually parse the path incorrectly and return an even worse path.
File.Copy(tmpLocation, targetFileName, true);
#if MONO
MaskedPermissions.setMaskedPermissions(targetFileName,
Mono.Unix.Native.FilePermissions.DEFFILEMODE);
#endif
}
catch (ArgumentException)
{
Expand Down Expand Up @@ -1071,4 +1075,4 @@ protected virtual void OnProgressChanged(long pos, long length, ApplicationJob j
}
}
}
}
}

0 comments on commit b57c1fe

Please sign in to comment.