LTCat 1 hete
szülő
commit
98157d65c7
2 módosított fájl, 5 hozzáadás és 9 törlés
  1. 0 1
      Shared/General/Config.cs
  2. 5 8
      Shared/Utils/FileUtils.cs

+ 0 - 1
Shared/General/Config.cs

@@ -231,7 +231,6 @@ public static class ConfigUtils {
         try {
             return FileUtils.Exists(filePath) ? JObject.Parse(FileUtils.ReadAsString(filePath)) : [];
         } catch {
-            // JSON 异常可能包含凭据内容,不保留原始异常,也不回退空配置。
             throw new InvalidDataException($"读取密钥文件失败({filePath}),请检查文件内容与加密格式。");
         }
     });

+ 5 - 8
Shared/Utils/FileUtils.cs

@@ -105,14 +105,12 @@ public static class FileUtils {
     }
 
     /// <summary>
-    /// 原子写入文件。成功时目标文件完整切换为新内容,失败时已有目标文件保持不变。
-    /// 不提供并发写入合并保证。
+    /// 原子写入文件。写入失败时目标文件保持不变。
     /// </summary>
     public static void WriteAtomic(string filePath, string text, Encoding? encoding = null) =>
         FileUtils.WriteAtomic(filePath, (encoding ?? new UTF8Encoding()).GetBytes(text));
     /// <summary>
-    /// 原子写入文件。成功时目标文件完整切换为新内容,失败时已有目标文件保持不变。
-    /// 不提供并发写入合并保证。
+    /// 原子写入文件。写入失败时目标文件保持不变。
     /// </summary>
     public static void WriteAtomic(string filePath, byte[] content) {
         string tempPath = $"{filePath}.{Guid.NewGuid():N}.tmp";
@@ -462,14 +460,13 @@ public static class FileUtils {
         => File.Exists(PathUtils.ForApi(filePath));
 
     /// <summary>
-    /// 检查文件是否未被其他进程占用
-    /// <para>被占用或无权读取时返回 <see langword="false"/>。</para>
+    /// 检查文件是否存在,且当前程序有独占的读写权限,未被其他进程占用。
     /// </summary>
     public static bool IsAvailable(string filePath) {
-        if (!Exists(filePath)) return true;
+        if (!Exists(filePath)) return false;
         try {
             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;
         } catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) {