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

Close file descriptors of created files #75

Merged
merged 29 commits into from
Jun 21, 2024
Merged

Close file descriptors of created files #75

merged 29 commits into from
Jun 21, 2024

Conversation

bgs99
Copy link
Collaborator

@bgs99 bgs99 commented Jun 18, 2024

  • Add native_handle_type to temporary files
  • Add file::native_handle method
  • Save the native handle
  • Close the file on destruction, releasing or moving

@bgs99 bgs99 requested a review from bugdea1er June 18, 2024 14:13
src/tmp.cpp Outdated Show resolved Hide resolved
@bugdea1er
Copy link
Owner

bugdea1er commented Jun 18, 2024

Actually it would be better to store the descriptor for the created file and close it explicitly when deleting it

create_file should return a pair of native handle and created path:

std::pair<fs::path, file::native_handle_type> create_file(std::string_view prefix, std::string_view suffix)

file should get a new private constructor to initialize both fields:

file::file(std::pair<fs::path, file::native_handle_type> handle, bool binary)
    : tmp::path(std::move(handle.first)),
      binary(binary),
      handle(handle.second) {}

file::file(std::string_view prefix, std::string_view suffix, bool binary)
    : file(create_file(prefix, suffix), binary) {}

And remove function should explicitly close the file before deleting, something like this:

#ifdef WIN32
            CloseHandle(handle);
#else
            close(handle);
#endif

@bugdea1er
Copy link
Owner

bugdea1er commented Jun 18, 2024

And since file will store a handle, it would be a good idea to add a public getter method for it:

/// Returns the underlying implementation-defined handle
[[nodiscard]] native_handle_type file::native_handle() const noexcept;

@bgs99
Copy link
Collaborator Author

bgs99 commented Jun 19, 2024

Added native handle management

@bgs99 bgs99 self-assigned this Jun 19, 2024
@bgs99 bgs99 added the bug Something isn't working label Jun 19, 2024
@bgs99 bgs99 changed the title Close file on creation Close file descriptors of created files Jun 19, 2024
src/tmp.cpp Outdated Show resolved Hide resolved
include/tmp/file Outdated Show resolved Hide resolved
@bgs99 bgs99 assigned bugdea1er and unassigned bgs99 Jun 20, 2024
@@ -174,6 +172,18 @@ void remove(const fs::path& path) noexcept {
}
}

/// Closes the given file, ignoring any errors
/// @param file The file to close
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Suggested change
/// @param file The file to close
/// @param file The file to close

src/tmp.cpp Outdated Show resolved Hide resolved
src/tmp.cpp Outdated Show resolved Hide resolved
tests/file.cpp Outdated Show resolved Hide resolved
@bugdea1er bugdea1er merged commit 5e64f54 into main Jun 21, 2024
12 checks passed
@bugdea1er bugdea1er deleted the keep-fd branch June 21, 2024 21:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants