Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_UTILS_H__ | 5 #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_UTILS_H__ |
| 6 #define CHROMEOS_PLATFORM_UPDATE_ENGINE_UTILS_H__ | 6 #define CHROMEOS_PLATFORM_UPDATE_ENGINE_UTILS_H__ |
| 7 | 7 |
| 8 #include <errno.h> | |
| 8 #include <algorithm> | 9 #include <algorithm> |
| 9 #include <set> | 10 #include <set> |
| 10 #include <string> | 11 #include <string> |
| 11 #include <vector> | 12 #include <vector> |
| 12 #include "update_engine/action.h" | 13 #include "update_engine/action.h" |
| 13 #include "update_engine/action_processor.h" | 14 #include "update_engine/action_processor.h" |
| 14 | 15 |
| 15 namespace chromeos_update_engine { | 16 namespace chromeos_update_engine { |
| 16 | 17 |
| 17 namespace utils { | 18 namespace utils { |
| 18 | 19 |
| 19 // Writes the data passed to path. The file at path will be overwritten if it | 20 // Writes the data passed to path. The file at path will be overwritten if it |
| 20 // exists. Returns true on success, false otherwise. | 21 // exists. Returns true on success, false otherwise. |
| 21 bool WriteFile(const char* path, const char* data, int data_len); | 22 bool WriteFile(const char* path, const char* data, int data_len); |
| 22 | 23 |
| 23 // Calls write() repeatedly until all count bytes at buf are written to | 24 // Calls write() or pwrite() repeatedly until all count bytes at buf are |
| 24 // fd or an error occurs. Returns true on success. | 25 // written to fd or an error occurs. Returns true on success. |
| 25 bool WriteAll(int fd, const void *buf, size_t count); | 26 bool WriteAll(int fd, const void* buf, size_t count); |
| 27 bool PWriteAll(int fd, const void* buf, size_t count, off_t offset); | |
| 28 | |
| 29 // Calls pread() repeatedly until count bytes are read, or EOF is reached. | |
| 30 // Returns number of bytes read in *bytes_read. Returns true on success. | |
| 31 bool PReadAll(int fd, void* buf, size_t count, off_t offset, | |
| 32 ssize_t* out_bytes_read); | |
| 26 | 33 |
| 27 // Returns the entire contents of the file at path. Returns true on success. | 34 // Returns the entire contents of the file at path. Returns true on success. |
| 28 bool ReadFile(const std::string& path, std::vector<char>* out); | 35 bool ReadFile(const std::string& path, std::vector<char>* out); |
| 29 bool ReadFileToString(const std::string& path, std::string* out); | 36 bool ReadFileToString(const std::string& path, std::string* out); |
| 30 | 37 |
| 31 std::string ErrnoNumberAsString(int err); | 38 std::string ErrnoNumberAsString(int err); |
| 32 | 39 |
| 33 // Strips duplicate slashes, and optionally removes all trailing slashes. | 40 // Strips duplicate slashes, and optionally removes all trailing slashes. |
| 34 // Does not compact /./ or /../. | 41 // Does not compact /./ or /../. |
| 35 std::string NormalizePath(const std::string& path, bool strip_trailing_slash); | 42 std::string NormalizePath(const std::string& path, bool strip_trailing_slash); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 48 // Calls mkstemp() with the template passed. Returns the filename in the | 55 // Calls mkstemp() with the template passed. Returns the filename in the |
| 49 // out param filename. If fd is non-NULL, the file fd returned by mkstemp | 56 // out param filename. If fd is non-NULL, the file fd returned by mkstemp |
| 50 // is not close()d and is returned in the out param 'fd'. However, if | 57 // is not close()d and is returned in the out param 'fd'. However, if |
| 51 // fd is NULL, the fd from mkstemp() will be closed. | 58 // fd is NULL, the fd from mkstemp() will be closed. |
| 52 // The last six chars of the template must be XXXXXX. | 59 // The last six chars of the template must be XXXXXX. |
| 53 // Returns true on success. | 60 // Returns true on success. |
| 54 bool MakeTempFile(const std::string& filename_template, | 61 bool MakeTempFile(const std::string& filename_template, |
| 55 std::string* filename, | 62 std::string* filename, |
| 56 int* fd); | 63 int* fd); |
| 57 | 64 |
| 65 // Calls mkdtemp() with the tempate passed. Returns the generated dirname | |
| 66 // in the dirname param. Returns TRUE on success. dirname must not be NULL. | |
| 67 bool MakeTempDirectory(const std::string& dirname_template, | |
| 68 std::string* dirname); | |
| 69 | |
| 58 // Deletes a directory and all its contents synchronously. Returns true | 70 // Deletes a directory and all its contents synchronously. Returns true |
| 59 // on success. This may be called with a regular file--it will just unlink it. | 71 // on success. This may be called with a regular file--it will just unlink it. |
| 60 // This WILL cross filesystem boundaries. | 72 // This WILL cross filesystem boundaries. |
| 61 bool RecursiveUnlinkDir(const std::string& path); | 73 bool RecursiveUnlinkDir(const std::string& path); |
| 62 | 74 |
| 63 // Synchronously mount or unmount a filesystem. Return true on success. | 75 // Synchronously mount or unmount a filesystem. Return true on success. |
| 64 // Mounts as ext3 with default options. | 76 // Mounts as ext3 with default options. |
| 65 bool MountFilesystem(const std::string& device, const std::string& mountpoint); | 77 bool MountFilesystem(const std::string& device, const std::string& mountpoint, |
| 78 unsigned long flags); | |
| 66 bool UnmountFilesystem(const std::string& mountpoint); | 79 bool UnmountFilesystem(const std::string& mountpoint); |
| 67 | 80 |
| 68 // Log a string in hex to LOG(INFO). Useful for debugging. | 81 // Log a string in hex to LOG(INFO). Useful for debugging. |
| 69 void HexDumpArray(const unsigned char* const arr, const size_t length); | 82 void HexDumpArray(const unsigned char* const arr, const size_t length); |
| 70 inline void HexDumpString(const std::string& str) { | 83 inline void HexDumpString(const std::string& str) { |
| 71 HexDumpArray(reinterpret_cast<const unsigned char*>(str.data()), str.size()); | 84 HexDumpArray(reinterpret_cast<const unsigned char*>(str.data()), str.size()); |
| 72 } | 85 } |
| 73 inline void HexDumpVector(const std::vector<char>& vect) { | 86 inline void HexDumpVector(const std::vector<char>& vect) { |
| 74 HexDumpArray(reinterpret_cast<const unsigned char*>(&vect[0]), vect.size()); | 87 HexDumpArray(reinterpret_cast<const unsigned char*>(&vect[0]), vect.size()); |
| 75 } | 88 } |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 144 if (fd_ && (*fd_ >= 0)) { | 157 if (fd_ && (*fd_ >= 0)) { |
| 145 close(*fd_); | 158 close(*fd_); |
| 146 *fd_ = -1; | 159 *fd_ = -1; |
| 147 } | 160 } |
| 148 } | 161 } |
| 149 private: | 162 private: |
| 150 int* fd_; | 163 int* fd_; |
| 151 bool should_close_; | 164 bool should_close_; |
| 152 }; | 165 }; |
| 153 | 166 |
| 167 // Utility class to delete a file when it goes out of scope. | |
| 168 class ScopedPathUnlinker { | |
| 169 public: | |
| 170 explicit ScopedPathUnlinker(const std::string& path) : path_(path) {} | |
| 171 ~ScopedPathUnlinker() { | |
| 172 if (unlink(path_.c_str()) < 0) { | |
| 173 std::string err_message = strerror(errno); | |
| 174 LOG(ERROR) << "Unable to unlink path " << path_ << ": " << err_message; | |
| 175 } | |
| 176 } | |
| 177 private: | |
| 178 const std::string path_; | |
|
Daniel Erat
2010/04/23 17:47:25
DISALLOW_COPY_AND_ASSIGN
adlr
2010/04/23 20:50:17
Done (for all in this file)
| |
| 179 }; | |
| 180 | |
| 181 // Utility class to delete an empty directory when it goes out of scope. | |
| 182 class ScopedDirRemover { | |
| 183 public: | |
| 184 explicit ScopedDirRemover(const std::string& path) : path_(path) {} | |
| 185 ~ScopedDirRemover() { | |
| 186 if (rmdir(path_.c_str()) < 0) | |
| 187 PLOG(ERROR) << "Unable to remove dir " << path_; | |
| 188 } | |
| 189 private: | |
| 190 const std::string path_; | |
|
Daniel Erat
2010/04/23 17:47:25
DISALLOW_COPY_AND_ASSIGN
adlr
2010/04/23 20:50:17
Done.
| |
| 191 }; | |
| 192 | |
| 154 // A little object to call ActionComplete on the ActionProcessor when | 193 // A little object to call ActionComplete on the ActionProcessor when |
| 155 // it's destructed. | 194 // it's destructed. |
| 156 class ScopedActionCompleter { | 195 class ScopedActionCompleter { |
| 157 public: | 196 public: |
| 158 explicit ScopedActionCompleter(ActionProcessor* processor, | 197 explicit ScopedActionCompleter(ActionProcessor* processor, |
| 159 AbstractAction* action) | 198 AbstractAction* action) |
| 160 : processor_(processor), | 199 : processor_(processor), |
| 161 action_(action), | 200 action_(action), |
| 162 success_(false), | 201 success_(false), |
| 163 should_complete_(true) {} | 202 should_complete_(true) {} |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 217 bool _success = (_x); \ | 256 bool _success = (_x); \ |
| 218 if (!_success) { \ | 257 if (!_success) { \ |
| 219 LOG(ERROR) << #_x " failed."; \ | 258 LOG(ERROR) << #_x " failed."; \ |
| 220 return; \ | 259 return; \ |
| 221 } \ | 260 } \ |
| 222 } while (0) | 261 } while (0) |
| 223 | 262 |
| 224 | 263 |
| 225 | 264 |
| 226 #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_UTILS_H__ | 265 #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_UTILS_H__ |
| OLD | NEW |