*
This commit is contained in:
2 files changed
+5
-9
No files matched your search
@@ -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}),请检查文件内容与加密格式。");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -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) {
|
||||||
|
|||||||
Reference in new issue
Block a user