More 4.0 work
[mono.git] / mcs / class / corlib / System.IO / FileInfo.cs
1 //------------------------------------------------------------------------------
2 // 
3 // System.IO.FileInfo.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, 2006 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
39 #if !NET_2_1
40 using System.Security.AccessControl;
41 #endif
42
43 namespace System.IO {
44
45         [Serializable]
46         [ComVisible (true)]
47         public sealed class FileInfo : FileSystemInfo
48         {
49                 private bool exists;
50
51                 public FileInfo (string fileName)
52                 {
53                         if (fileName == null)
54                                 throw new ArgumentNullException ("fileName");
55
56                         CheckPath (fileName);
57
58                         OriginalPath = fileName;
59                         FullPath = Path.GetFullPath (fileName);
60                 }
61
62                 private FileInfo (SerializationInfo info, StreamingContext context)
63                         : base (info, context)
64                 {
65                 }
66
67                 internal override void InternalRefresh ()
68                 {
69                         exists = File.Exists (FullPath);
70                 }
71
72                 // public properties
73
74                 public override bool Exists {
75                         get {
76                                 Refresh (false);
77
78                                 if (stat.Attributes == MonoIO.InvalidFileAttributes)
79                                         return false;
80
81                                 if ((stat.Attributes & FileAttributes.Directory) != 0)
82                                         return false;
83
84                                 return exists;
85                         }
86                 }
87
88                 public override string Name {
89                         get {
90                                 return Path.GetFileName (FullPath);
91                         }
92                 }
93
94 #if !NET_2_1
95                 public bool IsReadOnly {
96                         get {
97                                 if (!Exists)
98                                         throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".", OriginalPath);
99                                         
100                                 return ((stat.Attributes & FileAttributes.ReadOnly) != 0);
101                         }
102                                 
103                         set {
104                                 if (!Exists)
105                                         throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".", OriginalPath);
106                                         
107                                 FileAttributes attrs = File.GetAttributes(FullPath);
108
109                                 if (value) 
110                                         attrs |= FileAttributes.ReadOnly;
111                                 else
112                                         attrs &= ~FileAttributes.ReadOnly;
113
114                                 File.SetAttributes(FullPath, attrs);
115                         }
116                 }
117
118                 [MonoLimitation ("File encryption isn't supported (even on NTFS).")]
119                 [ComVisible (false)]
120                 public void Encrypt ()
121                 {
122                         // MS.NET support this only on NTFS file systems, i.e. it's a file-system (not a framework) feature.
123                         // otherwise it throws a NotSupportedException (or a PlatformNotSupportedException on older OS).
124                         // we throw the same (instead of a NotImplementedException) because most code should already be
125                         // handling this exception to work properly.
126                         throw new NotSupportedException (Locale.GetText ("File encryption isn't supported on any file system."));
127                 }
128
129                 [MonoLimitation ("File encryption isn't supported (even on NTFS).")]
130                 [ComVisible (false)]
131                 public void Decrypt ()
132                 {
133                         // MS.NET support this only on NTFS file systems, i.e. it's a file-system (not a framework) feature.
134                         // otherwise it throws a NotSupportedException (or a PlatformNotSupportedException on older OS).
135                         // we throw the same (instead of a NotImplementedException) because most code should already be
136                         // handling this exception to work properly.
137                         throw new NotSupportedException (Locale.GetText ("File encryption isn't supported on any file system."));
138                 }
139 #endif
140
141                 public long Length {
142                         get {
143                                 if (!Exists)
144                                         throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".", OriginalPath);
145
146                                 return stat.Length;
147                         }
148                 }
149
150                 public string DirectoryName {
151                         get {
152                                 return Path.GetDirectoryName (FullPath);
153                         }
154                 }
155
156                 public DirectoryInfo Directory {
157                         get {
158                                 return new DirectoryInfo (DirectoryName);
159                         }
160                 }
161
162                 // streamreader methods
163
164                 public StreamReader OpenText ()
165                 {
166                         return new StreamReader (Open (FileMode.Open, FileAccess.Read));
167                 }
168
169                 public StreamWriter CreateText ()
170                 {
171                         return new StreamWriter (Open (FileMode.Create, FileAccess.Write));
172                 }
173                 
174                 public StreamWriter AppendText ()
175                 {
176                         return new StreamWriter (Open (FileMode.Append, FileAccess.Write));
177                 }
178
179                 // filestream methods
180
181                 public FileStream Create ()
182                 {
183                         return File.Create (FullPath);
184                 }
185
186                 public FileStream OpenRead ()
187                 {
188                         return Open (FileMode.Open, FileAccess.Read, FileShare.Read);
189                 }
190
191                 public FileStream OpenWrite ()
192                 {
193                         return Open (FileMode.OpenOrCreate, FileAccess.Write);
194                 }
195
196                 public FileStream Open (FileMode mode)
197                 {
198                         return Open (mode, FileAccess.ReadWrite);
199                 }
200
201                 public FileStream Open (FileMode mode, FileAccess access)
202                 {
203                         return Open (mode, access, FileShare.None);
204                 }
205
206                 public FileStream Open (FileMode mode, FileAccess access, FileShare share)
207                 {
208                         return new FileStream (FullPath, mode, access, share);
209                 }
210
211                 // file methods
212
213                 public override void Delete ()
214                 {
215                         MonoIOError error;
216
217                         if (!MonoIO.Exists (FullPath, out error))
218                                 // a weird MS.NET behaviour
219                                 return;
220
221                         if (MonoIO.ExistsDirectory (FullPath, out error))
222                                 throw new UnauthorizedAccessException ("Access to the path \"" + FullPath + "\" is denied.");
223                         if (!MonoIO.DeleteFile (FullPath, out error))
224                                 throw MonoIO.GetException (OriginalPath, error);
225                 }
226
227                 public void MoveTo (string destFileName)
228                 {
229                         if (destFileName == null)
230                                 throw new ArgumentNullException ("destFileName");
231                         if (destFileName == Name || destFileName == FullName)
232                                 return;
233                         if (!File.Exists (FullPath))
234                                 throw new FileNotFoundException ();
235
236                         File.Move (FullPath, destFileName);
237                         this.FullPath = Path.GetFullPath (destFileName);
238                 }
239
240                 public FileInfo CopyTo (string destFileName)
241                 {
242                         return CopyTo (destFileName, false);
243                 }
244
245                 public FileInfo CopyTo (string destFileName, bool overwrite)
246                 {
247                         if (destFileName == null)
248                                 throw new ArgumentNullException ("destFileName");
249                         if (destFileName.Length == 0)
250                                 throw new ArgumentException ("An empty file name is not valid.", "destFileName");
251
252                         string dest = Path.GetFullPath (destFileName);
253
254                         if (overwrite && File.Exists (dest))
255                                 File.Delete (dest);
256
257                         File.Copy (FullPath, dest);
258                 
259                         return new FileInfo (dest);
260                 }
261
262                 public override string ToString ()
263                 {
264 #if NET_2_1
265                         // for Moonlight we *never* return paths, since ToString is not [SecurityCritical] we simply return the Name
266                         return Name;
267 #else
268                         return OriginalPath;
269 #endif
270                 }
271
272 #if !NET_2_1
273                 public FileSecurity GetAccessControl ()
274                 {
275                         throw new NotImplementedException ();
276                 }
277                 
278                 public FileSecurity GetAccessControl (AccessControlSections includeSections)
279                 {
280                         throw new NotImplementedException ();
281                 }
282
283                 [ComVisible (false)]
284                 public FileInfo Replace (string destinationFileName,
285                                          string destinationBackupFileName)
286                 {
287                         string destinationFullPath = null;
288                         if (!Exists)
289                                 throw new FileNotFoundException ();
290                         if (destinationFileName == null)
291                                 throw new ArgumentNullException ("destinationFileName");
292                         if (destinationFileName.Length == 0)
293                                 throw new ArgumentException ("An empty file name is not valid.", "destinationFileName");
294
295                         destinationFullPath = Path.GetFullPath (destinationFileName);
296
297                         if (!File.Exists (destinationFullPath))
298                                 throw new FileNotFoundException ();
299
300                         FileAttributes attrs = File.GetAttributes (destinationFullPath);
301
302                         if ( (attrs & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
303                                         throw new UnauthorizedAccessException (); 
304
305                         if (destinationBackupFileName != null) {
306                                 if (destinationBackupFileName.Length == 0)
307                                         throw new ArgumentException ("An empty file name is not valid.", "destinationBackupFileName");
308                                 File.Copy (destinationFullPath, Path.GetFullPath (destinationBackupFileName), true);
309                         }
310                         File.Copy (FullPath, destinationFullPath,true);
311                         File.Delete (FullPath);
312                         return new FileInfo (destinationFullPath);
313                 }
314                 
315                 [ComVisible (false)]
316                 [MonoLimitation ("We ignore the ignoreMetadataErrors parameter")]
317                 public FileInfo Replace (string destinationFileName,
318                                          string destinationBackupFileName,
319                                          bool ignoreMetadataErrors)
320                 {
321                         return Replace (destinationFileName, destinationBackupFileName);
322                 }
323
324                 public void SetAccessControl (FileSecurity fileSecurity)
325                 {
326                         throw new NotImplementedException ();
327                 }
328 #endif
329         }
330 }