1 //------------------------------------------------------------------------------
3 // System.IO.CheckPermission.cs
5 // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
7 // Author: Jim Richardson, develop@wtfo-guru.com
8 // Created: Saturday, August 25, 2001
10 // NOTE: All contributors can freely add to this class or make modifications
11 // that do not break existing usage of methods
12 //------------------------------------------------------------------------------
15 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
25 // The above copyright notice and this permission notice shall be
26 // included in all copies or substantial portions of the Software.
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.
39 using System.Security;
40 using System.Security.Permissions;
45 /// A utility class to assist with various permission validation in System.IO
47 internal sealed class CheckPermission
50 /// Generates and exception if caller doesn't have flags access to filesystem item specified by path
53 public static void Demand(FileIOPermissionAccess flags, string path)
55 //FileIOPermission ioPerm =
56 new FileIOPermission(flags, path);
57 // FIXME: FileIOPermission is not yet implemented
61 public static void Access(FileAccess access, string path)
66 Demand(FileIOPermissionAccess.Read, path);
68 case FileAccess.Write:
69 Demand(FileIOPermissionAccess.Write, path);
71 case FileAccess.ReadWrite:
72 Demand(FileIOPermissionAccess.Read | FileIOPermissionAccess.Write, path);
75 // TODO: determine what best to do here
76 throw new ArgumentException("Invalid FileAccess parameter");
81 public static void ModeAccess(FileMode mode, FileAccess access, string path, bool exists)
84 // TODO: this logic isn't entirely complete and accurate, yet
85 if((mode & (FileMode.CreateNew | FileMode.Create)) != 0)
87 CheckPermission.Demand(FileIOPermissionAccess.Write, Path.GetDirectoryName(path));
89 else if((mode & FileMode.OpenOrCreate) != 0)
93 CheckPermission.Demand(FileIOPermissionAccess.Write, Path.GetDirectoryName(path));
97 CheckPermission.Access(access, path);
102 CheckPermission.Access(access, path);
106 throw new FileNotFoundException("File not found", path);
111 } // namespace System.IO.Private