-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathCfgCache.cs
410 lines (346 loc) · 13.4 KB
/
CfgCache.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
using System;
using System.Xml;
using System.IO;
using System.Diagnostics;
using DigitalPlatform.Xml;
using DigitalPlatform.IO;
using DigitalPlatform.Core;
namespace DigitalPlatform.CirculationClient
{
/// <summary>
/// 配置文件前端缓冲
/// </summary>
public class CfgCache
{
XmlDocument _dom = null;
string m_strXmlFileName = ""; // 存储缓冲对照信息的xml文件
bool m_bChanged = false;
string m_strTempDir = "";
bool m_bAutoSave = true;
public CfgCache()
{
}
// 获得或设置临时文件目录
// 如果不设置临时文件目录, 则在需要创建临时文件的时候, 自动创建在系统临时文件目录中
// exception:
// 可能会抛出异常
public string TempDir
{
get
{
return m_strTempDir;
}
set
{
m_strTempDir = value;
// 创建目录
if (m_strTempDir != "")
{
// 如果目录不存在则创建之
// return:
// false 已经存在
// true 刚刚新创建
// exception:
// 可能会抛出异常 System.IO.DirectoryNotFoundException (未能找到路径“...”的一部分)
PathUtil.TryCreateDir(m_strTempDir);
}
}
}
// 是否在修改后立即保存到文件
public bool InstantSave
{
get
{
return m_bAutoSave;
}
set
{
m_bAutoSave = value;
}
}
/*
操作类型 crashReport -- 异常报告
主题 dp2circulation
发送者 xxxx
媒体类型 text
内容 发生未捕获的界面线程异常:
Type: System.IO.IOException
Message: 文件“C:\Documents and Settings\Administrator\dp2Circulation_v2\cfgcache\21.file”正由另一进程使用,因此该进程无法访问此文件。
Stack:
在 System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
在 System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath)
在 System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
在 System.IO.File.Create(String path)
在 DigitalPlatform.CirculationClient.CfgCache.NewTempFileName(Boolean bUseMacro)
在 DigitalPlatform.CirculationClient.CfgCache.PrepareLocalFile(String strCfgPath, String& strLocalName)
在 DigitalPlatform.CirculationClient.LibraryChannel.GetRes(Stop stop, CfgCache cache, String strPath, String strStyle, Byte[] remote_timestamp, String& strResult, String& strMetaData, Byte[]& baOutputTimeStamp, String& strOutputResPath, String& strError)
在 dp2Circulation.MainForm.GetCfgFile(LibraryChannel Channel, Stop stop, String strDbName, String strCfgFileName, Byte[] remote_timestamp, String& strContent, Byte[]& baOutputTimestamp, String& strError)
在 dp2Circulation.MainForm.InitialNormalDbProperties(Boolean bPrepareSearch)
在 dp2Circulation.MainForm.InitialProperties(Boolean bFullInitial, Boolean bRestoreLastOpenedWindow)
dp2Circulation 版本: dp2Circulation, Version=2.5.5759.36671, Culture=neutral, PublicKeyToken=null
操作系统:Microsoft Windows NT 5.1.2600 Service Pack 3
本机 MAC 地址: 00016C4E0ACA
操作时间 2015/10/10 9:53:43 (Sat, 10 Oct 2015 09:53:43 +0800)
前端地址 xxx 经由 http://dp2003.com/dp2library
* */
// 获得一个临时文件名
// 临时文件创建在 m_strTempDir目录中
string NewTempFileName(bool bUseMacro = true)
{
if (m_strTempDir == "")
{
// 2007/12/10
// TODO: 当人工删除了那些.file文件后,GetTempFileName() API就会用到原来被用过的那些编号,造成文件张冠李戴。
// 建议本类设计一个种子计数器,自行实现获得临时文件的函数
return Path.GetTempFileName();
}
string strFileName = "";
for (int i = 0; ; i++)
{
strFileName = PathUtil.MergePath(m_strTempDir, Convert.ToString(i) + ".file");
FileInfo fi = new FileInfo(strFileName);
if (fi.Exists == false)
{
try
{
// 创建一个0 byte的文件
using (FileStream f = File.Create(strFileName))
{
}
}
catch
{
continue;
}
if (bUseMacro == true)
return "%cfgcachedir%/" + PathUtil.PureName(strFileName);
return strFileName;
}
}
}
public int Load(string strXmlFileName,
out string strError)
{
strError = "";
_dom = new XmlDocument();
m_strXmlFileName = strXmlFileName; // 出错后也需要
try
{
_dom.Load(strXmlFileName);
}
catch (Exception ex)
{
_dom.LoadXml("<root/>"); // 虽然返回出错,但是dom是正确初始化了的
strError = ExceptionUtil.GetAutoText(ex);
return -1;
}
return 0;
}
// 升级。清理以前的残余文件
// 需要在 Load() 以后调用
public void Upgrade()
{
double value = 0;
if (_dom.DocumentElement != null)
{
string strVersion = _dom.DocumentElement.GetAttribute("version");
if (string.IsNullOrEmpty(strVersion) == false)
{
// 检查最低版本号
if (double.TryParse(strVersion, out value) == false)
value = 0;
}
}
// 升级到 2
if (value < 2 && _dom.DocumentElement != null)
{
_dom.DocumentElement.SetAttribute("version", "2");
m_bChanged = true;
this.ClearCfgCache();
}
}
public void AutoSave()
{
if (m_bChanged == false || m_bAutoSave == false)
return;
string strError;
Save(null, out strError);
}
// parameters:
// strXmlFileName 可以为null
public int Save(string strXmlFileName,
out string strError)
{
strError = "";
if (strXmlFileName == null)
strXmlFileName = m_strXmlFileName;
if (strXmlFileName == null)
{
strError = "m_strXmlFileName尚未初始化...";
return -1;
}
if (_dom != null)
_dom.Save(strXmlFileName);
m_bChanged = false;
return 0;
}
// 查找配置文件网络路径所对应的本地文件
// return:
// -1 error
// 0 not found
// 1 found
public int FindLocalFile(string strCfgPath,
out string strLocalName,
out string strTimeStamp)
{
// 2015/9/9
if (_dom == null || _dom.DocumentElement == null)
{
strLocalName = "";
strTimeStamp = "";
return -1;
}
strCfgPath = strCfgPath.ToLower(); // 导致大小写不敏感
XmlNode node = _dom.DocumentElement.SelectSingleNode("cfg[@path='" + strCfgPath + "']");
if (node == null)
{
strLocalName = "";
strTimeStamp = "";
return 0; // not found
}
strLocalName = DomUtil.GetAttr(node, "localname");
if (string.IsNullOrEmpty(strLocalName) == true)
goto DELETE;
// 替换文件名中的宏
strLocalName = strLocalName.Replace("%cfgcachedir%", this.m_strTempDir);
// 检查本地文件是否存在
FileInfo fi = new FileInfo(strLocalName);
if (fi.Exists == false)
goto DELETE;
strTimeStamp = DomUtil.GetAttr(node, "timestamp");
return 1;
DELETE:
strLocalName = "";
strTimeStamp = "";
// 删除这个信息不完整的节点
_dom.DocumentElement.RemoveChild(node);
m_bChanged = true;
AutoSave();
return 0; // not found
}
// 为一个网络路径准备本地文件
public int PrepareLocalFile(string strCfgPath,
out string strLocalName)
{
strCfgPath = strCfgPath.ToLower(); // 导致大小写不敏感
XmlNode node = _dom.DocumentElement.SelectSingleNode("cfg[@path='" + strCfgPath + "']");
if (node != null)
{
// 节点已经存在
strLocalName = DomUtil.GetAttr(node, "localname");
Debug.Assert(strLocalName != "", "已经存在的节点中localname属性为空");
}
else
{
node = _dom.CreateElement("cfg");
DomUtil.SetAttr(node, "path", strCfgPath);
strLocalName = NewTempFileName();
DomUtil.SetAttr(node, "localname", strLocalName);
node = _dom.DocumentElement.AppendChild(node);
m_bChanged = true;
AutoSave();
}
// 替换文件名中的宏
strLocalName = strLocalName.Replace("%cfgcachedir%", this.m_strTempDir);
return 1;
}
// 为已经存在的节点设置时间戳值
public int SetTimeStamp(string strCfgPath,
string strTimeStamp,
out string strError)
{
strError = "";
strCfgPath = strCfgPath.ToLower(); // 导致大小写不敏感
XmlNode node = _dom.DocumentElement.SelectSingleNode("cfg[@path='" + strCfgPath + "']");
if (node == null)
{
strError = "属性path值为 '" + strCfgPath + "'的<cfg>元素不存在...";
return -1;
}
DomUtil.SetAttr(node, "timestamp", strTimeStamp);
m_bChanged = true;
AutoSave();
return 0;
}
// 清除全部节点
public void ClearCfgCache()
{
XmlNodeList nodes = _dom.DocumentElement.SelectNodes("cfg");
for (int i = 0; i < nodes.Count; i++)
{
string strLocalName = DomUtil.GetAttr(nodes[i], "localname");
if (string.IsNullOrEmpty(strLocalName) == false)
{
// 替换文件名中的宏
strLocalName = strLocalName.Replace("%cfgcachedir%", this.m_strTempDir);
try // 2008/3/27
{
File.Delete(strLocalName);
}
catch
{
}
}
}
// 删除所有<cfg>节点
for (int i = 0; i < nodes.Count; i++)
{
_dom.DocumentElement.RemoveChild(nodes[i]);
}
m_bChanged = true;
AutoSave();
// 2013/4/12
// 删除 cfgacache 子目录以后重建
if (string.IsNullOrEmpty(this.m_strTempDir) == false)
{
PathUtil.DeleteDirectory(this.m_strTempDir);
try
{
// 如果目录不存在则创建之
// return:
// false 已经存在
// true 刚刚新创建
// exception:
// 可能会抛出异常 System.IO.DirectoryNotFoundException (未能找到路径“...”的一部分)
PathUtil.TryCreateDir(this.m_strTempDir);
}
catch
{
}
}
}
public int Delete(string strCfgPath,
out string strError)
{
strError = "";
strCfgPath = strCfgPath.ToLower(); // 导致大小写不敏感
XmlNode node = _dom.DocumentElement.SelectSingleNode("cfg[@path='" + strCfgPath + "']");
if (node == null)
{
strError = "属性path值为 '" + strCfgPath + "'的<cfg>元素不存在...";
return -1;
}
string strLocalName = DomUtil.GetAttr(node, "localname");
if (string.IsNullOrEmpty(strLocalName) == false)
{
// 替换文件名中的宏
strLocalName = strLocalName.Replace("%cfgcachedir%", this.m_strTempDir);
File.Delete(strLocalName);
}
_dom.DocumentElement.RemoveChild(node);
m_bChanged = true;
AutoSave();
return 0;
}
}
}