Improve a safety check when writing data into StatBuffer
[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
52                 public FileInfo (string fileName)
53                 {
54                         if (fileName == null)
55                                 throw new ArgumentNullException ("fileName");
56
57                         CheckPath (fileName);
58                         SecurityManager.EnsureElevatedPermissions (); // this is a no-op outside moonlight
59
60                         OriginalPath = fileName;
61                         FullPath = Path.GetFullPath (fileName);
62                 }
63
64                 private FileInfo (SerializationInfo info, StreamingContext context)
65                         : base (info, context)
66                 {
67                 }
68
69                 internal override void InternalRefresh ()
70                 {
71                         exists = File.Exists (FullPath);
72                 }
73
74                 // public properties
75
76                 public override bool Exists {
77                         get {
78                                 Refresh (false);
79
80                                 if (stat.Attributes == MonoIO.InvalidFileAttributes)
81                                         return false;
82
83                                 if ((stat.Attributes & FileAttributes.Directory) != 0)
84                                         return false;
85
86                                 return exists;
87                         }
88                 }
89
90                 public override string Name {
91                         get {
92                                 return Path.GetFileName (FullPath);
93                         }
94                 }
95
96                 public bool IsReadOnly {
97                         get {
98                                 if (!Exists)
99                                         throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".", OriginalPath);
100                                         
101                                 return ((stat.Attributes & FileAttributes.ReadOnly) != 0);
102                         }
103                                 
104                         set {
105                                 if (!Exists)
106                                         throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".", OriginalPath);
107                                         
108                                 FileAttributes attrs = File.GetAttributes(FullPath);
109
110                                 if (value) 
111                                         attrs |= FileAttributes.ReadOnly;
112                                 else
113                                         attrs &= ~FileAttributes.ReadOnly;
114
115                                 File.SetAttributes(FullPath, attrs);
116                         }
117                 }
118
119                 [MonoLimitation ("File encryption isn't supported (even on NTFS).")]
120                 [ComVisible (false)]
121                 public void Encrypt ()
122                 {
123                         // MS.NET support this only on NTFS file systems, i.e. it's a file-system (not a framework) feature.
124                         // otherwise it throws a NotSupportedException (or a PlatformNotSupportedException on older OS).
125                         // we throw the same (instead of a NotImplementedException) because most code should already be
126                         // handling this exception to work properly.
127                         throw new NotSupportedException (Locale.GetText ("File encryption isn't supported on any file system."));
128                 }
129
130                 [MonoLimitation ("File encryption isn't supported (even on NTFS).")]
131                 [ComVisible (false)]
132                 public void Decrypt ()
133                 {
134                         // MS.NET support this only on NTFS file systems, i.e. it's a file-system (not a framework) feature.
135                         // otherwise it throws a NotSupportedException (or a PlatformNotSupportedException on older OS).
136                         // we throw the same (instead of a NotImplementedException) because most code should already be
137                         // handling this exception to work properly.
138                         throw new NotSupportedException (Locale.GetText ("File encryption isn't supported on any file system."));
139                 }
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                         SecurityManager.EnsureElevatedPermissions (); // this is a no-op outside moonlight
218
219                         if (!MonoIO.Exists (FullPath, out error))
220                                 // a weird MS.NET behaviour
221                                 return;
222
223                         if (MonoIO.ExistsDirectory (FullPath, out error))
224                                 throw new UnauthorizedAccessException ("Access to the path \"" + FullPath + "\" is denied.");
225                         if (!MonoIO.DeleteFile (FullPath, out error))
226                                 throw MonoIO.GetException (OriginalPath, error);
227                 }
228
229                 public void MoveTo (string destFileName)
230                 {
231                         if (destFileName == null)
232                                 throw new ArgumentNullException ("destFileName");
233                         if (destFileName.Length == 0)
234                                 throw new ArgumentException ("An empty file name is not valid.", "destFileName");
235
236                         var destFullPath = Path.GetFullPath (destFileName);
237                         if (destFullPath == FullPath)
238                                 return;
239                         if (!File.Exists (FullPath))
240                                 throw new FileNotFoundException ();
241
242                         File.Move (FullPath, destFullPath);
243                         this.FullPath = destFullPath;
244                 }
245
246                 public FileInfo CopyTo (string destFileName)
247                 {
248                         return CopyTo (destFileName, false);
249                 }
250
251                 public FileInfo CopyTo (string destFileName, bool overwrite)
252                 {
253                         if (destFileName == null)
254                                 throw new ArgumentNullException ("destFileName");
255                         if (destFileName.Length == 0)
256                                 throw new ArgumentException ("An empty file name is not valid.", "destFileName");
257
258                         string dest = Path.GetFullPath (destFileName);
259
260                         if (overwrite && File.Exists (dest))
261                                 File.Delete (dest);
262
263                         File.Copy (FullPath, dest);
264                 
265                         return new FileInfo (dest);
266                 }
267
268                 public override string ToString ()
269                 {
270                         return OriginalPath;
271                 }
272
273 #if !MOBILE
274                 public FileSecurity GetAccessControl ()
275                 {
276                         return File.GetAccessControl (FullPath); 
277                 }
278                 
279                 public FileSecurity GetAccessControl (AccessControlSections includeSections)
280                 {
281                         return File.GetAccessControl (FullPath, includeSections);
282                 }
283
284                 [ComVisible (false)]
285                 public FileInfo Replace (string destinationFileName,
286                                          string destinationBackupFileName)
287                 {
288                         string destinationFullPath = null;
289                         if (!Exists)
290                                 throw new FileNotFoundException ();
291                         if (destinationFileName == null)
292                                 throw new ArgumentNullException ("destinationFileName");
293                         if (destinationFileName.Length == 0)
294                                 throw new ArgumentException ("An empty file name is not valid.", "destinationFileName");
295
296                         destinationFullPath = Path.GetFullPath (destinationFileName);
297
298                         if (!File.Exists (destinationFullPath))
299                                 throw new FileNotFoundException ();
300
301                         FileAttributes attrs = File.GetAttributes (destinationFullPath);
302
303                         if ( (attrs & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
304                                         throw new UnauthorizedAccessException (); 
305
306                         if (destinationBackupFileName != null) {
307                                 if (destinationBackupFileName.Length == 0)
308                                         throw new ArgumentException ("An empty file name is not valid.", "destinationBackupFileName");
309                                 File.Copy (destinationFullPath, Path.GetFullPath (destinationBackupFileName), true);
310                         }
311                         File.Copy (FullPath, destinationFullPath,true);
312                         File.Delete (FullPath);
313                         return new FileInfo (destinationFullPath);
314                 }
315                 
316                 [ComVisible (false)]
317                 [MonoLimitation ("We ignore the ignoreMetadataErrors parameter")]
318                 public FileInfo Replace (string destinationFileName,
319                                          string destinationBackupFileName,
320                                          bool ignoreMetadataErrors)
321                 {
322                         return Replace (destinationFileName, destinationBackupFileName);
323                 }
324
325                 public void SetAccessControl (FileSecurity fileSecurity)
326                 {
327                         File.SetAccessControl (FullPath, fileSecurity);
328                 }
329 #endif
330         }
331 }