2004-06-21 Atsushi Enomoto <atsushi@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
268                         string DirName;
269                         DirName = Path.GetDirectoryName(src);
270                         if (DirName != String.Empty && !Directory.Exists (DirName))
271                                 throw new DirectoryNotFoundException("Source directory not found: " + DirName);
272                         DirName = Path.GetDirectoryName(dest);
273                         if (DirName != String.Empty && !Directory.Exists (DirName))
274                                 throw new DirectoryNotFoundException("Destination directory not found: " + DirName);
275
276                         if (!MonoIO.MoveFile (src, dest, out error))
277                                 throw MonoIO.GetException (error);
278                 }
279                 
280                 public static FileStream Open (string path, FileMode mode)
281                 {       
282                         return new FileStream (path, mode, FileAccess.ReadWrite, FileShare.None);
283                 }
284                 
285                 public static FileStream Open (string path, FileMode mode, FileAccess access)
286                 {       
287                         return new FileStream (path, mode, access, FileShare.None);
288                 }
289
290                 public static FileStream Open (string path, FileMode mode, FileAccess access,
291                                                FileShare share)
292                 {
293                         return new FileStream (path, mode, access, share);
294                 }
295                 
296                 public static FileStream OpenRead (string path)
297                 {       
298                         return new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.Read);
299                 }
300
301                 public static StreamReader OpenText (string path)
302                 {
303                         return new StreamReader (path);
304                 }
305
306                 public static FileStream OpenWrite (string path)
307                 {
308                         return new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
309                 }
310
311                 public static void SetAttributes (string path,
312                                                   FileAttributes attributes)
313                 {
314                         MonoIOError error;
315                         CheckPathExceptions (path);
316                         
317                         if (!MonoIO.SetFileAttributes (path, attributes,
318                                                        out error)) {
319                                 throw MonoIO.GetException (path, error);
320                         }
321                 }
322
323                 public static void SetCreationTime (string path,
324                                                     DateTime creation_time)
325                 {
326                         MonoIOError error;
327                         CheckPathExceptions (path);
328                         if (!MonoIO.Exists (path, out error))
329                                 throw MonoIO.GetException (path, error);
330                         
331                         if (!MonoIO.SetCreationTime (path, creation_time, out error)) {
332                                 throw MonoIO.GetException (path, error);
333                         }
334                 }
335
336                 public static void SetCreationTimeUtc (string path,
337                                                     DateTime creation_time)
338                 {
339                         SetCreationTime (path, creation_time.ToLocalTime ());
340                 }
341
342                 public static void SetLastAccessTime (string path,DateTime last_access_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.SetLastAccessTime (path, last_access_time, out error)) {
350                                 throw MonoIO.GetException (path, error);
351                         }
352                 }
353
354                 public static void SetLastAccessTimeUtc (string path,DateTime last_access_time)
355                 {
356                         SetLastAccessTime (path, last_access_time.ToLocalTime ());
357                 }
358
359                 public static void SetLastWriteTime (string path,
360                                                      DateTime last_write_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.SetLastWriteTime (path, last_write_time, out error)) {
368                                 throw MonoIO.GetException (path, error);
369                         }
370                 }
371
372                 public static void SetLastWriteTimeUtc (string path,
373                                                      DateTime last_write_time)
374                 {
375                         SetLastWriteTime (path, last_write_time.ToLocalTime ());
376                 }
377
378                 #region Private
379
380                 private static void CheckPathExceptions (string path)
381                 {
382                         if (path == null)
383                                 throw new System.ArgumentNullException("Path is Null");
384                         if (path == "")
385                                 throw new System.ArgumentException("Path is Empty");
386                         if (path.Trim().Length == 0)
387                                 throw new ArgumentException ("Only blank characters in path");
388                         if (path.IndexOfAny (Path.InvalidPathChars) != -1)
389                                 throw new ArgumentException ("Path contains invalid chars");
390                 }
391
392                 #endregion
393         }
394 }