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