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;
34 using System.Runtime.InteropServices;
36 namespace System.CodeDom.Compiler {
41 [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
42 public class TempFileCollection:ICollection, IEnumerable, IDisposable
51 public TempFileCollection ()
52 : this (String.Empty, false)
56 public TempFileCollection(string tempDir)
57 : this (tempDir, false)
61 public TempFileCollection(string tempDir, bool keepFiles)
63 filehash=new Hashtable();
64 tempdir = (tempDir == null) ? String.Empty : tempDir;
68 public string BasePath
76 // note: this property *cannot* change TempDir property
77 string temp = tempdir;
79 temp = GetOwnTempDir ();
81 // Create a temporary file at the target directory. This ensures
82 // that the generated file name is unique.
85 int num = rnd.Next ();
87 basepath = Path.Combine (temp, num.ToString("x"));
88 string path = basepath + ".tmp";
91 f = new FileStream (path, FileMode.CreateNew);
93 catch (System.IO.IOException) {
105 // and you must have discovery access to the combined path
106 // note: the cache behaviour is tested in the CAS tests
107 if (SecurityManager.SecurityEnabled) {
108 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, basepath).Demand ();
116 string GetOwnTempDir ()
118 if (ownTempDir != null)
121 // this call ensure the Environment permissions check
122 string basedir = Path.GetTempPath ();
124 // Create a subdirectory with the correct user permissions
127 switch (Environment.OSVersion.Platform) {
128 case PlatformID.Win32S:
129 case PlatformID.Win32Windows:
130 case PlatformID.Win32NT:
131 case PlatformID.WinCE:
138 int num = rnd.Next ();
140 ownTempDir = Path.Combine (basedir, num.ToString("x"));
141 if (Directory.Exists (ownTempDir))
144 Directory.CreateDirectory (ownTempDir);
146 res = mkdir (ownTempDir, 0x1c0);
148 if (!Directory.Exists (ownTempDir))
149 throw new IOException ();
150 // Somebody already created the dir, keep trying
156 int ICollection.Count {
158 return filehash.Count;
165 return(filehash.Count);
169 public bool KeepFiles
179 public string TempDir
182 // note: we only return what we were supplied so there
183 // is no permission protecting this information
188 public string AddExtension(string fileExtension)
190 return(AddExtension(fileExtension, keepfiles));
193 public string AddExtension(string fileExtension, bool keepFile)
195 string filename=BasePath+"."+fileExtension;
196 AddFile(filename, keepFile);
200 public void AddFile(string fileName, bool keepFile)
202 filehash.Add(fileName, keepFile);
205 public void CopyTo(string[] fileNames, int start)
207 filehash.Keys.CopyTo(fileNames, start);
210 void ICollection.CopyTo(Array array, int start)
212 filehash.Keys.CopyTo(array, start);
215 object ICollection.SyncRoot {
221 bool ICollection.IsSynchronized {
227 void IDisposable.Dispose()
234 bool allDeleted = true;
235 string[] filenames = new string[filehash.Count];
236 filehash.Keys.CopyTo (filenames, 0);
238 foreach(string file in filenames) {
239 if((bool)filehash[file]==false) {
241 filehash.Remove(file);
245 if (basepath != null) {
246 string tmpFile = basepath + ".tmp";
247 File.Delete (tmpFile);
250 if (allDeleted && ownTempDir != null) {
251 Directory.Delete (ownTempDir, true);
256 IEnumerator IEnumerable.GetEnumerator ()
258 return(filehash.Keys.GetEnumerator());
261 public IEnumerator GetEnumerator()
263 return(filehash.Keys.GetEnumerator());
266 protected virtual void Dispose(bool disposing)
270 GC.SuppressFinalize (true);
274 ~TempFileCollection()
279 [DllImport ("libc")] private static extern int mkdir (string olpath, uint mode);