diff --git a/AutoCloseFD.h b/AutoCloseFD.h deleted file mode 100644 index 9b68469..0000000 --- a/AutoCloseFD.h +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (C) 2015 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include - -#include -#include -#include -#include - -#include - -// File descriptor which is automatically closed when this object is destroyed. -// Cannot be copied, since that would cause double-closes. -class AutoCloseFD { -public: - AutoCloseFD(const char *path, int flags = O_RDONLY, int mode = 0): - fd{TEMP_FAILURE_RETRY(open(path, flags | O_CLOEXEC, mode))} {} - AutoCloseFD(const std::string &path, int flags = O_RDONLY, int mode = 0): - AutoCloseFD(path.c_str(), flags, mode) {} - ~AutoCloseFD() { - if (fd != -1) { - int preserve_errno = errno; - if (close(fd) == -1) { - PLOG(ERROR) << "close(2) failed"; - }; - errno = preserve_errno; - } - } - AutoCloseFD(const AutoCloseFD&) = delete; - AutoCloseFD& operator=(const AutoCloseFD&) = delete; - explicit operator bool() {return fd != -1;} - int get() const {return fd;} -private: - const int fd; -}; - diff --git a/MetadataCrypt.cpp b/MetadataCrypt.cpp index b707549..743e08c 100644 --- a/MetadataCrypt.cpp +++ b/MetadataCrypt.cpp @@ -29,10 +29,10 @@ #include #include +#include #include #include -#include "AutoCloseFD.h" #include "EncryptInplace.h" #include "KeyStorage.h" #include "KeyUtil.h" @@ -105,8 +105,9 @@ static std::string default_key_params(const std::string& real_blkdev, const std: } static bool get_number_of_sectors(const std::string& real_blkdev, uint64_t *nr_sec) { - AutoCloseFD dev_fd(real_blkdev, O_RDONLY); - if (!dev_fd) { + android::base::unique_fd dev_fd(TEMP_FAILURE_RETRY(open( + real_blkdev.c_str(), O_RDONLY | O_CLOEXEC, 0))); + if (dev_fd == -1) { PLOG(ERROR) << "Unable to open " << real_blkdev << " to measure size"; return false; } @@ -143,8 +144,9 @@ static struct dm_ioctl* dm_ioctl_init(char *buffer, size_t buffer_size, static bool create_crypto_blk_dev(const std::string& dm_name, uint64_t nr_sec, const std::string& target_type, const std::string& crypt_params, std::string* crypto_blkdev) { - AutoCloseFD dm_fd("/dev/device-mapper", O_RDWR); - if (!dm_fd) { + android::base::unique_fd dm_fd(TEMP_FAILURE_RETRY(open( + "/dev/device-mapper", O_RDWR | O_CLOEXEC, 0))); + if (dm_fd == -1) { PLOG(ERROR) << "Cannot open device-mapper"; return false; } diff --git a/secdiscard.cpp b/secdiscard.cpp index fe51990..a335ab6 100644 --- a/secdiscard.cpp +++ b/secdiscard.cpp @@ -29,8 +29,7 @@ #include #include - -#include +#include namespace { @@ -107,8 +106,9 @@ bool secdiscard_path(const std::string &path) { if (block_device.empty()) { return false; } - AutoCloseFD fs_fd(block_device, O_RDWR | O_LARGEFILE); - if (!fs_fd) { + android::base::unique_fd fs_fd(TEMP_FAILURE_RETRY(open( + block_device.c_str(), O_RDWR | O_LARGEFILE | O_CLOEXEC, 0))); + if (fs_fd == -1) { PLOG(ERROR) << "Failed to open device " << block_device; return false; } @@ -128,8 +128,9 @@ bool secdiscard_path(const std::string &path) { // Read the file's FIEMAP std::unique_ptr path_fiemap(const std::string &path, uint32_t extent_count) { - AutoCloseFD fd(path); - if (!fd) { + android::base::unique_fd fd(TEMP_FAILURE_RETRY(open( + path.c_str(), O_RDONLY | O_CLOEXEC, 0))); + if (fd == -1) { if (errno == ENOENT) { PLOG(DEBUG) << "Unable to open " << path; } else {