mscorlib(4.0.0.0) API with additions
FileIOPermission.cs
1 using System.Collections;
2 using System.IO;
7 using System.Security.Util;
8 
10 {
12  [Serializable]
13  [ComVisible(true)]
14  public sealed class FileIOPermission : CodeAccessPermission, IUnrestrictedPermission, IBuiltInPermission
15  {
16  private FileIOAccess m_read;
17 
18  private FileIOAccess m_write;
19 
20  private FileIOAccess m_append;
21 
22  private FileIOAccess m_pathDiscovery;
23 
24  [OptionalField(VersionAdded = 2)]
25  private FileIOAccess m_viewAcl;
26 
27  [OptionalField(VersionAdded = 2)]
28  private FileIOAccess m_changeAcl;
29 
30  private bool m_unrestricted;
31 
35  {
36  get
37  {
38  if (m_unrestricted)
39  {
40  return FileIOPermissionAccess.AllAccess;
41  }
42  FileIOPermissionAccess fileIOPermissionAccess = FileIOPermissionAccess.NoAccess;
43  if (m_read != null && m_read.AllLocalFiles)
44  {
45  fileIOPermissionAccess |= FileIOPermissionAccess.Read;
46  }
47  if (m_write != null && m_write.AllLocalFiles)
48  {
49  fileIOPermissionAccess |= FileIOPermissionAccess.Write;
50  }
51  if (m_append != null && m_append.AllLocalFiles)
52  {
53  fileIOPermissionAccess |= FileIOPermissionAccess.Append;
54  }
55  if (m_pathDiscovery != null && m_pathDiscovery.AllLocalFiles)
56  {
57  fileIOPermissionAccess |= FileIOPermissionAccess.PathDiscovery;
58  }
59  return fileIOPermissionAccess;
60  }
61  set
62  {
63  if ((value & FileIOPermissionAccess.Read) != 0)
64  {
65  if (m_read == null)
66  {
67  m_read = new FileIOAccess();
68  }
69  m_read.AllLocalFiles = true;
70  }
71  else if (m_read != null)
72  {
73  m_read.AllLocalFiles = false;
74  }
75  if ((value & FileIOPermissionAccess.Write) != 0)
76  {
77  if (m_write == null)
78  {
79  m_write = new FileIOAccess();
80  }
81  m_write.AllLocalFiles = true;
82  }
83  else if (m_write != null)
84  {
85  m_write.AllLocalFiles = false;
86  }
87  if ((value & FileIOPermissionAccess.Append) != 0)
88  {
89  if (m_append == null)
90  {
91  m_append = new FileIOAccess();
92  }
93  m_append.AllLocalFiles = true;
94  }
95  else if (m_append != null)
96  {
97  m_append.AllLocalFiles = false;
98  }
99  if ((value & FileIOPermissionAccess.PathDiscovery) != 0)
100  {
101  if (m_pathDiscovery == null)
102  {
103  m_pathDiscovery = new FileIOAccess(pathDiscovery: true);
104  }
105  m_pathDiscovery.AllLocalFiles = true;
106  }
107  else if (m_pathDiscovery != null)
108  {
109  m_pathDiscovery.AllLocalFiles = false;
110  }
111  }
112  }
113 
117  {
118  get
119  {
120  if (m_unrestricted)
121  {
122  return FileIOPermissionAccess.AllAccess;
123  }
124  FileIOPermissionAccess fileIOPermissionAccess = FileIOPermissionAccess.NoAccess;
125  if (m_read != null && m_read.AllFiles)
126  {
127  fileIOPermissionAccess |= FileIOPermissionAccess.Read;
128  }
129  if (m_write != null && m_write.AllFiles)
130  {
131  fileIOPermissionAccess |= FileIOPermissionAccess.Write;
132  }
133  if (m_append != null && m_append.AllFiles)
134  {
135  fileIOPermissionAccess |= FileIOPermissionAccess.Append;
136  }
137  if (m_pathDiscovery != null && m_pathDiscovery.AllFiles)
138  {
139  fileIOPermissionAccess |= FileIOPermissionAccess.PathDiscovery;
140  }
141  return fileIOPermissionAccess;
142  }
143  set
144  {
145  if (value == FileIOPermissionAccess.AllAccess)
146  {
147  m_unrestricted = true;
148  return;
149  }
150  if ((value & FileIOPermissionAccess.Read) != 0)
151  {
152  if (m_read == null)
153  {
154  m_read = new FileIOAccess();
155  }
156  m_read.AllFiles = true;
157  }
158  else if (m_read != null)
159  {
160  m_read.AllFiles = false;
161  }
162  if ((value & FileIOPermissionAccess.Write) != 0)
163  {
164  if (m_write == null)
165  {
166  m_write = new FileIOAccess();
167  }
168  m_write.AllFiles = true;
169  }
170  else if (m_write != null)
171  {
172  m_write.AllFiles = false;
173  }
174  if ((value & FileIOPermissionAccess.Append) != 0)
175  {
176  if (m_append == null)
177  {
178  m_append = new FileIOAccess();
179  }
180  m_append.AllFiles = true;
181  }
182  else if (m_append != null)
183  {
184  m_append.AllFiles = false;
185  }
186  if ((value & FileIOPermissionAccess.PathDiscovery) != 0)
187  {
188  if (m_pathDiscovery == null)
189  {
190  m_pathDiscovery = new FileIOAccess(pathDiscovery: true);
191  }
192  m_pathDiscovery.AllFiles = true;
193  }
194  else if (m_pathDiscovery != null)
195  {
196  m_pathDiscovery.AllFiles = false;
197  }
198  }
199  }
200 
205  {
206  switch (state)
207  {
208  case PermissionState.Unrestricted:
209  m_unrestricted = true;
210  break;
211  case PermissionState.None:
212  m_unrestricted = false;
213  break;
214  default:
215  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidPermissionState"));
216  }
217  }
218 
223  [SecuritySafeCritical]
224  public FileIOPermission(FileIOPermissionAccess access, string path)
225  {
226  VerifyAccess(access);
227  string[] pathListOrig = new string[1]
228  {
229  path
230  };
231  AddPathList(access, pathListOrig, checkForDuplicates: false, needFullPath: true, copyPathList: false);
232  }
233 
238  [SecuritySafeCritical]
239  public FileIOPermission(FileIOPermissionAccess access, string[] pathList)
240  {
241  VerifyAccess(access);
242  AddPathList(access, pathList, checkForDuplicates: false, needFullPath: true, copyPathList: false);
243  }
244 
250  [SecuritySafeCritical]
251  public FileIOPermission(FileIOPermissionAccess access, AccessControlActions control, string path)
252  {
253  VerifyAccess(access);
254  string[] pathListOrig = new string[1]
255  {
256  path
257  };
258  AddPathList(access, control, pathListOrig, checkForDuplicates: false, needFullPath: true, copyPathList: false);
259  }
260 
266  [SecuritySafeCritical]
267  public FileIOPermission(FileIOPermissionAccess access, AccessControlActions control, string[] pathList)
268  : this(access, control, pathList, checkForDuplicates: true, needFullPath: true)
269  {
270  }
271 
272  [SecurityCritical]
273  internal FileIOPermission(FileIOPermissionAccess access, string[] pathList, bool checkForDuplicates, bool needFullPath)
274  {
275  VerifyAccess(access);
276  AddPathList(access, pathList, checkForDuplicates, needFullPath, copyPathList: true);
277  }
278 
279  [SecurityCritical]
280  internal FileIOPermission(FileIOPermissionAccess access, AccessControlActions control, string[] pathList, bool checkForDuplicates, bool needFullPath)
281  {
282  VerifyAccess(access);
283  AddPathList(access, control, pathList, checkForDuplicates, needFullPath, copyPathList: true);
284  }
285 
290  public void SetPathList(FileIOPermissionAccess access, string path)
291  {
292  string[] pathList = (path != null) ? new string[1]
293  {
294  path
295  } : new string[0];
296  SetPathList(access, pathList, checkForDuplicates: false);
297  }
298 
303  public void SetPathList(FileIOPermissionAccess access, string[] pathList)
304  {
305  SetPathList(access, pathList, checkForDuplicates: true);
306  }
307 
308  internal void SetPathList(FileIOPermissionAccess access, string[] pathList, bool checkForDuplicates)
309  {
310  SetPathList(access, AccessControlActions.None, pathList, checkForDuplicates);
311  }
312 
313  [SecuritySafeCritical]
314  internal void SetPathList(FileIOPermissionAccess access, AccessControlActions control, string[] pathList, bool checkForDuplicates)
315  {
316  VerifyAccess(access);
317  if ((access & FileIOPermissionAccess.Read) != 0)
318  {
319  m_read = null;
320  }
321  if ((access & FileIOPermissionAccess.Write) != 0)
322  {
323  m_write = null;
324  }
325  if ((access & FileIOPermissionAccess.Append) != 0)
326  {
327  m_append = null;
328  }
329  if ((access & FileIOPermissionAccess.PathDiscovery) != 0)
330  {
331  m_pathDiscovery = null;
332  }
333  if ((control & AccessControlActions.View) != 0)
334  {
335  m_viewAcl = null;
336  }
337  if ((control & AccessControlActions.Change) != 0)
338  {
339  m_changeAcl = null;
340  }
341  m_unrestricted = false;
342  AddPathList(access, control, pathList, checkForDuplicates, needFullPath: true, copyPathList: true);
343  }
344 
351  [SecuritySafeCritical]
352  public void AddPathList(FileIOPermissionAccess access, string path)
353  {
354  string[] pathListOrig = (path != null) ? new string[1]
355  {
356  path
357  } : new string[0];
358  AddPathList(access, pathListOrig, checkForDuplicates: false, needFullPath: true, copyPathList: false);
359  }
360 
367  [SecuritySafeCritical]
368  public void AddPathList(FileIOPermissionAccess access, string[] pathList)
369  {
370  AddPathList(access, pathList, checkForDuplicates: true, needFullPath: true, copyPathList: true);
371  }
372 
373  [SecurityCritical]
374  internal void AddPathList(FileIOPermissionAccess access, string[] pathListOrig, bool checkForDuplicates, bool needFullPath, bool copyPathList)
375  {
376  AddPathList(access, AccessControlActions.None, pathListOrig, checkForDuplicates, needFullPath, copyPathList);
377  }
378 
379  [SecurityCritical]
380  internal void AddPathList(FileIOPermissionAccess access, AccessControlActions control, string[] pathListOrig, bool checkForDuplicates, bool needFullPath, bool copyPathList)
381  {
382  if (pathListOrig == null)
383  {
384  throw new ArgumentNullException("pathList");
385  }
386  if (pathListOrig.Length == 0)
387  {
388  throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"));
389  }
390  VerifyAccess(access);
391  if (m_unrestricted)
392  {
393  return;
394  }
395  string[] array = pathListOrig;
396  if (copyPathList)
397  {
398  array = new string[pathListOrig.Length];
399  Array.Copy(pathListOrig, array, pathListOrig.Length);
400  }
401  CheckIllegalCharacters(array, needFullPath);
402  ArrayList values = StringExpressionSet.CreateListFromExpressions(array, needFullPath);
403  if ((access & FileIOPermissionAccess.Read) != 0)
404  {
405  if (m_read == null)
406  {
407  m_read = new FileIOAccess();
408  }
409  m_read.AddExpressions(values, checkForDuplicates);
410  }
411  if ((access & FileIOPermissionAccess.Write) != 0)
412  {
413  if (m_write == null)
414  {
415  m_write = new FileIOAccess();
416  }
417  m_write.AddExpressions(values, checkForDuplicates);
418  }
419  if ((access & FileIOPermissionAccess.Append) != 0)
420  {
421  if (m_append == null)
422  {
423  m_append = new FileIOAccess();
424  }
425  m_append.AddExpressions(values, checkForDuplicates);
426  }
427  if ((access & FileIOPermissionAccess.PathDiscovery) != 0)
428  {
429  if (m_pathDiscovery == null)
430  {
431  m_pathDiscovery = new FileIOAccess(pathDiscovery: true);
432  }
433  m_pathDiscovery.AddExpressions(values, checkForDuplicates);
434  }
435  if ((control & AccessControlActions.View) != 0)
436  {
437  if (m_viewAcl == null)
438  {
439  m_viewAcl = new FileIOAccess();
440  }
441  m_viewAcl.AddExpressions(values, checkForDuplicates);
442  }
443  if ((control & AccessControlActions.Change) != 0)
444  {
445  if (m_changeAcl == null)
446  {
447  m_changeAcl = new FileIOAccess();
448  }
449  m_changeAcl.AddExpressions(values, checkForDuplicates);
450  }
451  }
452 
459  [SecuritySafeCritical]
460  public string[] GetPathList(FileIOPermissionAccess access)
461  {
462  VerifyAccess(access);
463  ExclusiveAccess(access);
464  if (AccessIsSet(access, FileIOPermissionAccess.Read))
465  {
466  if (m_read == null)
467  {
468  return null;
469  }
470  return m_read.ToStringArray();
471  }
472  if (AccessIsSet(access, FileIOPermissionAccess.Write))
473  {
474  if (m_write == null)
475  {
476  return null;
477  }
478  return m_write.ToStringArray();
479  }
480  if (AccessIsSet(access, FileIOPermissionAccess.Append))
481  {
482  if (m_append == null)
483  {
484  return null;
485  }
486  return m_append.ToStringArray();
487  }
488  if (AccessIsSet(access, FileIOPermissionAccess.PathDiscovery))
489  {
490  if (m_pathDiscovery == null)
491  {
492  return null;
493  }
494  return m_pathDiscovery.ToStringArray();
495  }
496  return null;
497  }
498 
499  private static void VerifyAccess(FileIOPermissionAccess access)
500  {
501  if ((access & ~(FileIOPermissionAccess.Read | FileIOPermissionAccess.Write | FileIOPermissionAccess.Append | FileIOPermissionAccess.PathDiscovery)) != 0)
502  {
503  throw new ArgumentException(Environment.GetResourceString("Arg_EnumIllegalVal", (int)access));
504  }
505  }
506 
507  private static void ExclusiveAccess(FileIOPermissionAccess access)
508  {
509  if (access == FileIOPermissionAccess.NoAccess)
510  {
511  throw new ArgumentException(Environment.GetResourceString("Arg_EnumNotSingleFlag"));
512  }
513  if ((access & (access - 1)) != 0)
514  {
515  throw new ArgumentException(Environment.GetResourceString("Arg_EnumNotSingleFlag"));
516  }
517  }
518 
519  private static void CheckIllegalCharacters(string[] str, bool onlyCheckExtras)
520  {
521  int num = 0;
522  while (true)
523  {
524  if (num < str.Length)
525  {
526  if (str[num] == null)
527  {
528  throw new ArgumentNullException("path");
529  }
530  if (CheckExtraPathCharacters(str[num]))
531  {
532  break;
533  }
534  if (!onlyCheckExtras)
535  {
536  Path.CheckInvalidPathChars(str[num]);
537  }
538  num++;
539  continue;
540  }
541  return;
542  }
543  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidPathChars"));
544  }
545 
546  [MethodImpl(MethodImplOptions.AggressiveInlining)]
547  [SecuritySafeCritical]
548  private static bool CheckExtraPathCharacters(string path)
549  {
550  int num = (CodeAccessSecurityEngine.QuickCheckForAllDemands() && !AppContextSwitches.UseLegacyPathHandling) ? (PathInternal.IsDevice(path) ? 4 : 0) : 0;
551  for (int i = num; i < path.Length; i++)
552  {
553  char c = path[i];
554  if (c == '*' || c == '?' || c == '\0')
555  {
556  return true;
557  }
558  }
559  return false;
560  }
561 
562  private static bool AccessIsSet(FileIOPermissionAccess access, FileIOPermissionAccess question)
563  {
564  return (access & question) != FileIOPermissionAccess.NoAccess;
565  }
566 
567  private bool IsEmpty()
568  {
569  if (!m_unrestricted && (m_read == null || m_read.IsEmpty()) && (m_write == null || m_write.IsEmpty()) && (m_append == null || m_append.IsEmpty()) && (m_pathDiscovery == null || m_pathDiscovery.IsEmpty()) && (m_viewAcl == null || m_viewAcl.IsEmpty()))
570  {
571  if (m_changeAcl != null)
572  {
573  return m_changeAcl.IsEmpty();
574  }
575  return true;
576  }
577  return false;
578  }
579 
583  public bool IsUnrestricted()
584  {
585  return m_unrestricted;
586  }
587 
593  public override bool IsSubsetOf(IPermission target)
594  {
595  if (target == null)
596  {
597  return IsEmpty();
598  }
599  FileIOPermission fileIOPermission = target as FileIOPermission;
600  if (fileIOPermission == null)
601  {
602  throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", GetType().FullName));
603  }
604  if (fileIOPermission.IsUnrestricted())
605  {
606  return true;
607  }
608  if (IsUnrestricted())
609  {
610  return false;
611  }
612  if ((m_read == null || m_read.IsSubsetOf(fileIOPermission.m_read)) && (m_write == null || m_write.IsSubsetOf(fileIOPermission.m_write)) && (m_append == null || m_append.IsSubsetOf(fileIOPermission.m_append)) && (m_pathDiscovery == null || m_pathDiscovery.IsSubsetOf(fileIOPermission.m_pathDiscovery)) && (m_viewAcl == null || m_viewAcl.IsSubsetOf(fileIOPermission.m_viewAcl)))
613  {
614  if (m_changeAcl != null)
615  {
616  return m_changeAcl.IsSubsetOf(fileIOPermission.m_changeAcl);
617  }
618  return true;
619  }
620  return false;
621  }
622 
627  public override IPermission Intersect(IPermission target)
628  {
629  if (target == null)
630  {
631  return null;
632  }
633  FileIOPermission fileIOPermission = target as FileIOPermission;
634  if (fileIOPermission == null)
635  {
636  throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", GetType().FullName));
637  }
638  if (IsUnrestricted())
639  {
640  return target.Copy();
641  }
642  if (fileIOPermission.IsUnrestricted())
643  {
644  return Copy();
645  }
646  FileIOAccess fileIOAccess = (m_read == null) ? null : m_read.Intersect(fileIOPermission.m_read);
647  FileIOAccess fileIOAccess2 = (m_write == null) ? null : m_write.Intersect(fileIOPermission.m_write);
648  FileIOAccess fileIOAccess3 = (m_append == null) ? null : m_append.Intersect(fileIOPermission.m_append);
649  FileIOAccess fileIOAccess4 = (m_pathDiscovery == null) ? null : m_pathDiscovery.Intersect(fileIOPermission.m_pathDiscovery);
650  FileIOAccess fileIOAccess5 = (m_viewAcl == null) ? null : m_viewAcl.Intersect(fileIOPermission.m_viewAcl);
651  FileIOAccess fileIOAccess6 = (m_changeAcl == null) ? null : m_changeAcl.Intersect(fileIOPermission.m_changeAcl);
652  if ((fileIOAccess == null || fileIOAccess.IsEmpty()) && (fileIOAccess2 == null || fileIOAccess2.IsEmpty()) && (fileIOAccess3 == null || fileIOAccess3.IsEmpty()) && (fileIOAccess4 == null || fileIOAccess4.IsEmpty()) && (fileIOAccess5 == null || fileIOAccess5.IsEmpty()) && (fileIOAccess6 == null || fileIOAccess6.IsEmpty()))
653  {
654  return null;
655  }
656  FileIOPermission fileIOPermission2 = new FileIOPermission(PermissionState.None);
657  fileIOPermission2.m_unrestricted = false;
658  fileIOPermission2.m_read = fileIOAccess;
659  fileIOPermission2.m_write = fileIOAccess2;
660  fileIOPermission2.m_append = fileIOAccess3;
661  fileIOPermission2.m_pathDiscovery = fileIOAccess4;
662  fileIOPermission2.m_viewAcl = fileIOAccess5;
663  fileIOPermission2.m_changeAcl = fileIOAccess6;
664  return fileIOPermission2;
665  }
666 
671  public override IPermission Union(IPermission other)
672  {
673  if (other == null)
674  {
675  return Copy();
676  }
677  FileIOPermission fileIOPermission = other as FileIOPermission;
678  if (fileIOPermission == null)
679  {
680  throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", GetType().FullName));
681  }
682  if (IsUnrestricted() || fileIOPermission.IsUnrestricted())
683  {
684  return new FileIOPermission(PermissionState.Unrestricted);
685  }
686  FileIOAccess fileIOAccess = (m_read == null) ? fileIOPermission.m_read : m_read.Union(fileIOPermission.m_read);
687  FileIOAccess fileIOAccess2 = (m_write == null) ? fileIOPermission.m_write : m_write.Union(fileIOPermission.m_write);
688  FileIOAccess fileIOAccess3 = (m_append == null) ? fileIOPermission.m_append : m_append.Union(fileIOPermission.m_append);
689  FileIOAccess fileIOAccess4 = (m_pathDiscovery == null) ? fileIOPermission.m_pathDiscovery : m_pathDiscovery.Union(fileIOPermission.m_pathDiscovery);
690  FileIOAccess fileIOAccess5 = (m_viewAcl == null) ? fileIOPermission.m_viewAcl : m_viewAcl.Union(fileIOPermission.m_viewAcl);
691  FileIOAccess fileIOAccess6 = (m_changeAcl == null) ? fileIOPermission.m_changeAcl : m_changeAcl.Union(fileIOPermission.m_changeAcl);
692  if ((fileIOAccess == null || fileIOAccess.IsEmpty()) && (fileIOAccess2 == null || fileIOAccess2.IsEmpty()) && (fileIOAccess3 == null || fileIOAccess3.IsEmpty()) && (fileIOAccess4 == null || fileIOAccess4.IsEmpty()) && (fileIOAccess5 == null || fileIOAccess5.IsEmpty()) && (fileIOAccess6 == null || fileIOAccess6.IsEmpty()))
693  {
694  return null;
695  }
696  FileIOPermission fileIOPermission2 = new FileIOPermission(PermissionState.None);
697  fileIOPermission2.m_unrestricted = false;
698  fileIOPermission2.m_read = fileIOAccess;
699  fileIOPermission2.m_write = fileIOAccess2;
700  fileIOPermission2.m_append = fileIOAccess3;
701  fileIOPermission2.m_pathDiscovery = fileIOAccess4;
702  fileIOPermission2.m_viewAcl = fileIOAccess5;
703  fileIOPermission2.m_changeAcl = fileIOAccess6;
704  return fileIOPermission2;
705  }
706 
709  public override IPermission Copy()
710  {
711  FileIOPermission fileIOPermission = new FileIOPermission(PermissionState.None);
712  if (m_unrestricted)
713  {
714  fileIOPermission.m_unrestricted = true;
715  }
716  else
717  {
718  fileIOPermission.m_unrestricted = false;
719  if (m_read != null)
720  {
721  fileIOPermission.m_read = m_read.Copy();
722  }
723  if (m_write != null)
724  {
725  fileIOPermission.m_write = m_write.Copy();
726  }
727  if (m_append != null)
728  {
729  fileIOPermission.m_append = m_append.Copy();
730  }
731  if (m_pathDiscovery != null)
732  {
733  fileIOPermission.m_pathDiscovery = m_pathDiscovery.Copy();
734  }
735  if (m_viewAcl != null)
736  {
737  fileIOPermission.m_viewAcl = m_viewAcl.Copy();
738  }
739  if (m_changeAcl != null)
740  {
741  fileIOPermission.m_changeAcl = m_changeAcl.Copy();
742  }
743  }
744  return fileIOPermission;
745  }
746 
749  public override SecurityElement ToXml()
750  {
751  SecurityElement securityElement = CodeAccessPermission.CreatePermissionElement(this, "System.Security.Permissions.FileIOPermission");
752  if (!IsUnrestricted())
753  {
754  if (m_read != null && !m_read.IsEmpty())
755  {
756  securityElement.AddAttribute("Read", SecurityElement.Escape(m_read.ToString()));
757  }
758  if (m_write != null && !m_write.IsEmpty())
759  {
760  securityElement.AddAttribute("Write", SecurityElement.Escape(m_write.ToString()));
761  }
762  if (m_append != null && !m_append.IsEmpty())
763  {
764  securityElement.AddAttribute("Append", SecurityElement.Escape(m_append.ToString()));
765  }
766  if (m_pathDiscovery != null && !m_pathDiscovery.IsEmpty())
767  {
768  securityElement.AddAttribute("PathDiscovery", SecurityElement.Escape(m_pathDiscovery.ToString()));
769  }
770  if (m_viewAcl != null && !m_viewAcl.IsEmpty())
771  {
772  securityElement.AddAttribute("ViewAcl", SecurityElement.Escape(m_viewAcl.ToString()));
773  }
774  if (m_changeAcl != null && !m_changeAcl.IsEmpty())
775  {
776  securityElement.AddAttribute("ChangeAcl", SecurityElement.Escape(m_changeAcl.ToString()));
777  }
778  }
779  else
780  {
781  securityElement.AddAttribute("Unrestricted", "true");
782  }
783  return securityElement;
784  }
785 
790  [SecuritySafeCritical]
791  public override void FromXml(SecurityElement esd)
792  {
793  CodeAccessPermission.ValidateElement(esd, this);
794  if (XMLUtil.IsUnrestricted(esd))
795  {
796  m_unrestricted = true;
797  return;
798  }
799  m_unrestricted = false;
800  string text = esd.Attribute("Read");
801  if (text != null)
802  {
803  m_read = new FileIOAccess(text);
804  }
805  else
806  {
807  m_read = null;
808  }
809  text = esd.Attribute("Write");
810  if (text != null)
811  {
812  m_write = new FileIOAccess(text);
813  }
814  else
815  {
816  m_write = null;
817  }
818  text = esd.Attribute("Append");
819  if (text != null)
820  {
821  m_append = new FileIOAccess(text);
822  }
823  else
824  {
825  m_append = null;
826  }
827  text = esd.Attribute("PathDiscovery");
828  if (text != null)
829  {
830  m_pathDiscovery = new FileIOAccess(text);
831  m_pathDiscovery.PathDiscovery = true;
832  }
833  else
834  {
835  m_pathDiscovery = null;
836  }
837  text = esd.Attribute("ViewAcl");
838  if (text != null)
839  {
840  m_viewAcl = new FileIOAccess(text);
841  }
842  else
843  {
844  m_viewAcl = null;
845  }
846  text = esd.Attribute("ChangeAcl");
847  if (text != null)
848  {
849  m_changeAcl = new FileIOAccess(text);
850  }
851  else
852  {
853  m_changeAcl = null;
854  }
855  }
856 
857  int IBuiltInPermission.GetTokenIndex()
858  {
859  return GetTokenIndex();
860  }
861 
862  internal static int GetTokenIndex()
863  {
864  return 2;
865  }
866 
871  [ComVisible(false)]
872  public override bool Equals(object obj)
873  {
874  FileIOPermission fileIOPermission = obj as FileIOPermission;
875  if (fileIOPermission == null)
876  {
877  return false;
878  }
879  if (m_unrestricted && fileIOPermission.m_unrestricted)
880  {
881  return true;
882  }
883  if (m_unrestricted != fileIOPermission.m_unrestricted)
884  {
885  return false;
886  }
887  if (m_read == null)
888  {
889  if (fileIOPermission.m_read != null && !fileIOPermission.m_read.IsEmpty())
890  {
891  return false;
892  }
893  }
894  else if (!m_read.Equals(fileIOPermission.m_read))
895  {
896  return false;
897  }
898  if (m_write == null)
899  {
900  if (fileIOPermission.m_write != null && !fileIOPermission.m_write.IsEmpty())
901  {
902  return false;
903  }
904  }
905  else if (!m_write.Equals(fileIOPermission.m_write))
906  {
907  return false;
908  }
909  if (m_append == null)
910  {
911  if (fileIOPermission.m_append != null && !fileIOPermission.m_append.IsEmpty())
912  {
913  return false;
914  }
915  }
916  else if (!m_append.Equals(fileIOPermission.m_append))
917  {
918  return false;
919  }
920  if (m_pathDiscovery == null)
921  {
922  if (fileIOPermission.m_pathDiscovery != null && !fileIOPermission.m_pathDiscovery.IsEmpty())
923  {
924  return false;
925  }
926  }
927  else if (!m_pathDiscovery.Equals(fileIOPermission.m_pathDiscovery))
928  {
929  return false;
930  }
931  if (m_viewAcl == null)
932  {
933  if (fileIOPermission.m_viewAcl != null && !fileIOPermission.m_viewAcl.IsEmpty())
934  {
935  return false;
936  }
937  }
938  else if (!m_viewAcl.Equals(fileIOPermission.m_viewAcl))
939  {
940  return false;
941  }
942  if (m_changeAcl == null)
943  {
944  if (fileIOPermission.m_changeAcl != null && !fileIOPermission.m_changeAcl.IsEmpty())
945  {
946  return false;
947  }
948  }
949  else if (!m_changeAcl.Equals(fileIOPermission.m_changeAcl))
950  {
951  return false;
952  }
953  return true;
954  }
955 
958  [ComVisible(false)]
959  public override int GetHashCode()
960  {
961  return base.GetHashCode();
962  }
963 
964  [SecuritySafeCritical]
965  internal static void QuickDemand(FileIOPermissionAccess access, string fullPath, bool checkForDuplicates = false, bool needFullPath = true)
966  {
967  if (!CodeAccessSecurityEngine.QuickCheckForAllDemands())
968  {
969  new FileIOPermission(access, new string[1]
970  {
971  fullPath
972  }, checkForDuplicates, needFullPath).Demand();
973  }
974  else
975  {
976  EmulateFileIOPermissionChecks(fullPath);
977  }
978  }
979 
980  [SecuritySafeCritical]
981  internal static void QuickDemand(FileIOPermissionAccess access, string[] fullPathList, bool checkForDuplicates = false, bool needFullPath = true)
982  {
983  if (!CodeAccessSecurityEngine.QuickCheckForAllDemands())
984  {
985  new FileIOPermission(access, fullPathList, checkForDuplicates, needFullPath).Demand();
986  return;
987  }
988  foreach (string fullPath in fullPathList)
989  {
990  EmulateFileIOPermissionChecks(fullPath);
991  }
992  }
993 
994  [SecuritySafeCritical]
995  internal static void QuickDemand(PermissionState state)
996  {
997  if (!CodeAccessSecurityEngine.QuickCheckForAllDemands())
998  {
999  new FileIOPermission(state).Demand();
1000  }
1001  }
1002 
1003  [SecuritySafeCritical]
1004  internal static void QuickDemand(FileIOPermissionAccess access, AccessControlActions control, string fullPath, bool checkForDuplicates = false, bool needFullPath = true)
1005  {
1006  if (!CodeAccessSecurityEngine.QuickCheckForAllDemands())
1007  {
1008  new FileIOPermission(access, control, new string[1]
1009  {
1010  fullPath
1011  }, checkForDuplicates, needFullPath).Demand();
1012  }
1013  else
1014  {
1015  EmulateFileIOPermissionChecks(fullPath);
1016  }
1017  }
1018 
1019  [SecuritySafeCritical]
1020  internal static void QuickDemand(FileIOPermissionAccess access, AccessControlActions control, string[] fullPathList, bool checkForDuplicates = true, bool needFullPath = true)
1021  {
1022  if (!CodeAccessSecurityEngine.QuickCheckForAllDemands())
1023  {
1024  new FileIOPermission(access, control, fullPathList, checkForDuplicates, needFullPath).Demand();
1025  return;
1026  }
1027  foreach (string fullPath in fullPathList)
1028  {
1029  EmulateFileIOPermissionChecks(fullPath);
1030  }
1031  }
1032 
1033  internal static void EmulateFileIOPermissionChecks(string fullPath)
1034  {
1035  if (AppContextSwitches.UseLegacyPathHandling || !PathInternal.IsDevice(fullPath))
1036  {
1037  if (PathInternal.HasWildCardCharacters(fullPath))
1038  {
1039  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidPathChars"));
1040  }
1041  if (PathInternal.HasInvalidVolumeSeparator(fullPath))
1042  {
1043  throw new NotSupportedException(Environment.GetResourceString("Argument_PathFormatNotSupported"));
1044  }
1045  }
1046  }
1047  }
1048 }
override SecurityElement ToXml()
Creates an XML encoding of the permission and its current state.
Allows a permission to expose an unrestricted state.
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
FileIOPermissionAccess
Specifies the type of file access requested.
void SetPathList(FileIOPermissionAccess access, string path)
Sets the specified access to the specified file or directory, replacing the existing state of the per...
Definition: __Canon.cs:3
IPermission Copy()
Creates and returns an identical copy of the current permission.
bool IsUnrestricted()
Returns a value indicating whether the current permission is unrestricted.
FileIOPermission(PermissionState state)
Initializes a new instance of the T:System.Security.Permissions.FileIOPermission class with fully res...
override int GetHashCode()
Gets a hash code for the T:System.Security.Permissions.FileIOPermission object that is suitable for u...
string [] GetPathList(FileIOPermissionAccess access)
Gets all files and directories with the specified T:System.Security.Permissions.FileIOPermissionAcces...
static string Escape(string str)
Replaces invalid XML characters in a string with their valid XML equivalent.
override void FromXml(SecurityElement esd)
Reconstructs a permission with a specified state from an XML encoding.
override IPermission Union(IPermission other)
Creates a permission that is the union of the current permission and the specified permission.
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
void AddPathList(FileIOPermissionAccess access, string path)
Adds access for the specified file or directory to the existing state of the permission.
Represents the XML object model for encoding security objects. This class cannot be inherited.
Defines the underlying structure of all code access permissions.
FileIOPermission(FileIOPermissionAccess access, string path)
Initializes a new instance of the T:System.Security.Permissions.FileIOPermission class with the speci...
MethodImplOptions
Defines the details of how a method is implemented.
FileIOPermissionAccess AllFiles
Gets or sets the permitted access to all files.
void SetPathList(FileIOPermissionAccess access, string[] pathList)
Sets the specified access to the specified files and directories, replacing the current state for the...
Defines methods implemented by permission types.
Definition: IPermission.cs:7
AccessControlActions
Specifies the actions that are permitted for securable objects.
The exception that is thrown when one of the arguments provided to a method is not valid.
void Demand()
Forces a T:System.Security.SecurityException at run time if all callers higher in the call stack have...
FileIOPermission(FileIOPermissionAccess access, string[] pathList)
Initializes a new instance of the T:System.Security.Permissions.FileIOPermission class with the speci...
override bool Equals(object obj)
Determines whether the specified T:System.Security.Permissions.FileIOPermission object is equal to th...
PermissionState
Specifies whether a permission should have all or no access to resources at creation.
override IPermission Copy()
Creates and returns an identical copy of the current permission.
FileIOPermission(FileIOPermissionAccess access, AccessControlActions control, string path)
Initializes a new instance of the T:System.Security.Permissions.FileIOPermission class with the speci...
void AddAttribute(string name, string value)
Adds a name/value attribute to an XML element.
Specifies that the class can be serialized.
Controls the ability to access files and folders. This class cannot be inherited.
FileIOPermission(FileIOPermissionAccess access, AccessControlActions control, string[] pathList)
Initializes a new instance of the T:System.Security.Permissions.FileIOPermission class with the speci...
FileIOPermissionAccess AllLocalFiles
Gets or sets the permitted access to all local files.
override bool IsSubsetOf(IPermission target)
Determines whether the current permission is a subset of the specified permission.
Performs operations on T:System.String instances that contain file or directory path information....
Definition: Path.cs:13
void AddPathList(FileIOPermissionAccess access, string[] pathList)
Adds access for the specified files and directories to the existing state of the permission.
Implements the T:System.Collections.IList interface using an array whose size is dynamically increase...
Definition: ArrayList.cs:14
override IPermission Intersect(IPermission target)
Creates and returns a permission that is the intersection of the current permission and the specified...