2006-11-26 Miguel de Icaza <miguel@novell.com>
[mono-project.git] / mcs / class / corlib / System.IO / DriveInfo.cs
blobd421ea4254be738a5b1768cb3d712adb2c3b0c8e
1 //
2 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 #if NET_2_0
25 using System;
26 using System.Collections;
27 using System.Text;
28 using System.Runtime.Serialization;
29 using System.Runtime.InteropServices;
31 namespace System.IO {
32 [SerializableAttribute]
33 [ComVisibleAttribute(true)]
34 public sealed class DriveInfo : ISerializable {
35 _DriveType _drive_type;
36 DriveType drive_type;
37 string drive_format;
38 string path;
40 DriveInfo (_DriveType _drive_type, string path, string fstype)
42 this._drive_type = _drive_type;
43 this.drive_format = fstype;
44 this.path = path;
46 this.drive_type = ToDriveType (_drive_type, fstype);
49 public DriveInfo (string driveName)
51 DriveInfo [] drives = GetDrives ();
53 foreach (DriveInfo d in drives){
54 if (d.path == driveName){
55 this.path = d.path;
56 this.drive_type = d.drive_type;
57 this.drive_format = d.drive_format;
58 this.path = d.path;
59 this._drive_type = d._drive_type;
60 return;
63 throw new ArgumentException ("The drive name does not exist", "driveName");
66 enum _DriveType {
67 GenericUnix,
68 Linux,
69 Windows,
72 [MonoTODO("Always returns infinite")]
73 public long AvailableFreeSpace {
74 get {
75 if (DriveType == DriveType.CDRom || DriveType == DriveType.Ram || DriveType == DriveType.Unknown)
76 return 0;
77 return Int64.MaxValue;
81 [MonoTODO("Always returns infinite")]
82 public long TotalFreeSpace {
83 get {
84 if (DriveType == DriveType.CDRom || DriveType == DriveType.Ram || DriveType == DriveType.Unknown)
85 return 0;
86 return Int64.MaxValue;
90 [MonoTODO("Always returns infinite")]
91 public long TotalSize {
92 get {
93 return Int64.MaxValue;
97 [MonoTODO ("Currently only works on Mono/Unix")]
98 public string VolumeLabel {
99 get {
100 if (_drive_type != _DriveType.Windows)
101 return path;
102 else
103 return path;
107 public string DriveFormat {
108 get {
109 return drive_format;
113 static DriveType ToDriveType (_DriveType drive_type, string drive_format)
115 if (drive_type == _DriveType.Linux){
116 switch (drive_format){
117 case "tmpfs":
118 case "ramfs":
119 return DriveType.Ram;
120 case "iso9660":
121 return DriveType.CDRom;
122 case "ext2":
123 case "ext3":
124 case "sysv":
125 case "reiserfs":
126 case "ufs":
127 case "vfat":
128 case "udf":
129 case "hfs":
130 case "hpfs":
131 case "qnx4":
132 return DriveType.Fixed;
133 case "smbfs":
134 case "fuse":
135 case "nfs":
136 case "nfs4":
137 case "cifs":
138 case "ncpfs":
139 case "coda":
140 case "afs":
141 return DriveType.Network;
142 case "proc":
143 case "sysfs":
144 case "debugfs":
145 case "devpts":
146 case "securityfs":
147 return DriveType.Ram;
148 default:
149 return DriveType.Unknown;
151 } else {
152 return DriveType.Fixed;
156 public DriveType DriveType {
157 get {
158 return drive_type;
162 public string Name {
163 get {
164 return path;
168 public DirectoryInfo RootDirectory {
169 get {
170 return new DirectoryInfo (path);
174 [MonoTODO("It always returns true")]
175 public bool IsReady {
176 get {
177 if (_drive_type != _DriveType.Windows)
178 return true;
180 // Do something for Windows here.
181 return true;
185 static StreamReader TryOpen (string name)
187 if (File.Exists (name))
188 return new StreamReader (name, Encoding.ASCII);
189 return null;
192 static DriveInfo [] LinuxGetDrives ()
194 using (StreamReader mounts = TryOpen ("/proc/mounts")){
195 ArrayList drives = new ArrayList ();
196 string line;
198 while ((line = mounts.ReadLine ()) != null){
199 if (line.StartsWith ("rootfs"))
200 continue;
201 int p;
203 p = line.IndexOf (' ');
204 if (p == -1)
205 continue;
206 string rest = line.Substring (p+1);
207 p = rest.IndexOf (' ');
208 if (p == -1)
209 continue;
210 string path = rest.Substring (0, p);
211 rest = rest.Substring (p+1);
212 p = rest.IndexOf (' ');
213 if (p == -1)
214 continue;
215 string fstype = rest.Substring (0, p);
216 drives.Add (new DriveInfo (_DriveType.Linux, path, fstype));
219 return (DriveInfo []) drives.ToArray (typeof (DriveInfo));
223 static DriveInfo [] UnixGetDrives ()
225 DriveInfo [] di = null;
227 try {
228 using (StreamReader linux_ostype = TryOpen ("/proc/sys/kernel/ostype")){
229 Console.WriteLine ("here {0}", linux_ostype);
230 if (linux_ostype != null){
231 string line = linux_ostype.ReadLine ();
233 Console.WriteLine ("L: {0}", line);
234 if (line == "Linux")
235 di = LinuxGetDrives ();
239 if (di != null)
240 return di;
241 } catch (Exception e) {
242 Console.WriteLine ("Got {0}", e);
243 // If anything happens.
246 DriveInfo [] unknown = new DriveInfo [1];
247 unknown [0]= new DriveInfo (_DriveType.GenericUnix, "/", "unixfs");
249 return unknown;
252 static DriveInfo [] WindowsGetDrives ()
254 throw new NotImplementedException ();
257 [MonoTODO("Currently only implemented on Mono/Linux")]
258 public static DriveInfo[] GetDrives ()
260 int platform = (int) Environment.Platform;
262 if (platform == 4 || platform == 128)
263 return UnixGetDrives ();
264 else
265 return WindowsGetDrives ();
268 public void GetObjectData (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
270 throw new NotImplementedException ();
275 #endif