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 only works on Mono/Unix")]
98 public string VolumeLabel
{
100 if (_drive_type
!= _DriveType
.Windows
)
107 public string DriveFormat
{
113 static DriveType
ToDriveType (_DriveType drive_type
, string drive_format
)
115 if (drive_type
== _DriveType
.Linux
){
116 switch (drive_format
){
119 return DriveType
.Ram
;
121 return DriveType
.CDRom
;
132 return DriveType
.Fixed
;
141 return DriveType
.Network
;
147 return DriveType
.Ram
;
149 return DriveType
.Unknown
;
152 return DriveType
.Fixed
;
156 public DriveType DriveType
{
168 public DirectoryInfo RootDirectory
{
170 return new DirectoryInfo (path
);
174 [MonoTODO("It always returns true")]
175 public bool IsReady
{
177 if (_drive_type
!= _DriveType
.Windows
)
180 // Do something for Windows here.
185 static StreamReader
TryOpen (string name
)
187 if (File
.Exists (name
))
188 return new StreamReader (name
, Encoding
.ASCII
);
192 static DriveInfo
[] LinuxGetDrives ()
194 using (StreamReader mounts
= TryOpen ("/proc/mounts")){
195 ArrayList drives
= new ArrayList ();
198 while ((line
= mounts
.ReadLine ()) != null){
199 if (line
.StartsWith ("rootfs"))
203 p
= line
.IndexOf (' ');
206 string rest
= line
.Substring (p
+1);
207 p
= rest
.IndexOf (' ');
210 string path
= rest
.Substring (0, p
);
211 rest
= rest
.Substring (p
+1);
212 p
= rest
.IndexOf (' ');
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;
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
);
235 di
= LinuxGetDrives ();
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");
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 ();
265 return WindowsGetDrives ();
268 public void GetObjectData (System
.Runtime
.Serialization
.SerializationInfo info
, System
.Runtime
.Serialization
.StreamingContext context
)
270 throw new NotImplementedException ();