commit
56b0e8c1df
@ -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;
|
||||
};
|
||||
|
@ -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 <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <linux/fs.h>
|
||||
#include <linux/fiemap.h>
|
||||
#include <mntent.h>
|
||||
|
||||
#include <android-base/file.h>
|
||||
#include <android-base/logging.h>
|
||||
#include <android-base/unique_fd.h>
|
||||
|
||||
namespace {
|
||||
|
||||
std::unique_ptr<struct fiemap> 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<FILE, int(*)(FILE*)> 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<struct fiemap> 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<struct fiemap> alloc_fiemap(uint32_t extent_count)
|
||||
{
|
||||
size_t allocsize = offsetof(struct fiemap, fm_extents[extent_count]);
|
||||
std::unique_ptr<struct fiemap> 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;
|
||||
}
|
||||
|
||||
}
|
@ -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 <string>
|
||||
#include <linux/fiemap.h>
|
||||
|
||||
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<struct fiemap> PathFiemap(const std::string &path, uint32_t extent_count);
|
||||
|
||||
} // namespace vold
|
||||
} // namespace android
|
||||
|
||||
#endif
|
@ -0,0 +1,37 @@
|
||||
/*
|
||||
* 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 "KeyBuffer.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
|
||||
namespace android {
|
||||
namespace vold {
|
||||
|
||||
KeyBuffer operator+(KeyBuffer&& lhs, const KeyBuffer& rhs) {
|
||||
std::copy(rhs.begin(), rhs.end(), std::back_inserter(lhs));
|
||||
return std::move(lhs);
|
||||
}
|
||||
|
||||
KeyBuffer operator+(KeyBuffer&& lhs, const char* rhs) {
|
||||
std::copy(rhs, rhs + strlen(rhs), std::back_inserter(lhs));
|
||||
return std::move(lhs);
|
||||
}
|
||||
|
||||
} // namespace vold
|
||||
} // namespace android
|
||||
|
@ -0,0 +1,63 @@
|
||||
/*
|
||||
* 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_KEYBUFFER_H
|
||||
#define ANDROID_VOLD_KEYBUFFER_H
|
||||
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
namespace android {
|
||||
namespace vold {
|
||||
|
||||
/**
|
||||
* Variant of memset() that should never be optimized away. Borrowed from keymaster code.
|
||||
*/
|
||||
#ifdef __clang__
|
||||
#define OPTNONE __attribute__((optnone))
|
||||
#else // not __clang__
|
||||
#define OPTNONE __attribute__((optimize("O0")))
|
||||
#endif // not __clang__
|
||||
inline OPTNONE void* memset_s(void* s, int c, size_t n) {
|
||||
if (!s)
|
||||
return s;
|
||||
return memset(s, c, n);
|
||||
}
|
||||
#undef OPTNONE
|
||||
|
||||
// Allocator that delegates useful work to standard one but zeroes data before deallocating.
|
||||
class ZeroingAllocator : public std::allocator<char> {
|
||||
public:
|
||||
void deallocate(pointer p, size_type n)
|
||||
{
|
||||
memset_s(p, 0, n);
|
||||
std::allocator<char>::deallocate(p, n);
|
||||
}
|
||||
};
|
||||
|
||||
// Char vector that zeroes memory when deallocating.
|
||||
using KeyBuffer = std::vector<char, ZeroingAllocator>;
|
||||
|
||||
// Convenience methods to concatenate key buffers.
|
||||
KeyBuffer operator+(KeyBuffer&& lhs, const KeyBuffer& rhs);
|
||||
KeyBuffer operator+(KeyBuffer&& lhs, const char* rhs);
|
||||
|
||||
} // namespace vold
|
||||
} // namespace android
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in new issue