2017-07-16 25 views
5

Linux/Unix .NETコアでファイルアクセス権を設定する方法を学習しようとしています。私はすでにSystem.IO.FileSystemの方向性を指摘しているが、ここではどのように使用するかについてのドキュメントを見つけることはできないようだ。Linux/Unix with .NETコアのファイルアクセス権

簡単に言えば、私は、Linux上でのみ動作する.netコアアプリケーションからファイル644をchmodしたいと思いますが、進める方法が失われています。

ありがとうございました。

+0

マイクロソフトでは、Windowsでファイル許可モデルを使用し、Linux/UNIXに変換します。したがって、 'chmod'への呼び出しは内部であり、https://github.com/dotnet/corefx/blob/bffef76f6af208e2042a2f27bc081ee908bb390b/src/Common/src/Interop/Unix/System.Native/Interop.ChMod.csであり、 https://github.com/dotnet/corefx/blob/801dde95a5eac06140d0ac633ac3f9bfdd25aca5/src/System.IO.FileSystem/src/System/IO/FileSystemInfo.Unix.csあなたの場合、644を対応するWindowsファイルのパーミッションに変換する必要がありますWindowsの方法でファイルを操作します。 –

答えて

6

現時点では、.NET CoreにはAPIが組み込まれていません。ただし、.NET Coreチームは.NETコアでMono.Posixを利用できるように取り組んでいます。これにより、管理されたコードでこの種の操作を行うAPIが公開されます。 https://github.com/dotnet/corefx/issues/15289およびhttps://github.com/dotnet/corefx/issues/3186を参照してください。あなたはここで、このAPIの初期バージョンを試すことができます:https://www.nuget.org/packages/Mono.Posix.NETStandard/1.0.0-beta1

var unixFileInfo = new Mono.Unix.UnixFileInfo("test.txt"); 
    // set file permission to 644 
    unixFileInfo.FileAccessPermissions = 
     FileAccessPermissions.UserRead | FileAccessPermissions.UserWrite 
     | FileAccessPermissions.GroupRead 
     | FileAccessPermissions.OtherRead; 

あなたがMono.Posixを使用しない場合、あなたはネイティブコードを呼び出すことで、これと同じ機能を実装することができます。 P/Invokeを使用すると、chmod関数をlibcから呼び出すことができます。ネイティブAPIの詳細については、man 2 chmodを参照してください。

using System; 
using System.IO; 
using System.Runtime.InteropServices; 
using static System.Console; 

class Program 
{ 
    [DllImport("libc", SetLastError = true)] 
    private static extern int chmod(string pathname, int mode); 

    // user permissions 
    const int S_IRUSR = 0x100; 
    const int S_IWUSR = 0x80; 
    const int S_IXUSR = 0x40; 

    // group permission 
    const int S_IRGRP = 0x20; 
    const int S_IWGRP = 0x10; 
    const int S_IXGRP = 0x8; 

    // other permissions 
    const int S_IROTH = 0x4; 
    const int S_IWOTH = 0x2; 
    const int S_IXOTH = 0x1; 

    static void Main(string[] args) 
    { 
     WriteLine("Setting permissions to 0755 on test.sh"); 
     const int _0755 = 
      S_IRUSR | S_IXUSR | S_IWUSR 
      | S_IRGRP | S_IXGRP 
      | S_IROTH | S_IXOTH; 
     WriteLine("Result = " + chmod(Path.GetFullPath("test.sh"), (int)_0755)); 

     WriteLine("Setting permissions to 0644 on sample.txt"); 
     const int _0644 = 
      S_IRUSR | S_IWUSR 
      | S_IRGRP 
      | S_IROTH; 
     WriteLine("Result = " + chmod(Path.GetFullPath("sample.txt"), _0644)); 

     WriteLine("Setting permissions to 0600 on secret.txt"); 
     const int _0600 = S_IRUSR | S_IWUSR; 
     WriteLine("Result = " + chmod(Path.GetFullPath("secret.txt"), _0600)); 
    } 
} 
1

この問題は、新しいプロセスを開始し、bash chmodコマンドを実行するだけで解決しました。

例:

public static void Exec(string cmd) 
{ 
    var escapedArgs = cmd.Replace("\"", "\\\""); 

    var process = new Process 
    { 
     StartInfo = new ProcessStartInfo 
     { 
      RedirectStandardOutput = true, 
      UseShellExecute = false, 
      CreateNoWindow = true, 
      WindowStyle = ProcessWindowStyle.Hidden, 
      FileName = "/bin/bash", 
      Arguments = $"-c \"{escapedArgs}\"" 
     } 
    }; 

    process.Start(); 
    process.WaitForExit(); 
} 

、その後:

Exec("chmod 644 /path/to/file.txt"); 

あなたはまた、bashのコマンドの他のタイプを実行するには、このExecメソッドを使用することができます。

関連する問題