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.
25 using System.Collections;
27 using System.Runtime.Serialization;
28 using System.Runtime.CompilerServices;
29 using System.Runtime.InteropServices;
32 [SerializableAttribute]
33 [ComVisibleAttribute(true)]
34 public sealed class DriveInfo : ISerializable {
35 _DriveType _drive_type;
39 DriveInfo (_DriveType _drive_type, string path, string fstype)
41 this._drive_type = _drive_type;
42 this.drive_format = fstype;
46 public DriveInfo (string driveName)
48 DriveInfo [] drives = GetDrives ();
50 foreach (DriveInfo d in drives){
51 if (d.path == driveName){
53 this.drive_format = d.drive_format;
58 throw new ArgumentException ("The drive name does not exist", "driveName");
67 static void GetDiskFreeSpace (string path, out ulong availableFreeSpace, out ulong totalSize, out ulong totalFreeSpace)
70 if (!GetDiskFreeSpaceInternal (path, out availableFreeSpace, out totalSize, out totalFreeSpace, out error))
71 throw MonoIO.GetException (path, error);
74 public long AvailableFreeSpace {
76 ulong availableFreeSpace;
80 GetDiskFreeSpace (path, out availableFreeSpace, out totalSize, out totalFreeSpace);
81 return availableFreeSpace > long.MaxValue ? long.MaxValue : (long) availableFreeSpace;
85 public long TotalFreeSpace {
87 ulong availableFreeSpace;
91 GetDiskFreeSpace (path, out availableFreeSpace, out totalSize, out totalFreeSpace);
92 return totalFreeSpace > long.MaxValue ? long.MaxValue : (long) totalFreeSpace;
96 public long TotalSize {
98 ulong availableFreeSpace;
100 ulong totalFreeSpace;
102 GetDiskFreeSpace (path, out availableFreeSpace, out totalSize, out totalFreeSpace);
103 return totalSize > long.MaxValue ? long.MaxValue : (long) totalSize;
107 [MonoTODO ("Currently get only works on Mono/Unix; set not implemented")]
108 public string VolumeLabel {
110 if (_drive_type != _DriveType.Windows)
116 throw new NotImplementedException ();
120 public string DriveFormat {
126 public DriveType DriveType {
128 return (DriveType) GetDriveTypeInternal (path);
138 public DirectoryInfo RootDirectory {
140 return new DirectoryInfo (path);
144 [MonoTODO("It always returns true")]
145 public bool IsReady {
147 if (_drive_type != _DriveType.Windows)
150 // Do something for Windows here.
155 static StreamReader TryOpen (string name)
157 if (File.Exists (name))
158 return new StreamReader (name, Encoding.ASCII);
162 static DriveInfo [] LinuxGetDrives ()
164 using (StreamReader mounts = TryOpen ("/proc/mounts")){
165 ArrayList drives = new ArrayList ();
168 while ((line = mounts.ReadLine ()) != null){
169 if (line.StartsWith ("rootfs"))
173 p = line.IndexOf (' ');
176 string rest = line.Substring (p+1);
177 p = rest.IndexOf (' ');
180 string path = rest.Substring (0, p);
181 rest = rest.Substring (p+1);
182 p = rest.IndexOf (' ');
185 string fstype = rest.Substring (0, p);
186 drives.Add (new DriveInfo (_DriveType.Linux, path, fstype));
189 return (DriveInfo []) drives.ToArray (typeof (DriveInfo));
193 static DriveInfo [] UnixGetDrives ()
195 DriveInfo [] di = null;
198 using (StreamReader linux_ostype = TryOpen ("/proc/sys/kernel/ostype")){
199 if (linux_ostype != null){
200 string line = linux_ostype.ReadLine ();
202 di = LinuxGetDrives ();
208 } catch (Exception) {
209 // If anything happens.
212 DriveInfo [] unknown = new DriveInfo [1];
213 unknown [0]= new DriveInfo (_DriveType.GenericUnix, "/", "unixfs");
218 static DriveInfo [] WindowsGetDrives ()
220 throw new NotImplementedException ();
223 [MonoTODO("Currently only implemented on Mono/Linux")]
224 public static DriveInfo[] GetDrives ()
226 if (Environment.IsUnix)
227 return UnixGetDrives ();
229 return WindowsGetDrives ();
232 void ISerializable.GetObjectData (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
234 throw new NotImplementedException ();
237 public override string ToString ()
242 [MethodImplAttribute (MethodImplOptions.InternalCall)]
243 extern static bool GetDiskFreeSpaceInternal (string pathName, out ulong freeBytesAvail,
244 out ulong totalNumberOfBytes, out ulong totalNumberOfFreeBytes,
245 out MonoIOError error);
247 [MethodImplAttribute (MethodImplOptions.InternalCall)]
248 extern static uint GetDriveTypeInternal (string rootPathName);