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);
162 static char [] space = { ' ' };
163 static DriveInfo [] LinuxGetDrives ()
165 using (StreamReader mounts = TryOpen ("/proc/mounts")){
166 ArrayList drives = new ArrayList ();
169 while ((line = mounts.ReadLine ()) != null){
170 if (line.StartsWith ("rootfs"))
172 string [] parts = line.Split (space, 4);
173 if (parts.Length < 3)
175 string path = Unescape (parts [1]);
176 string fstype = parts [2];
177 drives.Add (new DriveInfo (_DriveType.Linux, path, fstype));
180 return (DriveInfo []) drives.ToArray (typeof (DriveInfo));
184 static string Unescape (string path)
186 StringBuilder sb = null;
189 int slash = path.IndexOf ('\\', start);
192 sb = new StringBuilder ();
193 string str = path.Substring (start, slash - start);
194 sb.Append (path.Substring (start, slash - start));
195 char c = (char) ((path [slash + 1] - '0') << 6);
196 c += (char) ((path [slash + 2] - '0') << 3);
197 c += (char) (path [slash + 3] - '0');
204 sb.Append (path.Substring (start));
205 return sb.ToString ();
209 static DriveInfo [] UnixGetDrives ()
211 DriveInfo [] di = null;
214 using (StreamReader linux_ostype = TryOpen ("/proc/sys/kernel/ostype")){
215 if (linux_ostype != null){
216 string line = linux_ostype.ReadLine ();
218 di = LinuxGetDrives ();
224 } catch (Exception) {
225 // If anything happens.
228 DriveInfo [] unknown = new DriveInfo [1];
229 unknown [0]= new DriveInfo (_DriveType.GenericUnix, "/", "unixfs");
234 static DriveInfo [] WindowsGetDrives ()
236 throw new NotImplementedException ();
239 [MonoTODO("Currently only implemented on Mono/Linux")]
240 public static DriveInfo[] GetDrives ()
242 if (Environment.IsUnix)
243 return UnixGetDrives ();
245 return WindowsGetDrives ();
248 void ISerializable.GetObjectData (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
250 throw new NotImplementedException ();
253 public override string ToString ()
258 [MethodImplAttribute (MethodImplOptions.InternalCall)]
259 extern static bool GetDiskFreeSpaceInternal (string pathName, out ulong freeBytesAvail,
260 out ulong totalNumberOfBytes, out ulong totalNumberOfFreeBytes,
261 out MonoIOError error);
263 [MethodImplAttribute (MethodImplOptions.InternalCall)]
264 extern static uint GetDriveTypeInternal (string rootPathName);