mirror of
https://github.com/beefytech/Beef.git
synced 2025-06-15 06:44:10 +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;
|
retry = true;
|
||||||
}
|
}
|
||||||
if (!retry)
|
if (!retry)
|
||||||
return .Err(.FileOpenError(fileOpenErr));
|
return .Err(.OpenError(fileOpenErr));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
break;
|
break;
|
||||||
|
@ -182,7 +182,7 @@ namespace Beefy
|
||||||
|
|
||||||
int fileLen = (.)sr.Length;
|
int fileLen = (.)sr.Length;
|
||||||
if (sr.TryRead(.((.)outBuffer.PrepareBuffer(fileLen), fileLen)) case .Err(let readErr))
|
if (sr.TryRead(.((.)outBuffer.PrepareBuffer(fileLen), fileLen)) case .Err(let readErr))
|
||||||
return .Err(.FileReadError(readErr));
|
return .Err(.ReadError(readErr));
|
||||||
|
|
||||||
if (onPreFilter != null)
|
if (onPreFilter != null)
|
||||||
onPreFilter();
|
onPreFilter();
|
||||||
|
|
|
@ -6,21 +6,24 @@ namespace System.IO
|
||||||
{
|
{
|
||||||
public enum FileOpenError
|
public enum FileOpenError
|
||||||
{
|
{
|
||||||
|
Unknown,
|
||||||
NotFound,
|
NotFound,
|
||||||
NotFile,
|
NotFile,
|
||||||
Unknown,
|
|
||||||
SharingViolation
|
SharingViolation
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum FileReadError
|
public enum FileReadError
|
||||||
{
|
{
|
||||||
Unknown
|
Unknown,
|
||||||
|
Timeout
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum FileError
|
public enum FileError
|
||||||
{
|
{
|
||||||
case FileOpenError(FileOpenError);
|
case Unknown;
|
||||||
case FileReadError(FileReadError);
|
case OpenError(FileOpenError);
|
||||||
|
case ReadError(FileReadError);
|
||||||
|
case SeekError;
|
||||||
}
|
}
|
||||||
|
|
||||||
static class File
|
static class File
|
||||||
|
@ -30,7 +33,7 @@ namespace System.IO
|
||||||
FileStream fs = scope FileStream();
|
FileStream fs = scope FileStream();
|
||||||
var result = fs.Open(path, .Open, .Read);
|
var result = fs.Open(path, .Open, .Read);
|
||||||
if (result case .Err(let err))
|
if (result case .Err(let err))
|
||||||
return .Err(.FileOpenError(err));
|
return .Err(.OpenError(err));
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
|
@ -61,9 +64,9 @@ namespace System.IO
|
||||||
{
|
{
|
||||||
StreamReader sr = scope StreamReader();
|
StreamReader sr = scope StreamReader();
|
||||||
if (sr.Open(path) case .Err(let err))
|
if (sr.Open(path) case .Err(let err))
|
||||||
return .Err(.FileOpenError(err));
|
return .Err(.OpenError(err));
|
||||||
if (sr.ReadToEnd(outText) case .Err)
|
if (sr.ReadToEnd(outText) case .Err)
|
||||||
return .Err(.FileReadError(.Unknown));
|
return .Err(.ReadError(.Unknown));
|
||||||
|
|
||||||
if (!preserveLineEnding)
|
if (!preserveLineEnding)
|
||||||
{
|
{
|
||||||
|
|
|
@ -62,12 +62,20 @@ namespace System.IO
|
||||||
return numBytesRead;
|
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;
|
Platform.BfpFileResult result = .Ok;
|
||||||
int numBytesRead = Platform.BfpFile_Read(mBfpFile, data.Ptr, data.Length, timeoutMS, &result);
|
int numBytesRead = Platform.BfpFile_Read(mBfpFile, data.Ptr, data.Length, timeoutMS, &result);
|
||||||
if ((result != .Ok) && (result != .PartialData))
|
if ((result != .Ok) && (result != .PartialData))
|
||||||
return .Err;
|
{
|
||||||
|
switch (result)
|
||||||
|
{
|
||||||
|
case .Timeout:
|
||||||
|
return .Err(.ReadError(.Timeout));
|
||||||
|
default:
|
||||||
|
return .Err(.ReadError(.Unknown));
|
||||||
|
}
|
||||||
|
}
|
||||||
return numBytesRead;
|
return numBytesRead;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -463,10 +471,10 @@ namespace System.IO
|
||||||
mUnderlyingLength = Platform.BfpFile_GetFileSize(mBfpFile);
|
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);
|
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)
|
if (result case .Ok)
|
||||||
mBfpFilePos = newPos;
|
mBfpFilePos = newPos;
|
||||||
return result;
|
return result;
|
||||||
|
@ -498,7 +506,7 @@ namespace System.IO
|
||||||
return numBytesRead;
|
return numBytesRead;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Result<int> TryRead(Span<uint8> data, int timeoutMS)
|
public Result<int, FileError> TryRead(Span<uint8> data, int timeoutMS)
|
||||||
{
|
{
|
||||||
if (mBfpFilePos != mPos)
|
if (mBfpFilePos != mPos)
|
||||||
Try!(SeekUnderlying(mPos));
|
Try!(SeekUnderlying(mPos));
|
||||||
|
@ -506,7 +514,15 @@ namespace System.IO
|
||||||
Platform.BfpFileResult result = .Ok;
|
Platform.BfpFileResult result = .Ok;
|
||||||
int numBytesRead = Platform.BfpFile_Read(mBfpFile, data.Ptr, data.Length, timeoutMS, &result);
|
int numBytesRead = Platform.BfpFile_Read(mBfpFile, data.Ptr, data.Length, timeoutMS, &result);
|
||||||
if ((result != .Ok) && (result != .PartialData))
|
if ((result != .Ok) && (result != .PartialData))
|
||||||
return .Err;
|
{
|
||||||
|
switch (result)
|
||||||
|
{
|
||||||
|
case .Timeout:
|
||||||
|
return .Err(.ReadError(.Timeout));
|
||||||
|
default:
|
||||||
|
return .Err(.ReadError(.Unknown));
|
||||||
|
}
|
||||||
|
}
|
||||||
return numBytesRead;
|
return numBytesRead;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -248,7 +248,8 @@ namespace System
|
||||||
AccessError = (int)Result.AccessError,
|
AccessError = (int)Result.AccessError,
|
||||||
PartialData = (int)Result.PartialData,
|
PartialData = (int)Result.PartialData,
|
||||||
InsufficientBuffer = (int)Result.InsufficientBuffer,
|
InsufficientBuffer = (int)Result.InsufficientBuffer,
|
||||||
NotEmpty = (int)Result.NotEmpty
|
Timeout = (int)Result.Timeout,
|
||||||
|
NotEmpty = (int)Result.NotEmpty,
|
||||||
};
|
};
|
||||||
|
|
||||||
[CallingConvention(.Stdcall), CLink]
|
[CallingConvention(.Stdcall), CLink]
|
||||||
|
|
|
@ -2900,6 +2900,12 @@ BFP_EXPORT intptr BFP_CALLTYPE BfpFile_Read(BfpFile* file, void* buffer, intptr
|
||||||
{
|
{
|
||||||
if (timeoutMS != -1)
|
if (timeoutMS != -1)
|
||||||
{
|
{
|
||||||
|
if (file->mAsyncData == NULL)
|
||||||
|
{
|
||||||
|
OUTRESULT(BfpFileResult_InvalidParameter);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
OverlappedReadResult overlapped;
|
OverlappedReadResult overlapped;
|
||||||
|
@ -2909,12 +2915,6 @@ 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
|
//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 (::ReadFileEx(file->mHandle, buffer, (uint32)size, &overlapped, OverlappedReadComplete))
|
||||||
{
|
{
|
||||||
if (file->mAsyncData == NULL)
|
|
||||||
{
|
|
||||||
OUTRESULT(BfpFileResult_InvalidParameter);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!file->mAsyncData->WaitAndResetEvent(timeoutMS))
|
if (!file->mAsyncData->WaitAndResetEvent(timeoutMS))
|
||||||
{
|
{
|
||||||
::CancelIoEx(file->mHandle, &overlapped);
|
::CancelIoEx(file->mHandle, &overlapped);
|
||||||
|
@ -2989,6 +2989,7 @@ BFP_EXPORT intptr BFP_CALLTYPE BfpFile_Read(BfpFile* file, void* buffer, intptr
|
||||||
switch (lastError)
|
switch (lastError)
|
||||||
{
|
{
|
||||||
case ERROR_BROKEN_PIPE: // Just an EOF
|
case ERROR_BROKEN_PIPE: // Just an EOF
|
||||||
|
OUTRESULT(BfpFileResult_Ok);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
OUTRESULT(BfpFileResult_UnknownError);
|
OUTRESULT(BfpFileResult_UnknownError);
|
||||||
|
|
|
@ -13204,7 +13204,7 @@ namespace IDE
|
||||||
editData.BuildHash(editData.mQueuedContent);
|
editData.BuildHash(editData.mQueuedContent);
|
||||||
}) case .Err(let err))
|
}) case .Err(let err))
|
||||||
{
|
{
|
||||||
if (err case .FileOpenError(.SharingViolation))
|
if (err case .OpenError(.SharingViolation))
|
||||||
{
|
{
|
||||||
// Put back on the list and try later
|
// Put back on the list and try later
|
||||||
mFileWatcher.AddChangedFile(changedFile);
|
mFileWatcher.AddChangedFile(changedFile);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue