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