From 03f89d3d9540c7f5fbc609d5bbcd99e444a06503 Mon Sep 17 00:00:00 2001 From: Paul Crowley Date: Fri, 16 Jun 2017 09:37:31 -0700 Subject: [PATCH] Move functions useful for crypto test into their own file More refactoring in advance of work on bug. Bug: 36029169 Test: compiles. Change-Id: Ic4cdd4761e4c2b11a3ddca5c3bbc4d5e42fac9d4 --- Android.mk | 5 ++- FileDeviceUtils.cpp | 115 ++++++++++++++++++++++++++++++++++++++++++++++++++++ FileDeviceUtils.h | 35 ++++++++++++++++ secdiscard.cpp | 78 ++--------------------------------- 4 files changed, 158 insertions(+), 75 deletions(-) create mode 100644 FileDeviceUtils.cpp create mode 100644 FileDeviceUtils.h diff --git a/Android.mk b/Android.mk index e92955f..4971ec7 100644 --- a/Android.mk +++ b/Android.mk @@ -158,7 +158,10 @@ LOCAL_CLANG := true LOCAL_TIDY := true LOCAL_TIDY_FLAGS := $(common_local_tidy_flags) LOCAL_TIDY_CHECKS := $(common_local_tidy_checks) -LOCAL_SRC_FILES:= secdiscard.cpp +LOCAL_SRC_FILES:= \ + FileDeviceUtils.cpp \ + secdiscard.cpp \ + LOCAL_MODULE:= secdiscard LOCAL_SHARED_LIBRARIES := libbase LOCAL_CFLAGS := $(vold_cflags) diff --git a/FileDeviceUtils.cpp b/FileDeviceUtils.cpp new file mode 100644 index 0000000..bc9f4bd --- /dev/null +++ b/FileDeviceUtils.cpp @@ -0,0 +1,115 @@ +/* + * Copyright (C) 2017 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 "FileDeviceUtils.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +namespace { + +std::unique_ptr alloc_fiemap(uint32_t extent_count); + +} + +namespace android { +namespace vold { + +// Given a file path, look for the corresponding block device in /proc/mount +std::string BlockDeviceForPath(const std::string &path) +{ + std::unique_ptr mnts(setmntent("/proc/mounts", "re"), endmntent); + if (!mnts) { + PLOG(ERROR) << "Unable to open /proc/mounts"; + return ""; + } + std::string result; + size_t best_length = 0; + struct mntent *mnt; // getmntent returns a thread local, so it's safe. + while ((mnt = getmntent(mnts.get())) != nullptr) { + auto l = strlen(mnt->mnt_dir); + if (l > best_length && + path.size() > l && + path[l] == '/' && + path.compare(0, l, mnt->mnt_dir) == 0) { + result = mnt->mnt_fsname; + best_length = l; + } + } + if (result.empty()) { + LOG(ERROR) <<"Didn't find a mountpoint to match path " << path; + return ""; + } + LOG(DEBUG) << "For path " << path << " block device is " << result; + return result; +} + +std::unique_ptr PathFiemap(const std::string &path, uint32_t extent_count) +{ + 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 { + PLOG(ERROR) << "Unable to open " << path; + } + return nullptr; + } + auto fiemap = alloc_fiemap(extent_count); + if (ioctl(fd.get(), FS_IOC_FIEMAP, fiemap.get()) != 0) { + PLOG(ERROR) << "Unable to FIEMAP " << path; + return nullptr; + } + auto mapped = fiemap->fm_mapped_extents; + if (mapped < 1 || mapped > extent_count) { + LOG(ERROR) << "Extent count not in bounds 1 <= " << mapped << " <= " << extent_count + << " in " << path; + return nullptr; + } + return fiemap; +} + +} // namespace vold +} // namespace android + +namespace { + +std::unique_ptr alloc_fiemap(uint32_t extent_count) +{ + size_t allocsize = offsetof(struct fiemap, fm_extents[extent_count]); + std::unique_ptr res(new (::operator new (allocsize)) struct fiemap); + memset(res.get(), 0, allocsize); + res->fm_start = 0; + res->fm_length = UINT64_MAX; + res->fm_flags = 0; + res->fm_extent_count = extent_count; + res->fm_mapped_extents = 0; + return res; +} + +} diff --git a/FileDeviceUtils.h b/FileDeviceUtils.h new file mode 100644 index 0000000..4c1d49a --- /dev/null +++ b/FileDeviceUtils.h @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2017 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. + */ + +#ifndef ANDROID_VOLD_FILEDEVICEUTILS_H +#define ANDROID_VOLD_FILEDEVICEUTILS_H + +#include +#include + +namespace android { +namespace vold { + +// Given a file path, look for the corresponding block device in /proc/mount +std::string BlockDeviceForPath(const std::string &path); + +// Read the file's FIEMAP +std::unique_ptr PathFiemap(const std::string &path, uint32_t extent_count); + +} // namespace vold +} // namespace android + +#endif diff --git a/secdiscard.cpp b/secdiscard.cpp index a335ab6..f9532ea 100644 --- a/secdiscard.cpp +++ b/secdiscard.cpp @@ -31,6 +31,8 @@ #include #include +#include "FileDeviceUtils.h" + namespace { struct Options { @@ -43,10 +45,7 @@ constexpr uint32_t max_extents = 32; bool read_command_line(int argc, const char * const argv[], Options &options); void usage(const char *progname); bool secdiscard_path(const std::string &path); -std::unique_ptr path_fiemap(const std::string &path, uint32_t extent_count); bool check_fiemap(const struct fiemap &fiemap, const std::string &path); -std::unique_ptr alloc_fiemap(uint32_t extent_count); -std::string block_device_for_path(const std::string &path); bool overwrite_with_zeros(int fd, off64_t start, off64_t length); } @@ -98,11 +97,11 @@ void usage(const char *progname) { // BLKSECDISCARD all content in "path", if it's small enough. bool secdiscard_path(const std::string &path) { - auto fiemap = path_fiemap(path, max_extents); + auto fiemap = android::vold::PathFiemap(path, max_extents); if (!fiemap || !check_fiemap(*fiemap, path)) { return false; } - auto block_device = block_device_for_path(path); + auto block_device = android::vold::BlockDeviceForPath(path); if (block_device.empty()) { return false; } @@ -125,33 +124,6 @@ bool secdiscard_path(const std::string &path) { return true; } -// Read the file's FIEMAP -std::unique_ptr path_fiemap(const std::string &path, uint32_t extent_count) -{ - 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 { - PLOG(ERROR) << "Unable to open " << path; - } - return nullptr; - } - auto fiemap = alloc_fiemap(extent_count); - if (ioctl(fd.get(), FS_IOC_FIEMAP, fiemap.get()) != 0) { - PLOG(ERROR) << "Unable to FIEMAP " << path; - return nullptr; - } - auto mapped = fiemap->fm_mapped_extents; - if (mapped < 1 || mapped > extent_count) { - LOG(ERROR) << "Extent count not in bounds 1 <= " << mapped << " <= " << extent_count - << " in " << path; - return nullptr; - } - return fiemap; -} - // Ensure that the FIEMAP covers the file and is OK to discard bool check_fiemap(const struct fiemap &fiemap, const std::string &path) { auto mapped = fiemap.fm_mapped_extents; @@ -169,48 +141,6 @@ bool check_fiemap(const struct fiemap &fiemap, const std::string &path) { return true; } -std::unique_ptr alloc_fiemap(uint32_t extent_count) -{ - size_t allocsize = offsetof(struct fiemap, fm_extents[extent_count]); - std::unique_ptr res(new (::operator new (allocsize)) struct fiemap); - memset(res.get(), 0, allocsize); - res->fm_start = 0; - res->fm_length = UINT64_MAX; - res->fm_flags = 0; - res->fm_extent_count = extent_count; - res->fm_mapped_extents = 0; - return res; -} - -// Given a file path, look for the corresponding block device in /proc/mount -std::string block_device_for_path(const std::string &path) -{ - std::unique_ptr mnts(setmntent("/proc/mounts", "re"), endmntent); - if (!mnts) { - PLOG(ERROR) << "Unable to open /proc/mounts"; - return ""; - } - std::string result; - size_t best_length = 0; - struct mntent *mnt; // getmntent returns a thread local, so it's safe. - while ((mnt = getmntent(mnts.get())) != nullptr) { - auto l = strlen(mnt->mnt_dir); - if (l > best_length && - path.size() > l && - path[l] == '/' && - path.compare(0, l, mnt->mnt_dir) == 0) { - result = mnt->mnt_fsname; - best_length = l; - } - } - if (result.empty()) { - LOG(ERROR) <<"Didn't find a mountpoint to match path " << path; - return ""; - } - LOG(DEBUG) << "For path " << path << " block device is " << result; - return result; -} - bool overwrite_with_zeros(int fd, off64_t start, off64_t length) { if (lseek64(fd, start, SEEK_SET) != start) { PLOG(ERROR) << "Seek failed for zero overwrite"; -- 2.11.0