New test.
[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                         do {
127                                 int num = rnd.Next ();
128                                 num++;
129                                 ownTempDir = Path.Combine (basedir, num.ToString("x"));
130                                 if (Directory.Exists (ownTempDir))
131                                         continue;
132                                 res = mkdir (ownTempDir, 0x1c0);
133                                 if (res != 0) {
134                                         if (!Directory.Exists (ownTempDir))
135                                                 throw new IOException ();
136                                         // Somebody already created the dir, keep trying
137                                 }
138                         } while (res != 0);
139                         return ownTempDir;
140                 }
141
142                 int ICollection.Count {
143                         get {
144                                 return filehash.Count;
145                         }
146                 }
147                 
148                 public int Count
149                 {
150                         get {
151                                 return(filehash.Count);
152                         }
153                 }
154
155                 public bool KeepFiles
156                 {
157                         get {
158                                 return(keepfiles);
159                         }
160                         set {
161                                 keepfiles=value;
162                         }
163                 }
164
165                 public string TempDir
166                 {
167                         get {
168                                 // note: we only return what we were supplied so there
169                                 // is no permission protecting this information
170                                 return tempdir;
171                         }
172                 }
173
174                 public string AddExtension(string fileExtension)
175                 {
176                         return(AddExtension(fileExtension, keepfiles));
177                 }
178
179                 public string AddExtension(string fileExtension, bool keepFile)
180                 {
181                         string filename=BasePath+"."+fileExtension;
182                         AddFile(filename, keepFile);
183                         return(filename);
184                 }
185
186                 public void AddFile(string fileName, bool keepFile)
187                 {
188                         filehash.Add(fileName, keepFile);
189                 }
190
191                 public void CopyTo(string[] fileNames, int start)
192                 {
193                         filehash.Keys.CopyTo(fileNames, start);
194                 }
195
196                 void ICollection.CopyTo(Array array, int start)
197                 {
198                         filehash.Keys.CopyTo(array, start);
199                 }
200
201                 object ICollection.SyncRoot {
202                         get {
203                                 return null;
204                         }
205                 }
206
207                 bool ICollection.IsSynchronized {
208                         get {
209                                 return(false);
210                         }
211                 }
212                 
213                 void IDisposable.Dispose() 
214                 {
215                         Dispose(true);
216                 }
217                 
218                 public void Delete()
219                 {
220                         bool allDeleted = true;
221                         string[] filenames = new string[filehash.Count];
222                         filehash.Keys.CopyTo (filenames, 0);
223
224                         foreach(string file in filenames) {
225                                 if((bool)filehash[file]==false) {
226                                         File.Delete(file);
227                                         filehash.Remove(file);
228                                 } else
229                                         allDeleted = false;
230                         }
231                         if (basepath != null) {
232                                 string tmpFile = basepath + ".tmp";
233                                 File.Delete (tmpFile);
234                                 basepath = null;
235                         }
236                         if (allDeleted && ownTempDir != null) {
237                                 Directory.Delete (ownTempDir, true);
238                                 ownTempDir = null;
239                         }
240                 }
241
242                 IEnumerator IEnumerable.GetEnumerator ()
243                 {
244                         return(filehash.Keys.GetEnumerator());
245                 }
246                 
247                 public IEnumerator GetEnumerator()
248                 {
249                         return(filehash.Keys.GetEnumerator());
250                 }
251
252                 protected virtual void Dispose(bool disposing)
253                 {
254                         Delete();
255                         if (disposing) {
256                                 GC.SuppressFinalize (true);
257                         }
258                 }
259
260                 ~TempFileCollection()
261                 {
262                         Dispose(false);
263                 }
264                 
265                 [DllImport ("libc")] private static extern int mkdir (string olpath, uint mode);
266         }
267 }