2 // System.CodeDom.Compiler TempFileCollection Class implementation
5 // Dick Porter (dick@ximian.com)
7 // (C) Copyright 2003 Ximian, Inc.
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
30 using System.Collections;
32 using System.Security;
33 using System.Security.Permissions;
35 namespace System.CodeDom.Compiler {
40 [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
41 public class TempFileCollection:ICollection, IEnumerable, IDisposable
49 public TempFileCollection ()
50 : this (String.Empty, false)
54 public TempFileCollection(string tempDir)
55 : this (tempDir, false)
59 public TempFileCollection(string tempDir, bool keepFiles)
61 filehash=new Hashtable();
62 tempdir = (tempDir == null) ? String.Empty : tempDir;
66 public string BasePath
70 // note: this property *cannot* change TempDir property
71 string temp = tempdir;
72 if (temp.Length == 0) {
73 // this call ensure the Environment permissions check
74 temp = Path.GetTempPath ();
80 // Create a temporary file at the target directory. This ensures
81 // that the generated file name is unique.
84 int num = rnd.Next ();
86 basepath = Path.Combine (temp, num.ToString("x"));
87 string path = basepath + ".tmp";
90 f = new FileStream (path, FileMode.CreateNew);
92 catch (System.IO.IOException) {
104 // and you must have discovery access to the combined path
105 // note: the cache behaviour is tested in the CAS tests
106 if (SecurityManager.SecurityEnabled) {
107 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, basepath).Demand ();
115 int ICollection.Count {
117 return filehash.Count;
124 return(filehash.Count);
128 public bool KeepFiles
138 public string TempDir
141 // note: we only return what we were supplied so there
142 // is no permission protecting this information
147 public string AddExtension(string fileExtension)
149 return(AddExtension(fileExtension, keepfiles));
152 public string AddExtension(string fileExtension, bool keepFile)
154 string filename=BasePath+"."+fileExtension;
155 AddFile(filename, keepFile);
159 public void AddFile(string fileName, bool keepFile)
161 filehash.Add(fileName, keepFile);
164 public void CopyTo(string[] fileNames, int start)
166 filehash.Keys.CopyTo(fileNames, start);
169 void ICollection.CopyTo(Array array, int start)
171 filehash.Keys.CopyTo(array, start);
174 object ICollection.SyncRoot {
180 bool ICollection.IsSynchronized {
186 void IDisposable.Dispose()
193 string[] filenames=new string[filehash.Count];
194 filehash.Keys.CopyTo(filenames, 0);
196 foreach(string file in filenames) {
197 if((bool)filehash[file]==false) {
199 filehash.Remove(file);
202 if (basepath != null) {
203 string tmpFile = basepath + ".tmp";
204 File.Delete (tmpFile);
208 IEnumerator IEnumerable.GetEnumerator ()
210 return(filehash.Keys.GetEnumerator());
213 public IEnumerator GetEnumerator()
215 return(filehash.Keys.GetEnumerator());
218 protected virtual void Dispose(bool disposing)
222 GC.SuppressFinalize (true);
226 ~TempFileCollection()