TempFileCollection.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //
  2. // System.CodeDom.Compiler TempFileCollection Class implementation
  3. //
  4. // Author:
  5. // Dick Porter ([email protected])
  6. //
  7. // (C) Copyright 2003 Ximian, Inc.
  8. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System.Collections;
  30. using System.IO;
  31. using System.Security;
  32. using System.Security.Permissions;
  33. namespace System.CodeDom.Compiler {
  34. #if NET_2_0
  35. [Serializable]
  36. #endif
  37. [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
  38. public class TempFileCollection:ICollection, IEnumerable, IDisposable
  39. {
  40. Hashtable filehash;
  41. string tempdir;
  42. bool keepfiles;
  43. string basepath;
  44. Random rnd;
  45. public TempFileCollection ()
  46. : this (String.Empty, false)
  47. {
  48. }
  49. public TempFileCollection(string tempDir)
  50. : this (tempDir, false)
  51. {
  52. }
  53. public TempFileCollection(string tempDir, bool keepFiles)
  54. {
  55. filehash=new Hashtable();
  56. tempdir = (tempDir == null) ? String.Empty : tempDir;
  57. keepfiles=keepFiles;
  58. }
  59. public string BasePath
  60. {
  61. get {
  62. if(basepath==null) {
  63. // note: this property *cannot* change TempDir property
  64. string temp = tempdir;
  65. if (temp.Length == 0) {
  66. // this call ensure the Environment permissions check
  67. temp = Path.GetTempPath ();
  68. }
  69. if (rnd == null)
  70. rnd = new Random ();
  71. string random = rnd.Next (10000,99999).ToString ();
  72. basepath = Path.Combine (temp, random);
  73. // and you must have discovery access to the combined path
  74. // note: the cache behaviour is tested in the CAS tests
  75. if (SecurityManager.SecurityEnabled) {
  76. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, basepath).Demand ();
  77. }
  78. }
  79. return(basepath);
  80. }
  81. }
  82. public int Count
  83. {
  84. get {
  85. return(filehash.Count);
  86. }
  87. }
  88. public bool KeepFiles
  89. {
  90. get {
  91. return(keepfiles);
  92. }
  93. set {
  94. keepfiles=value;
  95. }
  96. }
  97. public string TempDir
  98. {
  99. get {
  100. // note: we only return what we were supplied so there
  101. // is no permission protecting this information
  102. return tempdir;
  103. }
  104. }
  105. public string AddExtension(string fileExtension)
  106. {
  107. return(AddExtension(fileExtension, keepfiles));
  108. }
  109. public string AddExtension(string fileExtension, bool keepFile)
  110. {
  111. string filename=BasePath+"."+fileExtension;
  112. AddFile(filename, keepFile);
  113. return(filename);
  114. }
  115. public void AddFile(string fileName, bool keepFile)
  116. {
  117. filehash.Add(fileName, keepFile);
  118. }
  119. public void CopyTo(string[] fileNames, int start)
  120. {
  121. filehash.Keys.CopyTo(fileNames, start);
  122. }
  123. void ICollection.CopyTo(Array array, int start)
  124. {
  125. filehash.Keys.CopyTo(array, start);
  126. }
  127. object ICollection.SyncRoot {
  128. get {
  129. return null;
  130. }
  131. }
  132. bool ICollection.IsSynchronized {
  133. get {
  134. return(false);
  135. }
  136. }
  137. void IDisposable.Dispose()
  138. {
  139. Dispose(true);
  140. }
  141. public void Delete()
  142. {
  143. string[] filenames=new string[filehash.Count];
  144. filehash.Keys.CopyTo(filenames, 0);
  145. foreach(string file in filenames) {
  146. if((bool)filehash[file]==false) {
  147. File.Delete(file);
  148. filehash.Remove(file);
  149. }
  150. }
  151. }
  152. public IEnumerator GetEnumerator()
  153. {
  154. return(filehash.Keys.GetEnumerator());
  155. }
  156. protected virtual void Dispose(bool disposing)
  157. {
  158. Delete();
  159. if (disposing) {
  160. GC.SuppressFinalize (true);
  161. }
  162. }
  163. ~TempFileCollection()
  164. {
  165. Dispose(false);
  166. }
  167. }
  168. }