copied mono-api-diff.cs from mono-2-2 branch so new patch can be applied and history...
[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.Permissions;
39
40 namespace System.IO {
41         
42         [Serializable]
43         [FileIOPermission (SecurityAction.InheritanceDemand, Unrestricted = true)]
44         [ComVisible (true)]
45 #if NET_2_1
46         public abstract class FileSystemInfo {
47 #else
48         public abstract class FileSystemInfo : MarshalByRefObject, ISerializable {
49
50                 #region Implementation of ISerializable
51
52                 [ComVisible(false)]
53                 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
54                 {
55                         info.AddValue ("OriginalPath", OriginalPath, typeof(string));
56                         info.AddValue ("FullPath", FullPath, typeof(string));
57                 }
58
59                 #endregion Implementation of ISerializable
60 #endif
61                 // public properties
62
63                 public abstract bool Exists { get; }
64
65                 public abstract string Name { get; }
66
67                 public virtual string FullName {
68                         get {
69                                 return FullPath;
70                         }
71                 }
72
73                 public string Extension {
74                         get {
75                                 return Path.GetExtension (Name);
76                         }
77                 }
78
79                 public FileAttributes Attributes {
80                         get {
81                                 Refresh (false);
82                                 return stat.Attributes;
83                         }
84
85                         set {
86                                 MonoIOError error;
87                                 
88                                 if (!MonoIO.SetFileAttributes (FullName,
89                                                                value,
90                                                                out error))
91                                         throw MonoIO.GetException (FullName,
92                                                                    error);
93                                 Refresh (true);
94                         }
95                 }
96
97                 public DateTime CreationTime {
98                         get {
99                                 Refresh (false);
100                                 return DateTime.FromFileTime (stat.CreationTime);
101                         }
102
103                         set {
104                                 long filetime = value.ToFileTime ();
105                         
106                                 MonoIOError error;
107                                 
108                                 if (!MonoIO.SetFileTime (FullName, filetime,
109                                                          -1, -1, out error))
110                                         throw MonoIO.GetException (FullName,
111                                                                    error);
112                                 Refresh (true);
113                         }
114                 }
115
116                 [ComVisible(false)]
117                 public DateTime CreationTimeUtc {
118                         get {
119                                 return CreationTime.ToUniversalTime ();
120                         }
121
122                         set {
123                                 CreationTime = value.ToLocalTime ();
124                         }
125                 }
126
127                 public DateTime LastAccessTime {
128                         get {
129                                 Refresh (false);
130                                 return DateTime.FromFileTime (stat.LastAccessTime);
131                         }
132
133                         set {
134                                 long filetime = value.ToFileTime ();
135
136                                 MonoIOError error;
137                                 
138                                 if (!MonoIO.SetFileTime (FullName, -1,
139                                                          filetime, -1,
140                                                          out error))
141                                         throw MonoIO.GetException (FullName,
142                                                                    error);
143                                 Refresh (true);
144                         }
145                 }
146
147                 [ComVisible(false)]
148                 public DateTime LastAccessTimeUtc {
149                         get {
150                                 Refresh (false);
151                                 return LastAccessTime.ToUniversalTime ();
152                         }
153
154                         set {
155                                 LastAccessTime = value.ToLocalTime ();
156                         }
157                 }
158
159                 public DateTime LastWriteTime {
160                         get {
161                                 Refresh (false);
162                                 return DateTime.FromFileTime (stat.LastWriteTime);
163                         }
164
165                         set {
166                                 long filetime = value.ToFileTime ();
167
168                                 MonoIOError error;
169                                 
170                                 if (!MonoIO.SetFileTime (FullName, -1, -1,
171                                                          filetime, out error))
172                                         throw MonoIO.GetException (FullName,
173                                                                    error);
174                                 Refresh (true);
175                         }
176                 }
177
178                 [ComVisible(false)]
179                 public DateTime LastWriteTimeUtc {
180                         get {
181                                 Refresh (false);
182                                 return LastWriteTime.ToUniversalTime ();
183                         }
184
185                         set {
186                                 LastWriteTime = value.ToLocalTime ();
187                         }
188                 }
189
190                 // public methods
191
192                 public abstract void Delete ();
193
194                 public void Refresh ()
195                 {
196                         Refresh (true);
197                 }
198
199                 // protected
200
201                 protected FileSystemInfo ()
202                 {
203                         this.valid = false;
204                         this.FullPath = null;
205                 }
206
207                 protected FileSystemInfo (SerializationInfo info, StreamingContext context)
208                 {
209                         if (info == null)
210                         {
211                                 throw new ArgumentNullException("info");
212                         }
213
214                         FullPath = info.GetString("FullPath");
215                         OriginalPath = info.GetString("OriginalPath");
216                 }
217
218                 protected string FullPath;
219                 protected string OriginalPath;
220
221                 // internal
222
223                 internal void Refresh (bool force)
224                 {
225                         if (valid && !force)
226                                 return;
227
228                         MonoIOError error;
229                         
230                         MonoIO.GetFileStat (FullName, out stat, out error);
231                         /* Don't throw on error here, too much other
232                          * stuff relies on it not doing so...
233                          */
234                         
235                         valid = true;
236                         
237                         InternalRefresh ();
238                 }
239                 
240                 internal virtual void InternalRefresh ()
241                 {
242                 }
243
244                 internal void CheckPath (string path)
245                 {
246                         if (path == null)
247                                 throw new ArgumentNullException ("path");
248                         if (path.Length == 0)
249                                 throw new ArgumentException ("An empty file name is not valid.");
250                         if (path.IndexOfAny (Path.InvalidPathChars) != -1)
251                                 throw new ArgumentException ("Illegal characters in path.");
252                 }
253
254                 internal MonoIOStat stat;
255                 internal bool valid;
256         }
257 }