Skip to content

Commit

Permalink
resolve link before using File.Replace
Browse files Browse the repository at this point in the history
  • Loading branch information
taooceros committed Jan 9, 2025
1 parent 7cccbb0 commit a62b222
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions Flow.Launcher.Infrastructure/Storage/JsonStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ namespace Flow.Launcher.Infrastructure.Storage
protected JsonStorage()
{
}

public JsonStorage(string filePath)
{
FilePath = filePath;
DirectoryPath = Path.GetDirectoryName(filePath) ?? throw new ArgumentException("Invalid file path");

Helper.ValidateDirectory(DirectoryPath);
}

Expand Down Expand Up @@ -97,6 +98,7 @@ private async ValueTask<T> LoadBackupOrDefaultAsync()
return default;
}
}

private void RestoreBackup()
{
Log.Info($"|JsonStorage.Load|Failed to load settings.json, {BackupFilePath} restored successfully");
Expand Down Expand Up @@ -179,25 +181,21 @@ private void BackupOriginFile()
public void Save()
{
string serialized = JsonSerializer.Serialize(Data,
new JsonSerializerOptions
{
WriteIndented = true
});
new JsonSerializerOptions { WriteIndented = true });

File.WriteAllText(TempFilePath, serialized);

AtomicWriteSetting();
}

public async Task SaveAsync()
{
var tempOutput = File.OpenWrite(TempFilePath);
await JsonSerializer.SerializeAsync(tempOutput, Data,
new JsonSerializerOptions
{
WriteIndented = true
});
new JsonSerializerOptions { WriteIndented = true });
AtomicWriteSetting();
}

private void AtomicWriteSetting()
{
if (!File.Exists(FilePath))
Expand All @@ -206,9 +204,9 @@ private void AtomicWriteSetting()
}
else
{
File.Replace(TempFilePath, FilePath, BackupFilePath);
var finalFilePath = new FileInfo(FilePath).LinkTarget ?? FilePath;
File.Replace(TempFilePath, finalFilePath, BackupFilePath);
}
}

}
}

0 comments on commit a62b222

Please sign in to comment.