2006-05-04 LLuis Sanchez Gual <lluis@novell.com>
[mono.git] / mcs / class / System / System.CodeDom.Compiler / TempFileCollection.cs
1 //
2 // System.CodeDom.Compiler TempFileCollection Class implementation
3 //
4 // Author:
5 //      Dick Porter (dick@ximian.com)
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
30 using System.Collections;
31 using System.IO;
32 using System.Security;
33 using System.Security.Permissions;
34
35 namespace System.CodeDom.Compiler {
36
37 #if NET_2_0
38         [Serializable]
39 #endif
40         [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
41         public class TempFileCollection:ICollection, IEnumerable, IDisposable
42         {
43                 Hashtable filehash;
44                 string tempdir;
45                 bool keepfiles;
46                 string basepath;
47                 Random rnd;
48                 
49                 public TempFileCollection ()
50                         : this (String.Empty, false)
51                 {
52                 }
53
54                 public TempFileCollection(string tempDir)
55                         : this (tempDir, false)
56                 {
57                 }
58
59                 public TempFileCollection(string tempDir, bool keepFiles)
60                 {
61                         filehash=new Hashtable();
62                         tempdir = (tempDir == null) ? String.Empty : tempDir;
63                         keepfiles=keepFiles;
64                 }
65
66                 public string BasePath
67                 {
68                         get {
69                                 if(basepath==null) {
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 ();
75                                         }
76
77                                         if (rnd == null)
78                                                 rnd = new Random ();
79
80                                         // Create a temporary file at the target directory. This ensures
81                                         // that the generated file name is unique.
82                                         FileStream f = null;
83                                         do {
84                                                 int num = rnd.Next ();
85                                                 num++;
86                                                 basepath = Path.Combine (temp, num.ToString("x"));
87                                                 string path = basepath + ".tmp";
88
89                                                 try {
90                                                         f = new FileStream (path, FileMode.CreateNew);
91                                                 }
92                                                 catch (System.IO.IOException) {
93                                                         f = null;
94                                                         continue;
95                                                 }
96                                                 catch {
97                                                         // avoid endless loop
98                                                         throw;
99                                                 }
100                                         } while (f == null);
101                                         
102                                         f.Close ();
103                                         
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 ();
108                                         }
109                                 }
110
111                                 return(basepath);
112                         }
113                 }
114
115                 int ICollection.Count {
116                         get {
117                                 return filehash.Count;
118                         }
119                 }
120                 
121                 public int Count
122                 {
123                         get {
124                                 return(filehash.Count);
125                         }
126                 }
127
128                 public bool KeepFiles
129                 {
130                         get {
131                                 return(keepfiles);
132                         }
133                         set {
134                                 keepfiles=value;
135                         }
136                 }
137
138                 public string TempDir
139                 {
140                         get {
141                                 // note: we only return what we were supplied so there
142                                 // is no permission protecting this information
143                                 return tempdir;
144                         }
145                 }
146
147                 public string AddExtension(string fileExtension)
148                 {
149                         return(AddExtension(fileExtension, keepfiles));
150                 }
151
152                 public string AddExtension(string fileExtension, bool keepFile)
153                 {
154                         string filename=BasePath+"."+fileExtension;
155                         AddFile(filename, keepFile);
156                         return(filename);
157                 }
158
159                 public void AddFile(string fileName, bool keepFile)
160                 {
161                         filehash.Add(fileName, keepFile);
162                 }
163
164                 public void CopyTo(string[] fileNames, int start)
165                 {
166                         filehash.Keys.CopyTo(fileNames, start);
167                 }
168
169                 void ICollection.CopyTo(Array array, int start)
170                 {
171                         filehash.Keys.CopyTo(array, start);
172                 }
173
174                 object ICollection.SyncRoot {
175                         get {
176                                 return null;
177                         }
178                 }
179
180                 bool ICollection.IsSynchronized {
181                         get {
182                                 return(false);
183                         }
184                 }
185                 
186                 void IDisposable.Dispose() 
187                 {
188                         Dispose(true);
189                 }
190                 
191                 public void Delete()
192                 {
193                         string[] filenames=new string[filehash.Count];
194                         filehash.Keys.CopyTo(filenames, 0);
195
196                         foreach(string file in filenames) {
197                                 if((bool)filehash[file]==false) {
198                                         File.Delete(file);
199                                         filehash.Remove(file);
200                                 }
201                         }
202                         if (basepath != null) {
203                                 string tmpFile = basepath + ".tmp";
204                                 File.Delete (tmpFile);
205                         }
206                 }
207
208                 IEnumerator IEnumerable.GetEnumerator ()
209                 {
210                         return(filehash.Keys.GetEnumerator());
211                 }
212                 
213                 public IEnumerator GetEnumerator()
214                 {
215                         return(filehash.Keys.GetEnumerator());
216                 }
217
218                 protected virtual void Dispose(bool disposing)
219                 {
220                         Delete();
221                         if (disposing) {
222                                 GC.SuppressFinalize (true);
223                         }
224                 }
225
226                 ~TempFileCollection()
227                 {
228                         Dispose(false);
229                 }
230                 
231         }
232 }