tbarzic | 340a4aa | 2011-06-09 20:27:58 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Ben Chan | 617dbb8 | 2014-06-17 22:46:34 -0700 | [diff] [blame] | 5 | #include "image-burner/image_burner_utils.h" |
tbarzic | 340a4aa | 2011-06-09 20:27:58 -0700 | [diff] [blame] | 6 | |
Ben Chan | 58869eb | 2017-03-20 12:40:33 -0700 | [diff] [blame] | 7 | #include <memory> |
| 8 | |
Toni Barzic | aced013 | 2020-10-19 20:06:27 -0700 | [diff] [blame] | 9 | #include <base/bind.h> |
Toni Barzic | 9345c10 | 2016-09-09 17:36:01 -0700 | [diff] [blame] | 10 | #include <base/files/file_path.h> |
tbarzic | 340a4aa | 2011-06-09 20:27:58 -0700 | [diff] [blame] | 11 | #include <base/logging.h> |
Ben Chan | 58869eb | 2017-03-20 12:40:33 -0700 | [diff] [blame] | 12 | #include <base/memory/free_deleter.h> |
Ben Chan | dbace9f | 2017-04-04 15:30:44 -0700 | [diff] [blame] | 13 | #include <base/strings/stringprintf.h> |
Ben Chan | 617dbb8 | 2014-06-17 22:46:34 -0700 | [diff] [blame] | 14 | #include <rootdev/rootdev.h> |
tbarzic | 340a4aa | 2011-06-09 20:27:58 -0700 | [diff] [blame] | 15 | |
| 16 | namespace imageburn { |
Ben Chan | dbace9f | 2017-04-04 15:30:44 -0700 | [diff] [blame] | 17 | namespace { |
tbarzic | 340a4aa | 2011-06-09 20:27:58 -0700 | [diff] [blame] | 18 | |
tbarzic | 340a4aa | 2011-06-09 20:27:58 -0700 | [diff] [blame] | 19 | const int kFsyncRatio = 1024; |
| 20 | |
Ben Chan | dbace9f | 2017-04-04 15:30:44 -0700 | [diff] [blame] | 21 | bool RealPath(const char* path, std::string* real_path) { |
| 22 | std::unique_ptr<char, base::FreeDeleter> result(realpath(path, nullptr)); |
| 23 | if (!result) { |
| 24 | PLOG(ERROR) << "Couldn't get real path of " << path; |
| 25 | return false; |
| 26 | } |
| 27 | *real_path = result.get(); |
| 28 | return true; |
| 29 | } |
| 30 | |
| 31 | } // namespace |
| 32 | |
Toni Barzic | aced013 | 2020-10-19 20:06:27 -0700 | [diff] [blame] | 33 | BurnWriter::BurnWriter() |
| 34 | : fstat_callback_(base::BindRepeating(&base::File::Fstat)) {} |
tbarzic | 340a4aa | 2011-06-09 20:27:58 -0700 | [diff] [blame] | 35 | |
| 36 | bool BurnWriter::Open(const char* path) { |
Toni Barzic | 9345c10 | 2016-09-09 17:36:01 -0700 | [diff] [blame] | 37 | if (file_.IsValid()) |
| 38 | return false; |
| 39 | |
| 40 | file_.Initialize(base::FilePath(path), |
| 41 | base::File::FLAG_WRITE | base::File::FLAG_OPEN); |
| 42 | if (!file_.IsValid()) { |
Ben Chan | 617dbb8 | 2014-06-17 22:46:34 -0700 | [diff] [blame] | 43 | PLOG(ERROR) << "Couldn't open target path " << path; |
tbarzic | 340a4aa | 2011-06-09 20:27:58 -0700 | [diff] [blame] | 44 | return false; |
tbarzic | 340a4aa | 2011-06-09 20:27:58 -0700 | [diff] [blame] | 45 | } |
Toni Barzic | aced013 | 2020-10-19 20:06:27 -0700 | [diff] [blame] | 46 | |
| 47 | base::stat_wrapper_t st = {}; |
| 48 | if (fstat_callback_.Run(file_.GetPlatformFile(), &st) != 0) { |
| 49 | PLOG(ERROR) << "Unable to stat file for path " << path; |
| 50 | Close(); |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | if (!S_ISBLK(st.st_mode)) { |
| 55 | PLOG(ERROR) << "Attempt to write to non-block device " << path; |
| 56 | Close(); |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | LOG(INFO) << path << " opened"; |
| 61 | return true; |
tbarzic | 340a4aa | 2011-06-09 20:27:58 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | bool BurnWriter::Close() { |
Toni Barzic | 9345c10 | 2016-09-09 17:36:01 -0700 | [diff] [blame] | 65 | if (!file_.IsValid()) |
| 66 | return false; |
| 67 | file_.Close(); |
tbarzic | 340a4aa | 2011-06-09 20:27:58 -0700 | [diff] [blame] | 68 | return true; |
| 69 | } |
| 70 | |
Toni Barzic | 9345c10 | 2016-09-09 17:36:01 -0700 | [diff] [blame] | 71 | int BurnWriter::Write(const char* data_block, int data_size) { |
| 72 | const int written = file_.WriteAtCurrentPos(data_block, data_size); |
| 73 | if (written != data_size) { |
Ben Chan | 617dbb8 | 2014-06-17 22:46:34 -0700 | [diff] [blame] | 74 | PLOG(ERROR) << "Error writing to target file"; |
tbarzic | 340a4aa | 2011-06-09 20:27:58 -0700 | [diff] [blame] | 75 | return written; |
| 76 | } |
| 77 | |
Toni Barzic | 9345c10 | 2016-09-09 17:36:01 -0700 | [diff] [blame] | 78 | if (!writes_count_ && !file_.Flush()) { |
| 79 | PLOG(ERROR) << "Error flushing target file."; |
tbarzic | 340a4aa | 2011-06-09 20:27:58 -0700 | [diff] [blame] | 80 | return -1; |
| 81 | } |
Toni Barzic | 9345c10 | 2016-09-09 17:36:01 -0700 | [diff] [blame] | 82 | |
tbarzic | 340a4aa | 2011-06-09 20:27:58 -0700 | [diff] [blame] | 83 | writes_count_++; |
| 84 | if (writes_count_ == kFsyncRatio) |
| 85 | writes_count_ = 0; |
| 86 | |
| 87 | return written; |
| 88 | } |
| 89 | |
Toni Barzic | 9345c10 | 2016-09-09 17:36:01 -0700 | [diff] [blame] | 90 | BurnReader::BurnReader() {} |
tbarzic | 340a4aa | 2011-06-09 20:27:58 -0700 | [diff] [blame] | 91 | |
| 92 | bool BurnReader::Open(const char* path) { |
Toni Barzic | 9345c10 | 2016-09-09 17:36:01 -0700 | [diff] [blame] | 93 | if (file_.IsValid()) |
| 94 | return false; |
| 95 | |
| 96 | file_.Initialize(base::FilePath(path), |
| 97 | base::File::FLAG_READ | base::File::FLAG_OPEN); |
| 98 | if (!file_.IsValid()) { |
Ben Chan | 617dbb8 | 2014-06-17 22:46:34 -0700 | [diff] [blame] | 99 | PLOG(ERROR) << "Couldn't open source path " << path; |
tbarzic | 340a4aa | 2011-06-09 20:27:58 -0700 | [diff] [blame] | 100 | return false; |
tbarzic | 340a4aa | 2011-06-09 20:27:58 -0700 | [diff] [blame] | 101 | } |
Ben Chan | dbace9f | 2017-04-04 15:30:44 -0700 | [diff] [blame] | 102 | |
| 103 | // Obtains the real path of the file associated with the opened file |
| 104 | // descriptor by resolving the /proc/self/fd/<descriptor> symlink. Compares |
| 105 | // |path| against the real path determined by /proc/self/fd/<descriptor> to |
| 106 | // make sure |path| specifies the real path and avoids a potential TOCTOU |
| 107 | // race between the underlying realpath() and open() call. |
| 108 | int fd = file_.GetPlatformFile(); |
| 109 | std::string fd_path; |
| 110 | if (!RealPath(base::StringPrintf("/proc/self/fd/%d", fd).c_str(), &fd_path)) { |
| 111 | file_.Close(); |
| 112 | return false; |
| 113 | } |
| 114 | if (fd_path != path) { |
| 115 | LOG(ERROR) << path << " isn't a fully resolved path"; |
| 116 | file_.Close(); |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | LOG(INFO) << path << " opened"; |
| 121 | return true; |
tbarzic | 340a4aa | 2011-06-09 20:27:58 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | bool BurnReader::Close() { |
Toni Barzic | 9345c10 | 2016-09-09 17:36:01 -0700 | [diff] [blame] | 125 | if (!file_.IsValid()) |
| 126 | return false; |
| 127 | file_.Close(); |
tbarzic | 340a4aa | 2011-06-09 20:27:58 -0700 | [diff] [blame] | 128 | return true; |
| 129 | } |
| 130 | |
| 131 | int BurnReader::Read(char* data_block, int data_size) { |
Toni Barzic | 9345c10 | 2016-09-09 17:36:01 -0700 | [diff] [blame] | 132 | const int read = file_.ReadAtCurrentPos(data_block, data_size); |
tbarzic | 340a4aa | 2011-06-09 20:27:58 -0700 | [diff] [blame] | 133 | if (read < 0) |
Ben Chan | 617dbb8 | 2014-06-17 22:46:34 -0700 | [diff] [blame] | 134 | PLOG(ERROR) << "Error reading from source file"; |
tbarzic | 340a4aa | 2011-06-09 20:27:58 -0700 | [diff] [blame] | 135 | return read; |
| 136 | } |
| 137 | |
Ben Chan | 26c33e7 | 2014-08-07 00:30:44 -0700 | [diff] [blame] | 138 | int64_t BurnReader::GetSize() { |
Toni Barzic | 9345c10 | 2016-09-09 17:36:01 -0700 | [diff] [blame] | 139 | if (!file_.IsValid()) |
| 140 | return -1; |
| 141 | return file_.GetLength(); |
tbarzic | 340a4aa | 2011-06-09 20:27:58 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Ben Chan | 58869eb | 2017-03-20 12:40:33 -0700 | [diff] [blame] | 144 | bool BurnPathGetter::GetRealPath(const char* path, std::string* real_path) { |
Ben Chan | dbace9f | 2017-04-04 15:30:44 -0700 | [diff] [blame] | 145 | return RealPath(path, real_path); |
Ben Chan | 58869eb | 2017-03-20 12:40:33 -0700 | [diff] [blame] | 146 | } |
Toni Barzic | 9345c10 | 2016-09-09 17:36:01 -0700 | [diff] [blame] | 147 | |
Ben Chan | 58869eb | 2017-03-20 12:40:33 -0700 | [diff] [blame] | 148 | bool BurnPathGetter::GetRootPath(std::string* path) { |
tbarzic | 340a4aa | 2011-06-09 20:27:58 -0700 | [diff] [blame] | 149 | char root_path[PATH_MAX]; |
| 150 | if (rootdev(root_path, sizeof(root_path), true, true)) { |
| 151 | // Coult not get root path. |
| 152 | return false; |
| 153 | } |
| 154 | *path = root_path; |
| 155 | return true; |
| 156 | } |
| 157 | |
Ben Chan | 617dbb8 | 2014-06-17 22:46:34 -0700 | [diff] [blame] | 158 | } // namespace imageburn |