OLD | NEW |
1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium OS 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 <errno.h> |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 // Sets the current process priority to |priority|. Returns true on | 230 // Sets the current process priority to |priority|. Returns true on |
231 // success, false otherwise. | 231 // success, false otherwise. |
232 bool SetProcessPriority(ProcessPriority priority); | 232 bool SetProcessPriority(ProcessPriority priority); |
233 | 233 |
234 } // namespace utils | 234 } // namespace utils |
235 | 235 |
236 // Class to unmount FS when object goes out of scope | 236 // Class to unmount FS when object goes out of scope |
237 class ScopedFilesystemUnmounter { | 237 class ScopedFilesystemUnmounter { |
238 public: | 238 public: |
239 explicit ScopedFilesystemUnmounter(const std::string& mountpoint) | 239 explicit ScopedFilesystemUnmounter(const std::string& mountpoint) |
240 : mountpoint_(mountpoint) {} | 240 : mountpoint_(mountpoint), |
| 241 should_unmount_(true) {} |
241 ~ScopedFilesystemUnmounter() { | 242 ~ScopedFilesystemUnmounter() { |
242 utils::UnmountFilesystem(mountpoint_); | 243 if (should_unmount_) { |
| 244 utils::UnmountFilesystem(mountpoint_); |
| 245 } |
243 } | 246 } |
| 247 void set_should_unmount(bool unmount) { should_unmount_ = unmount; } |
244 private: | 248 private: |
245 const std::string mountpoint_; | 249 const std::string mountpoint_; |
| 250 bool should_unmount_; |
246 DISALLOW_COPY_AND_ASSIGN(ScopedFilesystemUnmounter); | 251 DISALLOW_COPY_AND_ASSIGN(ScopedFilesystemUnmounter); |
247 }; | 252 }; |
248 | 253 |
249 // Utility class to close a file descriptor | 254 // Utility class to close a file descriptor |
250 class ScopedFdCloser { | 255 class ScopedFdCloser { |
251 public: | 256 public: |
252 explicit ScopedFdCloser(int* fd) : fd_(fd), should_close_(true) {} | 257 explicit ScopedFdCloser(int* fd) : fd_(fd), should_close_(true) {} |
253 void set_should_close(bool should_close) { should_close_ = should_close; } | |
254 ~ScopedFdCloser() { | 258 ~ScopedFdCloser() { |
255 if (!should_close_) | 259 if (should_close_ && fd_ && (*fd_ >= 0)) { |
256 return; | |
257 if (fd_ && (*fd_ >= 0)) { | |
258 close(*fd_); | 260 close(*fd_); |
259 *fd_ = -1; | 261 *fd_ = -1; |
260 } | 262 } |
261 } | 263 } |
| 264 void set_should_close(bool should_close) { should_close_ = should_close; } |
262 private: | 265 private: |
263 int* fd_; | 266 int* fd_; |
264 bool should_close_; | 267 bool should_close_; |
265 DISALLOW_COPY_AND_ASSIGN(ScopedFdCloser); | 268 DISALLOW_COPY_AND_ASSIGN(ScopedFdCloser); |
266 }; | 269 }; |
267 | 270 |
268 // Utility class to delete a file when it goes out of scope. | 271 // Utility class to delete a file when it goes out of scope. |
269 class ScopedPathUnlinker { | 272 class ScopedPathUnlinker { |
270 public: | 273 public: |
271 explicit ScopedPathUnlinker(const std::string& path) : path_(path) {} | 274 explicit ScopedPathUnlinker(const std::string& path) : path_(path) {} |
272 ~ScopedPathUnlinker() { | 275 ~ScopedPathUnlinker() { |
273 if (unlink(path_.c_str()) < 0) { | 276 if (unlink(path_.c_str()) < 0) { |
274 std::string err_message = strerror(errno); | 277 std::string err_message = strerror(errno); |
275 LOG(ERROR) << "Unable to unlink path " << path_ << ": " << err_message; | 278 LOG(ERROR) << "Unable to unlink path " << path_ << ": " << err_message; |
276 } | 279 } |
277 } | 280 } |
278 private: | 281 private: |
279 const std::string path_; | 282 const std::string path_; |
280 DISALLOW_COPY_AND_ASSIGN(ScopedPathUnlinker); | 283 DISALLOW_COPY_AND_ASSIGN(ScopedPathUnlinker); |
281 }; | 284 }; |
282 | 285 |
283 // Utility class to delete an empty directory when it goes out of scope. | 286 // Utility class to delete an empty directory when it goes out of scope. |
284 class ScopedDirRemover { | 287 class ScopedDirRemover { |
285 public: | 288 public: |
286 explicit ScopedDirRemover(const std::string& path) : path_(path) {} | 289 explicit ScopedDirRemover(const std::string& path) |
| 290 : path_(path), |
| 291 should_remove_(true) {} |
287 ~ScopedDirRemover() { | 292 ~ScopedDirRemover() { |
288 if (rmdir(path_.c_str()) < 0) | 293 if (should_remove_ && (rmdir(path_.c_str()) < 0)) { |
289 PLOG(ERROR) << "Unable to remove dir " << path_; | 294 PLOG(ERROR) << "Unable to remove dir " << path_; |
| 295 } |
| 296 } |
| 297 void set_should_remove(bool should_remove) { should_remove_ = should_remove; } |
| 298 |
| 299 protected: |
| 300 const std::string path_; |
| 301 |
| 302 private: |
| 303 bool should_remove_; |
| 304 DISALLOW_COPY_AND_ASSIGN(ScopedDirRemover); |
| 305 }; |
| 306 |
| 307 // Utility class to unmount a filesystem mounted on a temporary directory and |
| 308 // delete the temporary directory when it goes out of scope. |
| 309 class ScopedTempUnmounter : public ScopedDirRemover { |
| 310 public: |
| 311 explicit ScopedTempUnmounter(const std::string& path) : |
| 312 ScopedDirRemover(path) {} |
| 313 ~ScopedTempUnmounter() { |
| 314 utils::UnmountFilesystem(path_); |
290 } | 315 } |
291 private: | 316 private: |
292 const std::string path_; | 317 DISALLOW_COPY_AND_ASSIGN(ScopedTempUnmounter); |
293 DISALLOW_COPY_AND_ASSIGN(ScopedDirRemover); | |
294 }; | 318 }; |
295 | 319 |
296 // A little object to call ActionComplete on the ActionProcessor when | 320 // A little object to call ActionComplete on the ActionProcessor when |
297 // it's destructed. | 321 // it's destructed. |
298 class ScopedActionCompleter { | 322 class ScopedActionCompleter { |
299 public: | 323 public: |
300 explicit ScopedActionCompleter(ActionProcessor* processor, | 324 explicit ScopedActionCompleter(ActionProcessor* processor, |
301 AbstractAction* action) | 325 AbstractAction* action) |
302 : processor_(processor), | 326 : processor_(processor), |
303 action_(action), | 327 action_(action), |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
358 bool _success = (_x); \ | 382 bool _success = (_x); \ |
359 if (!_success) { \ | 383 if (!_success) { \ |
360 LOG(ERROR) << #_x " failed."; \ | 384 LOG(ERROR) << #_x " failed."; \ |
361 return; \ | 385 return; \ |
362 } \ | 386 } \ |
363 } while (0) | 387 } while (0) |
364 | 388 |
365 | 389 |
366 | 390 |
367 #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_UTILS_H__ | 391 #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_UTILS_H__ |
OLD | NEW |