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;
77 temp = GetOwnTempDir ();
79 // Create a temporary file at the target directory. This ensures
80 // that the generated file name is unique.
83 int num = rnd.Next ();
85 basepath = Path.Combine (temp, num.ToString("x"));
86 string path = basepath + ".tmp";
89 f = new FileStream (path, FileMode.CreateNew);
91 catch (System.IO.IOException) {
103 // and you must have discovery access to the combined path
104 // note: the cache behaviour is tested in the CAS tests
105 if (SecurityManager.SecurityEnabled) {
106 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, basepath).Demand ();
114 string GetOwnTempDir ()
116 if (ownTempDir != null)
119 // this call ensure the Environment permissions check
120 string basedir = Path.GetTempPath ();
122 // Create a subdirectory with the correct user permissions
125 switch (Environment.OSVersion.Platform) {
126 case PlatformID.Win32S:
127 case PlatformID.Win32Windows:
128 case PlatformID.Win32NT:
129 case PlatformID.WinCE:
136 int num = rnd.Next ();
138 ownTempDir = Path.Combine (basedir, num.ToString("x"));
139 if (Directory.Exists (ownTempDir))
142 Directory.CreateDirectory (ownTempDir);
144 res = mkdir (ownTempDir, 0x1c0);
146 if (!Directory.Exists (ownTempDir))
147 throw new IOException ();
148 // Somebody already created the dir, keep trying
154 int ICollection.Count {
156 return filehash.Count;
163 return(filehash.Count);
167 public bool KeepFiles
177 public string TempDir
180 // note: we only return what we were supplied so there
181 // is no permission protecting this information
186 public string AddExtension(string fileExtension)
188 return(AddExtension(fileExtension, keepfiles));
191 public string AddExtension(string fileExtension, bool keepFile)
193 string filename=BasePath+"."+fileExtension;
194 AddFile(filename, keepFile);
198 public void AddFile(string fileName, bool keepFile)
200 filehash.Add(fileName, keepFile);
203 public void CopyTo(string[] fileNames, int start)
205 filehash.Keys.CopyTo(fileNames, start);
208 void ICollection.CopyTo(Array array, int start)
210 filehash.Keys.CopyTo(array, start);
213 object ICollection.SyncRoot {
219 bool ICollection.IsSynchronized {
225 void IDisposable.Dispose()
232 bool allDeleted = true;
233 string[] filenames = new string[filehash.Count];
234 filehash.Keys.CopyTo (filenames, 0);
236 foreach(string file in filenames) {
237 if((bool)filehash[file]==false) {
239 filehash.Remove(file);
243 if (basepath != null) {
244 string tmpFile = basepath + ".tmp";
245 File.Delete (tmpFile);
248 if (allDeleted && ownTempDir != null) {
249 Directory.Delete (ownTempDir, true);
254 IEnumerator IEnumerable.GetEnumerator ()
256 return(filehash.Keys.GetEnumerator());
259 public IEnumerator GetEnumerator()
261 return(filehash.Keys.GetEnumerator());
264 protected virtual void Dispose(bool disposing)
268 GC.SuppressFinalize (true);
272 ~TempFileCollection()
277 [DllImport ("libc")] private static extern int mkdir (string olpath, uint mode);