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 | |
Andrew de los Reyes | f3ed8e7 | 2011-02-16 10:35:46 -0800 | [diff] [blame] | 246 | // Assumes data points to a Closure. Runs it and returns FALSE; |
| 247 | gboolean GlibRunClosure(gpointer data); |
| 248 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 249 | } // namespace utils |
| 250 | |
| 251 | // Class to unmount FS when object goes out of scope |
| 252 | class ScopedFilesystemUnmounter { |
| 253 | public: |
| 254 | explicit ScopedFilesystemUnmounter(const std::string& mountpoint) |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 255 | : mountpoint_(mountpoint), |
| 256 | should_unmount_(true) {} |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 257 | ~ScopedFilesystemUnmounter() { |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 258 | if (should_unmount_) { |
| 259 | utils::UnmountFilesystem(mountpoint_); |
| 260 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 261 | } |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 262 | void set_should_unmount(bool unmount) { should_unmount_ = unmount; } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 263 | private: |
| 264 | const std::string mountpoint_; |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 265 | bool should_unmount_; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 266 | DISALLOW_COPY_AND_ASSIGN(ScopedFilesystemUnmounter); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 267 | }; |
| 268 | |
| 269 | // Utility class to close a file descriptor |
| 270 | class ScopedFdCloser { |
| 271 | public: |
| 272 | explicit ScopedFdCloser(int* fd) : fd_(fd), should_close_(true) {} |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 273 | ~ScopedFdCloser() { |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 274 | if (should_close_ && fd_ && (*fd_ >= 0)) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 275 | close(*fd_); |
| 276 | *fd_ = -1; |
| 277 | } |
| 278 | } |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 279 | void set_should_close(bool should_close) { should_close_ = should_close; } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 280 | private: |
| 281 | int* fd_; |
| 282 | bool should_close_; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 283 | DISALLOW_COPY_AND_ASSIGN(ScopedFdCloser); |
| 284 | }; |
| 285 | |
Thieu Le | 5c7d975 | 2010-12-15 16:09:28 -0800 | [diff] [blame] | 286 | // Utility class to close a file system |
| 287 | class ScopedExt2fsCloser { |
| 288 | public: |
| 289 | explicit ScopedExt2fsCloser(ext2_filsys filsys) : filsys_(filsys) {} |
| 290 | ~ScopedExt2fsCloser() { ext2fs_close(filsys_); } |
| 291 | |
| 292 | private: |
| 293 | ext2_filsys filsys_; |
| 294 | DISALLOW_COPY_AND_ASSIGN(ScopedExt2fsCloser); |
| 295 | }; |
| 296 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 297 | // Utility class to delete a file when it goes out of scope. |
| 298 | class ScopedPathUnlinker { |
| 299 | public: |
Darin Petkov | 52dcaeb | 2011-01-14 15:33:06 -0800 | [diff] [blame] | 300 | explicit ScopedPathUnlinker(const std::string& path) |
| 301 | : path_(path), |
| 302 | should_remove_(true) {} |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 303 | ~ScopedPathUnlinker() { |
Darin Petkov | 52dcaeb | 2011-01-14 15:33:06 -0800 | [diff] [blame] | 304 | if (should_remove_ && unlink(path_.c_str()) < 0) { |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 305 | std::string err_message = strerror(errno); |
| 306 | LOG(ERROR) << "Unable to unlink path " << path_ << ": " << err_message; |
| 307 | } |
| 308 | } |
Darin Petkov | 52dcaeb | 2011-01-14 15:33:06 -0800 | [diff] [blame] | 309 | void set_should_remove(bool should_remove) { should_remove_ = should_remove; } |
| 310 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 311 | private: |
| 312 | const std::string path_; |
Darin Petkov | 52dcaeb | 2011-01-14 15:33:06 -0800 | [diff] [blame] | 313 | bool should_remove_; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 314 | DISALLOW_COPY_AND_ASSIGN(ScopedPathUnlinker); |
| 315 | }; |
| 316 | |
| 317 | // Utility class to delete an empty directory when it goes out of scope. |
| 318 | class ScopedDirRemover { |
| 319 | public: |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 320 | explicit ScopedDirRemover(const std::string& path) |
| 321 | : path_(path), |
| 322 | should_remove_(true) {} |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 323 | ~ScopedDirRemover() { |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 324 | if (should_remove_ && (rmdir(path_.c_str()) < 0)) { |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 325 | PLOG(ERROR) << "Unable to remove dir " << path_; |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | void set_should_remove(bool should_remove) { should_remove_ = should_remove; } |
| 329 | |
| 330 | protected: |
| 331 | const std::string path_; |
| 332 | |
| 333 | private: |
| 334 | bool should_remove_; |
| 335 | DISALLOW_COPY_AND_ASSIGN(ScopedDirRemover); |
| 336 | }; |
| 337 | |
| 338 | // Utility class to unmount a filesystem mounted on a temporary directory and |
| 339 | // delete the temporary directory when it goes out of scope. |
| 340 | class ScopedTempUnmounter : public ScopedDirRemover { |
| 341 | public: |
| 342 | explicit ScopedTempUnmounter(const std::string& path) : |
| 343 | ScopedDirRemover(path) {} |
| 344 | ~ScopedTempUnmounter() { |
| 345 | utils::UnmountFilesystem(path_); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 346 | } |
| 347 | private: |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 348 | DISALLOW_COPY_AND_ASSIGN(ScopedTempUnmounter); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 349 | }; |
| 350 | |
| 351 | // A little object to call ActionComplete on the ActionProcessor when |
| 352 | // it's destructed. |
| 353 | class ScopedActionCompleter { |
| 354 | public: |
| 355 | explicit ScopedActionCompleter(ActionProcessor* processor, |
| 356 | AbstractAction* action) |
| 357 | : processor_(processor), |
| 358 | action_(action), |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 359 | code_(kActionCodeError), |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 360 | should_complete_(true) {} |
| 361 | ~ScopedActionCompleter() { |
| 362 | if (should_complete_) |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 363 | processor_->ActionComplete(action_, code_); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 364 | } |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 365 | void set_code(ActionExitCode code) { code_ = code; } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 366 | void set_should_complete(bool should_complete) { |
| 367 | should_complete_ = should_complete; |
| 368 | } |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 369 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 370 | private: |
| 371 | ActionProcessor* processor_; |
| 372 | AbstractAction* action_; |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 373 | ActionExitCode code_; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 374 | bool should_complete_; |
| 375 | DISALLOW_COPY_AND_ASSIGN(ScopedActionCompleter); |
| 376 | }; |
| 377 | |
| 378 | } // namespace chromeos_update_engine |
| 379 | |
| 380 | #define TEST_AND_RETURN_FALSE_ERRNO(_x) \ |
| 381 | do { \ |
| 382 | bool _success = (_x); \ |
| 383 | if (!_success) { \ |
| 384 | std::string _msg = \ |
| 385 | chromeos_update_engine::utils::ErrnoNumberAsString(errno); \ |
| 386 | LOG(ERROR) << #_x " failed: " << _msg; \ |
| 387 | return false; \ |
| 388 | } \ |
| 389 | } while (0) |
| 390 | |
| 391 | #define TEST_AND_RETURN_FALSE(_x) \ |
| 392 | do { \ |
| 393 | bool _success = (_x); \ |
| 394 | if (!_success) { \ |
| 395 | LOG(ERROR) << #_x " failed."; \ |
| 396 | return false; \ |
| 397 | } \ |
| 398 | } while (0) |
| 399 | |
| 400 | #define TEST_AND_RETURN_ERRNO(_x) \ |
| 401 | do { \ |
| 402 | bool _success = (_x); \ |
| 403 | if (!_success) { \ |
| 404 | std::string _msg = \ |
| 405 | chromeos_update_engine::utils::ErrnoNumberAsString(errno); \ |
| 406 | LOG(ERROR) << #_x " failed: " << _msg; \ |
| 407 | return; \ |
| 408 | } \ |
| 409 | } while (0) |
| 410 | |
| 411 | #define TEST_AND_RETURN(_x) \ |
| 412 | do { \ |
| 413 | bool _success = (_x); \ |
| 414 | if (!_success) { \ |
| 415 | LOG(ERROR) << #_x " failed."; \ |
| 416 | return; \ |
| 417 | } \ |
| 418 | } while (0) |
| 419 | |
Thieu Le | 5c7d975 | 2010-12-15 16:09:28 -0800 | [diff] [blame] | 420 | #define TEST_AND_RETURN_FALSE_ERRCODE(_x) \ |
| 421 | do { \ |
| 422 | errcode_t _error = (_x); \ |
| 423 | if (_error) { \ |
| 424 | errno = _error; \ |
| 425 | LOG(ERROR) << #_x " failed: " << _error; \ |
| 426 | return false; \ |
| 427 | } \ |
| 428 | } while (0) |
| 429 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 430 | |
| 431 | |
| 432 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_UTILS_H__ |