File: TempFilesPool.cs

package info (click to toggle)
keepass2 2.60%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,892 kB
  • sloc: cs: 119,878; xml: 6,087; ansic: 2,033; cpp: 738; sh: 50; makefile: 42
file content (355 lines) | stat: -rw-r--r-- 8,757 bytes parent folder | download
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
  KeePass Password Safe - The Open-Source Password Manager
  Copyright (C) 2003-2025 Dominik Reichl <dominik.reichl@t-online.de>

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading;

using KeePassLib.Native;
using KeePassLib.Utility;

namespace KeePass.Util
{
	[Flags]
	internal enum TempClearFlags
	{
		None = 0,

		RegisteredFiles = 1,
		RegisteredDirectories = 2,

		ContentTaggedFiles = 4,

		All = 0x7FFF
	}

	public sealed class TempFilesPool
	{
		private List<string> m_lFiles = new List<string>();
		private List<KeyValuePair<string, bool>> m_lDirs =
			new List<KeyValuePair<string, bool>>();

		private readonly Dictionary<string, bool> m_dContentLoc = new Dictionary<string, bool>();
		private readonly object m_oContentLocSync = new object();

		private long m_nThreads = 0;

		private string m_strContentTag = null;
		public string TempContentTag
		{
			get
			{
				if(m_strContentTag == null)
				{
					// The tag should consist only of lower case letters
					// and digits (for maximum compatibility); it can
					// for instance be used as CSS class name
					string strL = "cfbm4v27xyk0dk5lyeq5";
					string strR = "qxi7bxozyph6qyexr9kw"; // Together ~207 bit

					// Avoid that the content tag is directly visible in
					// the source code and binaries, in case the development
					// environment creates temporary files containing the
					// tag that might then be deleted unintentionally
					StringBuilder sb = new StringBuilder();
					sb.Append(strL);
					for(int i = strR.Length - 1; i >= 0; --i)
						sb.Append(strR[i]); // Reverse

					m_strContentTag = sb.ToString();
				}

				return m_strContentTag;
			}
		}

		public TempFilesPool()
		{
		}

#if DEBUG
		~TempFilesPool()
		{
			Debug.Assert(Interlocked.Read(ref m_nThreads) == 0);
		}
#endif

		internal void Clear(TempClearFlags f)
		{
			if((f & TempClearFlags.RegisteredFiles) != TempClearFlags.None)
			{
				List<string> lFailed = new List<string>();

				foreach(string strFile in m_lFiles)
				{
					try
					{
						if(File.Exists(strFile))
							File.Delete(strFile);
					}
					catch(Exception)
					{
						Debug.Assert(false);
						lFailed.Add(strFile);
					}
				}

				m_lFiles = lFailed;
			}

			if((f & TempClearFlags.RegisteredDirectories) != TempClearFlags.None)
			{
				List<KeyValuePair<string, bool>> lFailed =
					new List<KeyValuePair<string, bool>>();

				foreach(KeyValuePair<string, bool> kvp in m_lDirs)
				{
					try
					{
						if(Directory.Exists(kvp.Key))
							Directory.Delete(kvp.Key, kvp.Value);
					}
					catch(Exception)
					{
						Debug.Assert(false);
						lFailed.Add(kvp);
					}
				}

				m_lDirs = lFailed;
			}

			if((f & TempClearFlags.ContentTaggedFiles) != TempClearFlags.None)
				ClearContentAsync();
		}

		internal void WaitForThreads()
		{
			try
			{
				while(Interlocked.Read(ref m_nThreads) > 0)
				{
					Thread.Sleep(1);
				}
			}
			catch(Exception) { Debug.Assert(false); }
		}

		public void Add(string strTempFile)
		{
			if(string.IsNullOrEmpty(strTempFile)) { Debug.Assert(false); return; }

			m_lFiles.Add(strTempFile);
		}

		public void AddDirectory(string strTempDir, bool bRecursive)
		{
			if(string.IsNullOrEmpty(strTempDir)) { Debug.Assert(false); return; }

			m_lDirs.Add(new KeyValuePair<string, bool>(strTempDir, bRecursive));
		}

		public void AddContent(string strFilePattern, bool bRecursive)
		{
			if(string.IsNullOrEmpty(strFilePattern)) { Debug.Assert(false); return; }

			lock(m_oContentLocSync)
			{
				if(m_dContentLoc.ContainsKey(strFilePattern) && !bRecursive)
					return; // Do not overwrite recursive with non-recursive

				m_dContentLoc[strFilePattern] = bRecursive;
			}
		}

