Merge "Abolish AutoCloseFD.h in favour of unique_fd"

gugelfrei
Paul Crowley 7 years ago committed by Android (Google) Code Review
commit 547c045adc

@ -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 <string>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <android-base/logging.h>
// 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;
};

@ -29,10 +29,10 @@
#include <linux/dm-ioctl.h>
#include <android-base/logging.h>
#include <android-base/unique_fd.h>
#include <cutils/properties.h>
#include <fs_mgr.h>
#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;
}

@ -29,8 +29,7 @@
#include <mntent.h>
#include <android-base/logging.h>
#include <AutoCloseFD.h>
#include <android-base/unique_fd.h>
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<struct fiemap> 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 {

Loading…
Cancel
Save