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