Skip to content

Commit

Permalink
split filesystem
Browse files Browse the repository at this point in the history
  • Loading branch information
bitsandfoxes committed Sep 24, 2024
1 parent 4da424a commit 9ae1583
Show file tree
Hide file tree
Showing 20 changed files with 300 additions and 284 deletions.
2 changes: 1 addition & 1 deletion src/Sentry/GlobalSessionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ private void PersistSession(SessionUpdate update, DateTimeOffset? pauseTimestamp
{
_options.LogDebug("Creating persistence directory for session file at '{0}'.", _persistenceDirectoryPath);

if (!_options.FileSystem.CreateDirectory(_persistenceDirectoryPath))
if (_options.FileSystem.CreateDirectory(_persistenceDirectoryPath) is not FileOperationResult.Success)
{
_options.LogError("Failed to create persistent directory for session file.");
return;
Expand Down
12 changes: 6 additions & 6 deletions src/Sentry/Http/HttpTransportBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -386,14 +386,14 @@ private void HandleFailure(HttpResponseMessage response, Envelope envelope)
var destination = Path.Combine(destinationDirectory, "envelope_too_large",
(eventId ?? SentryId.Create()).ToString());

if (!_options.FileSystem.CreateDirectory(Path.GetDirectoryName(destination)!))
if (_options.FileSystem.CreateDirectory(Path.GetDirectoryName(destination)!) is not FileOperationResult.Success)
{
_options.LogError("Failed to create directory to store the envelope.");
return;
}

var envelopeFile = _options.FileSystem.CreateFileForWriting(destination);
if (envelopeFile == Stream.Null)
var (result, envelopeFile) = _options.FileSystem.CreateFileForWriting(destination);
if (result is not FileOperationResult.Success)
{
_options.LogError("Failed to create envelope file.");
return;
Expand Down Expand Up @@ -451,14 +451,14 @@ private async Task HandleFailureAsync(HttpResponseMessage response, Envelope env
var destination = Path.Combine(destinationDirectory, "envelope_too_large",
(eventId ?? SentryId.Create()).ToString());

if (!_options.FileSystem.CreateDirectory(Path.GetDirectoryName(destination)!))
if (_options.FileSystem.CreateDirectory(Path.GetDirectoryName(destination)!) is not FileOperationResult.Success)
{
_options.LogError("Failed to create directory to store the envelope.");
return;
}

var envelopeFile = _options.FileSystem.CreateFileForWriting(destination);
if (envelopeFile == Stream.Null)
var (result, envelopeFile) = _options.FileSystem.CreateFileForWriting(destination);
if (result is not FileOperationResult.Success)
{
_options.LogError("Failed to create envelope file.");
return;
Expand Down
5 changes: 3 additions & 2 deletions src/Sentry/ISentryJsonSerializable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ internal static class JsonSerializableExtensions
{
public static void WriteToFile(this ISentryJsonSerializable serializable, IFileSystem fileSystem, string filePath, IDiagnosticLogger? logger)
{
using var file = fileSystem.CreateFileForWriting(filePath);
if (file == Stream.Null)
var (result, file) = fileSystem.CreateFileForWriting(filePath);
if (result is not FileOperationResult.Success)
{
return;
}
Expand All @@ -32,5 +32,6 @@ public static void WriteToFile(this ISentryJsonSerializable serializable, IFileS

serializable.WriteTo(writer, logger);
writer.Flush();
file.Dispose();
}
}
130 changes: 0 additions & 130 deletions src/Sentry/Internal/FileSystem.cs

This file was deleted.

8 changes: 7 additions & 1 deletion src/Sentry/Internal/Http/CachingTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,13 @@ private async Task StoreToCacheAsync(

EnsureFreeSpaceInCache();

var stream = _fileSystem.CreateFileForWriting(envelopeFilePath);
var (result, stream) = _fileSystem.CreateFileForWriting(envelopeFilePath);
if (result is not FileOperationResult.Success)
{
_options.LogDebug("Failed to store to cache.");
return;
}

#if NETFRAMEWORK || NETSTANDARD2_0
using(stream)
#else
Expand Down
23 changes: 15 additions & 8 deletions src/Sentry/Internal/IFileSystem.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
namespace Sentry.Internal;

internal enum FileOperationResult
{
Success,
Failure,
Disabled
}

internal interface IFileSystem
{
// Note: This is not comprehensive. If you need other filesystem methods, add to this interface,
Expand All @@ -8,21 +15,21 @@ internal interface IFileSystem
IEnumerable<string> EnumerateFiles(string path);
IEnumerable<string> EnumerateFiles(string path, string searchPattern);
IEnumerable<string> EnumerateFiles(string path, string searchPattern, SearchOption searchOption);
DirectoryInfo? CreateDirectory(string path);
bool? DeleteDirectory(string path, bool recursive = false);
FileOperationResult CreateDirectory(string path);
FileOperationResult DeleteDirectory(string path, bool recursive = false);
bool DirectoryExists(string path);
bool FileExists(string path);
bool? MoveFile(string sourceFileName, string destFileName, bool overwrite = false);
bool? DeleteFile(string path);
FileOperationResult MoveFile(string sourceFileName, string destFileName, bool overwrite = false);
FileOperationResult DeleteFile(string path);
DateTimeOffset GetFileCreationTime(string path);
string? ReadAllTextFromFile(string file);
Stream? OpenFileForReading(string path);
Stream? OpenFileForReading(string path,
Stream OpenFileForReading(string path);
Stream OpenFileForReading(string path,
bool useAsync,
FileMode fileMode = FileMode.Open,
FileAccess fileAccess = FileAccess.Read,
FileShare fileShare = FileShare.ReadWrite,
int bufferSize = 4096);
Stream? CreateFileForWriting(string path);
bool? WriteAllTextToFile(string path, string contents);
(FileOperationResult, Stream) CreateFileForWriting(string path);
FileOperationResult WriteAllTextToFile(string path, string contents);
}
4 changes: 2 additions & 2 deletions src/Sentry/Internal/InstallationIdHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ internal class InstallationIdHelper(SentryOptions options)
var directoryPath = Path.Combine(rootPath, "Sentry", options.Dsn!.GetHashString());
var fileSystem = options.FileSystem;

if (!fileSystem.CreateDirectory(directoryPath))
if (fileSystem.CreateDirectory(directoryPath) is not FileOperationResult.Success)
{
options.LogDebug("Failed to create a directory for installation ID file ({0}).", directoryPath);
return null;
Expand All @@ -79,7 +79,7 @@ internal class InstallationIdHelper(SentryOptions options)

// Generate new installation ID and store it in a file
var id = Guid.NewGuid().ToString();
if (!fileSystem.WriteAllTextToFile(filePath, id))
if (fileSystem.WriteAllTextToFile(filePath, id) is not FileOperationResult.Success)
{
options.LogDebug("Failed to write Installation ID to file ({0}).", filePath);
return null;
Expand Down
57 changes: 57 additions & 0 deletions src/Sentry/Internal/ReadOnlyFilesystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using Sentry.Extensibility;

namespace Sentry.Internal;

internal class ReadOnlyFileSystem : IFileSystem
{
public IEnumerable<string> EnumerateFiles(string path) => Directory.EnumerateFiles(path);

public IEnumerable<string> EnumerateFiles(string path, string searchPattern) =>
Directory.EnumerateFiles(path, searchPattern);

public IEnumerable<string> EnumerateFiles(string path, string searchPattern, SearchOption searchOption) =>
Directory.EnumerateFiles(path, searchPattern, searchOption);

public FileOperationResult CreateDirectory(string path) => FileOperationResult.Disabled;

public FileOperationResult DeleteDirectory(string path, bool recursive = false) => FileOperationResult.Disabled;

public bool DirectoryExists(string path) => Directory.Exists(path);

public bool FileExists(string path) => File.Exists(path);

public FileOperationResult MoveFile(string sourceFileName, string destFileName, bool overwrite = false) =>
FileOperationResult.Disabled;

public FileOperationResult DeleteFile(string path) => FileOperationResult.Disabled;

public DateTimeOffset GetFileCreationTime(string path) => new FileInfo(path).CreationTimeUtc;

public string ReadAllTextFromFile(string path) => File.ReadAllText(path);

public Stream OpenFileForReading(string path) => File.OpenRead(path);

public Stream OpenFileForReading(string path,
bool useAsync,
FileMode fileMode = FileMode.Open,
FileAccess fileAccess = FileAccess.Read,
FileShare fileShare = FileShare.ReadWrite,
int bufferSize = 4096)
{
return new FileStream(
path,
fileMode,
fileAccess,
fileShare,
bufferSize: bufferSize,
useAsync: useAsync);
}

public (FileOperationResult, Stream) CreateFileForWriting(string path) => (FileOperationResult.Disabled, Stream.Null);

public FileOperationResult WriteAllTextToFile(string path, string contents)
{
File.WriteAllText(path, contents);
return File.Exists(path) ? FileOperationResult.Success : FileOperationResult.Failure;
}
}
Loading

0 comments on commit 9ae1583

Please sign in to comment.