* Stdlib.cs: Cache delegates passed to Stdlib.signal() so that they survive
[mono.git] / mcs / class / Mono.Posix / Mono.Unix / UnixDriveInfo.cs
1 //
2 // Mono.Unix/UnixDriveInfo.cs
3 //
4 // Authors:
5 //   Jonathan Pryor (jonpryor@vt.edu)
6 //
7 // (C) 2004 Jonathan Pryor
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System;
30 using System.Collections;
31 using System.IO;
32 using Mono.Unix;
33
34 namespace Mono.Unix {
35
36         public enum UnixDriveType {
37                 Unknown,
38                 NoRootDirectory,
39                 Removable,
40                 Fixed,
41                 Network,
42                 CDRom,
43                 Ram
44         }
45
46         // All methods & properties can throw IOException
47         public sealed class UnixDriveInfo
48         {
49                 private Statvfs stat;
50                 private Fstab   fstab;
51
52                 public UnixDriveInfo (string mountPoint)
53                 {
54                         if (mountPoint == null)
55                                 throw new ArgumentNullException ("mountPoint");
56                         fstab = Syscall.getfsfile (mountPoint);
57                         if (fstab == null)
58                                 throw new ArgumentException ("mountPoint isn't valid: " + mountPoint);
59                         // throws ArgumentException if driveName isn't valid
60                         // though .NET also has a DriveNotFoundException class, so maybe that's
61                         // more appropriate?
62                 }
63
64                 public static UnixDriveInfo GetForSpecialFile (string specialFile)
65                 {
66                         if (specialFile == null)
67                                 throw new ArgumentNullException ("specialFile");
68                         Fstab f = Syscall.getfsspec (specialFile);
69                         if (f == null)
70                                 throw new ArgumentException ("specialFile isn't valid: " + specialFile);
71                         return new UnixDriveInfo (f);
72                 }
73
74                 private UnixDriveInfo (Fstab fstab)
75                 {
76                         this.fstab = fstab;
77                 }
78
79                 public long AvailableFreeSpace {
80                         get {Refresh (); return (long) (stat.f_bavail * stat.f_bsize);}
81                 }
82
83                 public string DriveFormat {
84                         get {return fstab.fs_vfstype;}
85                 }
86
87                 public UnixDriveType DriveType {
88                         get {return UnixDriveType.Unknown;}
89                 }
90
91                 // this throws no exceptions
92                 public bool IsReady {
93                         get {return Refresh (false);}
94                 }
95
96                 public string Name {
97                         get {return fstab.fs_file;}
98                 }
99
100                 public UnixDirectoryInfo RootDirectory {
101                         get {return new UnixDirectoryInfo (fstab.fs_file);}
102                 }
103
104                 public long TotalFreeSpace {
105                         get {Refresh (); return (long) (stat.f_bfree * stat.f_bsize);}
106                 }
107
108                 public long TotalSize {
109                         get {Refresh (); return (long) (stat.f_frsize * stat.f_blocks);}
110                 }
111
112                 // also throws SecurityException if caller lacks perms
113                 public string VolumeLabel {
114                         get {return fstab.fs_spec;}
115                         // set {}
116                 }
117
118                 public ulong MaximumFilenameLength {
119                         get {Refresh (); return stat.f_namemax;}
120                 }
121
122                 public static UnixDriveInfo[] GetDrives ()
123                 {
124                         // throws IOException, UnauthorizedAccessException (no permission)
125                         ArrayList entries = new ArrayList ();
126                         Syscall.SetLastError ((Error) 0);
127
128                         lock (Syscall.fstab_lock) {
129                                 int r = Syscall.setfsent ();
130                                 if (r != 1)
131                                         throw new IOException ("Error calling setfsent(3)", new UnixIOException ());
132                                 try {
133                                         Fstab fs;
134                                         while ((fs = Syscall.getfsent()) != null) {
135                                                 // avoid virtual entries, such as "swap"
136                                                 if (fs.fs_file.StartsWith ("/"))
137                                                         entries.Add (new UnixDriveInfo (fs));
138                                         }
139                                 }
140                                 finally {
141                                         Syscall.endfsent ();
142                                 }
143                         }
144                         return (UnixDriveInfo[]) entries.ToArray (typeof(UnixDriveInfo));
145                 }
146
147                 public override string ToString ()
148                 {
149                         return VolumeLabel;
150                 }
151
152                 private void Refresh ()
153                 {
154                         Refresh (true);
155                 }
156
157                 private bool Refresh (bool throwException)
158                 {
159                         int r = Syscall.statvfs (fstab.fs_file, out stat);
160                         if (r == -1 && throwException) {
161                                 Error e = Syscall.GetLastError ();
162                                 throw new IOException (UnixMarshal.GetErrorDescription (e),
163                                         UnixMarshal.CreateExceptionForError (e));
164                         }
165                         else if (r == -1)
166                                 return false;
167                         return true;
168                 }
169         }
170 }
171
172 // vim: noexpandtab