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

Cleanup test File utilities #1989

Merged
merged 3 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crypto/test/file_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ bool TemporaryFile::Init(bssl::Span<const uint8_t> content) {
#else
std::string path = temp_dir + "bssl_tmp_file.XXXXXX";
// TODO(davidben): Use |path.data()| when we require C++17.
mode_t prev_umask = umask(S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
Copy link
Contributor

Choose a reason for hiding this comment

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

Where did this mask come from?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This particular just ensures that Read/Write permissions for "group" and "others" will not be granted when the file is created. (Typically, created files allow others to read.) https://man7.org/linux/man-pages/man2/umask.2.html

int fd = mkstemp(&path[0]);
umask(prev_umask);
if (fd < 0) {
perror("Could not create temporary file");
return false;
Expand Down
12 changes: 9 additions & 3 deletions crypto/test/file_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,15 @@ class ScopedFD {
explicit ScopedFD(int fd) : fd_(fd) {}
~ScopedFD() { reset(); }

ScopedFD(ScopedFD &&other) { *this = std::move(other); }
ScopedFD &operator=(ScopedFD other) {
ScopedFD(ScopedFD &&other) noexcept { *this = std::move(other); }
ScopedFD &operator=(ScopedFD&& other) {
reset(other.release());
return *this;
}

ScopedFD(const ScopedFD &other) = delete;
ScopedFD &operator=(ScopedFD& other) = delete;

bool is_valid() const { return fd_ >= 0; }
int get() const { return fd_; }

Expand Down Expand Up @@ -85,7 +88,7 @@ class TemporaryFile {
TemporaryFile() = default;
~TemporaryFile();

TemporaryFile(TemporaryFile &other) { *this = std::move(other); }
TemporaryFile(TemporaryFile&& other) noexcept { *this = std::move(other); }
TemporaryFile& operator=(TemporaryFile&&other) {
// Ensure |path_| is empty so it doesn't try to delete the File.
auto old_other_path = other.path_;
Expand All @@ -94,6 +97,9 @@ class TemporaryFile {
return *this;
}

TemporaryFile(const TemporaryFile&) = delete;
TemporaryFile& operator=(const TemporaryFile&) = delete;

// Init initializes the temporary file with the specified content. It returns
// true on success and false on error. On error, callers should call
// |IgnoreTempFileErrors| to determine whether to ignore the error.
Expand Down
Loading