2004-09-08 Marek Safar <marek.safar@seznam.cz>
[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 ("src");
77                         if (dest.Trim () == "" || dest.IndexOfAny (Path.InvalidPathChars) != -1)
78                                 throw new ArgumentException ("dest");
79                         if (!Exists (src))
80                                 throw new FileNotFoundException (src + " does not exist", src);
81
82                         if ((GetAttributes(src) & FileAttributes.Directory) == FileAttributes.Directory){
83                                 throw new ArgumentException(src + " is a directory");
84                         }
85                         
86                         if (Exists (dest)) {
87                                 if ((GetAttributes(dest) & FileAttributes.Directory) == FileAttributes.Directory){
88                                         throw new ArgumentException(dest + " is a directory");  
89                                 }
90                                 if (!overwrite)
91                                         throw new IOException (dest + " already exists");
92                         }
93
94                         string DirName = Path.GetDirectoryName(dest);
95                         if (DirName != String.Empty && !Directory.Exists (DirName))
96                                 throw new DirectoryNotFoundException("Destination directory not found: " + DirName);
97
98                         MonoIOError error;
99                         
100                         if (!MonoIO.CopyFile (src, dest, overwrite, out error))
101                                 throw MonoIO.GetException (error);
102                 }
103
104                 public static FileStream Create (string path)
105                 {
106                         return Create (path, 8192);
107                 }
108
109                 public static FileStream Create (string path, int buffersize)
110                 {
111                         if (null == path)
112                                 throw new ArgumentNullException("path");
113                         if (String.Empty == path.Trim() || path.IndexOfAny(Path.InvalidPathChars) >= 0)
114                                 throw new ArgumentException("path");
115
116                         string DirName = Path.GetDirectoryName(path);
117                         if (DirName != String.Empty && !Directory.Exists (DirName))
118                                 throw new DirectoryNotFoundException("Destination directory not found: " + DirName);
119                         if (Exists(path)){
120                                 if ((GetAttributes(path) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly){
121                                         throw new UnauthorizedAccessException(path + " is a read-only");        
122                                 }
123                         }
124
125                         return new FileStream (path, FileMode.Create, FileAccess.ReadWrite,
126                                                FileShare.None, buffersize);
127                 }
128
129                 public static StreamWriter CreateText(string path)
130                 
131                 {
132                         return new StreamWriter (path, false);
133                 
134                 }
135                 
136                 
137                 
138                 public static void Delete (string path)
139                 {
140                         if (null == path)
141                                 throw new ArgumentNullException("path");
142                         if (String.Empty == path.Trim() || path.IndexOfAny(Path.InvalidPathChars) >= 0)
143                                 throw new ArgumentException("path");
144                         if (Directory.Exists (path))
145                                 throw new UnauthorizedAccessException("path is a directory");
146
147                         string DirName = Path.GetDirectoryName(path);
148                         if (DirName != String.Empty && !Directory.Exists (DirName))
149                                 throw new DirectoryNotFoundException("Destination directory not found: " + DirName);
150
151                         MonoIOError error;
152                         
153                         if (!MonoIO.DeleteFile (path, out error)){
154                                 Exception e = MonoIO.GetException (path, error);
155                                 if (! (e is FileNotFoundException))
156                                         throw e;
157                         }
158                 }
159
160                 public static bool Exists (string path)
161                 {
162                         // For security reasons no exceptions are
163                         // thrown, only false is returned if there is
164                         // any problem with the path or permissions.
165                         // Minimizes what information can be
166                         // discovered by using this method.
167                         if (null == path || String.Empty == path.Trim()
168                             || path.IndexOfAny(Path.InvalidPathChars) >= 0) {
169                                 return false;
170                         }
171
172                         MonoIOError error;
173                         bool exists;
174                         
175                         exists = MonoIO.ExistsFile (path, out error);
176                         if (error != MonoIOError.ERROR_SUCCESS &&
177                             error != MonoIOError.ERROR_FILE_NOT_FOUND &&
178                             error != MonoIOError.ERROR_PATH_NOT_FOUND) {
179                                 throw MonoIO.GetException (path, error);
180                         }
181                         
182                         return(exists);
183                 }
184
185                 public static FileAttributes GetAttributes (string path)
186                 {
187                         if (null == path) {
188                                 throw new ArgumentNullException("path");
189                         }
190                         
191                         if (String.Empty == path.Trim()) {
192                                 throw new ArgumentException("Path is empty");
193                         }
194
195                         if (path.IndexOfAny(Path.InvalidPathChars) >= 0) {
196                                 throw new ArgumentException("Path contains invalid chars");
197                         }
198
199                         MonoIOError error;
200                         FileAttributes attrs;
201                         
202                         attrs = MonoIO.GetFileAttributes (path, out error);
203                         if (error != MonoIOError.ERROR_SUCCESS) {
204                                 throw MonoIO.GetException (path, error);
205                         }
206
207                         return(attrs);
208                 }
209
210                 public static DateTime GetCreationTime (string path)
211                 {
212                         MonoIOStat stat;
213                         MonoIOError error;
214                         CheckPathExceptions (path);
215                         
216                         if (!MonoIO.GetFileStat (path, out stat, out error))
217                                 throw new IOException (path);
218                         return DateTime.FromFileTime (stat.CreationTime);
219                 }
220
221                 public static DateTime GetCreationTimeUtc (string path)
222                 {
223                         return GetCreationTime (path).ToUniversalTime ();
224                 }
225
226                 public static DateTime GetLastAccessTime (string path)
227                 {
228                         MonoIOStat stat;
229                         MonoIOError error;
230                         CheckPathExceptions (path);
231
232                         if (!MonoIO.GetFileStat (path, out stat, out error))
233                                 throw new IOException (path);
234                         return DateTime.FromFileTime (stat.LastAccessTime);
235                 }
236
237                 public static DateTime GetLastAccessTimeUtc (string path)
238                 {
239                         return GetLastAccessTime (path).ToUniversalTime ();
240                 }
241
242                 public static DateTime GetLastWriteTime (string path)
243                 {
244                         MonoIOStat stat;
245                         MonoIOError error;
246                         CheckPathExceptions (path);
247
248                         if (!MonoIO.GetFileStat (path, out stat, out error))
249                                 throw new IOException (path);
250                         return DateTime.FromFileTime (stat.LastWriteTime);
251                 }
252
253                 public static DateTime GetLastWriteTimeUtc (string path)
254                 {
255                         return GetLastWriteTime (path).ToUniversalTime ();
256                 }
257
258                 public static void Move (string src, string dest)
259                 {
260                         MonoIOError error;
261
262                         if (src == null)
263                                 throw new ArgumentNullException ("src");
264                         if (dest == null)
265                                 throw new ArgumentNullException ("dest");
266                         if (src.Trim () == "" || src.IndexOfAny (Path.InvalidPathChars) != -1)
267                                 throw new ArgumentException ("src");
268                         if (dest.Trim () == "" || dest.IndexOfAny (Path.InvalidPathChars) != -1)
269                                 throw new ArgumentException ("dest");
270                         if (!MonoIO.Exists (src, out error))
271                                 throw new FileNotFoundException (src + " does not exist", src);
272                         if (MonoIO.ExistsDirectory (dest, out error))
273                                         throw new IOException (dest + " is a directory");       
274                         if (MonoIO.Exists (dest, out error))
275                                 throw new IOException (dest + " already exists");
276
277                         string DirName;
278                         DirName = Path.GetDirectoryName(src);
279                         if (DirName != String.Empty && !Directory.Exists (DirName))
280                                 throw new DirectoryNotFoundException("Source directory not found: " + DirName);
281                         DirName = Path.GetDirectoryName(dest);
282                         if (DirName != String.Empty && !Directory.Exists (DirName))
283                                 throw new DirectoryNotFoundException("Destination directory not found: " + DirName);
284
285                         if (!MonoIO.MoveFile (src, dest, out error))
286                                 throw MonoIO.GetException (error);
287                 }
288                 
289                 public static FileStream Open (string path, FileMode mode)
290                 {       
291                         return new FileStream (path, mode, FileAccess.ReadWrite, FileShare.None);
292                 }
293                 
294                 public static FileStream Open (string path, FileMode mode, FileAccess access)
295                 {       
296                         return new FileStream (path, mode, access, FileShare.None);
297                 }
298
299                 public static FileStream Open (string path, FileMode mode, FileAccess access,
300                                                FileShare share)
301                 {
302                         return new FileStream (path, mode, access, share);
303                 }
304                 
305                 public static FileStream OpenRead (string path)
306                 {       
307                         return new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.Read);
308                 }
309
310                 public static StreamReader OpenText (string path)
311                 {
312                         return new StreamReader (path);
313                 }
314
315                 public static FileStream OpenWrite (string path)
316                 {
317                         return new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
318                 }
319
320                 public static void SetAttributes (string path,
321                                                   FileAttributes attributes)
322                 {
323                         MonoIOError error;
324                         CheckPathExceptions (path);
325                         
326                         if (!MonoIO.SetFileAttributes (path, attributes,
327                                                        out error)) {
328                                 throw MonoIO.GetException (path, error);
329                         }
330                 }
331
332                 public static void SetCreationTime (string path,
333                                                     DateTime creation_time)
334                 {
335                         MonoIOError error;
336                         CheckPathExceptions (path);
337                         if (!MonoIO.Exists (path, out error))
338                                 throw MonoIO.GetException (path, error);
339                         
340                         if (!MonoIO.SetCreationTime (path, creation_time, out error)) {
341                                 throw MonoIO.GetException (path, error);
342                         }
343                 }
344
345                 public static void SetCreationTimeUtc (string path,
346                                                     DateTime creation_time)
347                 {
348                         SetCreationTime (path, creation_time.ToLocalTime ());
349                 }
350
351                 public static void SetLastAccessTime (string path,DateTime last_access_time)
352                 {
353                         MonoIOError error;
354                         CheckPathExceptions (path);
355                         if (!MonoIO.Exists (path, out error))
356                                 throw MonoIO.GetException (path, error);
357
358                         if (!MonoIO.SetLastAccessTime (path, last_access_time, out error)) {
359                                 throw MonoIO.GetException (path, error);
360                         }
361                 }
362
363                 public static void SetLastAccessTimeUtc (string path,DateTime last_access_time)
364                 {
365                         SetLastAccessTime (path, last_access_time.ToLocalTime ());
366                 }
367
368                 public static void SetLastWriteTime (string path,
369                                                      DateTime last_write_time)
370                 {
371                         MonoIOError error;
372                         CheckPathExceptions (path);
373                         if (!MonoIO.Exists (path, out error))
374                                 throw MonoIO.GetException (path, error);
375
376                         if (!MonoIO.SetLastWriteTime (path, last_write_time, out error)) {
377                                 throw MonoIO.GetException (path, error);
378                         }
379                 }
380
381                 public static void SetLastWriteTimeUtc (string path,
382                                                      DateTime last_write_time)
383                 {
384                         SetLastWriteTime (path, last_write_time.ToLocalTime ());
385                 }
386
387                 #region Private
388
389                 private static void CheckPathExceptions (string path)
390                 {
391                         if (path == null)
392                                 throw new System.ArgumentNullException("Path is Null");
393                         if (path == "")
394                                 throw new System.ArgumentException("Path is Empty");
395                         if (path.Trim().Length == 0)
396                                 throw new ArgumentException ("Only blank characters in path");
397                         if (path.IndexOfAny (Path.InvalidPathChars) != -1)
398                                 throw new ArgumentException ("Path contains invalid chars");
399                 }
400
401                 #endregion
402         }
403 }