You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The operating system will close file descriptors on exit. If you have a long-running process, you should close them yourself though, otherwise you'll eventually run out of file descriptors.
Sorry to necro this thread so badly but I am really struggling with this.
As my process opens a file descriptor to read & write to a file I need to close it. However in the event failure during my programs lifecycle it may come to pass that I never end up writing to the file and have to call close without having written anything. Now if I do this the entire file content is cleared (since I opened it in w+ mode).
This is very undesired behavior since the file contained information which is now lost. This behavior does not occur when using open(file, 'a+') as that positions the cursor at the end of the file but therefore it writes from the end of the file rather than the start thereby not overwriting the existing content.
I am unable to find a way to either arbitrarily seek the cursor nor close the file without it removing any content behind the cursor. So while theoretically I could forgo calling close that would not be the correct solution.
Edit: I am using node.js v20.17.0
Edit2: It seems to be that the behavior I am observing is a problem with the open rather than the close. the w+ truncates the content on open. Therefore this isn't a problem with close and unrelated to this thread. Pardon the necro :(
@CEbbinghaus Yes, you should avoid opening the file if you don't want to truncate it. You probably actually want to use the write(), fsync(), and close() on a temporary file and then rename() to your target file pattern if you are trying to rewrite the file arbitrarily. Otherwise, if you are basically implementing a database or tail logging, then you'd want to open it in append mode as you described.
Activity
bnoordhuis commentedon Oct 3, 2015
The operating system will close file descriptors on exit. If you have a long-running process, you should close them yourself though, otherwise you'll eventually run out of file descriptors.
vsemozhetbyt commentedon Oct 3, 2015
Thank you.
binki commentedon Oct 24, 2016
Could this be added to the documentation of
fs.open()?CEbbinghaus commentedon Aug 25, 2024
Sorry to necro this thread so badly but I am really struggling with this.As my process opens a file descriptor to read & write to a file I need to close it. However in the event failure during my programs lifecycle it may come to pass that I never end up writing to the file and have to callclosewithout having written anything. Now if I do this the entire file content is cleared (since I opened it inw+mode).simple reproThis is very undesired behavior since the file contained information which is now lost. This behavior does not occur when usingopen(file, 'a+')as that positions the cursor at the end of the file but therefore it writes from the end of the file rather than the start thereby not overwriting the existing content.I am unable to find a way to either arbitrarily seek the cursor nor close the file without it removing any content behind the cursor. So while theoretically I could forgo calling close that would not be the correct solution.Edit: I am using node.js v20.17.0Edit2: It seems to be that the behavior I am observing is a problem with the open rather than the close. the
w+truncates the content on open. Therefore this isn't a problem with close and unrelated to this thread. Pardon the necro :(binki commentedon Aug 25, 2024
@CEbbinghaus Yes, you should avoid opening the file if you don't want to truncate it. You probably actually want to use the
write(),fsync(), andclose()on a temporary file and thenrename()to your target file pattern if you are trying to rewrite the file arbitrarily. Otherwise, if you are basically implementing a database or tail logging, then you'd want to open it in append mode as you described.CEbbinghaus commentedon Aug 25, 2024
For anyone that may stumble upon this thread in future with a similar problem to me I have found the best solution to be:
This way if no data is written the file is closed with the same content it had and if we are writing to it we overwrite the data that was there.
binki commentedon Aug 25, 2024
@CEbbinghaus that is unsafe. Use the pattern of writing to a temporary file and renaming.