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