merge -r 58784:58785
[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                                         string random = rnd.Next (10000,99999).ToString ();
81                                         basepath = Path.Combine (temp, random);
82
83                                         // and you must have discovery access to the combined path
84                                         // note: the cache behaviour is tested in the CAS tests
85                                         if (SecurityManager.SecurityEnabled) {
86                                                 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, basepath).Demand ();
87                                         }
88                                 }
89
90                                 return(basepath);
91                         }
92                 }
93
94                 public int Count
95                 {
96                         get {
97                                 return(filehash.Count);
98                         }
99                 }
100
101                 public bool KeepFiles
102                 {
103                         get {
104                                 return(keepfiles);
105                         }
106                         set {
107                                 keepfiles=value;
108                         }
109                 }
110
111                 public string TempDir
112                 {
113                         get {
114                                 // note: we only return what we were supplied so there
115                                 // is no permission protecting this information
116                                 return tempdir;
117                         }
118                 }
119
120                 public string AddExtension(string fileExtension)
121                 {
122                         return(AddExtension(fileExtension, keepfiles));
123                 }
124
125                 public string AddExtension(string fileExtension, bool keepFile)
126                 {
127                         string filename=BasePath+"."+fileExtension;
128                         AddFile(filename, keepFile);
129                         return(filename);
130                 }
131
132                 public void AddFile(string fileName, bool keepFile)
133                 {
134                         filehash.Add(fileName, keepFile);
135                 }
136
137                 public void CopyTo(string[] fileNames, int start)
138                 {
139                         filehash.Keys.CopyTo(fileNames, start);
140                 }
141
142                 void ICollection.CopyTo(Array array, int start)
143                 {
144                         filehash.Keys.CopyTo(array, start);
145                 }
146
147                 object ICollection.SyncRoot {
148                         get {
149                                 return null;
150                         }
151                 }
152
153                 bool ICollection.IsSynchronized {
154                         get {
155                                 return(false);
156                         }
157                 }
158                 
159                 void IDisposable.Dispose() 
160                 {
161                         Dispose(true);
162                 }
163                 
164                 public void Delete()
165                 {
166                         string[] filenames=new string[filehash.Count];
167                         filehash.Keys.CopyTo(filenames, 0);
168
169                         foreach(string file in filenames) {
170                                 if((bool)filehash[file]==false) {
171                                         File.Delete(file);
172                                         filehash.Remove(file);
173                                 }
174                         }
175                 }
176
177                 public IEnumerator GetEnumerator()
178                 {
179                         return(filehash.Keys.GetEnumerator());
180                 }
181
182                 protected virtual void Dispose(bool disposing)
183                 {
184                         Delete();
185                         if (disposing) {
186                                 GC.SuppressFinalize (true);
187                         }
188                 }
189
190                 ~TempFileCollection()
191                 {
192                         Dispose(false);
193                 }
194                 
195         }
196 }