2 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
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:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
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.
26 using System.Collections;
28 using System.Runtime.Serialization;
29 using System.Runtime.InteropServices;
32 [SerializableAttribute]
33 [ComVisibleAttribute(true)]
34 public sealed class DriveInfo : ISerializable {
35 _DriveType _drive_type;
40 DriveInfo (_DriveType _drive_type, string path, string fstype)
42 this._drive_type = _drive_type;
43 this.drive_format = fstype;
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){
56 this.drive_type = d.drive_type;
57 this.drive_format = d.drive_format;
59 this._drive_type = d._drive_type;
63 throw new ArgumentException ("The drive name does not exist", "driveName");
72 [MonoTODO("Always returns infinite")]
73 public long AvailableFreeSpace {
75 if (DriveType == DriveType.CDRom || DriveType == DriveType.Ram || DriveType == DriveType.Unknown)
77 return Int64.MaxValue;
81 [MonoTODO("Always returns infinite")]
82 public long TotalFreeSpace {
84 if (DriveType == DriveType.CDRom || DriveType == DriveType.Ram || DriveType == DriveType.Unknown)
86 return Int64.MaxValue;
90 [MonoTODO("Always returns infinite")]
91 public long TotalSize {
93 return Int64.MaxValue;
97 [MonoTODO ("Currently get only works on Mono/Unix; set not implemented")]
98 public string VolumeLabel {
100 if (_drive_type != _DriveType.Windows)
106 throw new NotImplementedException ();
110 public string DriveFormat {
116 static DriveType ToDriveType (_DriveType drive_type, string drive_format)
118 if (drive_type == _DriveType.Linux){
119 switch (drive_format){
122 return DriveType.Ram;
124 return DriveType.CDRom;
135 return DriveType.Fixed;
144 return DriveType.Network;
150 return DriveType.Ram;
152 return DriveType.Unknown;
155 return DriveType.Fixed;
159 public DriveType DriveType {
171 public DirectoryInfo RootDirectory {
173 return new DirectoryInfo (path);
177 [MonoTODO("It always returns true")]
178 public bool IsReady {
180 if (_drive_type != _DriveType.Windows)
183 // Do something for Windows here.
188 static StreamReader TryOpen (string name)
190 if (File.Exists (name))
191 return new StreamReader (name, Encoding.ASCII);
195 static DriveInfo [] LinuxGetDrives ()
197 using (StreamReader mounts = TryOpen ("/proc/mounts")){
198 ArrayList drives = new ArrayList ();
201 while ((line = mounts.ReadLine ()) != null){
202 if (line.StartsWith ("rootfs"))
206 p = line.IndexOf (' ');
209 string rest = line.Substring (p+1);
210 p = rest.IndexOf (' ');
213 string path = rest.Substring (0, p);
214 rest = rest.Substring (p+1);
215 p = rest.IndexOf (' ');
218 string fstype = rest.Substring (0, p);
219 drives.Add (new DriveInfo (_DriveType.Linux, path, fstype));
222 return (DriveInfo []) drives.ToArray (typeof (DriveInfo));
226 static DriveInfo [] UnixGetDrives ()
228 DriveInfo [] di = null;
231 using (StreamReader linux_ostype = TryOpen ("/proc/sys/kernel/ostype")){
232 Console.WriteLine ("here {0}", linux_ostype);
233 if (linux_ostype != null){
234 string line = linux_ostype.ReadLine ();
236 Console.WriteLine ("L: {0}", line);
238 di = LinuxGetDrives ();
244 } catch (Exception e) {
245 Console.WriteLine ("Got {0}", e);
246 // If anything happens.
249 DriveInfo [] unknown = new DriveInfo [1];
250 unknown [0]= new DriveInfo (_DriveType.GenericUnix, "/", "unixfs");
255 static DriveInfo [] WindowsGetDrives ()
257 throw new NotImplementedException ();
260 [MonoTODO("Currently only implemented on Mono/Linux")]
261 public static DriveInfo[] GetDrives ()
263 int platform = (int) Environment.Platform;
265 if (platform == 4 || platform == 128)
266 return UnixGetDrives ();
268 return WindowsGetDrives ();
271 void ISerializable.GetObjectData (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
273 throw new NotImplementedException ();
276 public override string ToString ()