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

Mention that ERR_FILE_EOF is not always an error #8590

Open
andreymal opened this issue Dec 8, 2023 · 6 comments
Open

Mention that ERR_FILE_EOF is not always an error #8590

andreymal opened this issue Dec 8, 2023 · 6 comments

Comments

@andreymal
Copy link

Let's look at a naive implementation of error handling:

var fp := FileAccess.open("res://myfile.txt", FileAccess.READ)
var data := fp.get_buffer(65536)
if fp.get_error():
    print("PANIK I/O IS BROKEN YOUR HDD IS DYING AAAAAAA")

If the file is small enough, get_buffer reaches EOF, and get_error returns 18 (ERR_FILE_EOF).

But unless the user expects exactly 65536 bytes in the buffer, it's not an error, and ERR_FILE_EOF can be ignored.

This may be obvious for experienced users, but it's easy to overlook it.

I suggest mentioning somewhere (probably in the FileAccess documentation) that reaching EOF when reading dynamically sized data (i.e. when using get_buffer, get_line and probably other methods) doesn't always indicate something bad and can be safely ignored in some cases.

@AThousandShips
Copy link
Member

AThousandShips commented Dec 8, 2023

This isn't true, it is an error to try read more than is available in the file, you should use some other method to read from the file, this error tells you that you are, in fact, reading too much, that's what the error is for, reading like this is not proper IMO and shouldn't be encouraged

@andreymal
Copy link
Author

@AThousandShips why? It is never interpreted as an error in the read function in Linux and Windows, for example. In particular, read(2) states explicitly:

It is not an error if this number is smaller than the number of bytes requested

Is there a good reason why Godot shouldn't follow the same logic?

@AThousandShips
Copy link
Member

AThousandShips commented Dec 8, 2023

Well those are not the same functions

As I said I have an issue with suggesting behavior that I don't think is good code, and the method documentation says:

Returns next length bytes of the file as a PackedByteArray.

Reading like this makes it riskier and makes error handling harder, instead people should, in my opinion, read in a way that considers the data remaining

Also it's just us here, please don't ping me 🙂

@andreymal
Copy link
Author

This implies calling get_length() (its implementation in FileAccessUnix is quite slow, by the way) and calculating the buffer size on the fly, which probably makes the code even more error-prone and difficult to maintain 🤔

And get_line() can't know the data remaining

@AThousandShips
Copy link
Member

AThousandShips commented Dec 8, 2023

And get_line() can't know the data remaining

Yes, but that's different, it isn't provided an explicit length, you can do this way with providing a longer length, but I'd say that saying "don't worry" sends the wrong message, but again, taht's my opinion on this. We return an error, that error should be respected

And especially, IMO, if you are reading in this way you are expected to know what you do

@andreymal
Copy link
Author

andreymal commented Dec 8, 2023

Hm, there is actually a warning in FileAccessMemory::get_buffer:

	if (read < p_length) {
		WARN_PRINT("Reading less data than requested");
	}

But other implementations of FileAccess::get_buffer don't have this warning, which is inconsistent.

Seems like a clear choice needs to be made:

  • If EOF is an error, then this warning should probably be added to other implementations
  • If EOF is not always an error and can be ignored in some cases, then this warning should probably be removed to make FileAccessMemory consistent with other implementations and not annoy users with potentially useless warnings

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

No branches or pull requests

2 participants