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