2005-02-06 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / corlib / System.IO / CheckPermission.cs
1 //------------------------------------------------------------------------------
2 // 
3 // System.IO.CheckPermission.cs 
4 //
5 // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
6 // 
7 // Author:         Jim Richardson, develop@wtfo-guru.com
8 // Created:        Saturday, August 25, 2001 
9 //
10 // NOTE: All contributors can freely add to this class or make modifications
11 //       that do not break existing usage of methods 
12 //------------------------------------------------------------------------------
13
14 //
15 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
16 //
17 // Permission is hereby granted, free of charge, to any person obtaining
18 // a copy of this software and associated documentation files (the
19 // "Software"), to deal in the Software without restriction, including
20 // without limitation the rights to use, copy, modify, merge, publish,
21 // distribute, sublicense, and/or sell copies of the Software, and to
22 // permit persons to whom the Software is furnished to do so, subject to
23 // the following conditions:
24 // 
25 // The above copyright notice and this permission notice shall be
26 // included in all copies or substantial portions of the Software.
27 // 
28 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 //
36
37
38 using System;
39 using System.Security;
40 using System.Security.Permissions;
41
42 namespace System.IO
43 {
44         /// <summary>
45         /// A utility class to assist with various permission validation in System.IO
46         /// </summary>
47         internal sealed class CheckPermission
48         {
49                 /// <summary>
50                 /// Generates and exception if caller doesn't have flags access to filesystem item specified by path
51                 /// </summary>
52                 [MonoTODO]
53                 public static void Demand(FileIOPermissionAccess flags, string path)
54                 {
55                         FileIOPermission ioPerm = new FileIOPermission(flags, path);
56                         // FIXME: FileIOPermission is not yet implemented
57                         //ioPerm.Demand();
58                 }               
59                 
60                 public static void Access(FileAccess access, string path)
61                 {
62                         switch(access)
63                         {
64                         case FileAccess.Read:
65                                 Demand(FileIOPermissionAccess.Read, path);
66                                 break;
67                         case FileAccess.Write:
68                                 Demand(FileIOPermissionAccess.Write, path);
69                                 break;
70                         case FileAccess.ReadWrite:
71                                 Demand(FileIOPermissionAccess.Read | FileIOPermissionAccess.Write, path);
72                                 break;
73                         default:
74                                 // TODO: determine what best to do here
75                                 throw new ArgumentException("Invalid FileAccess parameter");
76                         }
77                 }
78
79                 [MonoTODO]
80                 public static void ModeAccess(FileMode mode, FileAccess access, string path, bool exists)
81                 {
82 #if false
83                         // TODO: this logic isn't entirely complete and accurate, yet
84                         if((mode & (FileMode.CreateNew | FileMode.Create)) != 0)
85                         {
86                                 CheckPermission.Demand(FileIOPermissionAccess.Write, Path.GetDirectoryName(path));
87                         }
88                         else if((mode & FileMode.OpenOrCreate) != 0)
89                         {
90                                 if(!exists)
91                                 {
92                                         CheckPermission.Demand(FileIOPermissionAccess.Write, Path.GetDirectoryName(path));
93                                 }
94                                 else
95                                 {
96                                         CheckPermission.Access(access, path);
97                                 }
98                         }
99                         else if(exists)
100                         {
101                                 CheckPermission.Access(access, path);
102                         }
103                         else
104                         {
105                                 throw new FileNotFoundException("File not found", path);
106                         }
107 #endif
108                 }
109         }
110 }       // namespace System.IO.Private