Merge pull request #3800 from madewokherd/mingwbuild
[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 #if FEATURE_MONO_CAS
107                                         // and you must have discovery access to the combined path
108                                         // note: the cache behaviour is tested in the CAS tests
109                                         if (SecurityManager.SecurityEnabled) {
110                                                 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, basepath).Demand ();
111                                         }
112 #endif
113                                 }
114
115                                 return(basepath);
116                         }
117                 }
118
119                 string CreateOwnTempDir ()
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 && filenames.Length > 0) {
251                                 Directory.Delete (ownTempDir, true);
252                         }
253                 }
254
255                 IEnumerator IEnumerable.GetEnumerator ()
256                 {
257                         return(filehash.Keys.GetEnumerator());
258                 }
259                 
260                 public IEnumerator GetEnumerator()
261                 {
262                         return(filehash.Keys.GetEnumerator());
263                 }
264
265                 protected virtual void Dispose(bool disposing)
266                 {
267                         Delete();
268                         if (disposing) {
269                                 GC.SuppressFinalize (true);
270                         }
271                 }
272
273                 ~TempFileCollection()
274                 {
275                         Dispose(false);
276                 }
277                 
278                 [DllImport ("libc")] private static extern int mkdir (string olpath, uint mode);
279         }
280 }