2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[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 using System.Runtime.InteropServices;
35
36 namespace System.CodeDom.Compiler {
37
38 #if NET_2_0
39         [Serializable]
40 #endif
41         [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
42         public class TempFileCollection:ICollection, IEnumerable, IDisposable
43         {
44                 Hashtable filehash;
45                 string tempdir;
46                 bool keepfiles;
47                 string basepath;
48                 Random rnd;
49                 string ownTempDir;
50                 
51                 public TempFileCollection ()
52                         : this (String.Empty, false)
53                 {
54                 }
55
56                 public TempFileCollection(string tempDir)
57                         : this (tempDir, false)
58                 {
59                 }
60
61                 public TempFileCollection(string tempDir, bool keepFiles)
62                 {
63                         filehash=new Hashtable();
64                         tempdir = (tempDir == null) ? String.Empty : tempDir;
65                         keepfiles=keepFiles;
66                 }
67
68                 public string BasePath
69                 {
70                         get {
71                                 if(basepath==null) {
72                                 
73                                         if (rnd == null)
74                                                 rnd = new Random ();
75
76                                         // note: this property *cannot* change TempDir property
77                                         string temp = tempdir;
78                                         if (temp.Length == 0)
79                                                 temp = GetOwnTempDir ();
80
81                                         // Create a temporary file at the target directory. This ensures
82                                         // that the generated file name is unique.
83                                         FileStream f = null;
84                                         do {
85                                                 int num = rnd.Next ();
86                                                 num++;
87                                                 basepath = Path.Combine (temp, num.ToString("x"));
88                                                 string path = basepath + ".tmp";
89
90                                                 try {
91                                                         f = new FileStream (path, FileMode.CreateNew);
92                                                 }
93                                                 catch (System.IO.IOException) {
94                                                         f = null;
95                                                         continue;
96                                                 }
97                                                 catch {
98                                                         // avoid endless loop
99                                                         throw;
100                                                 }
101                                         } while (f == null);
102                                         
103                                         f.Close ();
104                                         
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 ();
109                                         }
110                                 }
111
112                                 return(basepath);
113                         }
114                 }
115                 
116                 string GetOwnTempDir ()
117                 {
118                         if (ownTempDir != null)
119                                 return ownTempDir;
120
121                         // this call ensure the Environment permissions check
122                         string basedir = Path.GetTempPath ();
123                         
124                         // Create a subdirectory with the correct user permissions
125                         int res = -1;
126                         bool win32 = false;
127                         switch (Environment.OSVersion.Platform) {
128                         case PlatformID.Win32S:
129                         case PlatformID.Win32Windows:
130                         case PlatformID.Win32NT:
131                         case PlatformID.WinCE:
132                                 win32 = true;
133                                 res = 0;
134                                 break;
135                         }
136
137                         do {
138                                 int num = rnd.Next ();
139                                 num++;
140                                 ownTempDir = Path.Combine (basedir, num.ToString("x"));
141                                 if (Directory.Exists (ownTempDir))
142                                         continue;
143                                 if (win32)
144                                         Directory.CreateDirectory (ownTempDir);
145                                 else
146                                         res = mkdir (ownTempDir, 0x1c0);
147                                 if (res != 0) {
148                                         if (!Directory.Exists (ownTempDir))
149                                                 throw new IOException ();
150                                         // Somebody already created the dir, keep trying
151                                 }
152                         } while (res != 0);
153                         return ownTempDir;
154                 }
155
156                 int ICollection.Count {
157                         get {
158                                 return filehash.Count;
159                         }
160                 }
161                 
162                 public int Count
163                 {
164                         get {
165                                 return(filehash.Count);
166                         }
167                 }
168
169                 public bool KeepFiles
170                 {
171                         get {
172                                 return(keepfiles);
173                         }
174                         set {
175                                 keepfiles=value;
176                         }
177                 }
178
179                 public string TempDir
180                 {
181                         get {
182                                 // note: we only return what we were supplied so there
183                                 // is no permission protecting this information
184                                 return tempdir;
185                         }
186                 }
187
188                 public string AddExtension(string fileExtension)
189                 {
190                         return(AddExtension(fileExtension, keepfiles));
191                 }
192
193                 public string AddExtension(string fileExtension, bool keepFile)
194                 {
195                         string filename=BasePath+"."+fileExtension;
196                         AddFile(filename, keepFile);
197                         return(filename);
198                 }
199
200                 public void AddFile(string fileName, bool keepFile)
201                 {
202                         filehash.Add(fileName, keepFile);
203                 }
204
205                 public void CopyTo(string[] fileNames, int start)
206                 {
207                         filehash.Keys.CopyTo(fileNames, start);
208                 }
209
210                 void ICollection.CopyTo(Array array, int start)
211                 {
212                         filehash.Keys.CopyTo(array, start);
213                 }
214
215                 object ICollection.SyncRoot {
216                         get {
217                                 return null;
218                         }
219                 }
220
221                 bool ICollection.IsSynchronized {
222                         get {
223                                 return(false);
224                         }
225                 }
226                 
227                 void IDisposable.Dispose() 
228                 {
229                         Dispose(true);
230                 }
231                 
232                 public void Delete()
233                 {
234                         bool allDeleted = true;
235                         string[] filenames = new string[filehash.Count];
236                         filehash.Keys.CopyTo (filenames, 0);
237
238                         foreach(string file in filenames) {
239                                 if((bool)filehash[file]==false) {
240                                         File.Delete(file);
241                                         filehash.Remove(file);
242                                 } else
243                                         allDeleted = false;
244                         }
245                         if (basepath != null) {
246                                 string tmpFile = basepath + ".tmp";
247                                 File.Delete (tmpFile);
248                                 basepath = null;
249                         }
250                         if (allDeleted && ownTempDir != null) {
251                                 Directory.Delete (ownTempDir, true);
252                                 ownTempDir = null;
253                         }
254                 }
255
256                 IEnumerator IEnumerable.GetEnumerator ()
257                 {
258                         return(filehash.Keys.GetEnumerator());
259                 }
260                 
261                 public IEnumerator GetEnumerator()
262                 {
263                         return(filehash.Keys.GetEnumerator());
264                 }
265
266                 protected virtual void Dispose(bool disposing)
267                 {
268                         Delete();
269                         if (disposing) {
270                                 GC.SuppressFinalize (true);
271                         }
272                 }
273
274                 ~TempFileCollection()
275                 {
276                         Dispose(false);
277                 }
278                 
279                 [DllImport ("libc")] private static extern int mkdir (string olpath, uint mode);
280         }
281 }