Skip to content

Commit

Permalink
make filesystem smarter
Browse files Browse the repository at this point in the history
  • Loading branch information
bitsandfoxes committed Sep 23, 2024
1 parent c3ebbab commit 4da424a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 24 deletions.
48 changes: 32 additions & 16 deletions src/Sentry/Internal/FileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,18 @@ public IEnumerable<string> EnumerateFiles(string path, string searchPattern) =>
public IEnumerable<string> EnumerateFiles(string path, string searchPattern, SearchOption searchOption) =>
Directory.EnumerateFiles(path, searchPattern, searchOption);

public bool CreateDirectory(string path)
public DirectoryInfo? CreateDirectory(string path)
{
if (!_options?.DisableFileWrite is false)
{
_options?.LogDebug("Skipping creating directory. Writing to file system has been explicitly disabled.");
return false;
return null;
}

Directory.CreateDirectory(path);
return true;
return Directory.CreateDirectory(path);
}

public bool DeleteDirectory(string path, bool recursive = false)
public bool? DeleteDirectory(string path, bool recursive = false)
{
if (!_options?.DisableFileWrite is false)
{
Expand All @@ -40,19 +39,19 @@ public bool DeleteDirectory(string path, bool recursive = false)
}

Directory.Delete(path, recursive);
return true;
return !Directory.Exists(path);
}

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

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

public bool MoveFile(string sourceFileName, string destFileName, bool overwrite = false)
public bool? MoveFile(string sourceFileName, string destFileName, bool overwrite = false)
{
if (!_options?.DisableFileWrite is false)
{
_options?.LogDebug("Skipping moving file. Writing to file system has been explicitly disabled.");
return false;
return null;
}

#if NETCOREAPP3_0_OR_GREATER
Expand All @@ -68,19 +67,25 @@ public bool MoveFile(string sourceFileName, string destFileName, bool overwrite
File.Move(sourceFileName, destFileName);
}
#endif

if (File.Exists(sourceFileName) || !File.Exists(destFileName))
{
return false;
}

return true;
}

public bool DeleteFile(string path)
public bool? DeleteFile(string path)
{
if (!_options?.DisableFileWrite is false)
{
_options?.LogDebug("Skipping deleting file. Writing to file system has been explicitly disabled.");
return false;
return null;
}

File.Delete(path);
return true;
return !File.Exists(path);
}

public DateTimeOffset GetFileCreationTime(string path) => new FileInfo(path).CreationTimeUtc;
Expand All @@ -89,26 +94,37 @@ public bool DeleteFile(string path)

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

public Stream CreateFileForWriting(string 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 Stream? CreateFileForWriting(string path)
{
if (!_options?.DisableFileWrite is false)
{
_options?.LogDebug("Skipping file for writing. Writing to file system has been explicitly disabled.");
return Stream.Null;
return null;
}

return File.Create(path);
}

public bool WriteAllTextToFile(string path, string contents)
public bool? WriteAllTextToFile(string path, string contents)
{
if (!_options?.DisableFileWrite is false)
{
_options?.LogDebug("Skipping writing all text to file. Writing to file system has been explicitly disabled.");
return false;
return null;
}

File.WriteAllText(path, contents);
return true;
return File.Exists(path);
}
}
22 changes: 14 additions & 8 deletions src/Sentry/Internal/IFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,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);
bool CreateDirectory(string path);
bool DeleteDirectory(string path, bool recursive = false);
DirectoryInfo? CreateDirectory(string path);
bool? 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);
bool? MoveFile(string sourceFileName, string destFileName, bool overwrite = false);
bool? DeleteFile(string path);
DateTimeOffset GetFileCreationTime(string path);
string ReadAllTextFromFile(string file);
Stream OpenFileForReading(string path);
Stream CreateFileForWriting(string path);
bool WriteAllTextToFile(string path, string contents);
string? ReadAllTextFromFile(string file);
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);
}

0 comments on commit 4da424a

Please sign in to comment.