credits
[mono.git] / mcs / class / corlib / System.IO / File.cs
1 // 
2 // System.IO.FIle.cs 
3 //
4 // 
5 // Authors:
6 //   Miguel de Icaza (miguel@ximian.com)
7 //   Jim Richardson  (develop@wtfo-guru.com)
8 //   Dan Lewis       (dihlewis@yahoo.co.uk)
9 //   Ville Palo      (vi64pa@kolumbus.fi)
10 //
11 // Copyright 2002 Ximian, Inc. http://www.ximian.com
12 // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
13 //
14
15 //
16 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
17 //
18 // Permission is hereby granted, free of charge, to any person obtaining
19 // a copy of this software and associated documentation files (the
20 // "Software"), to deal in the Software without restriction, including
21 // without limitation the rights to use, copy, modify, merge, publish,
22 // distribute, sublicense, and/or sell copies of the Software, and to
23 // permit persons to whom the Software is furnished to do so, subject to
24 // the following conditions:
25 // 
26 // The above copyright notice and this permission notice shall be
27 // included in all copies or substantial portions of the Software.
28 // 
29 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 //
37
38 using System;
39
40 namespace System.IO
41 {
42         /// <summary>
43         /// 
44         /// </summary>
45         public
46 #if NET_2_0
47         static
48 #else
49         sealed
50 #endif
51         class File
52         {
53
54 #if !NET_2_0
55                 private File () {}
56 #endif
57                 
58                 public static StreamWriter AppendText (string path)
59                 {       
60                         return new StreamWriter (path, true);
61                 }
62
63                 [MonoTODO("Security Permision Checks")]
64                 public static void Copy (string sourceFilename, string destFilename)
65                 {
66                         Copy (sourceFilename, destFilename, false);
67                 }
68
69                 public static void Copy (string src, string dest, bool overwrite)
70                 {       
71                         if (src == null)
72                                 throw new ArgumentNullException ("src");
73                         if (dest == null)
74                                 throw new ArgumentNullException ("dest");
75                         if (src.Trim () == "" || src.IndexOfAny (Path.InvalidPathChars) != -1)
76                                 throw new ArgumentException (Locale.GetText ("src is null"));
77                         if (dest.Trim () == "" || dest.IndexOfAny (Path.InvalidPathChars) != -1)
78                                 throw new ArgumentException (Locale.GetText ("dest is empty or contains invalid characters"));
79                         if (!Exists (src))
80                                 throw new FileNotFoundException (Locale.GetText ("{0} does not exist", src), src);
81
82                         if ((GetAttributes(src) & FileAttributes.Directory) == FileAttributes.Directory){
83                                 throw new ArgumentException(Locale.GetText ("{0} is a directory", src));
84                         }
85                         
86                         if (Exists (dest)) {
87                                 if ((GetAttributes(dest) & FileAttributes.Directory) == FileAttributes.Directory){
88                                         throw new ArgumentException (Locale.GetText ("{0} is a directory", dest));
89                                 }
90                                 if (!overwrite)
91                                         throw new IOException (Locale.GetText ("{0} already exists", dest));
92                         }
93
94                         string DirName = Path.GetDirectoryName(dest);
95                         if (DirName != String.Empty && !Directory.Exists (DirName))
96                                 throw new DirectoryNotFoundException (Locale.GetText ("Destination directory not found: {0}",DirName));
97
98                         MonoIOError error;
99                         
100                         if (!MonoIO.CopyFile (src, dest, overwrite, out error)){
101                                 string p = Locale.GetText ("{0}\" or \"{1}", src, dest);
102                                 throw MonoIO.GetException (p, error);
103                         }
104                 }
105
106                 public static FileStream Create (string path)
107                 {
108                         return Create (path, 8192);
109                 }
110
111                 public static FileStream Create (string path, int buffersize)
112                 {
113                         if (null == path)
114                                 throw new ArgumentNullException ("path");
115                         if (String.Empty == path.Trim() || path.IndexOfAny(Path.InvalidPathChars) >= 0)
116                                 throw new ArgumentException (Locale.GetText ("path is invalid"));
117
118                         string DirName = Path.GetDirectoryName(path);
119                         if (DirName != String.Empty && !Directory.Exists (DirName))
120                                 throw new DirectoryNotFoundException (Locale.GetText ("Destination directory not found: {0}", DirName));
121                         if (Exists(path)){
122                                 if ((GetAttributes(path) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly){
123                                         throw new UnauthorizedAccessException (Locale.GetText ("{0} is read-only", path));
124                                 }
125                         }
126
127                         return new FileStream (path, FileMode.Create, FileAccess.ReadWrite,
128                                                FileShare.None, buffersize);
129                 }
130
131                 public static StreamWriter CreateText(string path)
132                 
133                 {
134                         return new StreamWriter (path, false);
135                 
136                 }
137                 
138                 
139                 
140                 public static void Delete (string path)
141                 {
142                         if (null == path)
143                                 throw new ArgumentNullException("path");
144                         if (String.Empty == path.Trim() || path.IndexOfAny(Path.InvalidPathChars) >= 0)
145                                 throw new ArgumentException("path");
146                         if (Directory.Exists (path))
147                                 throw new UnauthorizedAccessException(Locale.GetText ("{0} is a directory", path));
148
149                         string DirName = Path.GetDirectoryName(path);
150                         if (DirName != String.Empty && !Directory.Exists (DirName))
151                                 throw new DirectoryNotFoundException (Locale.GetText ("Destination directory not found: {0}", DirName));
152
153                         MonoIOError error;
154                         
155                         if (!MonoIO.DeleteFile (path, out error)){
156                                 Exception e = MonoIO.GetException (path, error);
157                                 if (! (e is FileNotFoundException))
158                                         throw e;
159                         }
160                 }
161
162                 public static bool Exists (string path)
163                 {
164                         // For security reasons no exceptions are
165                         // thrown, only false is returned if there is
166                         // any problem with the path or permissions.
167                         // Minimizes what information can be
168                         // discovered by using this method.
169                         if (null == path || String.Empty == path.Trim()
170                             || path.IndexOfAny(Path.InvalidPathChars) >= 0) {
171                                 return false;
172                         }
173
174                         MonoIOError error;
175                         bool exists;
176                         
177                         exists = MonoIO.ExistsFile (path, out error);
178                         if (error != MonoIOError.ERROR_SUCCESS &&
179                             error != MonoIOError.ERROR_FILE_NOT_FOUND &&
180                             error != MonoIOError.ERROR_PATH_NOT_FOUND &&
181                             error != MonoIOError.ERROR_INVALID_NAME) {
182                                 throw MonoIO.GetException (path, error);
183                         }
184                         
185                         return(exists);
186                 }
187
188                 public static FileAttributes GetAttributes (string path)
189                 {
190                         if (null == path) {
191                                 throw new ArgumentNullException("path");
192                         }
193                         
194                         if (String.Empty == path.Trim()) {
195                                 throw new ArgumentException (Locale.GetText ("Path is empty"));
196                         }
197
198                         if (path.IndexOfAny(Path.InvalidPathChars) >= 0) {
199                                 throw new ArgumentException(Locale.GetText ("Path contains invalid chars"));
200                         }
201
202                         MonoIOError error;
203                         FileAttributes attrs;
204                         
205                         attrs = MonoIO.GetFileAttributes (path, out error);
206                         if (error != MonoIOError.ERROR_SUCCESS) {
207                                 throw MonoIO.GetException (path, error);
208                         }
209
210                         return(attrs);
211                 }
212
213                 public static DateTime GetCreationTime (string path)
214                 {
215                         MonoIOStat stat;
216                         MonoIOError error;
217                         CheckPathExceptions (path);
218                         
219                         if (!MonoIO.GetFileStat (path, out stat, out error))
220                                 throw new IOException (path);
221                         return DateTime.FromFileTime (stat.CreationTime);
222                 }
223
224                 public static DateTime GetCreationTimeUtc (string path)
225                 {
226                         return GetCreationTime (path).ToUniversalTime ();
227                 }
228
229                 public static DateTime GetLastAccessTime (string path)
230                 {
231                         MonoIOStat stat;
232                         MonoIOError error;
233                         CheckPathExceptions (path);
234
235                         if (!MonoIO.GetFileStat (path, out stat, out error))
236                                 throw new IOException (path);
237                         return DateTime.FromFileTime (stat.LastAccessTime);
238                 }
239
240                 public static DateTime GetLastAccessTimeUtc (string path)
241                 {
242                         return GetLastAccessTime (path).ToUniversalTime ();
243                 }
244
245                 public static DateTime GetLastWriteTime (string path)
246                 {
247                         MonoIOStat stat;
248                         MonoIOError error;
249                         CheckPathExceptions (path);
250
251                         if (!MonoIO.GetFileStat (path, out stat, out error))
252                                 throw new IOException (path);
253                         return DateTime.FromFileTime (stat.LastWriteTime);
254                 }
255
256                 public static DateTime GetLastWriteTimeUtc (string path)
257                 {
258                         return GetLastWriteTime (path).ToUniversalTime ();
259                 }
260
261                 public static void Move (string src, string dest)
262                 {
263                         MonoIOError error;
264
265                         if (src == null)
266                                 throw new ArgumentNullException ("src");
267                         if (dest == null)
268                                 throw new ArgumentNullException ("dest");
269                         if (src.Trim () == "" || src.IndexOfAny (Path.InvalidPathChars) != -1)
270                                 throw new ArgumentException ("src");
271                         if (dest.Trim () == "" || dest.IndexOfAny (Path.InvalidPathChars) != -1)
272                                 throw new ArgumentException ("dest");
273                         if (!MonoIO.Exists (src, out error))
274                                 throw new FileNotFoundException (Locale.GetText ("{0} does not exist", src), src);
275                         if (MonoIO.ExistsDirectory (dest, out error))
276                                         throw new IOException (Locale.GetText ("{0} is a directory", dest));    
277
278                         // Don't check for this error here to allow the runtime to check if src and dest
279                         // are equal. Comparing src and dest is not enough.
280                         //if (MonoIO.Exists (dest, out error))
281                         //      throw new IOException (Locale.GetText ("{0} already exists", dest));
282
283                         string DirName;
284                         DirName = Path.GetDirectoryName(src);
285                         if (DirName != String.Empty && !Directory.Exists (DirName))
286                                 throw new DirectoryNotFoundException(Locale.GetText ("Source directory not found: {0}", DirName));
287                         DirName = Path.GetDirectoryName(dest);
288                         if (DirName != String.Empty && !Directory.Exists (DirName))
289                                 throw new DirectoryNotFoundException(Locale.GetText ("Destination directory not found: {0}", DirName));
290
291                         if (!MonoIO.MoveFile (src, dest, out error)) {
292                                 if (error == MonoIOError.ERROR_ALREADY_EXISTS)
293                                         throw MonoIO.GetException (dest, error);
294                                 throw MonoIO.GetException (error);
295                         }
296                 }
297                 
298                 public static FileStream Open (string path, FileMode mode)
299                 {       
300                         return new FileStream (path, mode, FileAccess.ReadWrite, FileShare.None);
301                 }
302                 
303                 public static FileStream Open (string path, FileMode mode, FileAccess access)
304                 {       
305                         return new FileStream (path, mode, access, FileShare.None);
306                 }
307
308                 public static FileStream Open (string path, FileMode mode, FileAccess access,
309                                                FileShare share)
310                 {
311                         return new FileStream (path, mode, access, share);
312                 }
313                 
314                 public static FileStream OpenRead (string path)
315                 {       
316                         return new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.Read);
317                 }
318
319                 public static StreamReader OpenText (string path)
320                 {
321                         return new StreamReader (path);
322                 }
323
324                 public static FileStream OpenWrite (string path)
325                 {
326                         return new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
327                 }
328
329                 public static void SetAttributes (string path,
330                                                   FileAttributes attributes)
331                 {
332                         MonoIOError error;
333                         CheckPathExceptions (path);
334                         
335                         if (!MonoIO.SetFileAttributes (path, attributes,
336                                                        out error)) {
337                                 throw MonoIO.GetException (path, error);
338                         }
339                 }
340
341                 public static void SetCreationTime (string path,
342                                                     DateTime creation_time)
343                 {
344                         MonoIOError error;
345                         CheckPathExceptions (path);
346                         if (!MonoIO.Exists (path, out error))
347                                 throw MonoIO.GetException (path, error);
348                         
349                         if (!MonoIO.SetCreationTime (path, creation_time, out error)) {
350                                 throw MonoIO.GetException (path, error);
351                         }
352                 }
353
354                 public static void SetCreationTimeUtc (string path,
355                                                     DateTime creation_time)
356                 {
357                         SetCreationTime (path, creation_time.ToLocalTime ());
358                 }
359
360                 public static void SetLastAccessTime (string path,DateTime last_access_time)
361                 {
362                         MonoIOError error;
363                         CheckPathExceptions (path);
364                         if (!MonoIO.Exists (path, out error))
365                                 throw MonoIO.GetException (path, error);
366
367                         if (!MonoIO.SetLastAccessTime (path, last_access_time, out error)) {
368                                 throw MonoIO.GetException (path, error);
369                         }
370                 }
371
372                 public static void SetLastAccessTimeUtc (string path,DateTime last_access_time)
373                 {
374                         SetLastAccessTime (path, last_access_time.ToLocalTime ());
375                 }
376
377                 public static void SetLastWriteTime (string path,
378                                                      DateTime last_write_time)
379                 {
380                         MonoIOError error;
381                         CheckPathExceptions (path);
382                         if (!MonoIO.Exists (path, out error))
383                                 throw MonoIO.GetException (path, error);
384
385                         if (!MonoIO.SetLastWriteTime (path, last_write_time, out error)) {
386                                 throw MonoIO.GetException (path, error);
387                         }
388                 }
389
390                 public static void SetLastWriteTimeUtc (string path,
391                                                      DateTime last_write_time)
392                 {
393                         SetLastWriteTime (path, last_write_time.ToLocalTime ());
394                 }
395
396                 #region Private
397
398                 private static void CheckPathExceptions (string path)
399                 {
400                         if (path == null)
401                                 throw new System.ArgumentNullException("path");
402                         if (path == "")
403                                 throw new System.ArgumentException(Locale.GetText ("Path is empty"));
404                         if (path.Trim().Length == 0)
405                                 throw new ArgumentException (Locale.GetText ("Path is empty"));
406                         if (path.IndexOfAny (Path.InvalidPathChars) != -1)
407                                 throw new ArgumentException (Locale.GetText ("Path contains invalid chars"));
408                 }
409
410                 #endregion
411         }
412 }