mirror of
https://github.com/beefytech/Beef.git
synced 2025-06-10 12:32:20 +02:00
Improved errors on files
This commit is contained in:
parent
85c936f014
commit
befc60aa63
6 changed files with 48 additions and 27 deletions
|
@ -173,7 +173,7 @@ namespace Beefy
|
|||
retry = true;
|
||||
}
|
||||
if (!retry)
|
||||
return .Err(.FileOpenError(fileOpenErr));
|
||||
return .Err(.OpenError(fileOpenErr));
|
||||
}
|
||||
else
|
||||
break;
|
||||
|
@ -182,7 +182,7 @@ namespace Beefy
|
|||
|
||||
int fileLen = (.)sr.Length;
|
||||
if (sr.TryRead(.((.)outBuffer.PrepareBuffer(fileLen), fileLen)) case .Err(let readErr))
|
||||
return .Err(.FileReadError(readErr));
|
||||
return .Err(.ReadError(readErr));
|
||||
|
||||
if (onPreFilter != null)
|
||||
onPreFilter();
|
||||
|
|
|
@ -6,21 +6,24 @@ namespace System.IO
|
|||
{
|
||||
public enum FileOpenError
|
||||
{
|
||||
Unknown,
|
||||
NotFound,
|
||||
NotFile,
|
||||
Unknown,
|
||||
SharingViolation
|
||||
}
|
||||
|
||||
public enum FileReadError
|
||||
{
|
||||
Unknown
|
||||
Unknown,
|
||||
Timeout
|
||||
}
|
||||
|
||||
public enum FileError
|
||||
{
|
||||
case FileOpenError(FileOpenError);
|
||||
case FileReadError(FileReadError);
|
||||
case Unknown;
|
||||
case OpenError(FileOpenError);
|
||||
case ReadError(FileReadError);
|
||||
case SeekError;
|
||||
}
|
||||
|
||||
static class File
|
||||
|
@ -30,7 +33,7 @@ namespace System.IO
|
|||
FileStream fs = scope FileStream();
|
||||
var result = fs.Open(path, .Open, .Read);
|
||||
if (result case .Err(let err))
|
||||
return .Err(.FileOpenError(err));
|
||||
return .Err(.OpenError(err));
|
||||
|
||||
while (true)
|
||||
{
|
||||
|
@ -61,9 +64,9 @@ namespace System.IO
|
|||
{
|
||||
StreamReader sr = scope StreamReader();
|
||||
if (sr.Open(path) case .Err(let err))
|
||||
return .Err(.FileOpenError(err));
|
||||
return .Err(.OpenError(err));
|
||||
if (sr.ReadToEnd(outText) case .Err)
|
||||
return .Err(.FileReadError(.Unknown));
|
||||
return .Err(.ReadError(.Unknown));
|
||||
|
||||
if (!preserveLineEnding)
|
||||
{
|
||||
|
|
|
@ -62,12 +62,20 @@ namespace System.IO
|
|||
return numBytesRead;
|
||||
}
|
||||
|
||||
public virtual Result<int> TryRead(Span<uint8> data, int timeoutMS)
|
||||
public virtual Result<int, FileError> TryRead(Span<uint8> data, int timeoutMS)
|
||||
{
|
||||
Platform.BfpFileResult result = .Ok;
|
||||
int numBytesRead = Platform.BfpFile_Read(mBfpFile, data.Ptr, data.Length, timeoutMS, &result);
|
||||
if ((result != .Ok) && (result != .PartialData))
|
||||
return .Err;
|
||||
{
|
||||
switch (result)
|
||||
{
|
||||
case .Timeout:
|
||||
return .Err(.ReadError(.Timeout));
|
||||
default:
|
||||
return .Err(.ReadError(.Unknown));
|
||||
}
|
||||
}
|
||||
return numBytesRead;
|
||||
}
|
||||
|
||||
|
@ -463,10 +471,10 @@ namespace System.IO
|
|||
mUnderlyingLength = Platform.BfpFile_GetFileSize(mBfpFile);
|
||||
}
|
||||
|
||||
protected Result<void> SeekUnderlying(int64 offset, Platform.BfpFileSeekKind seekKind = .Absolute)
|
||||
protected Result<void, FileError> SeekUnderlying(int64 offset, Platform.BfpFileSeekKind seekKind = .Absolute)
|
||||
{
|
||||
int64 newPos = Platform.BfpFile_Seek(mBfpFile, offset, seekKind);
|
||||
Result<void> result = ((seekKind == .Absolute) && (newPos != offset)) ? .Err : .Ok;
|
||||
Result<void, FileError> result = ((seekKind == .Absolute) && (newPos != offset)) ? .Err(.SeekError) : .Ok;
|
||||
if (result case .Ok)
|
||||
mBfpFilePos = newPos;
|
||||
return result;
|
||||
|
@ -498,7 +506,7 @@ namespace System.IO
|
|||
return numBytesRead;
|
||||
}
|
||||
|
||||
public Result<int> TryRead(Span<uint8> data, int timeoutMS)
|
||||
public Result<int, FileError> TryRead(Span<uint8> data, int timeoutMS)
|
||||
{
|
||||
if (mBfpFilePos != mPos)
|
||||
Try!(SeekUnderlying(mPos));
|
||||
|
@ -506,7 +514,15 @@ namespace System.IO
|
|||
Platform.BfpFileResult result = .Ok;
|
||||
int numBytesRead = Platform.BfpFile_Read(mBfpFile, data.Ptr, data.Length, timeoutMS, &result);
|
||||
if ((result != .Ok) && (result != .PartialData))
|
||||
return .Err;
|
||||
{
|
||||
switch (result)
|
||||
{
|
||||
case .Timeout:
|
||||
return .Err(.ReadError(.Timeout));
|
||||
default:
|
||||
return .Err(.ReadError(.Unknown));
|
||||
}
|
||||
}
|
||||
return numBytesRead;
|
||||
}
|
||||
|
||||
|
|
|
@ -248,7 +248,8 @@ namespace System
|
|||
AccessError = (int)Result.AccessError,
|
||||
PartialData = (int)Result.PartialData,
|
||||
InsufficientBuffer = (int)Result.InsufficientBuffer,
|
||||
NotEmpty = (int)Result.NotEmpty
|
||||
Timeout = (int)Result.Timeout,
|
||||
NotEmpty = (int)Result.NotEmpty,
|
||||
};
|
||||
|
||||
[CallingConvention(.Stdcall), CLink]
|
||||
|
|
|
@ -1613,7 +1613,7 @@ public:
|
|||
startupInfo.hStdInput = INVALID_HANDLE_VALUE;
|
||||
|
||||
if ((flags & BfpSpawnFlag_RedirectStdOutput) != 0)
|
||||
CreatePipe(mStandardOutputReadPipeHandle, startupInfo.hStdOutput, false);
|
||||
CreatePipe(mStandardOutputReadPipeHandle, startupInfo.hStdOutput, false);
|
||||
else
|
||||
startupInfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
|
||||
|
@ -2899,7 +2899,13 @@ static void WINAPI OverlappedReadComplete(DWORD dwErrorCode, DWORD dwNumberOfByt
|
|||
BFP_EXPORT intptr BFP_CALLTYPE BfpFile_Read(BfpFile* file, void* buffer, intptr size, int timeoutMS, BfpFileResult* outResult)
|
||||
{
|
||||
if (timeoutMS != -1)
|
||||
{
|
||||
{
|
||||
if (file->mAsyncData == NULL)
|
||||
{
|
||||
OUTRESULT(BfpFileResult_InvalidParameter);
|
||||
return 0;
|
||||
}
|
||||
|
||||
while (true)
|
||||
{
|
||||
OverlappedReadResult overlapped;
|
||||
|
@ -2908,13 +2914,7 @@ BFP_EXPORT intptr BFP_CALLTYPE BfpFile_Read(BfpFile* file, void* buffer, intptr
|
|||
|
||||
//TODO: this doesn't set file stream location. It only works for streams like pipes, sockets, etc
|
||||
if (::ReadFileEx(file->mHandle, buffer, (uint32)size, &overlapped, OverlappedReadComplete))
|
||||
{
|
||||
if (file->mAsyncData == NULL)
|
||||
{
|
||||
OUTRESULT(BfpFileResult_InvalidParameter);
|
||||
return 0;
|
||||
}
|
||||
|
||||
{
|
||||
if (!file->mAsyncData->WaitAndResetEvent(timeoutMS))
|
||||
{
|
||||
::CancelIoEx(file->mHandle, &overlapped);
|
||||
|
@ -2981,7 +2981,7 @@ BFP_EXPORT intptr BFP_CALLTYPE BfpFile_Read(BfpFile* file, void* buffer, intptr
|
|||
if (bytesRead != size)
|
||||
OUTRESULT(BfpFileResult_PartialData);
|
||||
else
|
||||
OUTRESULT(BfpFileResult_Ok);
|
||||
OUTRESULT(BfpFileResult_Ok);
|
||||
return bytesRead;
|
||||
}
|
||||
|
||||
|
@ -2989,6 +2989,7 @@ BFP_EXPORT intptr BFP_CALLTYPE BfpFile_Read(BfpFile* file, void* buffer, intptr
|
|||
switch (lastError)
|
||||
{
|
||||
case ERROR_BROKEN_PIPE: // Just an EOF
|
||||
OUTRESULT(BfpFileResult_Ok);
|
||||
break;
|
||||
default:
|
||||
OUTRESULT(BfpFileResult_UnknownError);
|
||||
|
|
|
@ -13204,7 +13204,7 @@ namespace IDE
|
|||
editData.BuildHash(editData.mQueuedContent);
|
||||
}) case .Err(let err))
|
||||
{
|
||||
if (err case .FileOpenError(.SharingViolation))
|
||||
if (err case .OpenError(.SharingViolation))
|
||||
{
|
||||
// Put back on the list and try later
|
||||
mFileWatcher.AddChangedFile(changedFile);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue