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