2010年1月18日月曜日

MONO_IOMAP

についてのメモ。

MONO_IOMAP は、Mono で使用できる環境変数で、「man mono」では、次のように説明されている。
PORTABILITY

On Unix-based systems, Mono provides a mechanism to emulate the Windows-style file access, this includes providing a case insensitive view of the file system, directory separator mapping (from \ to /) and stripping the drive letters.

This functionality is enabled by setting the MONO_IOMAP environment variable to one of all, drive and case.

See the description for MONO_IOMAP in the environment variables section for more details.
移植性

Unix ベースのシステムで、Mono は Windows スタイルのファイルへのアクセスをエミュレートするためのメカニズムを提供します。これには、大文字と小文字の区別をしないファイルシステム上の表示、ディレクトリの区切り文字のマッピング(「\」から「/」へ変換)、ドライブ文字の除去が含まれます。

この機能は、MONO_IOMAP 環境変数に、「all」「drive」「case」の内、どれか一つを設定することで有効になります。

詳細については、環境変数のセクションで MONO_IOMAP の説明を参照して下さい。
MONO_IOMAP

Enables some filename rewriting support to assist badly-written applications that hard-code Windows paths. Set to a colon-separated list of "drive" to strip drive letters, or "case" to do case-insensitive file matching in every directory in a path. "all" enables all rewriting methods. (Backslashes are always mapped to slashes if this variable is set to a valid option.)
For example, this would work from the shell:

MONO_IOMAP=drive:case
export MONO_IOMAP

If you are using mod_mono to host your web applications, you can use the MonoSetEnv directive, like this:

MonoSetEnv MONO_IOMAP=all
MONO_IOMAP

Windows でのパスをハードコードするような、まずい書き方をされたアプリケーションを支援するために、いくつかのファイル名書き換えサポートを有効にします。コロンで区切られたリストによる設定が可能で、"drive" はドライブ文字の除去、また、"case" は大文字と小文字を区別しないで、すべてのディレクトリ内のパスで、ファイルマッチングを行います。"all" はすべての書き換え手法を有効にします。(この変数に有効なオプションが設定されている場合、バックスラッシュは、常にスラッシュにマップされます。)
例えば、シェルからでは、このような記述が有効になります:

MONO_IOMAP=drive:case
export MONO_IOMAP

Web アプリケーションをホストするのに mod_mono を使用する場合、このように MonoSetEnv ディレクティブを使用できます:

MonoSetEnv MONO_IOMAP=all
それで、次のような簡単なコードで MONO_IOMAP の効き具合を試してみた。

iomaptest.cs:
  1. using System;  
  2. using System.IO;  
  3.   
  4. public class IOMapTest  
  5. {  
  6.   public static void Main()  
  7.   {  
  8.     //using(StreamReader sr = new StreamReader("/home/sta/data/test.txt"))  
  9.     using(StreamReader sr = new StreamReader(@"C:\home\sta\data\test.txt"))  
  10.     {  
  11.       string line;  
  12.       while((line = sr.ReadLine()) != null)  
  13.       {  
  14.         Console.WriteLine(line);  
  15.       }  
  16.     }  
  17.   }  
  18. }  
  19. /* 
  20.  * Build: 
  21.  * 
  22.  *   gmcs iomaptest.cs 
  23.  * 
  24.  * Run: 
  25.  * 
  26.  *   mono iomaptest.exe 
  27.  * 
  28.  */  
/home/sta/data/test.txt:
  1. File opened successfully.  
$ mono iomaptest.exe 

Unhandled Exception: System.IO.FileNotFoundException: Could not find file "/home/sta/src/cs/C:\home\sta\data\test.txt".
File name: '/home/sta/src/cs/C:\home\sta\data\test.txt'
...
$ export MONO_IOMAP=drive
$ mono iomaptest.exe
File opened successfully.

また、
  1. using(StreamReader sr = new StreamReader(@"C:\home\sta\data\Test.txt"))  
とした場合、

$ mono iomaptest.exe 

Unhandled Exception: System.IO.FileNotFoundException: Could not find file "/home/sta/src/cs/C:\home\sta\data\Test.txt".
File name: '/home/sta/src/cs/C:\home\sta\data\Test.txt'
...
$ export MONO_IOMAP=drive:case
$ mono iomaptest.exe
File opened successfully.
$ export MONO_IOMAP=all
$ mono iomaptest.exe
File opened successfully.

ということは、確認できた。

つまり、「環境変数 MONO_IOMAP に all を設定しておけばいいんだYo!」ということは言えるかもしれないが、「そうは問屋は卸さんよ、フフフ」という具合に、うまくいかない場合も実はあったが、それはまた後日。

0 件のコメント:

コメントを投稿