2004-09-08 Miguel de Icaza <miguel@ximian.com>
[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 (Locale.GetText ("src is null"));
73                         if (dest == null)
74                                 throw new ArgumentNullException (Locale.GetText ("dest is null"));
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                                 throw MonoIO.GetException (path, error);
182                         }
183                         
184                         return(exists);
185                 }
186
187                 public static FileAttributes GetAttributes (string path)
188                 {
189                         if (null == path) {
190                                 throw new ArgumentNullException("path");
191                         }
192                         
193                         if (String.Empty == path.Trim()) {
194                                 throw new ArgumentException (Locale.GetText ("Path is empty"));
195                         }
196
197                         if (path.IndexOfAny(Path.InvalidPathChars) >= 0) {
198                                 throw new ArgumentException(Locale.GetText ("Path contains invalid chars"));
199                         }
200
201                         MonoIOError error;
202                         FileAttributes attrs;
203                         
204                         attrs = MonoIO.GetFileAttributes (path, out error);
205                         if (error != MonoIOError.ERROR_SUCCESS) {
206                                 throw MonoIO.GetException (path, error);
207                         }
208
209                         return(attrs);
210                 }
211
212                 public static DateTime GetCreationTime (string path)
213                 {
214                         MonoIOStat stat;
215                         MonoIOError error;
216                         CheckPathExceptions (path);
217                         
218                         if (!MonoIO.GetFileStat (path, out stat, out error))
219                                 throw new IOException (path);
220                         return DateTime.FromFileTime (stat.CreationTime);
221                 }
222
223                 public static DateTime GetCreationTimeUtc (string path)
224                 {
225                         return GetCreationTime (path).ToUniversalTime ();
226                 }
227
228                 public static DateTime GetLastAccessTime (string path)
229                 {
230                         MonoIOStat stat;
231                         MonoIOError error;
232                         CheckPathExceptions (path);
233
234                         if (!MonoIO.GetFileStat (path, out stat, out error))
235                                 throw new IOException (path);
236                         return DateTime.FromFileTime (stat.LastAccessTime);
237                 }
238
239                 public static DateTime GetLastAccessTimeUtc (string path)
240                 {
241                         return GetLastAccessTime (path).ToUniversalTime ();
242                 }
243
244                 public static DateTime GetLastWriteTime (string path)
245                 {
246                         MonoIOStat stat;
247                         MonoIOError error;
248                         CheckPathExceptions (path);
249
250                         if (!MonoIO.GetFileStat (path, out stat, out error))
251                                 throw new IOException (path);
252                         return DateTime.FromFileTime (stat.LastWriteTime);
253                 }
254
255                 public static DateTime GetLastWriteTimeUtc (string path)
256                 {
257                         return GetLastWriteTime (path).ToUniversalTime ();
258                 }
259
260                 public static void Move (string src, string dest)
261                 {
262                         MonoIOError error;
263
264                         if (src == null)
265                                 throw new ArgumentNullException ("src");
266                         if (dest == null)
267                                 throw new ArgumentNullException ("dest");
268                         if (src.Trim () == "" || src.IndexOfAny (Path.InvalidPathChars) != -1)
269                                 throw new ArgumentException ("src");
270                         if (dest.Trim () == "" || dest.IndexOfAny (Path.InvalidPathChars) != -1)
271                                 throw new ArgumentException ("dest");
272                         if (!MonoIO.Exists (src, out error))
273                                 throw new FileNotFoundException (Locale.GetText ("{0} does not exist", src), src);
274                         if (MonoIO.ExistsDirectory (dest, out error))
275                                         throw new IOException (Locale.GetText ("{0} is a directory", dest));    
276                         if (MonoIO.Exists (dest, out error))
277                                 throw new IOException (Locale.GetText ("{0} already exists", dest));
278
279                         string DirName;
280                         DirName = Path.GetDirectoryName(src);
281                         if (DirName != String.Empty && !Directory.Exists (DirName))
282                                 throw new DirectoryNotFoundException(Locale.GetText ("Source directory not found: {0}", DirName));
283                         DirName = Path.GetDirectoryName(dest);
284                         if (DirName != String.Empty && !Directory.Exists (DirName))
285                                 throw new DirectoryNotFoundException(Locale.GetText ("Destination directory not found: {0}", DirName));
286
287                         if (!MonoIO.MoveFile (src, dest, out error))
288                                 throw MonoIO.GetException (error);
289                 }
290                 
291                 public static FileStream Open (string path, FileMode mode)
292                 {       
293                         return new FileStream (path, mode, FileAccess.ReadWrite, FileShare.None);
294                 }
295                 
296                 public static FileStream Open (string path, FileMode mode, FileAccess access)
297                 {       
298                         return new FileStream (path, mode, access, FileShare.None);
299                 }
300
301                 public static FileStream Open (string path, FileMode mode, FileAccess access,
302                                                FileShare share)
303                 {
304                         return new FileStream (path, mode, access, share);
305                 }
306                 
307                 public static FileStream OpenRead (string path)
308                 {       
309                         return new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.Read);
310                 }
311
312                 public static StreamReader OpenText (string path)
313                 {
314                         return new StreamReader (path);
315                 }
316
317                 public static FileStream OpenWrite (string path)
318                 {
319                         return new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
320                 }
321
322                 public static void SetAttributes (string path,
323                                                   FileAttributes attributes)
324                 {
325                         MonoIOError error;
326                         CheckPathExceptions (path);
327                         
328                         if (!MonoIO.SetFileAttributes (path, attributes,
329                                                        out error)) {
330                                 throw MonoIO.GetException (path, error);
331                         }
332                 }
333
334                 public static void SetCreationTime (string path,
335                                                     DateTime creation_time)
336                 {
337                         MonoIOError error;
338                         CheckPathExceptions (path);
339                         if (!MonoIO.Exists (path, out error))
340                                 throw MonoIO.GetException (path, error);
341                         
342                         if (!MonoIO.SetCreationTime (path, creation_time, out error)) {
343                                 throw MonoIO.GetException (path, error);
344                         }
345                 }
346
347                 public static void SetCreationTimeUtc (string path,
348                                                     DateTime creation_time)
349                 {
350                         SetCreationTime (path, creation_time.ToLocalTime ());
351                 }
352
353                 public static void SetLastAccessTime (string path,DateTime last_access_time)
354                 {
355                         MonoIOError error;
356                         CheckPathExceptions (path);
357                         if (!MonoIO.Exists (path, out error))
358                                 throw MonoIO.GetException (path, error);
359
360                         if (!MonoIO.SetLastAccessTime (path, last_access_time, out error)) {
361                                 throw MonoIO.GetException (path, error);
362                         }
363                 }
364
365                 public static void SetLastAccessTimeUtc (string path,DateTime last_access_time)
366                 {
367                         SetLastAccessTime (path, last_access_time.ToLocalTime ());
368                 }
369
370                 public static void SetLastWriteTime (string path,
371                                                      DateTime last_write_time)
372                 {
373                         MonoIOError error;
374                         CheckPathExceptions (path);
375                         if (!MonoIO.Exists (path, out error))
376                                 throw MonoIO.GetException (path, error);
377
378                         if (!MonoIO.SetLastWriteTime (path, last_write_time, out error)) {
379                                 throw MonoIO.GetException (path, error);
380                         }
381                 }
382
383                 public static void SetLastWriteTimeUtc (string path,
384                                                      DateTime last_write_time)
385                 {
386                         SetLastWriteTime (path, last_write_time.ToLocalTime ());
387                 }
388
389                 #region Private
390
391                 private static void CheckPathExceptions (string path)
392                 {
393                         if (path == null)
394                                 throw new System.ArgumentNullException("path");
395                         if (path == "")
396                                 throw new System.ArgumentException(Locale.GetText ("Path is empty"));
397                         if (path.Trim().Length == 0)
398                                 throw new ArgumentException (Locale.GetText ("Path is empty"));
399                         if (path.IndexOfAny (Path.InvalidPathChars) != -1)
400                                 throw new ArgumentException (Locale.GetText ("Path contains invalid chars"));
401                 }
402
403                 #endregion
404         }
405 }