Browse Source

Linux /proc/mounts might have escaped characters

	Translate escaped characters in mount paths.

	Fixes bug #637078.
Gonzalo Paniagua Javier 15 years ago
parent
commit
cdf1247873
1 changed files with 30 additions and 14 deletions
  1. 30 14
      mcs/class/corlib/System.IO/DriveInfo.cs

+ 30 - 14
mcs/class/corlib/System.IO/DriveInfo.cs

@@ -159,6 +159,7 @@ namespace System.IO {
 			return null;
 		}
 
+		static char [] space = { ' ' };
 		static DriveInfo [] LinuxGetDrives ()
 		{
 			using (StreamReader mounts = TryOpen ("/proc/mounts")){
@@ -168,27 +169,42 @@ namespace System.IO {
 				while ((line = mounts.ReadLine ()) != null){
 					if (line.StartsWith ("rootfs"))
 						continue;
-					int p;
-
-					p = line.IndexOf (' ');
-					if (p == -1)
-						continue;
-					string rest = line.Substring (p+1);
-					p = rest.IndexOf (' ');
-					if (p == -1)
+					string [] parts = line.Split (space, 4);
+					if (parts.Length < 3)
 						continue;
-					string path = rest.Substring (0, p);
-					rest = rest.Substring (p+1);
-					p = rest.IndexOf (' ');
-					if (p == -1)
-						continue;
-					string fstype = rest.Substring (0, p);
+					string path = Unescape (parts [1]);
+					string fstype = parts [2];
 					drives.Add (new DriveInfo (_DriveType.Linux, path, fstype));
 				}
 
 				return (DriveInfo []) drives.ToArray (typeof (DriveInfo));
 			}
 		}
+
+		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 ();
+					string str = path.Substring (start, slash - start);
+					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 ()
 		{