[Mono-list] File IO error

Mike Gray mike@mikegray.org
Sat, 04 May 2002 13:22:20 -0400

In looking at the code, I think you are correct.  I am just getting started 
here, so I am not sure I tracked it back correctly, but here was my logic 
(hoping someone will let me know if this is not actually the correct path 
back through the code).  I have also included a patch to the System.IO tests 
that should show the error on Linux (but it works correctly on Win32).

FileStream constructor calls
MonoIO.Open which is a
MethodImplOptions.InternalCall which maps to
ves_icall_System_IO_MonoIO_Open (in mono/mono/metadata/file-io.c)
which calls CreateFile with the appropriate mode (OPEN_EXISTING in your 
On Win32, CreateFile is a native call and your example below works.
On Linux, CreateFile is implemented in io.c (mono/mono/io-layer/).
convert_flags is called, but as you can see in this function, the 
OPEN_EXISTING case doesn't set anything to flags variable.  I am not a Linux 
programmer, so I am not sure if this is just the default behavior - if 
open() is called and file doesn't exist, it returns an error?  If open() 
fails, the only thing done is to return INVALID_HANDLE_VALUE.

Anyway, just saw a response from Dan Lewis so I will leave it to him since 
he likely knows way more than I do.  Here is the patch to the unit test:

Index: AllTests.cs
RCS file: /mono/mcs/class/corlib/Test/System.IO/AllTests.cs,v
retrieving revision 1.4
diff -u -r1.4 AllTests.cs
--- AllTests.cs	15 Feb 2002 18:55:45 -0000	1.4
+++ AllTests.cs	4 May 2002 16:55:43 -0000
@@ -26,6 +26,7 @@
+                                suite.AddTest(FileTest.Suite);
                                 return suite;
Index: FileTest.cs
RCS file: /mono/mcs/class/corlib/Test/System.IO/FileTest.cs,v
retrieving revision 1.1
diff -u -r1.1 FileTest.cs
--- FileTest.cs	5 Feb 2002 20:48:09 -0000	1.1
+++ FileTest.cs	4 May 2002 16:55:43 -0000
@@ -37,9 +37,20 @@
			get { return new TestSuite (typeof (FileTest)); }

+                public void TestOpen ()
+                {
+                        int caughtException = 0;
+                        try {
+                                FileStream stream = 
File.Open("filedoesnotexist", FileMode.Open);
+                        } catch (FileNotFoundException) {
+                                caughtException = 1;
+                        }
+                        AssertEquals ("File 'filedoesnotexist' should not 
exist", 1, caughtException);
+                }
		public void TestExists ()
-			Assert ("File filetest/test should exist", File.Exists 
+			Assert ("File filetest/test should exist", File.Exists ("TheTests.cs"));

		public void TestCreate ()

Have fun,

Mike Gray


I think you were the one working on the file IO bits, so I'll address
this to you.  Anyone else who wants to fix it, please submit a patch to
the list.

I worked on StreamReader bugs a bit, and found some errors in how file
IO exceptions are thrown.  Here's a simple test case that shows a
typical problem when the file cannot be found.  An incorrect exception
is thrown: (btw, bugzilla is down right now so I'm sending to the list)

using System.IO;
using System;

namespace NS {
	class C {
		public static int Main() {
			try {
				FileStream f = new
FileStream("filenotthereforsure", FileMode.Open);
			} catch (FileNotFoundException) {
				return 0;
			return 1;

Run this on mono on Linux, and you'll see the problem.

Good night,
Nick D.

Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp.