Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolve link before using File.Replace #3177

Draft
wants to merge 2 commits into
base: dev
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix resource management and async/sync mixing issues.

The current implementation has several critical issues:

  1. The FileStream is not properly disposed
  2. AtomicWriteSetting is called synchronously in an async method
  3. The file might be left in an inconsistent state if an error occurs

Apply this fix:

     public async Task SaveAsync()
     {
-        var tempOutput = File.OpenWrite(TempFilePath);
-        await JsonSerializer.SerializeAsync(tempOutput, Data,
-            new JsonSerializerOptions { WriteIndented = true });
-        AtomicWriteSetting();
+        await using (var tempOutput = File.OpenWrite(TempFilePath))
+        {
+            await JsonSerializer.SerializeAsync(tempOutput, Data,
+                new JsonSerializerOptions { WriteIndented = true });
+            await tempOutput.FlushAsync();
+        }
+        await Task.Run(AtomicWriteSetting);
     }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
{
var tempOutput = File.OpenWrite(TempFilePath);
await JsonSerializer.SerializeAsync(tempOutput, Data,
new JsonSerializerOptions
{
WriteIndented = true
});
new JsonSerializerOptions { WriteIndented = true });
AtomicWriteSetting();
}
{
await using (var tempOutput = File.OpenWrite(TempFilePath))
{
await JsonSerializer.SerializeAsync(tempOutput, Data,
new JsonSerializerOptions { WriteIndented = true });
await tempOutput.FlushAsync();
}
await Task.Run(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);
}
}

}
}
Loading