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

[FileAccess] Return error codes from store_* methods. #78289

Merged
merged 1 commit into from
Nov 29, 2024

Conversation

bruvzg
Copy link
Member

@bruvzg bruvzg commented Jun 15, 2023

Changes FileAccess methods from void store_*(...) to Error store_*().

See #77398, I was not able to reproduce an issue (might be related to overzealous antivirus activity), but file writes can fail for various reasons, and returning error seems like a good idea.

@bruvzg bruvzg added this to the 4.x milestone Jun 15, 2023
@RedworkDE
Copy link
Member

FileAccess::get_error() already exists to get errors from load/store operations, tho it is pretty unclear when you should check it and when it is reset. IMO it should be fixed to be used consistently first, unless you plan on removing it entirely in favor of returning error codes, in which case what about the load methods?

@AThousandShips
Copy link
Member

AThousandShips commented Jun 16, 2023

Maybe returning a bool for the write operations instead? To indicate success, and use the error method to check, makes the immediate check simpler, so you don't need to do err != OK, but still signals clearly

Alternatively add a simple convenience function has_error to do the same simplification, because that feels less unwieldy than comparing an error, also unsure how completely the get_error method is implemented, maybe the focus should be on making it more functional

@bruvzg
Copy link
Member Author

bruvzg commented Jun 16, 2023

Changed return values to bool, errors were non-descriptive anyway. get_error only handle EOF and file opening.

@bruvzg bruvzg force-pushed the fo_write_errs branch 2 times, most recently from f6de7d8 to 82ba4ca Compare June 16, 2023 11:52
@bruvzg bruvzg force-pushed the fo_write_errs branch 2 times, most recently from 1e608ae to e513626 Compare July 6, 2023 07:10
@bruvzg bruvzg marked this pull request as ready for review July 6, 2023 08:46
@bruvzg bruvzg requested review from a team as code owners July 6, 2023 08:46
val writtenBytes = fileChannel.write(buffer)
if (writtenBytes > 0) {
endOfFile = false
}
return (totalBytes == writtenBytes)
Copy link
Contributor

Choose a reason for hiding this comment

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

I feel like as long as any bytes are written, we should return true especially as an error would trigger an exception.

Copy link
Member Author

Choose a reason for hiding this comment

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

Not writing all bytes is definitely a failure, but if it will trigger an exception (which seems to be the case for the file), there's no need for this check.

Copy link
Contributor

@m4gr3d m4gr3d left a comment

Choose a reason for hiding this comment

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

The updates to the Android section look good!

@m4gr3d m4gr3d self-requested a review September 6, 2023 18:42
@raulsntos
Copy link
Member

Note that this will break compatibility in C# and there's no way to provide compat methods because the only thing that changes is the return type. We could consider adding new methods instead (e.g.: try_store_double).

@YuriSizov YuriSizov modified the milestones: 4.x, 4.3 Oct 2, 2023
@Bromeon
Copy link
Contributor

Bromeon commented Nov 11, 2023

Changed return values to bool, errors were non-descriptive anyway. get_error only handle EOF and file opening.

Are we sure that bool is enough? There are different reasons why writing can fail (opened in wrong mode, I/O error, disk space, etc.). Not that we see ourselves in the same situation in a few months, but with a too limited return code.

So it looks like get_error() is pretty much useless for writing at the moment? Do we plan to change that in the future?

Copy link
Member

Choose a reason for hiding this comment

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

Needs to be moved to the 4.2 file now that it exists

@akien-mga akien-mga modified the milestones: 4.3, 4.4 May 16, 2024
@bruvzg bruvzg force-pushed the fo_write_errs branch 4 times, most recently from 4b4545a to 18193f9 Compare November 23, 2024 14:11
@akien-mga
Copy link
Member

Needs rebase.

@akien-mga akien-mga merged commit a0a459e into godotengine:master Nov 29, 2024
13 of 19 checks passed
@akien-mga
Copy link
Member

Thanks!

Merged a bit too early - the CI failures are not caused by this PR but a previous merge batch.

@Bromeon
Copy link
Contributor

Bromeon commented Nov 29, 2024

Are we sure that bool is enough? There are different reasons why writing can fail (opened in wrong mode, I/O error, disk space, etc.). Not that we see ourselves in the same situation in a few months, but with a too limited return code.

So it looks like get_error() is pretty much useless for writing at the moment? Do we plan to change that in the future?

Could someone clarify that? Not arguing against the PR but I'm writing a higher-level abstraction and would appreciate some insight in the intended error handling ☺️ thanks!

@bruvzg
Copy link
Member Author

bruvzg commented Nov 29, 2024

I'm not sure why you would need any additional information, file write errors are non recoverable, the only thing you can do is close the file handle and assume that file, and disk it was on are gone.

@bruvzg
Copy link
Member Author

bruvzg commented Nov 29, 2024

Also the amount of info we can get is limited and platform dependent, so it won't be consistent.

But if you have some specific cases for it in mind, we can handle it by setting the last error value returned by get_error.

@Bromeon
Copy link
Contributor

Bromeon commented Nov 30, 2024

There are tons of reasons why writing can fail, file/medium gone is just one of them. There may be permission issues, another process locking the handle, disk full, ... Even C provides minimal information with errno, most other languages build more on top.

I'm not asking for a cross-platform API or error codes or so, just wondered if there's a particular design reason why the already existing get_error isn't used for writing? 🙂 (It may just be a case of "not yet implemented", which is perfectly fine as well 👍)

@geekley
Copy link

geekley commented Mar 3, 2025

Documentation doesn't explain the meaning of the bool return value. Is true returned on success or on error? The comments in this PR don't clarify it either.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants