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

Add FileAccess.set_length(new_length) to truncate existing files #6776

Closed
tagcode opened this issue Apr 29, 2023 · 8 comments
Closed

Add FileAccess.set_length(new_length) to truncate existing files #6776

tagcode opened this issue Apr 29, 2023 · 8 comments
Milestone

Comments

@tagcode
Copy link

tagcode commented Apr 29, 2023

Describe the project you are working on

Modifying existing files, such as game state, cannot be shortened.

I cannot truncate file.

Describe the problem or limitation you are having in your project

File can be enlarged by writing new content, but cannot be shortened.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

Add FileAccess.set_length()

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

Calls OS IO function that shortens file size.

If this enhancement will not be used often, can it be worked around with a few lines of script?

Not that I know of.

Is there a reason why this should be core and not an add-on in the asset library?

Basic necessity low-level IO function.

@AThousandShips
Copy link
Member

AThousandShips commented Apr 29, 2023

I'm not sure how common this is in lower level details, or the way truncation of files is supported in various platforms other than copying the data

There's no way to do this as far as I know in the standard c++ library for example

@tagcode
Copy link
Author

tagcode commented Apr 29, 2023

Really? C++ standard library is just an abstraction and a wrapper to OS calls. Other abstractions have this.

On C# there is FileStream.SetLength(). On Windows it seems to call Kernel32.dll.

On Linux I believe it calls ftruncate?

@bruvzg
Copy link
Member

bruvzg commented Apr 29, 2023

There's no way to do this as far as I know in the standard c++ library for example

Godot is not limited to standard C++ file API, it's already relaying on unistd.h and Windows API for it, so this is not an issue.

ftruncate should be supported on all Linux/BSD, Android and macOS/iOS systems. And WinAPI have https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-setendoffile.

@Calinou Calinou changed the title FileAccess.set_length(long new_length) Add FileAccess.set_length(new_length) to truncate existing files Apr 29, 2023
@Calinou
Copy link
Member

Calinou commented Apr 29, 2023

How does this compare to opening a file, reading its first N bytes and saving that over the existing file? I assume a ftruncate() call is faster, but by how much on average (on a SSD)?

@AThousandShips
Copy link
Member

AThousandShips commented Apr 29, 2023

Godot is not limited to standard C++ file API

@bruvzg I am aware that's why I said "for example" as I was unsure how cross platform supported this was for an open file

@tagcode
Copy link
Author

tagcode commented Apr 29, 2023

How does this compare to opening a file, reading its first N bytes and saving that over the existing file? I assume a ftruncate() call is faster, but by how much on average (on a SSD)?

  1. File might be very large, e.g. 1GB
  2. OS differences cause unexpected behaviour. There might be open handles to the file. On linux old handles would refer to old file, changes would not be visible. On windows deleting would be forbidden and operation would fail. Truncate, on the other hand, would not forbid open handles.

@somebody1234
Copy link

i think this is pretty valuable because the handle never gets closed - which is useful for text editors which may want to prevent the file from being deleted from under the engine's nose

@akien-mga
Copy link
Member

Implemented by godotengine/godot#90403.

@akien-mga akien-mga added this to the 4.3 milestone Apr 22, 2024
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

6 participants