-
Notifications
You must be signed in to change notification settings - Fork 5
/
TemporaryFile.cs
331 lines (302 loc) · 12.1 KB
/
TemporaryFile.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using log4net;
namespace JPLOPS.Util
{
public class TemporaryFile
{
class TemporaryFileConfig : SingletonConfig<TemporaryFileConfig>
{
[ConfigEnvironmentVariable("LANDFORM_TEMP")]
public string Dir = "tmp";
[ConfigEnvironmentVariable("LANDFORM_TEMP_MAX_AGE_SEC")]
public long MaxAge = 24 * 60 * 60;
[ConfigEnvironmentVariable("LANDFORM_TEMP_MAX_DISK_BYTES")]
public long MaxDiskBytes = 10L * 1024L * 1024L * 1024L;
}
public delegate void FilenameDelegate(string s);
public delegate void DirectoryDelegate(string s);
public delegate void MultipleFilenameDelegate(string[] s);
public static string TemporaryDirectory
{
get { return TemporaryFileConfig.Instance.Dir; }
set { TemporaryFileConfig.Instance.Dir = Path.GetFullPath(value); }
}
private static ILog logger = LogManager.GetLogger(typeof(TemporaryFile));
static TemporaryFile()
{
TemporaryDirectory = TemporaryFileConfig.Instance.Dir;
}
/// <summary>
/// Execute a delegate with a temporary filename, and move the temp file to
/// it's final location when the delegate completes.
/// </summary>
/// <param name="destination">Temp file will be moved to this path when the delegate completes.</param>
/// <param name="func">Delegate to execute.</param>
public static void GetAndMove(string destination, FilenameDelegate func, bool replaceExisting = true,
object moveLock = null)
{
string file = GetTempName(destination);
try
{
func(file);
if (File.Exists(file))
{
PathHelper.MoveFileAtomic(file, destination, replaceExisting, moveLock);
}
}
catch (Exception)
{
if (File.Exists(file))
{
PathHelper.DeleteWithRetry(file, logger);
}
throw;
}
}
/// <summary>
/// Execute a delegate with a temporary filename, and delete the temp file when
/// the delegate completes.
/// </summary>
/// <param name="extension">filename extension for the temporary file, must include a ".", and only the part starting with the last "." will be used</param>
/// <param name="func">Delegate to execute.</param>
public static void GetAndDelete(string extension, FilenameDelegate func)
{
string file = GetTempName(extension);
try
{
func(file);
}
finally
{
if (File.Exists(file))
{
PathHelper.DeleteWithRetry(file, logger);
}
}
}
/// <summary>
/// Execute a delegate with a temporary directory and delete the temp directory when the delegate completes
/// </summary>
/// <param name="func">Delegate to execute</param>
public static void GetAndDeleteDirectory(DirectoryDelegate func)
{
string dir = GetTempSubdir();
try
{
func(dir);
}
finally
{
if (Directory.Exists(dir))
{
Directory.Delete(dir, true);
}
}
}
/// <summary>
/// Get multiple temporary files that will be deleted at the end of the delegate function block
/// </summary>
/// <param name="count"></param>
/// <param name="extension"></param>
/// <param name="func"></param>
public static void GetAndDeleteMultiple(int count, string extension, MultipleFilenameDelegate func)
{
string[] tmpFiles = new string[count];
for(int i = 0; i < tmpFiles.Length; i++)
{
tmpFiles[i] = GetTempName(extension);
}
try
{
func(tmpFiles);
}
finally
{
for (int i = 0; i < tmpFiles.Length; i++)
{
if (File.Exists(tmpFiles[i]))
{
PathHelper.DeleteWithRetry(tmpFiles[i], logger);
}
}
}
}
/// <summary>
/// Get multiple temporary files that will be deleted at the end of the delegate function block
/// </summary>
/// <param name="extensions">The extensions to be used for each file. There will be as many files as extensions</param>
/// <param name="func"></param>
public static void GetAndDeleteMultiple(string[] extensions, MultipleFilenameDelegate func)
{
string[] tmpFiles = new string[extensions.Count()];
for (int i = 0; i < tmpFiles.Length; i++)
{
tmpFiles[i] = GetTempName(extensions[i]);
}
try
{
func(tmpFiles);
}
finally
{
for (int i = 0; i < tmpFiles.Length; i++)
{
if (File.Exists(tmpFiles[i]))
{
PathHelper.DeleteWithRetry(tmpFiles[i], logger);
}
}
}
}
/// <summary>
/// Provide a guid temp directory so caller can save specific file names at a unique path
/// </summary>
/// <param name="name">if not null or empty then get subdir with given name, else generate a random unique name</param>
/// <returns></returns>
public static string GetTempSubdir(string name = null)
{
if (string.IsNullOrEmpty(name))
{
name = Guid.NewGuid().ToString();
}
string p = Path.Combine(TemporaryDirectory, name);
PathHelper.EnsureExists(Path.GetFullPath(p));
return p;
}
/// <summary>
/// Delete temp directory and all contents
/// </summary>
public static void DeleteTempDirectory()
{
if (File.Exists(TemporaryDirectory))
{
Directory.Delete(TemporaryDirectory, true);
}
}
/// <summary>
/// Delete temp subdirectory and all contents
/// </summary>
public static void DeleteTempSubdir(string name)
{
if (File.Exists(name))
{
Directory.Delete(name, true);
}
}
/// <summary>
/// Clean up contents of temp directory by deleting old files.
/// </summary>
/// <param name="subdir">subdirectory of temp dir to operate on, or whole temp dir if null or empty</param>
/// <param name="recursive">whether to operate recursively</param>
/// <param name="maxAge">if negative use TemporaryFileConfig.MaxAge, if zero then ignore age, if positive then try to remove all files older than this age in seconds</param>
/// <param name="maxDiskBytes">if negative use TemporaryFileConfig.MaxDiskBytes, if zero then ignore disk usage, if positive then try to remove old files until disk usage is less than this limit</param>
/// <param name="alwaysDelete">if non-null then always delete files matching this predicate</param>
/// <param name="deleteEmptySubdirs">if recursive then delete subdirs which are empty or became empty</param>
/// <returns></returns>
public static void CleanupTempDirectoryLRU(string subdir = null, bool recursive = true,
long maxAge = -1, long maxDiskBytes = -1,
Func<string, bool> alwaysDelete = null,
bool deleteEmptySubdirs = true)
{
if (maxAge < 0)
{
maxAge = TemporaryFileConfig.Instance.MaxAge;
}
if (maxDiskBytes < 0)
{
maxDiskBytes = TemporaryFileConfig.Instance.MaxDiskBytes;
}
var dir = !string.IsNullOrEmpty(subdir) ? Path.Combine(TemporaryDirectory, subdir) : TemporaryDirectory;
IEnumerable<FileInfo> files =
PathHelper.ListFiles(dir, recursive: recursive).OrderBy(i => i.LastAccessTime); //sort oldest first
long totalDiskUsage = files.Aggregate(0L, (n, f) => n + f.Length), diskUsageBefore = totalDiskUsage;
bool wasTooBig = maxDiskBytes > 0 && totalDiskUsage > maxDiskBytes;
int nf = files.Count(), nd = 0, ne = 0;
Func<FileInfo, bool> deleteFile = f =>
{
try
{
var b = f.Length;
File.Delete(f.FullName);
totalDiskUsage -= b;
nd++;
return true;
}
catch (Exception ex)
{
logger.WarnFormat("error deleting temp file {0}: {1}", f.FullName, ex.Message);
ne++;
return false;
}
};
//if we have an alwaysDelete predicate then go through all the files
//and try to delete the ones that match it
//the ones that remain are the ones that don't match it or that failed to delete
if (alwaysDelete != null)
{
var remaining = new List<FileInfo>();
foreach (var f in files)
{
if (!alwaysDelete(f.FullName) || !deleteFile(f))
{
remaining.Add(f);
}
}
files = (IEnumerable<FileInfo>)remaining;
}
var now = DateTime.Now;
foreach (var f in files)
{
bool tooBig = maxDiskBytes > 0 && totalDiskUsage > maxDiskBytes;
bool tooOld = maxAge > 0 && (now - f.LastAccessTime).TotalSeconds > maxAge;
if (!tooBig && !tooOld)
{
break;
}
deleteFile(f);
}
if (recursive && deleteEmptySubdirs)
{
var opt = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
IEnumerable<DirectoryInfo> dirs = new DirectoryInfo(dir).GetFileSystemInfos("*", opt)
.Where(i => i is DirectoryInfo)
.Select(i => i as DirectoryInfo)
.OrderByDescending(i => i.FullName.Length); //check children before parents
foreach (var d in dirs)
{
if (!d.EnumerateFileSystemInfos().Any())
{
try
{
d.Delete();
}
catch (Exception ex)
{
logger.WarnFormat("error deleting empty directory {0}: {1}", d.FullName, ex.Message);
}
}
}
}
if (nd > 0 || ne > 0 || wasTooBig)
{
double gb = 1024.0 * 1024.0 * 1024.0;
logger.InfoFormat("cleaned up temp dir {0}, deleted {1}/{2} files, {3} errors, " +
"{4:F3}G before, {5:F3}G after",
dir, nd, nf, ne, diskUsageBefore/gb, totalDiskUsage/gb);
}
}
private static string GetTempName(string ext)
{
PathHelper.EnsureExists(TemporaryDirectory);
string f = Path.Combine(TemporaryDirectory, Guid.NewGuid() + Path.GetExtension(ext));
if (File.Exists(f))
{
File.Delete(f);
}
return Path.Combine(TemporaryDirectory, f);
}
}
}