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