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 <algorithm> | 8 #include <algorithm> |
9 #include <set> | 9 #include <set> |
10 #include <string> | 10 #include <string> |
11 #include <vector> | 11 #include <vector> |
12 #include "update_engine/action.h" | 12 #include "update_engine/action.h" |
13 #include "update_engine/action_processor.h" | 13 #include "update_engine/action_processor.h" |
14 | 14 |
15 namespace chromeos_update_engine { | 15 namespace chromeos_update_engine { |
16 | 16 |
17 namespace utils { | 17 namespace utils { |
18 | 18 |
19 // Writes the data passed to path. The file at path will be overwritten if it | 19 // Writes the data passed to path. The file at path will be overwritten if it |
20 // exists. Returns true on success, false otherwise. | 20 // exists. Returns true on success, false otherwise. |
21 bool WriteFile(const char* path, const char* data, int data_len); | 21 bool WriteFile(const char* path, const char* data, int data_len); |
22 | 22 |
| 23 // calls write() repeatedly until all count bytes at buf are written to |
| 24 // fd or an error occurs. Returns true on success. |
| 25 bool WriteAll(int fd, const void *buf, size_t count); |
| 26 |
23 // Returns the entire contents of the file at path. Returns true on success. | 27 // Returns the entire contents of the file at path. Returns true on success. |
24 bool ReadFile(const std::string& path, std::vector<char>* out); | 28 bool ReadFile(const std::string& path, std::vector<char>* out); |
25 bool ReadFileToString(const std::string& path, std::string* out); | 29 bool ReadFileToString(const std::string& path, std::string* out); |
26 | 30 |
27 std::string ErrnoNumberAsString(int err); | 31 std::string ErrnoNumberAsString(int err); |
28 | 32 |
29 // Strips duplicate slashes, and optionally removes all trailing slashes. | 33 // Strips duplicate slashes, and optionally removes all trailing slashes. |
30 // Does not compact /./ or /../. | 34 // Does not compact /./ or /../. |
31 std::string NormalizePath(const std::string& path, bool strip_trailing_slash); | 35 std::string NormalizePath(const std::string& path, bool strip_trailing_slash); |
32 | 36 |
33 // Returns true if the file exists for sure. Returns false if it doesn't exist, | 37 // Returns true if the file exists for sure. Returns false if it doesn't exist, |
34 // or an error occurs. | 38 // or an error occurs. |
35 bool FileExists(const char* path); | 39 bool FileExists(const char* path); |
36 | 40 |
37 // The last 6 chars of path must be XXXXXX. They will be randomly changed | 41 // The last 6 chars of path must be XXXXXX. They will be randomly changed |
38 // and a non-existent path will be returned. Intentionally makes a copy | 42 // and a non-existent path will be returned. Intentionally makes a copy |
39 // of the string passed in. | 43 // of the string passed in. |
40 // NEVER CALL THIS FUNCTION UNLESS YOU ARE SURE | 44 // NEVER CALL THIS FUNCTION UNLESS YOU ARE SURE |
41 // THAT YOUR PROCESS WILL BE THE ONLY THING WRITING FILES IN THIS DIRECTORY. | 45 // THAT YOUR PROCESS WILL BE THE ONLY THING WRITING FILES IN THIS DIRECTORY. |
42 std::string TempFilename(std::string path); | 46 std::string TempFilename(std::string path); |
43 | 47 |
| 48 // 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 |
| 50 // 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. |
| 52 // The last six chars of the template must be XXXXXX. |
| 53 // Returns true on success. |
| 54 bool MakeTempFile(const std::string& filename_template, |
| 55 std::string* filename, |
| 56 int* fd); |
| 57 |
44 // Deletes a directory and all its contents synchronously. Returns true | 58 // Deletes a directory and all its contents synchronously. Returns true |
45 // on success. This may be called with a regular file--it will just unlink it. | 59 // on success. This may be called with a regular file--it will just unlink it. |
46 // This WILL cross filesystem boundaries. | 60 // This WILL cross filesystem boundaries. |
47 bool RecursiveUnlinkDir(const std::string& path); | 61 bool RecursiveUnlinkDir(const std::string& path); |
48 | 62 |
49 // Synchronously mount or unmount a filesystem. Return true on success. | 63 // Synchronously mount or unmount a filesystem. Return true on success. |
50 // Mounts as ext3 with default options. | 64 // Mounts as ext3 with default options. |
51 bool MountFilesystem(const std::string& device, const std::string& mountpoint); | 65 bool MountFilesystem(const std::string& device, const std::string& mountpoint); |
52 bool UnmountFilesystem(const std::string& mountpoint); | 66 bool UnmountFilesystem(const std::string& mountpoint); |
53 | 67 |
(...skipping 25 matching lines...) Expand all Loading... |
79 std::set<ValueType> ret; | 93 std::set<ValueType> ret; |
80 ret.insert(value); | 94 ret.insert(value); |
81 return ret; | 95 return ret; |
82 } | 96 } |
83 | 97 |
84 template<typename T> | 98 template<typename T> |
85 bool VectorContainsValue(const std::vector<T>& vect, const T& value) { | 99 bool VectorContainsValue(const std::vector<T>& vect, const T& value) { |
86 return std::find(vect.begin(), vect.end(), value) != vect.end(); | 100 return std::find(vect.begin(), vect.end(), value) != vect.end(); |
87 } | 101 } |
88 | 102 |
| 103 template<typename T> |
| 104 bool VectorIndexOf(const std::vector<T>& vect, const T& value, |
| 105 typename std::vector<T>::size_type* out_index) { |
| 106 typename std::vector<T>::const_iterator it = std::find(vect.begin(), |
| 107 vect.end(), |
| 108 value); |
| 109 if (it == vect.end()) { |
| 110 return false; |
| 111 } else { |
| 112 *out_index = it - vect.begin(); |
| 113 return true; |
| 114 } |
| 115 } |
| 116 |
89 // Returns the currently booted device. "/dev/sda1", for example. | 117 // Returns the currently booted device. "/dev/sda1", for example. |
90 // This will not interpret LABEL= or UUID=. You'll need to use findfs | 118 // This will not interpret LABEL= or UUID=. You'll need to use findfs |
91 // or something with equivalent funcionality to interpret those. | 119 // or something with equivalent funcionality to interpret those. |
92 const std::string BootDevice(); | 120 const std::string BootDevice(); |
93 | 121 |
94 } // namespace utils | 122 } // namespace utils |
95 | 123 |
96 // Class to unmount FS when object goes out of scope | 124 // Class to unmount FS when object goes out of scope |
97 class ScopedFilesystemUnmounter { | 125 class ScopedFilesystemUnmounter { |
98 public: | 126 public: |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 bool _success = (_x); \ | 217 bool _success = (_x); \ |
190 if (!_success) { \ | 218 if (!_success) { \ |
191 LOG(ERROR) << #_x " failed."; \ | 219 LOG(ERROR) << #_x " failed."; \ |
192 return; \ | 220 return; \ |
193 } \ | 221 } \ |
194 } while (0) | 222 } while (0) |
195 | 223 |
196 | 224 |
197 | 225 |
198 #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_UTILS_H__ | 226 #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_UTILS_H__ |
OLD | NEW |