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 = new FileIOPermission(flags, path);
56 // FIXME: FileIOPermission is not yet implemented
60 public static void Access(FileAccess access, string path)
65 Demand(FileIOPermissionAccess.Read, path);
67 case FileAccess.Write:
68 Demand(FileIOPermissionAccess.Write, path);
70 case FileAccess.ReadWrite:
71 Demand(FileIOPermissionAccess.Read | FileIOPermissionAccess.Write, path);
74 // TODO: determine what best to do here
75 throw new ArgumentException("Invalid FileAccess parameter");
80 public static void ModeAccess(FileMode mode, FileAccess access, string path, bool exists)
83 // TODO: this logic isn't entirely complete and accurate, yet
84 if((mode & (FileMode.CreateNew | FileMode.Create)) != 0)
86 CheckPermission.Demand(FileIOPermissionAccess.Write, Path.GetDirectoryName(path));
88 else if((mode & FileMode.OpenOrCreate) != 0)
92 CheckPermission.Demand(FileIOPermissionAccess.Write, Path.GetDirectoryName(path));
96 CheckPermission.Access(access, path);
101 CheckPermission.Access(access, path);
105 throw new FileNotFoundException("File not found", path);
110 } // namespace System.IO.Private