Merge pull request #946 from akoeplinger/fix-mono-parallel
[mono.git] / mcs / class / corlib / System.IO / DriveInfo.cs
index 402a4350fcacf68aaf2bf98ef53799505677e029..6c4c7eee87ce0b3a2563058aeed2b769b3bcff3c 100644 (file)
@@ -32,21 +32,29 @@ namespace System.IO {
        [SerializableAttribute] 
        [ComVisibleAttribute(true)] 
        public sealed class DriveInfo : ISerializable {
-               _DriveType _drive_type;
                string drive_format;
                string path;
 
-               DriveInfo (_DriveType _drive_type, string path, string fstype)
+               DriveInfo (string path, string fstype)
                {
-                       this._drive_type = _drive_type;
                        this.drive_format = fstype;
                        this.path = path;
                }
 
                public DriveInfo (string driveName)
                {
-                       DriveInfo [] drives = GetDrives ();
+                       if (!Environment.IsUnix) {
+                               if (driveName == null || driveName.Length == 0)
+                                       throw new ArgumentException ("The drive name is null or empty", "driveName");
+
+                               if (driveName.Length >= 2 && driveName [1] != ':')
+                                       throw new ArgumentException ("Invalid drive name", "driveName");
 
+                               // Convert the path to a standard format so we can find it later.
+                               driveName = String.Concat (Char.ToUpperInvariant (driveName [0]).ToString (), ":\\");
+                       }
+
+                       DriveInfo [] drives = GetDrives ();
                        foreach (DriveInfo d in drives){
                                if (d.path == driveName){
                                        this.path = d.path;
@@ -58,12 +66,6 @@ namespace System.IO {
                        throw new ArgumentException ("The drive name does not exist", "driveName");
                }
                
-               enum _DriveType {
-                       GenericUnix,
-                       Linux,
-                       Windows,
-               }
-
                static void GetDiskFreeSpace (string path, out ulong availableFreeSpace, out ulong totalSize, out ulong totalFreeSpace)
                {
                        MonoIOError error;
@@ -107,10 +109,7 @@ namespace System.IO {
                [MonoTODO ("Currently get only works on Mono/Unix; set not implemented")]
                public string VolumeLabel {
                        get {
-                               if (_drive_type != _DriveType.Windows)
-                                       return path;
-                               else
-                                       return path;
+                               return path;
                        }
                        set {
                                throw new NotImplementedException ();
@@ -144,111 +143,21 @@ namespace System.IO {
                [MonoTODO("It always returns true")]
                public bool IsReady {
                        get {
-                               if (_drive_type != _DriveType.Windows)
-                                       return true;
-
-                               // Do something for Windows here.
                                return true;
                        }
                }
                
-               static StreamReader TryOpen (string name)
-               {
-                       if (File.Exists (name))
-                               return new StreamReader (name);
-                       return null;
-               }
-
-               static char [] space = { ' ' };
-               static DriveInfo [] LinuxGetDrives ()
-               {
-                       using (StreamReader mounts = TryOpen ("/proc/mounts")){
-                               var drives = new List<DriveInfo> ();
-                               string line;
-                               
-                               while ((line = mounts.ReadLine ()) != null){
-                                       if (line.StartsWith ("rootfs"))
-                                               continue;
-                                       string [] parts = line.Split (space, 4);
-                                       if (parts.Length < 3)
-                                               continue;
-                                       string path = Unescape (parts [1]);
-                                       string fstype = parts [2];
-                                       drives.Add (new DriveInfo (_DriveType.Linux, path, fstype));
-                               }
-
-                               return drives.ToArray ();
-                       }
-               }
-
-               static string Unescape (string path)
-               {
-                       StringBuilder sb = null;
-                       int start = 0;
-                       do {
-                               int slash = path.IndexOf ('\\', start);
-                               if (slash >= 0) {
-                                       if (sb == null)
-                                               sb = new StringBuilder ();
-                                       sb.Append (path.Substring (start, slash - start));
-                                       char c = (char) ((path [slash + 1] - '0') << 6);
-                                       c += (char) ((path [slash + 2] - '0') << 3);
-                                       c += (char) (path [slash + 3] - '0');
-                                       sb.Append (c);
-                                       start = slash + 4;
-                                       continue;
-                               }
-                               if (start == 0)
-                                       return path;
-                               sb.Append (path.Substring (start));
-                               return sb.ToString ();
-                       } while (true);
-               }
-               
-               static DriveInfo [] UnixGetDrives ()
-               {
-                       DriveInfo [] di = null;
-
-                       try {
-                               using (StreamReader linux_ostype = TryOpen ("/proc/sys/kernel/ostype")){
-                                       if (linux_ostype != null){
-                                               string line = linux_ostype.ReadLine ();
-                                               if (line == "Linux")
-                                                       di = LinuxGetDrives ();
-                                       }
-                               }
-                               
-                               if (di != null)
-                                       return di;
-                       } catch (Exception) {
-                               // If anything happens.
-                       }
-                       
-                       DriveInfo [] unknown = new DriveInfo [1];
-                       unknown [0]= new DriveInfo (_DriveType.GenericUnix, "/", "unixfs");
-
-                       return unknown;
-               }
-
-               static DriveInfo [] WindowsGetDrives ()
+               [MonoTODO("In windows, alldrives are 'Fixed'")]
+               public static DriveInfo[] GetDrives ()
                {
-                       string [] drives = Environment.GetLogicalDrives ();
+                       var drives = Environment.GetLogicalDrives ();
                        DriveInfo [] infos = new DriveInfo [drives.Length];
                        int i = 0;
-                       foreach (string s in drives) {
-                               infos [i++] = new DriveInfo (_DriveType.Windows, s, "Fixed");
-                       }
+                       foreach (string s in drives)
+                               infos [i++] = new DriveInfo (s, GetDriveFormat (s));
+
                        return infos;
                }
-               
-               [MonoTODO("In windows, alldrives are 'Fixed'")]
-               public static DriveInfo[] GetDrives ()
-               {
-                       if (Environment.IsUnix)
-                               return UnixGetDrives ();
-                       else
-                               return WindowsGetDrives ();
-               }
 
                void ISerializable.GetObjectData (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
                {
@@ -267,5 +176,8 @@ namespace System.IO {
 
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
                extern static uint GetDriveTypeInternal (string rootPathName);
+
+               [MethodImplAttribute (MethodImplOptions.InternalCall)]
+               extern static string GetDriveFormat (string rootPathName);
        }
 }