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