* corlib_test.dll.sources: added Consts.cs.
[mono.git] / mcs / class / corlib / System.IO / DriveInfo.cs
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.
22 //
23
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;
30
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;
39
40                 DriveInfo (_DriveType _drive_type, string path, string fstype)
41                 {
42                         this._drive_type = _drive_type;
43                         this.drive_format = fstype;
44                         this.path = path;
45
46                         this.drive_type = ToDriveType (_drive_type, fstype);
47                 }
48
49                 public DriveInfo (string driveName)
50                 {
51                         DriveInfo [] drives = GetDrives ();
52
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;
61                                 }
62                         }
63                         throw new ArgumentException ("The drive name does not exist", "driveName");
64                 }
65                 
66                 enum _DriveType {
67                         GenericUnix,
68                         Linux,
69                         Windows,
70                 }
71                 
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;
78                         }
79                 }
80
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;
87                         }
88                 }
89
90                 [MonoTODO("Always returns infinite")]
91                 public long TotalSize {
92                         get {
93                                 return Int64.MaxValue;
94                         }
95                 }
96
97                 [MonoTODO ("Currently get only works on Mono/Unix; set not implemented")]
98                 public string VolumeLabel {
99                         get {
100                                 if (_drive_type != _DriveType.Windows)
101                                         return path;
102                                 else
103                                         return path;
104                         }
105                         set {
106                                 throw new NotImplementedException ();
107                         }
108                 }
109                 
110                 public string DriveFormat {
111                         get {
112                                 return drive_format;
113                         }
114                 }
115
116                 static DriveType ToDriveType (_DriveType drive_type, string drive_format)
117                 {
118                         if (drive_type == _DriveType.Linux){
119                                 switch (drive_format){
120                                 case "tmpfs":
121                                 case "ramfs":
122                                         return DriveType.Ram;
123                                 case "iso9660":
124                                         return DriveType.CDRom;
125                                 case "ext2":
126                                 case "ext3":
127                                 case "sysv":
128                                 case "reiserfs":
129                                 case "ufs":
130                                 case "vfat":
131                                 case "udf":
132                                 case "hfs":
133                                 case "hpfs":
134                                 case "qnx4":
135                                         return DriveType.Fixed;
136                                 case "smbfs":
137                                 case "fuse":
138                                 case "nfs":
139                                 case "nfs4":
140                                 case "cifs":
141                                 case "ncpfs":
142                                 case "coda":
143                                 case "afs":
144                                         return DriveType.Network;
145                                 case "proc":
146                                 case "sysfs":
147                                 case "debugfs":
148                                 case "devpts":
149                                 case "securityfs":
150                                         return DriveType.Ram;
151                                 default:
152                                         return DriveType.Unknown;
153                                 }
154                         } else {
155                                 return DriveType.Fixed;
156                         }
157                 }
158                 
159                 public DriveType DriveType {
160                         get {
161                                 return drive_type;
162                         }
163                 }
164
165                 public string Name {
166                         get {
167                                 return path;
168                         }
169                 }
170
171                 public DirectoryInfo RootDirectory {
172                         get {
173                                 return new DirectoryInfo (path);
174                         }
175                 }
176
177                 [MonoTODO("It always returns true")]
178                 public bool IsReady {
179                         get {
180                                 if (_drive_type != _DriveType.Windows)
181                                         return true;
182
183                                 // Do something for Windows here.
184                                 return true;
185                         }
186                 }
187                 
188                 static StreamReader TryOpen (string name)
189                 {
190                         if (File.Exists (name))
191                                 return new StreamReader (name, Encoding.ASCII);
192                         return null;
193                 }
194
195                 static DriveInfo [] LinuxGetDrives ()
196                 {
197                         using (StreamReader mounts = TryOpen ("/proc/mounts")){
198                                 ArrayList drives = new ArrayList ();
199                                 string line;
200                                 
201                                 while ((line = mounts.ReadLine ()) != null){
202                                         if (line.StartsWith ("rootfs"))
203                                                 continue;
204                                         int p;
205
206                                         p = line.IndexOf (' ');
207                                         if (p == -1)
208                                                 continue;
209                                         string rest = line.Substring (p+1);
210                                         p = rest.IndexOf (' ');
211                                         if (p == -1)
212                                                 continue;
213                                         string path = rest.Substring (0, p);
214                                         rest = rest.Substring (p+1);
215                                         p = rest.IndexOf (' ');
216                                         if (p == -1)
217                                                 continue;
218                                         string fstype = rest.Substring (0, p);
219                                         drives.Add (new DriveInfo (_DriveType.Linux, path, fstype));
220                                 }
221
222                                 return (DriveInfo []) drives.ToArray (typeof (DriveInfo));
223                         }
224                 }
225                 
226                 static DriveInfo [] UnixGetDrives ()
227                 {
228                         DriveInfo [] di = null;
229
230                         try {
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 ();
235
236                                                 Console.WriteLine ("L: {0}", line);
237                                                 if (line == "Linux")
238                                                         di = LinuxGetDrives ();
239                                         }
240                                 }
241                                 
242                                 if (di != null)
243                                         return di;
244                         } catch (Exception e) {
245                                 Console.WriteLine ("Got {0}", e);
246                                 // If anything happens.
247                         }
248                         
249                         DriveInfo [] unknown = new DriveInfo [1];
250                         unknown [0]= new DriveInfo (_DriveType.GenericUnix, "/", "unixfs");
251
252                         return unknown;
253                 }
254
255                 static DriveInfo [] WindowsGetDrives ()
256                 {
257                         throw new NotImplementedException ();
258                 }
259                 
260                 [MonoTODO("Currently only implemented on Mono/Linux")]
261                 public static DriveInfo[] GetDrives ()
262                 {
263                         int platform = (int) Environment.Platform;
264
265                         if (platform == 4 || platform == 128)
266                                 return UnixGetDrives ();
267                         else
268                                 return WindowsGetDrives ();
269                 }
270
271                 void ISerializable.GetObjectData (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
272                 {
273                         throw new NotImplementedException ();
274                 }
275
276                 public override string ToString ()
277                 {
278                         return(Name);
279                 }
280         }
281 }
282
283 #endif