Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_UTILS_H__ |
| 6 | #define CHROMEOS_PLATFORM_UPDATE_ENGINE_UTILS_H__ |
| 7 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 8 | #include <errno.h> |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 9 | |
Andrew de los Reyes | 81ebcd8 | 2010-03-09 15:56:18 -0800 | [diff] [blame] | 10 | #include <algorithm> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 11 | #include <set> |
| 12 | #include <string> |
| 13 | #include <vector> |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 14 | |
Thieu Le | 5c7d975 | 2010-12-15 16:09:28 -0800 | [diff] [blame] | 15 | #include <ext2fs/ext2fs.h> |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 16 | #include <glib.h> |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 17 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 18 | #include "update_engine/action.h" |
| 19 | #include "update_engine/action_processor.h" |
| 20 | |
| 21 | namespace chromeos_update_engine { |
| 22 | |
| 23 | namespace utils { |
| 24 | |
Darin Petkov | a07586b | 2010-10-20 13:41:15 -0700 | [diff] [blame] | 25 | // Returns true if this is an official Chrome OS build, false otherwise. |
Darin Petkov | 33d3064 | 2010-08-04 10:18:57 -0700 | [diff] [blame] | 26 | bool IsOfficialBuild(); |
| 27 | |
Darin Petkov | 2a0e633 | 2010-09-24 14:43:41 -0700 | [diff] [blame] | 28 | // Returns true if the OOBE process has been completed and EULA accepted, false |
| 29 | // otherwise. |
| 30 | bool IsOOBEComplete(); |
| 31 | |
Darin Petkov | c91dd6b | 2011-01-10 12:31:34 -0800 | [diff] [blame^] | 32 | // Returns true if the boot mode is normal, false otherwise (e.g., developer or |
| 33 | // recovery). |
| 34 | bool IsNormalBootMode(); |
| 35 | |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 36 | // Writes the data passed to path. The file at path will be overwritten if it |
| 37 | // exists. Returns true on success, false otherwise. |
| 38 | bool WriteFile(const char* path, const char* data, int data_len); |
| 39 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 40 | // Calls write() or pwrite() repeatedly until all count bytes at buf are |
| 41 | // written to fd or an error occurs. Returns true on success. |
| 42 | bool WriteAll(int fd, const void* buf, size_t count); |
| 43 | bool PWriteAll(int fd, const void* buf, size_t count, off_t offset); |
| 44 | |
| 45 | // Calls pread() repeatedly until count bytes are read, or EOF is reached. |
| 46 | // Returns number of bytes read in *bytes_read. Returns true on success. |
| 47 | bool PReadAll(int fd, void* buf, size_t count, off_t offset, |
| 48 | ssize_t* out_bytes_read); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 49 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 50 | // Returns the entire contents of the file at path. Returns true on success. |
| 51 | bool ReadFile(const std::string& path, std::vector<char>* out); |
| 52 | bool ReadFileToString(const std::string& path, std::string* out); |
| 53 | |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 54 | // Returns the size of the file at path. If the file doesn't exist or some |
| 55 | // error occurrs, -1 is returned. |
| 56 | off_t FileSize(const std::string& path); |
| 57 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 58 | std::string ErrnoNumberAsString(int err); |
| 59 | |
| 60 | // Strips duplicate slashes, and optionally removes all trailing slashes. |
| 61 | // Does not compact /./ or /../. |
| 62 | std::string NormalizePath(const std::string& path, bool strip_trailing_slash); |
| 63 | |
| 64 | // Returns true if the file exists for sure. Returns false if it doesn't exist, |
| 65 | // or an error occurs. |
| 66 | bool FileExists(const char* path); |
| 67 | |
Darin Petkov | 30291ed | 2010-11-12 10:23:06 -0800 | [diff] [blame] | 68 | // Returns true if |path| exists and is a symbolic link. |
| 69 | bool IsSymlink(const char* path); |
| 70 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 71 | // The last 6 chars of path must be XXXXXX. They will be randomly changed |
| 72 | // and a non-existent path will be returned. Intentionally makes a copy |
| 73 | // of the string passed in. |
| 74 | // NEVER CALL THIS FUNCTION UNLESS YOU ARE SURE |
| 75 | // THAT YOUR PROCESS WILL BE THE ONLY THING WRITING FILES IN THIS DIRECTORY. |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 76 | std::string TempFilename(std::string path); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 77 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 78 | // Calls mkstemp() with the template passed. Returns the filename in the |
| 79 | // out param filename. If fd is non-NULL, the file fd returned by mkstemp |
| 80 | // is not close()d and is returned in the out param 'fd'. However, if |
| 81 | // fd is NULL, the fd from mkstemp() will be closed. |
| 82 | // The last six chars of the template must be XXXXXX. |
| 83 | // Returns true on success. |
| 84 | bool MakeTempFile(const std::string& filename_template, |
| 85 | std::string* filename, |
| 86 | int* fd); |
| 87 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 88 | // Calls mkdtemp() with the tempate passed. Returns the generated dirname |
| 89 | // in the dirname param. Returns TRUE on success. dirname must not be NULL. |
| 90 | bool MakeTempDirectory(const std::string& dirname_template, |
| 91 | std::string* dirname); |
| 92 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 93 | // Deletes a directory and all its contents synchronously. Returns true |
| 94 | // on success. This may be called with a regular file--it will just unlink it. |
| 95 | // This WILL cross filesystem boundaries. |
| 96 | bool RecursiveUnlinkDir(const std::string& path); |
| 97 | |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 98 | // Returns the root device for a partition. For example, |
Darin Petkov | f74eb65 | 2010-08-04 12:08:38 -0700 | [diff] [blame] | 99 | // RootDevice("/dev/sda3") returns "/dev/sda". Returns an empty string |
| 100 | // if the input device is not of the "/dev/xyz" form. |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 101 | std::string RootDevice(const std::string& partition_device); |
| 102 | |
| 103 | // Returns the partition number, as a string, of partition_device. For example, |
Darin Petkov | f74eb65 | 2010-08-04 12:08:38 -0700 | [diff] [blame] | 104 | // PartitionNumber("/dev/sda3") returns "3". |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 105 | std::string PartitionNumber(const std::string& partition_device); |
| 106 | |
Darin Petkov | f74eb65 | 2010-08-04 12:08:38 -0700 | [diff] [blame] | 107 | // Returns the sysfs block device for a root block device. For |
| 108 | // example, SysfsBlockDevice("/dev/sda") returns |
| 109 | // "/sys/block/sda". Returns an empty string if the input device is |
| 110 | // not of the "/dev/xyz" form. |
| 111 | std::string SysfsBlockDevice(const std::string& device); |
| 112 | |
| 113 | // Returns true if the root |device| (e.g., "/dev/sdb") is known to be |
| 114 | // removable, false otherwise. |
| 115 | bool IsRemovableDevice(const std::string& device); |
| 116 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 117 | // Synchronously mount or unmount a filesystem. Return true on success. |
| 118 | // Mounts as ext3 with default options. |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 119 | bool MountFilesystem(const std::string& device, const std::string& mountpoint, |
| 120 | unsigned long flags); |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 121 | bool UnmountFilesystem(const std::string& mountpoint); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 122 | |
Darin Petkov | d3f8c89 | 2010-10-12 21:38:45 -0700 | [diff] [blame] | 123 | // Returns the block count and the block byte size of the ext3 file system on |
| 124 | // |device| (which may be a real device or a path to a filesystem image) or on |
| 125 | // an opened file descriptor |fd|. The actual file-system size is |block_count| |
| 126 | // * |block_size| bytes. Returns true on success, false otherwise. |
| 127 | bool GetFilesystemSize(const std::string& device, |
| 128 | int* out_block_count, |
| 129 | int* out_block_size); |
| 130 | bool GetFilesystemSizeFromFD(int fd, |
| 131 | int* out_block_count, |
| 132 | int* out_block_size); |
| 133 | |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 134 | enum BootLoader { |
| 135 | BootLoader_SYSLINUX = 0, |
| 136 | BootLoader_CHROME_FIRMWARE = 1 |
| 137 | }; |
| 138 | // Detects which bootloader this system uses and returns it via the out |
| 139 | // param. Returns true on success. |
| 140 | bool GetBootloader(BootLoader* out_bootloader); |
| 141 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 142 | // Returns the error message, if any, from a GError pointer. |
| 143 | const char* GetGErrorMessage(const GError* error); |
| 144 | |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 145 | // Initiates a system reboot. Returns true on success, false otherwise. |
| 146 | bool Reboot(); |
| 147 | |
Andrew de los Reyes | 712b3ac | 2011-01-07 13:47:52 -0800 | [diff] [blame] | 148 | // Schedules a Main Loop callback to trigger the crash reporter to perform an |
| 149 | // upload as if this process had crashed. |
| 150 | void ScheduleCrashReporterUpload(); |
| 151 | |
Darin Petkov | 5c0a8af | 2010-08-24 13:39:13 -0700 | [diff] [blame] | 152 | // Fuzzes an integer |value| randomly in the range: |
| 153 | // [value - range / 2, value + range - range / 2] |
| 154 | int FuzzInt(int value, unsigned int range); |
| 155 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 156 | // Log a string in hex to LOG(INFO). Useful for debugging. |
| 157 | void HexDumpArray(const unsigned char* const arr, const size_t length); |
| 158 | inline void HexDumpString(const std::string& str) { |
| 159 | HexDumpArray(reinterpret_cast<const unsigned char*>(str.data()), str.size()); |
| 160 | } |
| 161 | inline void HexDumpVector(const std::vector<char>& vect) { |
| 162 | HexDumpArray(reinterpret_cast<const unsigned char*>(&vect[0]), vect.size()); |
| 163 | } |
| 164 | |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 165 | extern const char* const kStatefulPartition; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 166 | |
| 167 | bool StringHasSuffix(const std::string& str, const std::string& suffix); |
| 168 | bool StringHasPrefix(const std::string& str, const std::string& prefix); |
| 169 | |
| 170 | template<typename KeyType, typename ValueType> |
| 171 | bool MapContainsKey(const std::map<KeyType, ValueType>& m, const KeyType& k) { |
| 172 | return m.find(k) != m.end(); |
| 173 | } |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 174 | template<typename KeyType> |
| 175 | bool SetContainsKey(const std::set<KeyType>& s, const KeyType& k) { |
| 176 | return s.find(k) != s.end(); |
| 177 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 178 | |
| 179 | template<typename ValueType> |
| 180 | std::set<ValueType> SetWithValue(const ValueType& value) { |
| 181 | std::set<ValueType> ret; |
| 182 | ret.insert(value); |
| 183 | return ret; |
| 184 | } |
| 185 | |
Andrew de los Reyes | 0ce161b | 2010-02-22 15:27:01 -0800 | [diff] [blame] | 186 | template<typename T> |
| 187 | bool VectorContainsValue(const std::vector<T>& vect, const T& value) { |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 188 | return std::find(vect.begin(), vect.end(), value) != vect.end(); |
Andrew de los Reyes | 0ce161b | 2010-02-22 15:27:01 -0800 | [diff] [blame] | 189 | } |
| 190 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 191 | template<typename T> |
| 192 | bool VectorIndexOf(const std::vector<T>& vect, const T& value, |
| 193 | typename std::vector<T>::size_type* out_index) { |
| 194 | typename std::vector<T>::const_iterator it = std::find(vect.begin(), |
| 195 | vect.end(), |
| 196 | value); |
| 197 | if (it == vect.end()) { |
| 198 | return false; |
| 199 | } else { |
| 200 | *out_index = it - vect.begin(); |
| 201 | return true; |
| 202 | } |
| 203 | } |
| 204 | |
Andrew de los Reyes | cc92cd3 | 2010-10-05 16:56:14 -0700 | [diff] [blame] | 205 | template<typename ValueType> |
| 206 | void ApplyMap(std::vector<ValueType>* collection, |
| 207 | const std::map<ValueType, ValueType>& the_map) { |
| 208 | for (typename std::vector<ValueType>::iterator it = collection->begin(); |
| 209 | it != collection->end(); ++it) { |
| 210 | typename std::map<ValueType, ValueType>::const_iterator map_it = |
| 211 | the_map.find(*it); |
| 212 | if (map_it != the_map.end()) { |
| 213 | *it = map_it->second; |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 218 | // Returns the currently booted device. "/dev/sda3", for example. |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 219 | // This will not interpret LABEL= or UUID=. You'll need to use findfs |
| 220 | // or something with equivalent funcionality to interpret those. |
| 221 | const std::string BootDevice(); |
| 222 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 223 | // Returns the currently booted kernel device, "dev/sda2", for example. |
| 224 | // Client must pass in the boot device. The suggested calling convention |
| 225 | // is: BootKernelDevice(BootDevice()). |
| 226 | // This function works by doing string modification on boot_device. |
| 227 | // Returns empty string on failure. |
| 228 | const std::string BootKernelDevice(const std::string& boot_device); |
| 229 | |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 230 | enum ProcessPriority { |
| 231 | kProcessPriorityHigh = -10, |
| 232 | kProcessPriorityNormal = 0, |
| 233 | kProcessPriorityLow = 10, |
| 234 | }; |
| 235 | |
| 236 | // Compares process priorities and returns an integer that is less |
| 237 | // than, equal to or greater than 0 if |priority_lhs| is, |
| 238 | // respectively, lower than, same as or higher than |priority_rhs|. |
| 239 | int ComparePriorities(ProcessPriority priority_lhs, |
| 240 | ProcessPriority priority_rhs); |
| 241 | |
| 242 | // Sets the current process priority to |priority|. Returns true on |
| 243 | // success, false otherwise. |
| 244 | bool SetProcessPriority(ProcessPriority priority); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 245 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 246 | } // namespace utils |
| 247 | |
| 248 | // Class to unmount FS when object goes out of scope |
| 249 | class ScopedFilesystemUnmounter { |
| 250 | public: |
| 251 | explicit ScopedFilesystemUnmounter(const std::string& mountpoint) |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 252 | : mountpoint_(mountpoint), |
| 253 | should_unmount_(true) {} |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 254 | ~ScopedFilesystemUnmounter() { |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 255 | if (should_unmount_) { |
| 256 | utils::UnmountFilesystem(mountpoint_); |
| 257 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 258 | } |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 259 | void set_should_unmount(bool unmount) { should_unmount_ = unmount; } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 260 | private: |
| 261 | const std::string mountpoint_; |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 262 | bool should_unmount_; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 263 | DISALLOW_COPY_AND_ASSIGN(ScopedFilesystemUnmounter); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 264 | }; |
| 265 | |
| 266 | // Utility class to close a file descriptor |
| 267 | class ScopedFdCloser { |
| 268 | public: |
| 269 | explicit ScopedFdCloser(int* fd) : fd_(fd), should_close_(true) {} |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 270 | ~ScopedFdCloser() { |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 271 | if (should_close_ && fd_ && (*fd_ >= 0)) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 272 | close(*fd_); |
| 273 | *fd_ = -1; |
| 274 | } |
| 275 | } |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 276 | void set_should_close(bool should_close) { should_close_ = should_close; } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 277 | private: |
| 278 | int* fd_; |
| 279 | bool should_close_; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 280 | DISALLOW_COPY_AND_ASSIGN(ScopedFdCloser); |
| 281 | }; |
| 282 | |
Thieu Le | 5c7d975 | 2010-12-15 16:09:28 -0800 | [diff] [blame] | 283 | // Utility class to close a file system |
| 284 | class ScopedExt2fsCloser { |
| 285 | public: |
| 286 | explicit ScopedExt2fsCloser(ext2_filsys filsys) : filsys_(filsys) {} |
| 287 | ~ScopedExt2fsCloser() { ext2fs_close(filsys_); } |
| 288 | |
| 289 | private: |
| 290 | ext2_filsys filsys_; |
| 291 | DISALLOW_COPY_AND_ASSIGN(ScopedExt2fsCloser); |
| 292 | }; |
| 293 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 294 | // Utility class to delete a file when it goes out of scope. |
| 295 | class ScopedPathUnlinker { |
| 296 | public: |
| 297 | explicit ScopedPathUnlinker(const std::string& path) : path_(path) {} |
| 298 | ~ScopedPathUnlinker() { |
| 299 | if (unlink(path_.c_str()) < 0) { |
| 300 | std::string err_message = strerror(errno); |
| 301 | LOG(ERROR) << "Unable to unlink path " << path_ << ": " << err_message; |
| 302 | } |
| 303 | } |
| 304 | private: |
| 305 | const std::string path_; |
| 306 | DISALLOW_COPY_AND_ASSIGN(ScopedPathUnlinker); |
| 307 | }; |
| 308 | |
| 309 | // Utility class to delete an empty directory when it goes out of scope. |
| 310 | class ScopedDirRemover { |
| 311 | public: |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 312 | explicit ScopedDirRemover(const std::string& path) |
| 313 | : path_(path), |
| 314 | should_remove_(true) {} |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 315 | ~ScopedDirRemover() { |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 316 | if (should_remove_ && (rmdir(path_.c_str()) < 0)) { |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 317 | PLOG(ERROR) << "Unable to remove dir " << path_; |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | void set_should_remove(bool should_remove) { should_remove_ = should_remove; } |
| 321 | |
| 322 | protected: |
| 323 | const std::string path_; |
| 324 | |
| 325 | private: |
| 326 | bool should_remove_; |
| 327 | DISALLOW_COPY_AND_ASSIGN(ScopedDirRemover); |
| 328 | }; |
| 329 | |
| 330 | // Utility class to unmount a filesystem mounted on a temporary directory and |
| 331 | // delete the temporary directory when it goes out of scope. |
| 332 | class ScopedTempUnmounter : public ScopedDirRemover { |
| 333 | public: |
| 334 | explicit ScopedTempUnmounter(const std::string& path) : |
| 335 | ScopedDirRemover(path) {} |
| 336 | ~ScopedTempUnmounter() { |
| 337 | utils::UnmountFilesystem(path_); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 338 | } |
| 339 | private: |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 340 | DISALLOW_COPY_AND_ASSIGN(ScopedTempUnmounter); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 341 | }; |
| 342 | |
| 343 | // A little object to call ActionComplete on the ActionProcessor when |
| 344 | // it's destructed. |
| 345 | class ScopedActionCompleter { |
| 346 | public: |
| 347 | explicit ScopedActionCompleter(ActionProcessor* processor, |
| 348 | AbstractAction* action) |
| 349 | : processor_(processor), |
| 350 | action_(action), |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 351 | code_(kActionCodeError), |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 352 | should_complete_(true) {} |
| 353 | ~ScopedActionCompleter() { |
| 354 | if (should_complete_) |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 355 | processor_->ActionComplete(action_, code_); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 356 | } |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 357 | void set_code(ActionExitCode code) { code_ = code; } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 358 | void set_should_complete(bool should_complete) { |
| 359 | should_complete_ = should_complete; |
| 360 | } |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 361 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 362 | private: |
| 363 | ActionProcessor* processor_; |
| 364 | AbstractAction* action_; |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 365 | ActionExitCode code_; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 366 | bool should_complete_; |
| 367 | DISALLOW_COPY_AND_ASSIGN(ScopedActionCompleter); |
| 368 | }; |
| 369 | |
| 370 | } // namespace chromeos_update_engine |
| 371 | |
| 372 | #define TEST_AND_RETURN_FALSE_ERRNO(_x) \ |
| 373 | do { \ |
| 374 | bool _success = (_x); \ |
| 375 | if (!_success) { \ |
| 376 | std::string _msg = \ |
| 377 | chromeos_update_engine::utils::ErrnoNumberAsString(errno); \ |
| 378 | LOG(ERROR) << #_x " failed: " << _msg; \ |
| 379 | return false; \ |
| 380 | } \ |
| 381 | } while (0) |
| 382 | |
| 383 | #define TEST_AND_RETURN_FALSE(_x) \ |
| 384 | do { \ |
| 385 | bool _success = (_x); \ |
| 386 | if (!_success) { \ |
| 387 | LOG(ERROR) << #_x " failed."; \ |
| 388 | return false; \ |
| 389 | } \ |
| 390 | } while (0) |
| 391 | |
| 392 | #define TEST_AND_RETURN_ERRNO(_x) \ |
| 393 | do { \ |
| 394 | bool _success = (_x); \ |
| 395 | if (!_success) { \ |
| 396 | std::string _msg = \ |
| 397 | chromeos_update_engine::utils::ErrnoNumberAsString(errno); \ |
| 398 | LOG(ERROR) << #_x " failed: " << _msg; \ |
| 399 | return; \ |
| 400 | } \ |
| 401 | } while (0) |
| 402 | |
| 403 | #define TEST_AND_RETURN(_x) \ |
| 404 | do { \ |
| 405 | bool _success = (_x); \ |
| 406 | if (!_success) { \ |
| 407 | LOG(ERROR) << #_x " failed."; \ |
| 408 | return; \ |
| 409 | } \ |
| 410 | } while (0) |
| 411 | |
Thieu Le | 5c7d975 | 2010-12-15 16:09:28 -0800 | [diff] [blame] | 412 | #define TEST_AND_RETURN_FALSE_ERRCODE(_x) \ |
| 413 | do { \ |
| 414 | errcode_t _error = (_x); \ |
| 415 | if (_error) { \ |
| 416 | errno = _error; \ |
| 417 | LOG(ERROR) << #_x " failed: " << _error; \ |
| 418 | return false; \ |
| 419 | } \ |
| 420 | } while (0) |
| 421 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 422 | |
| 423 | |
| 424 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_UTILS_H__ |