		public void AddWebBrowserPrintContent()
		{
			if(!NativeLib.IsUnix())
			{
				// MSHTML may create and forget temporary files under
				// C:\\Users\\USER\\AppData\\Local\\Temp\\*.htm
				// (e.g. when printing fails)
				AddContent("*.htm", false);
			}
		}

		public string GetTempFileName()
		{
			return GetTempFileName(true);
		}

		public string GetTempFileName(bool bCreateEmptyFile)
		{
			string strFile = Path.GetTempFileName();
			m_lFiles.Add(strFile);

			if(!bCreateEmptyFile)
			{
				try { File.Delete(strFile); }
				catch(Exception) { Debug.Assert(false); }
			}

			return strFile;
		}

		public string GetTempFileName(string strFileExt)
		{
			if(string.IsNullOrEmpty(strFileExt))
				return GetTempFileName();

			try
			{
				while(true)
				{
					string str = UrlUtil.EnsureTerminatingSeparator(
						UrlUtil.GetTempPath(), false);
					str += "Temp_";

					byte[] pbRandom = new byte[9];
					Program.GlobalRandom.NextBytes(pbRandom);
					str += StrUtil.AlphaNumericOnly(Convert.ToBase64String(
						pbRandom, Base64FormattingOptions.None));

					str += "." + strFileExt;

					if(!File.Exists(str))
					{
						m_lFiles.Add(str);
						return str;
					}
				}
			}
			catch(Exception) { Debug.Assert(false); }

			return GetTempFileName();
		}

		public bool Delete(string strTempFile)
		{
			if(string.IsNullOrEmpty(strTempFile)) { Debug.Assert(false); return false; }

			int i = m_lFiles.IndexOf(strTempFile);
			if(i < 0) { Debug.Assert(false); return false; }

			try
			{
				File.Delete(strTempFile);

				m_lFiles.RemoveAt(i);
				return true;
			}
			catch(Exception) { Debug.Assert(false); }

			return false;
		}

		private void ClearContentAsync()
		{
			lock(m_oContentLocSync)
			{
				if(m_dContentLoc.Count == 0) return;
			}

			Interlocked.Increment(ref m_nThreads); // Here, not in thread
			try { ThreadPool.QueueUserWorkItem(this.ClearContentTh); }
			catch(Exception)
			{
				Debug.Assert(false);
				Interlocked.Decrement(ref m_nThreads);
			}
		}

		private void ClearContentTh(object state)
		{
			try
			{
				Debug.Assert(Interlocked.Read(ref m_nThreads) > 0);

				string strTag = m_strContentTag;
				if(string.IsNullOrEmpty(strTag)) { Debug.Assert(false); return; }

				UnicodeEncoding ue = new UnicodeEncoding(false, false, false);
				byte[] pbTagA = StrUtil.Utf8.GetBytes(strTag);
				byte[] pbTagW = ue.GetBytes(strTag);

				string strTempPath = UrlUtil.GetTempPath();

				Dictionary<string, bool> dToDo;
				lock(m_oContentLocSync)
				{
					dToDo = new Dictionary<string, bool>(m_dContentLoc);
					m_dContentLoc.Clear();
				}

				foreach(KeyValuePair<string, bool> kvp in dToDo)
				{
					bool bSuccess = false;
					try
					{
						bSuccess = ClearContentPriv(strTempPath, kvp.Key, kvp.Value,
							pbTagA, pbTagW);
					}
					catch(Exception) { Debug.Assert(false); }

					if(!bSuccess)
					{
						lock(m_oContentLocSync)
						{
							m_dContentLoc[kvp.Key] = kvp.Value; // Try again next time
						}
					}
				}
			}
			catch(Exception) { Debug.Assert(false); }
			finally { Interlocked.Decrement(ref m_nThreads); }
		}

		private bool ClearContentPriv(string strTempPath, string strFilePattern,
			bool bRecursive, byte[] pbTagA, byte[] pbTagW)
		{
			List<string> lFiles = UrlUtil.GetFilePaths(strTempPath, strFilePattern,
				(bRecursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly));
			bool bSuccess = true;

			foreach(string strFile in lFiles)
			{
				if(string.IsNullOrEmpty(strFile)) continue;
				if((strFile == ".") || (strFile == "..")) continue;

				try
				{
					byte[] pb = File.ReadAllBytes(strFile);
					if(pb == null) { Debug.Assert(false); continue; }

					if((MemUtil.IndexOf(pb, pbTagA) >= 0) ||
						(MemUtil.IndexOf(pb, pbTagW) >= 0))
					{
						File.Delete(strFile);
					}
				}
				catch(Exception) { Debug.Assert(false); bSuccess = false; }
			}

			return bSuccess;
		}
	}
}