「C Sharpの基礎 - ディレクトリ」の版間の差分

ナビゲーションに移動 検索に移動
523行目: 523行目:
<br><br>
<br><br>


== パーミッション ==
== ディレクトリのパーミッション ==
.NET 5以降では、<code>System.IO.FileSystem</code>名前空間に<code>UnixFileMode</code>列挙型が追加された。<br>
Linuxにおいて、ディレクトリのセキュリティ設定は、主に権限 (パーミッション) の設定を通じて行う。<br>
より細かい制御 (ACLの設定等) が必要な場合は、追加のライブラリやシステムコールが必要になる可能性がある。<br>
<br>
<u>.NET 5以降</u>では、<code>System.IO.FileSystem</code>名前空間に<code>UnixFileMode</code>列挙型が追加された。<br>
この列挙型を使用して、UNIX系OSのファイルパーミッションを扱うことができる。<br>
この列挙型を使用して、UNIX系OSのファイルパーミッションを扱うことができる。<br>
<br>
<br>
権限の設定は、I/Oエラーが発生する可能性が高いため、エラーハンドリングを実装することが重要である。<br>
<br>
<u>※注意 1</u><br>
<u>ただし、<code>UnixFileMode</code>プロパティはLinux環境でのみ動作することに注意する。</u><br>
<u>ただし、<code>UnixFileMode</code>プロパティはLinux環境でのみ動作することに注意する。</u><br>
<br>
<br>
<u>※注意 2</u><br>
<u>root権限または適切な権限を持つユーザとして実行する必要がある。</u><br>
<u>そうでない場合、例外UnauthorizedAccessExceptionが発生する可能性がある。</u><br>
<br>
==== パーミッションの取得 ====
  <syntaxhighlight lang="c#">
  <syntaxhighlight lang="c#">
  using System;
  using System;
574行目: 585行目:
       {
       {
           Console.WriteLine($"エラーが発生: {ex.Message}");
           Console.WriteLine($"エラーが発生: {ex.Message}");
      }
    }
}
</syntaxhighlight>
<br>
==== パーミッションの設定 ====
以下の例では、指定したディレクトリのパーミッションを設定している。<br>
<br>
<syntaxhighlight lang="c#">
using System;
using System.IO;
class Program
{
    static void Main(string[] args)
    {
      string directoryPath = "/home/user/testdir";
      try
      {
          // ディレクトリが存在することを確認
          if (Directory.Exists(directoryPath))
          {
            // ディレクトリのパーミッションを755に設定 (例: 755 - rwxr-xr-x)
            File.SetUnixFileMode(directoryPath, UnixFileMode.UserRead    |
                                                UnixFileMode.UserWrite    |
                                                UnixFileMode.UserExecute  |
                                                UnixFileMode.GroupRead    |
                                                UnixFileMode.GroupExecute |
                                                UnixFileMode.OtherRead    |
                                                UnixFileMode.OtherExecute);
            // 現在のパーミッションを取得して8進数で表示
            UnixFileMode currentMode = File.GetUnixFileMode(directoryPath);
            Console.WriteLine($"現在のパーミッション: {Convert.ToString((int)currentMode, 8)}");
          }
          else
          {
            Console.WriteLine($"ディレクトリ '{directoryPath}' が存在しない");
          }
      }
      catch (UnauthorizedAccessException e)
      {
          Console.WriteLine($"エラー: アクセス権限がない {e.Message}");
      }
      catch (IOException e)
      {
          Console.WriteLine($"エラー: I/O操作中にエラーが発生 {e.Message}");
      }
    }
}
</syntaxhighlight>
<br>
==== 再帰的なパーミッションの設定 ====
以下の例では、再帰的にディレクトリとそのサブディレクトリのパーミッションを設定している。<br>
<br>
<u>※注意</u><br>
<u>再帰的処理では、大規模なディレクトリ構造を扱う場合にスタックオーバーフローする可能性がある。</u><br>
<u>そのような場合は、非再帰的なアプローチや非同期処理を検討する必要がある。</u><br>
<br>
<syntaxhighlight lang="c#">
using System;
using System.IO;
class Program
{
    static void Main(string[] args)
    {
      string directoryPath = "/home/user/testdir";
      try
      {
          SetPermissionsRecursively(directoryPath);
      }
      catch (UnauthorizedAccessException e)
      {
          Console.WriteLine($"エラー: アクセス権限がない {e.Message}");
      }
      catch (IOException e)
      {
          Console.WriteLine($"エラー: I/O操作中にエラーが発生 {e.Message}");
      }
    }
    static void SetPermissionsRecursively(string path)
    {
      // ディレクトリのパーミッションを755に設定 (例: 755 - rwxr-xr-x)
      File.SetUnixFileMode(path, UnixFileMode.UserRead    |
                                  UnixFileMode.UserWrite    |
                                  UnixFileMode.UserExecute  |
                                  UnixFileMode.GroupRead    |
                                  UnixFileMode.GroupExecute |
                                  UnixFileMode.OtherRead    |
                                  UnixFileMode.OtherExecute);
      // サブディレクトリに対して再帰的に処理
      foreach (string subDir in Directory.GetDirectories(path))
      {
          SetPermissionsRecursively(subDir);
      }
      // ファイルのパーミッションも644に設定する場合 (例: 644 - rw-r--r--)
      foreach (string file in Directory.GetFiles(path))
      {
          File.SetUnixFileMode(file, UnixFileMode.UserRead  |
                                    UnixFileMode.UserWrite |
                                    UnixFileMode.GroupRead |
                                    UnixFileMode.OtherRead);
       }
       }
     }
     }

案内メニュー