In Test/System.IO:
[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 = 
56                                 new FileIOPermission(flags, path);
57                         // FIXME: FileIOPermission is not yet implemented
58                         //ioPerm.Demand();
59                 }               
60                 
61                 public static void Access(FileAccess access, string path)
62                 {
63                         switch(access)
64                         {
65                         case FileAccess.Read:
66                                 Demand(FileIOPermissionAccess.Read, path);
67                                 break;
68                         case FileAccess.Write:
69                                 Demand(FileIOPermissionAccess.Write, path);
70                                 break;
71                         case FileAccess.ReadWrite:
72                                 Demand(FileIOPermissionAccess.Read | FileIOPermissionAccess.Write, path);
73                                 break;
74                         default:
75                                 // TODO: determine what best to do here
76                                 throw new ArgumentException("Invalid FileAccess parameter");
77                         }
78                 }
79
80                 [MonoTODO]
81                 public static void ModeAccess(FileMode mode, FileAccess access, string path, bool exists)
82                 {
83 #if false
84                         // TODO: this logic isn't entirely complete and accurate, yet
85                         if((mode & (FileMode.CreateNew | FileMode.Create)) != 0)
86                         {
87                                 CheckPermission.Demand(FileIOPermissionAccess.Write, Path.GetDirectoryName(path));
88                         }
89                         else if((mode & FileMode.OpenOrCreate) != 0)
90                         {
91                                 if(!exists)
92                                 {
93                                         CheckPermission.Demand(FileIOPermissionAccess.Write, Path.GetDirectoryName(path));
94                                 }
95                                 else
96                                 {
97                                         CheckPermission.Access(access, path);
98                                 }
99                         }
100                         else if(exists)
101                         {
102                                 CheckPermission.Access(access, path);
103                         }
104                         else
105                         {
106                                 throw new FileNotFoundException("File not found", path);
107                         }
108 #endif
109                 }
110         }
111 }       // namespace System.IO.Private