123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- #include <nvram/core/storage.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <android-base/file.h>
- #include <android-base/logging.h>
- #include <android-base/unique_fd.h>
- #include <nvram/core/logger.h>
- namespace {
- const char kHeaderFileName[] = "header";
- const char kSpaceDataFileNamePattern[] = "space_%08x";
- const char kTempFileName[] = "temp";
- const off_t kMaxFileSize = 2048;
- using NameBuffer = char[16];
- int g_data_dir_fd = -1;
- bool FormatSpaceFileName(NameBuffer name, uint32_t index) {
- int ret =
- snprintf(name, sizeof(NameBuffer), kSpaceDataFileNamePattern, index);
- return ret >= 0 && ret < static_cast<int>(sizeof(NameBuffer));
- };
- nvram::storage::Status DeleteFile(const char* name) {
- if (TEMP_FAILURE_RETRY(unlinkat(g_data_dir_fd, name, 0))) {
- if (errno == ENOENT) {
- return nvram::storage::Status::kNotFound;
- }
- PLOG(ERROR) << "Failed to remove " << name;
- return nvram::storage::Status::kStorageError;
- }
- return nvram::storage::Status::kSuccess;
- }
- nvram::storage::Status LoadFile(const char* name, nvram::Blob* blob) {
- android::base::unique_fd data_file_fd(
- TEMP_FAILURE_RETRY(openat(g_data_dir_fd, name, O_RDONLY)));
- if (data_file_fd.get() < 0) {
- if (errno == ENOENT) {
- return nvram::storage::Status::kNotFound;
- }
- PLOG(ERROR) << "Failed to open " << name;
- return nvram::storage::Status::kStorageError;
- }
- struct stat data_file_stat;
- if (TEMP_FAILURE_RETRY(fstat(data_file_fd.get(), &data_file_stat))) {
- PLOG(ERROR) << "Failed to stat " << name;
- return nvram::storage::Status::kStorageError;
- }
- if (data_file_stat.st_size > kMaxFileSize) {
- LOG(ERROR) << "Bad size for " << name << ":" << data_file_stat.st_size;
- return nvram::storage::Status::kStorageError;
- }
- if (!blob->Resize(data_file_stat.st_size)) {
- LOG(ERROR) << "Failed to allocate read buffer for " << name;
- return nvram::storage::Status::kStorageError;
- }
- if (!android::base::ReadFully(data_file_fd.get(), blob->data(),
- blob->size())) {
- PLOG(ERROR) << "Failed to read " << name;
- return nvram::storage::Status::kStorageError;
- }
- return nvram::storage::Status::kSuccess;
- }
- nvram::storage::Status StoreFile(const char* name, const nvram::Blob& blob) {
- android::base::unique_fd data_file_fd(TEMP_FAILURE_RETRY(
- openat(g_data_dir_fd, kTempFileName, O_WRONLY | O_CREAT | O_TRUNC,
- S_IRUSR | S_IWUSR)));
- if (data_file_fd.get() < 0) {
- if (errno == ENOENT) {
- return nvram::storage::Status::kNotFound;
- }
- PLOG(ERROR) << "Failed to open " << kTempFileName;
- return nvram::storage::Status::kStorageError;
- }
- if (!android::base::WriteFully(data_file_fd.get(), blob.data(),
- blob.size())) {
- PLOG(ERROR) << "Failed to write " << kTempFileName;
- DeleteFile(kTempFileName);
- return nvram::storage::Status::kStorageError;
- }
-
- if (TEMP_FAILURE_RETRY(fdatasync(data_file_fd.get()))) {
- PLOG(ERROR) << "Failed to sync " << kTempFileName;
- DeleteFile(kTempFileName);
- return nvram::storage::Status::kStorageError;
- }
- data_file_fd.reset();
-
- if (TEMP_FAILURE_RETRY(
- renameat(g_data_dir_fd, kTempFileName, g_data_dir_fd, name))) {
- PLOG(ERROR) << "Failed to move " << kTempFileName << " to " << name;
- DeleteFile(kTempFileName);
- return nvram::storage::Status::kStorageError;
- }
-
- if (TEMP_FAILURE_RETRY(fsync(g_data_dir_fd))) {
- PLOG(ERROR) << "Failed to sync data directory";
- return nvram::storage::Status::kStorageError;
- }
- return nvram::storage::Status::kSuccess;
- }
- }
- void InitStorage(int data_dir_fd) {
- g_data_dir_fd = data_dir_fd;
- }
- namespace nvram {
- namespace storage {
- Status LoadHeader(Blob* blob) {
- return LoadFile(kHeaderFileName, blob);
- }
- Status StoreHeader(const Blob& blob) {
- return StoreFile(kHeaderFileName, blob);
- }
- Status LoadSpace(uint32_t index, Blob* blob) {
- NameBuffer name;
- if (!FormatSpaceFileName(name, index)) {
- return Status::kStorageError;
- }
- return LoadFile(name, blob);
- }
- Status StoreSpace(uint32_t index, const Blob& blob) {
- NameBuffer name;
- if (!FormatSpaceFileName(name, index)) {
- return Status::kStorageError;
- }
- return StoreFile(name, blob);
- }
- Status DeleteSpace(uint32_t index) {
- NameBuffer name;
- if (!FormatSpaceFileName(name, index)) {
- return Status::kStorageError;
- }
- return DeleteFile(name);
- }
- }
- }
|