blob: 1b3095b2f91c30b2923a1fa88b42f5ae55458cf1 [file] [log] [blame]
Gilad Arnoldd7b513d2012-05-10 14:25:27 -07001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
adlr@google.com3defe6a2009-12-04 20:57:17 +00002// 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
Andrew de los Reyes81ebcd82010-03-09 15:56:18 -08008#include <algorithm>
Han Shen2643cb72012-06-26 14:45:33 -07009#include <errno.h>
adlr@google.com3defe6a2009-12-04 20:57:17 +000010#include <set>
11#include <string>
Han Shen2643cb72012-06-26 14:45:33 -070012#include <unistd.h>
adlr@google.com3defe6a2009-12-04 20:57:17 +000013#include <vector>
Darin Petkovc6c135c2010-08-11 13:36:18 -070014
Gilad Arnoldf9609112012-02-29 11:38:47 -080015#include <base/eintr_wrapper.h>
Gilad Arnoldd7b513d2012-05-10 14:25:27 -070016#include <base/time.h>
Thieu Le5c7d9752010-12-15 16:09:28 -080017#include <ext2fs/ext2fs.h>
Andrew de los Reyesc7020782010-04-28 10:46:04 -070018#include <glib.h>
Darin Petkovc6c135c2010-08-11 13:36:18 -070019
adlr@google.com3defe6a2009-12-04 20:57:17 +000020#include "update_engine/action.h"
21#include "update_engine/action_processor.h"
22
23namespace chromeos_update_engine {
24
25namespace utils {
26
Darin Petkova07586b2010-10-20 13:41:15 -070027// Returns true if this is an official Chrome OS build, false otherwise.
Darin Petkov33d30642010-08-04 10:18:57 -070028bool IsOfficialBuild();
29
Darin Petkov44d98d92011-03-21 16:08:11 -070030// Returns true if the boot mode is normal or if it's unable to determine the
31// boot mode. Returns false if the boot mode is developer.
Darin Petkovc91dd6b2011-01-10 12:31:34 -080032bool IsNormalBootMode();
33
Darin Petkovf2065b42011-05-17 16:36:27 -070034// Returns the HWID or an empty string on error.
35std::string GetHardwareClass();
36
Andrew de los Reyes970bb282009-12-09 16:34:04 -080037// Writes the data passed to path. The file at path will be overwritten if it
38// exists. Returns true on success, false otherwise.
39bool WriteFile(const char* path, const char* data, int data_len);
40
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070041// Calls write() or pwrite() repeatedly until all count bytes at buf are
42// written to fd or an error occurs. Returns true on success.
43bool WriteAll(int fd, const void* buf, size_t count);
44bool PWriteAll(int fd, const void* buf, size_t count, off_t offset);
45
46// Calls pread() repeatedly until count bytes are read, or EOF is reached.
47// Returns number of bytes read in *bytes_read. Returns true on success.
48bool PReadAll(int fd, void* buf, size_t count, off_t offset,
49 ssize_t* out_bytes_read);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070050
Gilad Arnold19a45f02012-07-19 12:36:10 -070051// Opens |path| for reading and appends its entire content to the container
52// pointed to by |out_p|. Returns true upon successfully reading all of the
53// file's content, false otherwise, in which case the state of the output
54// container is unknown.
55bool ReadFile(const std::string& path, std::vector<char>* out_p);
56bool ReadFile(const std::string& path, std::string* out_p);
57
58// Invokes |cmd| in a pipe and appends its stdout to the container pointed to by
59// |out_p|. Returns true upon successfully reading all of the output, false
60// otherwise, in which case the state of the output container is unknown.
61bool ReadPipe(const std::string& cmd, std::vector<char>* out_p);
62bool ReadPipe(const std::string& cmd, std::string* out_p);
adlr@google.com3defe6a2009-12-04 20:57:17 +000063
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -070064// Returns the size of the file at path. If the file doesn't exist or some
65// error occurrs, -1 is returned.
66off_t FileSize(const std::string& path);
67
adlr@google.com3defe6a2009-12-04 20:57:17 +000068std::string ErrnoNumberAsString(int err);
69
70// Strips duplicate slashes, and optionally removes all trailing slashes.
71// Does not compact /./ or /../.
72std::string NormalizePath(const std::string& path, bool strip_trailing_slash);
73
74// Returns true if the file exists for sure. Returns false if it doesn't exist,
75// or an error occurs.
76bool FileExists(const char* path);
77
Darin Petkov30291ed2010-11-12 10:23:06 -080078// Returns true if |path| exists and is a symbolic link.
79bool IsSymlink(const char* path);
80
adlr@google.com3defe6a2009-12-04 20:57:17 +000081// The last 6 chars of path must be XXXXXX. They will be randomly changed
82// and a non-existent path will be returned. Intentionally makes a copy
83// of the string passed in.
84// NEVER CALL THIS FUNCTION UNLESS YOU ARE SURE
85// THAT YOUR PROCESS WILL BE THE ONLY THING WRITING FILES IN THIS DIRECTORY.
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080086std::string TempFilename(std::string path);
adlr@google.com3defe6a2009-12-04 20:57:17 +000087
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070088// Calls mkstemp() with the template passed. Returns the filename in the
89// out param filename. If fd is non-NULL, the file fd returned by mkstemp
90// is not close()d and is returned in the out param 'fd'. However, if
91// fd is NULL, the fd from mkstemp() will be closed.
92// The last six chars of the template must be XXXXXX.
93// Returns true on success.
94bool MakeTempFile(const std::string& filename_template,
95 std::string* filename,
96 int* fd);
97
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070098// Calls mkdtemp() with the template passed. Returns the generated dirname
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070099// in the dirname param. Returns TRUE on success. dirname must not be NULL.
100bool MakeTempDirectory(const std::string& dirname_template,
101 std::string* dirname);
102
adlr@google.com3defe6a2009-12-04 20:57:17 +0000103// Deletes a directory and all its contents synchronously. Returns true
104// on success. This may be called with a regular file--it will just unlink it.
105// This WILL cross filesystem boundaries.
106bool RecursiveUnlinkDir(const std::string& path);
107
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700108// Returns the root device for a partition. For example,
Darin Petkovf74eb652010-08-04 12:08:38 -0700109// RootDevice("/dev/sda3") returns "/dev/sda". Returns an empty string
110// if the input device is not of the "/dev/xyz" form.
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700111std::string RootDevice(const std::string& partition_device);
112
113// Returns the partition number, as a string, of partition_device. For example,
Darin Petkovf74eb652010-08-04 12:08:38 -0700114// PartitionNumber("/dev/sda3") returns "3".
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700115std::string PartitionNumber(const std::string& partition_device);
116
Darin Petkovf74eb652010-08-04 12:08:38 -0700117// Returns the sysfs block device for a root block device. For
118// example, SysfsBlockDevice("/dev/sda") returns
119// "/sys/block/sda". Returns an empty string if the input device is
120// not of the "/dev/xyz" form.
121std::string SysfsBlockDevice(const std::string& device);
122
123// Returns true if the root |device| (e.g., "/dev/sdb") is known to be
124// removable, false otherwise.
125bool IsRemovableDevice(const std::string& device);
126
adlr@google.com3defe6a2009-12-04 20:57:17 +0000127// Synchronously mount or unmount a filesystem. Return true on success.
128// Mounts as ext3 with default options.
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700129bool MountFilesystem(const std::string& device, const std::string& mountpoint,
130 unsigned long flags);
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800131bool UnmountFilesystem(const std::string& mountpoint);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000132
Darin Petkovd3f8c892010-10-12 21:38:45 -0700133// Returns the block count and the block byte size of the ext3 file system on
134// |device| (which may be a real device or a path to a filesystem image) or on
135// an opened file descriptor |fd|. The actual file-system size is |block_count|
136// * |block_size| bytes. Returns true on success, false otherwise.
137bool GetFilesystemSize(const std::string& device,
138 int* out_block_count,
139 int* out_block_size);
140bool GetFilesystemSizeFromFD(int fd,
141 int* out_block_count,
142 int* out_block_size);
143
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700144// Returns the string representation of the given UTC time.
145// such as "11/14/2011 14:05:30 GMT".
146std::string ToString(const base::Time utc_time);
147
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700148enum BootLoader {
149 BootLoader_SYSLINUX = 0,
150 BootLoader_CHROME_FIRMWARE = 1
151};
152// Detects which bootloader this system uses and returns it via the out
153// param. Returns true on success.
154bool GetBootloader(BootLoader* out_bootloader);
155
Darin Petkova0b9e772011-10-06 05:05:56 -0700156// Returns the error message, if any, from a GError pointer. Frees the GError
157// object and resets error to NULL.
158std::string GetAndFreeGError(GError** error);
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700159
Darin Petkov296889c2010-07-23 16:20:54 -0700160// Initiates a system reboot. Returns true on success, false otherwise.
161bool Reboot();
162
Andrew de los Reyes712b3ac2011-01-07 13:47:52 -0800163// Schedules a Main Loop callback to trigger the crash reporter to perform an
164// upload as if this process had crashed.
165void ScheduleCrashReporterUpload();
166
Darin Petkov5c0a8af2010-08-24 13:39:13 -0700167// Fuzzes an integer |value| randomly in the range:
168// [value - range / 2, value + range - range / 2]
169int FuzzInt(int value, unsigned int range);
170
adlr@google.com3defe6a2009-12-04 20:57:17 +0000171// Log a string in hex to LOG(INFO). Useful for debugging.
172void HexDumpArray(const unsigned char* const arr, const size_t length);
173inline void HexDumpString(const std::string& str) {
174 HexDumpArray(reinterpret_cast<const unsigned char*>(str.data()), str.size());
175}
176inline void HexDumpVector(const std::vector<char>& vect) {
177 HexDumpArray(reinterpret_cast<const unsigned char*>(&vect[0]), vect.size());
178}
179
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800180extern const char* const kStatefulPartition;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000181
182bool StringHasSuffix(const std::string& str, const std::string& suffix);
183bool StringHasPrefix(const std::string& str, const std::string& prefix);
184
185template<typename KeyType, typename ValueType>
186bool MapContainsKey(const std::map<KeyType, ValueType>& m, const KeyType& k) {
187 return m.find(k) != m.end();
188}
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800189template<typename KeyType>
190bool SetContainsKey(const std::set<KeyType>& s, const KeyType& k) {
191 return s.find(k) != s.end();
192}
adlr@google.com3defe6a2009-12-04 20:57:17 +0000193
194template<typename ValueType>
195std::set<ValueType> SetWithValue(const ValueType& value) {
196 std::set<ValueType> ret;
197 ret.insert(value);
198 return ret;
199}
200
Andrew de los Reyes0ce161b2010-02-22 15:27:01 -0800201template<typename T>
202bool VectorContainsValue(const std::vector<T>& vect, const T& value) {
Darin Petkovc1a8b422010-07-19 11:34:49 -0700203 return std::find(vect.begin(), vect.end(), value) != vect.end();
Andrew de los Reyes0ce161b2010-02-22 15:27:01 -0800204}
205
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700206template<typename T>
207bool VectorIndexOf(const std::vector<T>& vect, const T& value,
208 typename std::vector<T>::size_type* out_index) {
209 typename std::vector<T>::const_iterator it = std::find(vect.begin(),
210 vect.end(),
211 value);
212 if (it == vect.end()) {
213 return false;
214 } else {
215 *out_index = it - vect.begin();
216 return true;
217 }
218}
219
Andrew de los Reyescc92cd32010-10-05 16:56:14 -0700220template<typename ValueType>
221void ApplyMap(std::vector<ValueType>* collection,
222 const std::map<ValueType, ValueType>& the_map) {
223 for (typename std::vector<ValueType>::iterator it = collection->begin();
224 it != collection->end(); ++it) {
225 typename std::map<ValueType, ValueType>::const_iterator map_it =
226 the_map.find(*it);
227 if (map_it != the_map.end()) {
228 *it = map_it->second;
229 }
230 }
231}
232
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700233// Returns the currently booted device. "/dev/sda3", for example.
adlr@google.com3defe6a2009-12-04 20:57:17 +0000234// This will not interpret LABEL= or UUID=. You'll need to use findfs
235// or something with equivalent funcionality to interpret those.
236const std::string BootDevice();
237
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700238// Returns the currently booted kernel device, "dev/sda2", for example.
239// Client must pass in the boot device. The suggested calling convention
240// is: BootKernelDevice(BootDevice()).
241// This function works by doing string modification on boot_device.
242// Returns empty string on failure.
243const std::string BootKernelDevice(const std::string& boot_device);
244
Darin Petkovc6c135c2010-08-11 13:36:18 -0700245enum ProcessPriority {
246 kProcessPriorityHigh = -10,
247 kProcessPriorityNormal = 0,
248 kProcessPriorityLow = 10,
249};
250
251// Compares process priorities and returns an integer that is less
252// than, equal to or greater than 0 if |priority_lhs| is,
253// respectively, lower than, same as or higher than |priority_rhs|.
254int ComparePriorities(ProcessPriority priority_lhs,
255 ProcessPriority priority_rhs);
256
257// Sets the current process priority to |priority|. Returns true on
258// success, false otherwise.
259bool SetProcessPriority(ProcessPriority priority);
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700260
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -0800261// Assumes data points to a Closure. Runs it and returns FALSE;
262gboolean GlibRunClosure(gpointer data);
263
Gilad Arnoldd7b513d2012-05-10 14:25:27 -0700264// Converts seconds into human readable notation including days, hours, minutes
265// and seconds. For example, 185 will yield 3m5s, 4300 will yield 1h11m40s, and
266// 360000 will yield 4d4h0m0s. Zero padding not applied. Seconds are always
267// shown in the result.
268std::string FormatSecs(unsigned secs);
269
270// Converts a TimeDelta into human readable notation including days, hours,
271// minutes, seconds and fractions of a second down to microsecond granularity,
272// as necessary; for example, an output of 5d2h0m15.053s means that the input
273// time was precise to the milliseconds only. Zero padding not applied, except
274// for fractions. Seconds are always shown, but fractions thereof are only shown
275// when applicable.
276std::string FormatTimeDelta(base::TimeDelta delta);
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800277
adlr@google.com3defe6a2009-12-04 20:57:17 +0000278} // namespace utils
279
Jay Srinivasan08fce042012-06-07 16:31:01 -0700280
adlr@google.com3defe6a2009-12-04 20:57:17 +0000281// Class to unmount FS when object goes out of scope
282class ScopedFilesystemUnmounter {
283 public:
284 explicit ScopedFilesystemUnmounter(const std::string& mountpoint)
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800285 : mountpoint_(mountpoint),
286 should_unmount_(true) {}
adlr@google.com3defe6a2009-12-04 20:57:17 +0000287 ~ScopedFilesystemUnmounter() {
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800288 if (should_unmount_) {
289 utils::UnmountFilesystem(mountpoint_);
290 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000291 }
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800292 void set_should_unmount(bool unmount) { should_unmount_ = unmount; }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000293 private:
294 const std::string mountpoint_;
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800295 bool should_unmount_;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700296 DISALLOW_COPY_AND_ASSIGN(ScopedFilesystemUnmounter);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000297};
298
299// Utility class to close a file descriptor
300class ScopedFdCloser {
301 public:
302 explicit ScopedFdCloser(int* fd) : fd_(fd), should_close_(true) {}
adlr@google.com3defe6a2009-12-04 20:57:17 +0000303 ~ScopedFdCloser() {
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800304 if (should_close_ && fd_ && (*fd_ >= 0)) {
Gilad Arnoldf9609112012-02-29 11:38:47 -0800305 if (!close(*fd_))
306 *fd_ = -1;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000307 }
308 }
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800309 void set_should_close(bool should_close) { should_close_ = should_close; }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000310 private:
311 int* fd_;
312 bool should_close_;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700313 DISALLOW_COPY_AND_ASSIGN(ScopedFdCloser);
314};
315
Gilad Arnoldf9609112012-02-29 11:38:47 -0800316// An EINTR-immune file descriptor closer.
317class ScopedEintrSafeFdCloser {
318 public:
319 explicit ScopedEintrSafeFdCloser(int* fd) : fd_(fd), should_close_(true) {}
320 ~ScopedEintrSafeFdCloser() {
321 if (should_close_ && fd_ && (*fd_ >= 0)) {
322 if (!HANDLE_EINTR(close(*fd_)))
323 *fd_ = -1;
324 }
325 }
326 void set_should_close(bool should_close) { should_close_ = should_close; }
327 private:
328 int* fd_;
329 bool should_close_;
330 DISALLOW_COPY_AND_ASSIGN(ScopedEintrSafeFdCloser);
331};
332
Thieu Le5c7d9752010-12-15 16:09:28 -0800333// Utility class to close a file system
334class ScopedExt2fsCloser {
335 public:
336 explicit ScopedExt2fsCloser(ext2_filsys filsys) : filsys_(filsys) {}
337 ~ScopedExt2fsCloser() { ext2fs_close(filsys_); }
338
339 private:
340 ext2_filsys filsys_;
341 DISALLOW_COPY_AND_ASSIGN(ScopedExt2fsCloser);
342};
343
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700344// Utility class to delete a file when it goes out of scope.
345class ScopedPathUnlinker {
346 public:
Darin Petkov52dcaeb2011-01-14 15:33:06 -0800347 explicit ScopedPathUnlinker(const std::string& path)
348 : path_(path),
349 should_remove_(true) {}
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700350 ~ScopedPathUnlinker() {
Darin Petkov52dcaeb2011-01-14 15:33:06 -0800351 if (should_remove_ && unlink(path_.c_str()) < 0) {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700352 std::string err_message = strerror(errno);
353 LOG(ERROR) << "Unable to unlink path " << path_ << ": " << err_message;
354 }
355 }
Darin Petkov52dcaeb2011-01-14 15:33:06 -0800356 void set_should_remove(bool should_remove) { should_remove_ = should_remove; }
357
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700358 private:
359 const std::string path_;
Darin Petkov52dcaeb2011-01-14 15:33:06 -0800360 bool should_remove_;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700361 DISALLOW_COPY_AND_ASSIGN(ScopedPathUnlinker);
362};
363
364// Utility class to delete an empty directory when it goes out of scope.
365class ScopedDirRemover {
366 public:
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800367 explicit ScopedDirRemover(const std::string& path)
368 : path_(path),
369 should_remove_(true) {}
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700370 ~ScopedDirRemover() {
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800371 if (should_remove_ && (rmdir(path_.c_str()) < 0)) {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700372 PLOG(ERROR) << "Unable to remove dir " << path_;
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800373 }
374 }
375 void set_should_remove(bool should_remove) { should_remove_ = should_remove; }
376
377 protected:
378 const std::string path_;
379
380 private:
381 bool should_remove_;
382 DISALLOW_COPY_AND_ASSIGN(ScopedDirRemover);
383};
384
385// Utility class to unmount a filesystem mounted on a temporary directory and
386// delete the temporary directory when it goes out of scope.
387class ScopedTempUnmounter : public ScopedDirRemover {
388 public:
389 explicit ScopedTempUnmounter(const std::string& path) :
390 ScopedDirRemover(path) {}
391 ~ScopedTempUnmounter() {
392 utils::UnmountFilesystem(path_);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700393 }
394 private:
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800395 DISALLOW_COPY_AND_ASSIGN(ScopedTempUnmounter);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000396};
397
398// A little object to call ActionComplete on the ActionProcessor when
399// it's destructed.
400class ScopedActionCompleter {
401 public:
402 explicit ScopedActionCompleter(ActionProcessor* processor,
403 AbstractAction* action)
404 : processor_(processor),
405 action_(action),
Darin Petkovc1a8b422010-07-19 11:34:49 -0700406 code_(kActionCodeError),
adlr@google.com3defe6a2009-12-04 20:57:17 +0000407 should_complete_(true) {}
408 ~ScopedActionCompleter() {
409 if (should_complete_)
Darin Petkovc1a8b422010-07-19 11:34:49 -0700410 processor_->ActionComplete(action_, code_);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000411 }
Darin Petkovc1a8b422010-07-19 11:34:49 -0700412 void set_code(ActionExitCode code) { code_ = code; }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000413 void set_should_complete(bool should_complete) {
414 should_complete_ = should_complete;
415 }
Darin Petkovc1a8b422010-07-19 11:34:49 -0700416
adlr@google.com3defe6a2009-12-04 20:57:17 +0000417 private:
418 ActionProcessor* processor_;
419 AbstractAction* action_;
Darin Petkovc1a8b422010-07-19 11:34:49 -0700420 ActionExitCode code_;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000421 bool should_complete_;
422 DISALLOW_COPY_AND_ASSIGN(ScopedActionCompleter);
423};
424
425} // namespace chromeos_update_engine
426
427#define TEST_AND_RETURN_FALSE_ERRNO(_x) \
428 do { \
429 bool _success = (_x); \
430 if (!_success) { \
431 std::string _msg = \
432 chromeos_update_engine::utils::ErrnoNumberAsString(errno); \
433 LOG(ERROR) << #_x " failed: " << _msg; \
434 return false; \
435 } \
436 } while (0)
437
438#define TEST_AND_RETURN_FALSE(_x) \
439 do { \
440 bool _success = (_x); \
441 if (!_success) { \
442 LOG(ERROR) << #_x " failed."; \
443 return false; \
444 } \
445 } while (0)
446
447#define TEST_AND_RETURN_ERRNO(_x) \
448 do { \
449 bool _success = (_x); \
450 if (!_success) { \
451 std::string _msg = \
452 chromeos_update_engine::utils::ErrnoNumberAsString(errno); \
453 LOG(ERROR) << #_x " failed: " << _msg; \
454 return; \
455 } \
456 } while (0)
457
458#define TEST_AND_RETURN(_x) \
459 do { \
460 bool _success = (_x); \
461 if (!_success) { \
462 LOG(ERROR) << #_x " failed."; \
463 return; \
464 } \
465 } while (0)
466
Thieu Le5c7d9752010-12-15 16:09:28 -0800467#define TEST_AND_RETURN_FALSE_ERRCODE(_x) \
468 do { \
469 errcode_t _error = (_x); \
470 if (_error) { \
471 errno = _error; \
472 LOG(ERROR) << #_x " failed: " << _error; \
473 return false; \
474 } \
475 } while (0)
476
adlr@google.com3defe6a2009-12-04 20:57:17 +0000477
478
479#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_UTILS_H__