TempFileCollection.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. int ICollection.Count {
  83. get {
  84. return filehash.Count;
  85. }
  86. }
  87. public int Count
  88. {
  89. get {
  90. return(filehash.Count);
  91. }
  92. }
  93. public bool KeepFiles
  94. {
  95. get {
  96. return(keepfiles);
  97. }
  98. set {
  99. keepfiles=value;
  100. }
  101. }
  102. public string TempDir
  103. {
  104. get {
  105. // note: we only return what we were supplied so there
  106. // is no permission protecting this information
  107. return tempdir;
  108. }
  109. }
  110. public string AddExtension(string fileExtension)
  111. {
  112. return(AddExtension(fileExtension, keepfiles));
  113. }
  114. public string AddExtension(string fileExtension, bool keepFile)
  115. {
  116. string filename=BasePath+"."+fileExtension;
  117. AddFile(filename, keepFile);
  118. return(filename);
  119. }
  120. public void AddFile(string fileName, bool keepFile)
  121. {
  122. filehash.Add(fileName, keepFile);
  123. }
  124. public void CopyTo(string[] fileNames, int start)
  125. {
  126. filehash.Keys.CopyTo(fileNames, start);
  127. }
  128. void ICollection.CopyTo(Array array, int start)
  129. {
  130. filehash.Keys.CopyTo(array, start);
  131. }
  132. object ICollection.SyncRoot {
  133. get {
  134. return null;
  135. }
  136. }
  137. bool ICollection.IsSynchronized {
  138. get {
  139. return(false);
  140. }
  141. }
  142. void IDisposable.Dispose()
  143. {
  144. Dispose(true);
  145. }
  146. public void Delete()
  147. {
  148. string[] filenames=new string[filehash.Count];
  149. filehash.Keys.CopyTo(filenames, 0);
  150. foreach(string file in filenames) {
  151. if((bool)filehash[file]==false) {
  152. File.Delete(file);
  153. filehash.Remove(file);
  154. }
  155. }
  156. }
  157. IEnumerator IEnumerable.GetEnumerator ()
  158. {
  159. return(filehash.Keys.GetEnumerator());
  160. }
  161. public IEnumerator GetEnumerator()
  162. {
  163. return(filehash.Keys.GetEnumerator());
  164. }
  165. protected virtual void Dispose(bool disposing)
  166. {
  167. Delete();
  168. if (disposing) {
  169. GC.SuppressFinalize (true);
  170. }
  171. }
  172. ~TempFileCollection()
  173. {
  174. Dispose(false);
  175. }
  176. }
  177. }