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 {
39 [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
40 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
74 // note: this property *cannot* change TempDir property
75 string temp = tempdir;
76 if (temp.Length == 0) {
77 if (ownTempDir != null) {
79 Directory.CreateDirectory (temp);
81 temp = CreateOwnTempDir ();
85 // Create a temporary file at the target directory. This ensures
86 // that the generated file name is unique.
87 int test_counter = 1000;
89 int num = rnd.Next ();
91 basepath = Path.Combine (temp, num.ToString("x"));
92 string path = basepath + ".tmp";
95 using (var f = new FileStream (path, FileMode.CreateNew)) {
98 } catch (IOException) {
99 if (test_counter-- > 0)
107 // and you must have discovery access to the combined path
108 // note: the cache behaviour is tested in the CAS tests
109 if (SecurityManager.SecurityEnabled) {
110 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, basepath).Demand ();
119 string CreateOwnTempDir ()
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 && filenames.Length > 0) {
251 Directory.Delete (ownTempDir, true);
255 IEnumerator IEnumerable.GetEnumerator ()
257 return(filehash.Keys.GetEnumerator());
260 public IEnumerator GetEnumerator()
262 return(filehash.Keys.GetEnumerator());
265 protected virtual void Dispose(bool disposing)
269 GC.SuppressFinalize (true);
273 ~TempFileCollection()
278 [DllImport ("libc")] private static extern int mkdir (string olpath, uint mode);