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