blob: 7920f3c74916d04a9b8951c06e348c451f68b847 [file] [log] [blame]
adlr@google.com3defe6a2009-12-04 20:57:17 +00001// 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
3// found in the LICENSE file.
4
5#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_UTILS_H__
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_UTILS_H__
7
8#include <set>
9#include <string>
10#include <vector>
11#include "update_engine/action.h"
12#include "update_engine/action_processor.h"
13
14namespace chromeos_update_engine {
15
16namespace utils {
17
Andrew de los Reyes970bb282009-12-09 16:34:04 -080018// Writes the data passed to path. The file at path will be overwritten if it
19// exists. Returns true on success, false otherwise.
20bool WriteFile(const char* path, const char* data, int data_len);
21
adlr@google.com3defe6a2009-12-04 20:57:17 +000022// Returns the entire contents of the file at path. Returns true on success.
23bool ReadFile(const std::string& path, std::vector<char>* out);
24bool ReadFileToString(const std::string& path, std::string* out);
25
26std::string ErrnoNumberAsString(int err);
27
28// Strips duplicate slashes, and optionally removes all trailing slashes.
29// Does not compact /./ or /../.
30std::string NormalizePath(const std::string& path, bool strip_trailing_slash);
31
32// Returns true if the file exists for sure. Returns false if it doesn't exist,
33// or an error occurs.
34bool FileExists(const char* path);
35
36// The last 6 chars of path must be XXXXXX. They will be randomly changed
37// and a non-existent path will be returned. Intentionally makes a copy
38// of the string passed in.
39// NEVER CALL THIS FUNCTION UNLESS YOU ARE SURE
40// THAT YOUR PROCESS WILL BE THE ONLY THING WRITING FILES IN THIS DIRECTORY.
41std::string TempFilename(string path);
42
43// Deletes a directory and all its contents synchronously. Returns true
44// on success. This may be called with a regular file--it will just unlink it.
45// This WILL cross filesystem boundaries.
46bool RecursiveUnlinkDir(const std::string& path);
47
48// Synchronously mount or unmount a filesystem. Return true on success.
49// Mounts as ext3 with default options.
50bool MountFilesystem(const string& device, const string& mountpoint);
51bool UnmountFilesystem(const string& mountpoint);
52
53// Log a string in hex to LOG(INFO). Useful for debugging.
54void HexDumpArray(const unsigned char* const arr, const size_t length);
55inline void HexDumpString(const std::string& str) {
56 HexDumpArray(reinterpret_cast<const unsigned char*>(str.data()), str.size());
57}
58inline void HexDumpVector(const std::vector<char>& vect) {
59 HexDumpArray(reinterpret_cast<const unsigned char*>(&vect[0]), vect.size());
60}
61
62extern const string kStatefulPartition;
63
64bool StringHasSuffix(const std::string& str, const std::string& suffix);
65bool StringHasPrefix(const std::string& str, const std::string& prefix);
66
67template<typename KeyType, typename ValueType>
68bool MapContainsKey(const std::map<KeyType, ValueType>& m, const KeyType& k) {
69 return m.find(k) != m.end();
70}
71
72template<typename ValueType>
73std::set<ValueType> SetWithValue(const ValueType& value) {
74 std::set<ValueType> ret;
75 ret.insert(value);
76 return ret;
77}
78
79// Returns the currently booted device. "/dev/sda1", for example.
80// This will not interpret LABEL= or UUID=. You'll need to use findfs
81// or something with equivalent funcionality to interpret those.
82const std::string BootDevice();
83
84} // namespace utils
85
86// Class to unmount FS when object goes out of scope
87class ScopedFilesystemUnmounter {
88 public:
89 explicit ScopedFilesystemUnmounter(const std::string& mountpoint)
90 : mountpoint_(mountpoint) {}
91 ~ScopedFilesystemUnmounter() {
92 utils::UnmountFilesystem(mountpoint_);
93 }
94 private:
95 const std::string mountpoint_;
96};
97
98// Utility class to close a file descriptor
99class ScopedFdCloser {
100 public:
101 explicit ScopedFdCloser(int* fd) : fd_(fd), should_close_(true) {}
102 void set_should_close(bool should_close) { should_close_ = should_close; }
103 ~ScopedFdCloser() {
104 if (!should_close_)
105 return;
106 if (fd_ && (*fd_ >= 0)) {
107 close(*fd_);
108 *fd_ = -1;
109 }
110 }
111 private:
112 int* fd_;
113 bool should_close_;
114};
115
116// A little object to call ActionComplete on the ActionProcessor when
117// it's destructed.
118class ScopedActionCompleter {
119 public:
120 explicit ScopedActionCompleter(ActionProcessor* processor,
121 AbstractAction* action)
122 : processor_(processor),
123 action_(action),
124 success_(false),
125 should_complete_(true) {}
126 ~ScopedActionCompleter() {
127 if (should_complete_)
128 processor_->ActionComplete(action_, success_);
129 }
130 void set_success(bool success) {
131 success_ = success;
132 }
133 void set_should_complete(bool should_complete) {
134 should_complete_ = should_complete;
135 }
136 private:
137 ActionProcessor* processor_;
138 AbstractAction* action_;
139 bool success_;
140 bool should_complete_;
141 DISALLOW_COPY_AND_ASSIGN(ScopedActionCompleter);
142};
143
144} // namespace chromeos_update_engine
145
146#define TEST_AND_RETURN_FALSE_ERRNO(_x) \
147 do { \
148 bool _success = (_x); \
149 if (!_success) { \
150 std::string _msg = \
151 chromeos_update_engine::utils::ErrnoNumberAsString(errno); \
152 LOG(ERROR) << #_x " failed: " << _msg; \
153 return false; \
154 } \
155 } while (0)
156
157#define TEST_AND_RETURN_FALSE(_x) \
158 do { \
159 bool _success = (_x); \
160 if (!_success) { \
161 LOG(ERROR) << #_x " failed."; \
162 return false; \
163 } \
164 } while (0)
165
166#define TEST_AND_RETURN_ERRNO(_x) \
167 do { \
168 bool _success = (_x); \
169 if (!_success) { \
170 std::string _msg = \
171 chromeos_update_engine::utils::ErrnoNumberAsString(errno); \
172 LOG(ERROR) << #_x " failed: " << _msg; \
173 return; \
174 } \
175 } while (0)
176
177#define TEST_AND_RETURN(_x) \
178 do { \
179 bool _success = (_x); \
180 if (!_success) { \
181 LOG(ERROR) << #_x " failed."; \
182 return; \
183 } \
184 } while (0)
185
186
187
188#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_UTILS_H__