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