This commit is contained in:
LTCat committed 2026-10-17 04:13:58 +08:00
1 parent c774ae4c85
commit 98157d65c7
2 files changed
+5 -9

No files matched your search

-1
View File
@@ -231,7 +231,6 @@ public static class ConfigUtils {
try { try {
return FileUtils.Exists(filePath) ? JObject.Parse(FileUtils.ReadAsString(filePath)) : []; return FileUtils.Exists(filePath) ? JObject.Parse(FileUtils.ReadAsString(filePath)) : [];
} catch { } catch {
// JSON 异常可能包含凭据内容,不保留原始异常,也不回退空配置。
throw new InvalidDataException($"读取密钥文件失败({filePath}),请检查文件内容与加密格式。"); throw new InvalidDataException($"读取密钥文件失败({filePath}),请检查文件内容与加密格式。");
} }
}); });
+5 -8
View File
@@ -105,14 +105,12 @@ public static class FileUtils {
} }
/// <summary> /// <summary>
/// 原子写入文件。成功时目标文件完整切换为新内容,失败时已有目标文件保持不变。 /// 原子写入文件。写入失败时目标文件保持不变。
/// 不提供并发写入合并保证。
/// </summary> /// </summary>
public static void WriteAtomic(string filePath, string text, Encoding? encoding = null) => public static void WriteAtomic(string filePath, string text, Encoding? encoding = null) =>
FileUtils.WriteAtomic(filePath, (encoding ?? new UTF8Encoding()).GetBytes(text)); FileUtils.WriteAtomic(filePath, (encoding ?? new UTF8Encoding()).GetBytes(text));
/// <summary> /// <summary>
/// 原子写入文件。成功时目标文件完整切换为新内容,失败时已有目标文件保持不变。 /// 原子写入文件。写入失败时目标文件保持不变。
/// 不提供并发写入合并保证。
/// </summary> /// </summary>
public static void WriteAtomic(string filePath, byte[] content) { public static void WriteAtomic(string filePath, byte[] content) {
string tempPath = $"{filePath}.{Guid.NewGuid():N}.tmp"; string tempPath = $"{filePath}.{Guid.NewGuid():N}.tmp";
@@ -462,14 +460,13 @@ public static class FileUtils {
=> File.Exists(PathUtils.ForApi(filePath)); => File.Exists(PathUtils.ForApi(filePath));
/// <summary> /// <summary>
/// 检查文件是否未被其他进程占用 /// 检查文件是否存在,且当前程序有独占的读写权限,未被其他进程占用。
/// <para>被占用或无权读取时返回 <see langword="false"/>。</para>
/// </summary> /// </summary>
public static bool IsAvailable(string filePath) { public static bool IsAvailable(string filePath) {
if (!Exists(filePath)) return true; if (!Exists(filePath)) return false;
try { try {
Retrier.Attempt(maxAttempts: 3, delay: _ => TimeSpan.FromMilliseconds(200), isRetryAllowed: ex => ex is IOException, action: _ => { Retrier.Attempt(maxAttempts: 3, delay: _ => TimeSpan.FromMilliseconds(200), isRetryAllowed: ex => ex is IOException, action: _ => {
using var stream = new FileStream(PathUtils.ForApi(filePath), FileMode.Open, FileAccess.Read, FileShare.None); using var unused = new FileStream(PathUtils.ForApi(filePath), FileMode.Open, FileAccess.ReadWrite, FileShare.None);
}); });
return true; return true;
} catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) { } catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) {