Merge pull request #168 from tomaszkubacki/master
[mono.git] / mcs / class / corlib / System.IO / FileSystemInfo.cs
1 //------------------------------------------------------------------------------
2 // 
3 // System.IO.FileSystemInfo.cs 
4 //
5 // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
6 // 
7 // Author:         Jim Richardson, develop@wtfo-guru.com
8 //                 Dan Lewis (dihlewis@yahoo.co.uk)
9 // Created:        Monday, August 13, 2001 
10 //
11 //------------------------------------------------------------------------------
12
13 //
14 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
15 //
16 // Permission is hereby granted, free of charge, to any person obtaining
17 // a copy of this software and associated documentation files (the
18 // "Software"), to deal in the Software without restriction, including
19 // without limitation the rights to use, copy, modify, merge, publish,
20 // distribute, sublicense, and/or sell copies of the Software, and to
21 // permit persons to whom the Software is furnished to do so, subject to
22 // the following conditions:
23 // 
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
26 // 
27 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 //
35
36 using System.Runtime.InteropServices;
37 using System.Runtime.Serialization;
38 using System.Security;
39 using System.Security.Permissions;
40
41 namespace System.IO {
42         
43         [Serializable]
44         [FileIOPermission (SecurityAction.InheritanceDemand, Unrestricted = true)]
45         [ComVisible (true)]
46 #if NET_2_1
47         public abstract class FileSystemInfo {
48 #else
49         public abstract class FileSystemInfo : MarshalByRefObject, ISerializable {
50
51                 #region Implementation of ISerializable
52
53                 [ComVisible(false)]
54                 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
55                 {
56                         info.AddValue ("OriginalPath", OriginalPath, typeof(string));
57                         info.AddValue ("FullPath", FullPath, typeof(string));
58                 }
59
60                 #endregion Implementation of ISerializable
61 #endif
62                 // public properties
63
64                 public abstract bool Exists { get; }
65
66                 public abstract string Name { get; }
67
68                 public virtual string FullName {
69                         get {
70                                 return FullPath;
71                         }
72                 }
73
74                 public string Extension {
75                         get {
76                                 return Path.GetExtension (Name);
77                         }
78                 }
79
80                 public FileAttributes Attributes {
81                         get {
82                                 Refresh (false);
83                                 return stat.Attributes;
84                         }
85
86                         set {
87                                 MonoIOError error;
88                                 
89                                 if (!MonoIO.SetFileAttributes (FullName,
90                                                                value,
91                                                                out error))
92                                         throw MonoIO.GetException (FullName,
93                                                                    error);
94                                 Refresh (true);
95                         }
96                 }
97
98                 public DateTime CreationTime {
99                         get {
100                                 Refresh (false);
101                                 return DateTime.FromFileTime (stat.CreationTime);
102                         }
103
104                         set {
105                                 SecurityManager.EnsureElevatedPermissions (); // this is a no-op outside moonlight
106
107                                 long filetime = value.ToFileTime ();
108                         
109                                 MonoIOError error;
110                                 
111                                 if (!MonoIO.SetFileTime (FullName, filetime,
112                                                          -1, -1, out error))
113                                         throw MonoIO.GetException (FullName,
114                                                                    error);
115                                 Refresh (true);
116                         }
117                 }
118
119                 [ComVisible(false)]
120                 public DateTime CreationTimeUtc {
121                         get {
122                                 return CreationTime.ToUniversalTime ();
123                         }
124
125                         set {
126                                 CreationTime = value.ToLocalTime ();
127                         }
128                 }
129
130                 public DateTime LastAccessTime {
131                         get {
132                                 Refresh (false);
133                                 return DateTime.FromFileTime (stat.LastAccessTime);
134                         }
135
136                         set {
137                                 SecurityManager.EnsureElevatedPermissions (); // this is a no-op outside moonlight
138
139                                 long filetime = value.ToFileTime ();
140
141                                 MonoIOError error;
142                                 
143                                 if (!MonoIO.SetFileTime (FullName, -1,
144                                                          filetime, -1,
145                                                          out error))
146                                         throw MonoIO.GetException (FullName,
147                                                                    error);
148                                 Refresh (true);
149                         }
150                 }
151
152                 [ComVisible(false)]
153                 public DateTime LastAccessTimeUtc {
154                         get {
155                                 Refresh (false);
156                                 return LastAccessTime.ToUniversalTime ();
157                         }
158
159                         set {
160                                 LastAccessTime = value.ToLocalTime ();
161                         }
162                 }
163
164                 public DateTime LastWriteTime {
165                         get {
166                                 Refresh (false);
167                                 return DateTime.FromFileTime (stat.LastWriteTime);
168                         }
169
170                         set {
171                                 SecurityManager.EnsureElevatedPermissions (); // this is a no-op outside moonlight
172
173                                 long filetime = value.ToFileTime ();
174
175                                 MonoIOError error;
176                                 
177                                 if (!MonoIO.SetFileTime (FullName, -1, -1,
178                                                          filetime, out error))
179                                         throw MonoIO.GetException (FullName,
180                                                                    error);
181                                 Refresh (true);
182                         }
183                 }
184
185                 [ComVisible(false)]
186                 public DateTime LastWriteTimeUtc {
187                         get {
188                                 Refresh (false);
189                                 return LastWriteTime.ToUniversalTime ();
190                         }
191
192                         set {
193                                 LastWriteTime = value.ToLocalTime ();
194                         }
195                 }
196
197                 // public methods
198
199                 public abstract void Delete ();
200
201                 public void Refresh ()
202                 {
203                         Refresh (true);
204                 }
205
206                 // protected
207
208                 protected FileSystemInfo ()
209                 {
210                         this.valid = false;
211                         this.FullPath = null;
212                 }
213
214                 protected FileSystemInfo (SerializationInfo info, StreamingContext context)
215                 {
216                         if (info == null)
217                         {
218                                 throw new ArgumentNullException("info");
219                         }
220
221                         FullPath = info.GetString("FullPath");
222                         OriginalPath = info.GetString("OriginalPath");
223                 }
224
225                 protected string FullPath;
226                 protected string OriginalPath;
227
228                 // internal
229
230                 internal void Refresh (bool force)
231                 {
232                         if (valid && !force)
233                                 return;
234
235                         MonoIOError error;
236                         
237                         MonoIO.GetFileStat (FullName, out stat, out error);
238                         /* Don't throw on error here, too much other
239                          * stuff relies on it not doing so...
240                          */
241                         
242                         valid = true;
243                         
244                         InternalRefresh ();
245                 }
246                 
247                 internal virtual void InternalRefresh ()
248                 {
249                 }
250
251                 internal void CheckPath (string path)
252                 {
253                         if (path == null)
254                                 throw new ArgumentNullException ("path");
255                         if (path.Length == 0)
256                                 throw new ArgumentException ("An empty file name is not valid.");
257                         if (path.IndexOfAny (Path.InvalidPathChars) != -1)
258                                 throw new ArgumentException ("Illegal characters in path.");
259                         if (Environment.IsRunningOnWindows) {
260                                 int idx = path.IndexOf (':');
261                                 if (idx >= 0 && idx != 1)
262                                         throw new ArgumentException ("path");
263                         }
264                 }
265
266                 internal MonoIOStat stat;
267                 internal bool valid;
268         }
269 }