No description
Find a file
Tom Cherry 11a3aeeae3 init: introduce Result<T> for return values and error handling
init tries to propagate error information up to build context before
logging errors.  This is a good thing, however too often init has the
overly verbose paradigm for error handling, below:

bool CalculateResult(const T& input, U* output, std::string* err)

bool CalculateAndUseResult(const T& input, std::string* err) {
  U output;
  std::string calculate_result_err;
  if (!CalculateResult(input, &output, &calculate_result_err)) {
    *err = "CalculateResult " + input + " failed: " +
      calculate_result_err;
      return false;
  }
  UseResult(output);
  return true;
}

Even more common are functions that return only true/false but also
require passing a std::string* err in order to see the error message.

This change introduces a Result<T> that is use to either hold a
successful return value of type T or to hold an error message as a
std::string.  If the functional only returns success or a failure with
an error message, Result<Success> may be used.  The classes Error and
ErrnoError are used to indicate a failed Result<T>.

A successful Result<T> is constructed implicitly from any type that
can be implicitly converted to T or from the constructor arguments for
T.  This allows you to return a type T directly from a function that
returns Result<T>.

Error and ErrnoError are used to construct a Result<T> has
failed. Each of these classes take an ostream as an input and are
implicitly cast to a Result<T> containing that failure.  ErrnoError()
additionally appends ": " + strerror(errno) to the end of  the failure
string to aid in interacting with C APIs.

The end result is that the above code snippet is turned into the much
clearer example below:

Result<U> CalculateResult(const T& input);

Result<Success> CalculateAndUseResult(const T& input) {
  auto output = CalculateResult(input);
  if (!output) {
    return Error() << "CalculateResult " << input << " failed: "
                   << output.error();
  }
  UseResult(*output);
  return Success();
}

This change also makes this conversion for some of the util.cpp
functions that used the old paradigm.

Test: boot bullhead, init unit tests
Merged-In: I1e7d3a8820a79362245041251057fbeed2f7979b
Change-Id: I1e7d3a8820a79362245041251057fbeed2f7979b
2017-08-14 14:07:30 -07:00
adb Fix reference to out of scope local in adb_thread_setname. 2017-08-02 20:34:06 +00:00
adf libadfhwc: Fix adf_hwc_close 2017-03-02 17:40:16 +00:00
base Merge "Revert "base: work around thread safety analysis bug."" 2017-08-02 23:10:42 +00:00
bootstat bootstat: switch from root.root to system.log 2017-08-09 15:08:21 -07:00
cpio Possible null pointer miss on realloc 2017-03-23 22:41:14 +01:00
debuggerd Merge "Compare new unwinder to old unwinder in debuggerd." 2017-08-12 16:06:48 +00:00
demangle Mark libdemangle as vendor_available 2017-06-14 18:58:22 +09:00
fastboot fastboot: Add --disable-verity and --disable-verification options. 2017-08-07 16:52:55 -04:00
fingerprintd fingerprint: bump hw api version to 2.1 [DO NOT MERGE] 2017-01-27 22:11:15 +00:00
fs_mgr Merge "Allow the use of a custom Android DT directory" 2017-07-29 06:58:07 +00:00
gatekeeperd Stop asking for old versions of C++ in system/core. 2017-08-02 14:06:28 -07:00
healthd healthd: notify listeners using local copy of list, drop lock 2017-06-29 22:25:40 +00:00
include Move android_filesystem_config.h => fs_config.h 2017-08-02 16:31:19 -07:00
init init: introduce Result<T> for return values and error handling 2017-08-14 14:07:30 -07:00
libappfuse libappfuse: use an explicit buffer size 2017-08-02 14:11:25 -07:00
libbacktrace Add demangling of function name. 2017-08-11 15:17:46 -07:00
libbinderwrapper libbinderwrapper: Android.mk -> Android.bp 2017-04-25 15:55:26 -07:00
libcrypto_utils Rely on the platform -std default. 2016-10-10 14:31:12 -07:00
libcutils Add HSM AID. 2017-08-10 20:25:11 +01:00
libdiskconfig Mark libdiskconfig vendor_available. 2017-05-24 14:21:48 -07:00
libion Remove LOCAL_CLANG and clang: true 2017-07-25 14:29:50 +02:00
libkeyutils Add libkeyutils. 2017-05-10 10:40:11 -07:00
liblog Merge "Fix clang-tidy performance warnings in system/core." 2017-08-04 21:05:51 +00:00
libmemtrack liblog: use log/log.h when utilizing ALOG macros 2017-01-11 09:31:15 -08:00
libmemunreachable Remove LOCAL_CLANG and clang: true 2017-07-25 14:29:50 +02:00
libmetricslogger Remove LOCAL_CLANG and clang: true 2017-07-25 14:29:50 +02:00
libnativebridge Remove LOCAL_CLANG and clang: true 2017-07-25 14:29:50 +02:00
libnativeloader Remove LOCAL_CLANG and clang: true 2017-07-25 14:29:50 +02:00
libnetutils Mark libnetutils vendor_available. 2017-05-24 14:18:35 -07:00
libpackagelistparser Remove LOCAL_CLANG and clang: true 2017-07-25 14:29:50 +02:00
libpixelflinger Va_end should be used with va_start 2017-03-23 22:41:42 +01:00
libprocessgroup Add memcg related configs to init. 2017-07-18 15:58:40 -07:00
libprocinfo libprocinfo: add support for parsing process state. 2017-06-27 15:06:27 -07:00
libsparse libsparse: Fix odd-sized input files total_blks 2017-04-19 10:32:45 -05:00
libsuspend Mark libsuspend vendor_available. 2017-05-24 14:44:30 -07:00
libsync Remove LOCAL_CLANG and clang: true 2017-07-25 14:29:50 +02:00
libsystem libutils: export system_headers 2017-04-28 09:42:50 +09:00
libsysutils Remove old LOG_EVENT_* code from libsysutils 2017-07-11 15:14:26 -07:00
libunwindstack Fix another set of bugs. 2017-08-10 17:37:32 -07:00
libusbhost Convert libusbhost to Android.bp 2017-04-20 08:37:12 -07:00
libutils Merge "Remove TODOs for std::string removal." 2017-08-02 20:35:45 +00:00
libziparchive libziparchive: Use ReadAtOffset exclusively 2017-07-25 18:12:12 +00:00
lmkd lmkd: Android.mk -> Android.bp 2017-04-28 15:20:18 -07:00
logcat Don't try to strip a shell script 2017-08-09 20:20:48 -07:00
logd Merge "logd + liblogd to Android.bp" 2017-06-30 19:32:41 +00:00
logwrapper Merge "logwrapper: add a benchmark for android_fork_execvp_ext" 2017-03-23 11:56:10 +00:00
mkbootimg mkbootimg: use int for os_version and os_patch_level 2016-03-29 16:06:37 -07:00
reboot Convert more Android.mk files to Android.bp 2016-07-13 17:41:45 -07:00
rootdir Include asan options from data partition. 2017-08-09 15:32:23 -07:00
run-as Define range of GIDs for cached app data. 2016-12-13 13:28:08 -07:00
sdcard Remove LOCAL_CLANG and clang: true 2017-07-25 14:29:50 +02:00
shell_and_utilities Remove reference to deleted gzip module 2017-07-06 22:33:22 +00:00
toolbox Merge "Add building and installing of grep for vendor." 2017-06-16 23:24:32 +00:00
trusty Merge "Enable non-secure side to receive messages > 4K" 2017-08-12 03:02:01 +00:00
.clang-format Add a 2 width option of clang format. 2017-03-10 13:01:39 -08:00
.clang-format-2 Only allow short functions in class definitions. 2017-03-28 12:31:37 -07:00
.clang-format-4 Only allow short functions in class definitions. 2017-03-28 12:31:37 -07:00
.gitignore Ignore adb/*.pyc files 2015-08-11 12:59:58 -07:00
Android.bp Export android_filesystem_config.h as a filegroup 2017-01-17 18:20:28 -08:00
Android.mk Remove the simulator target from all makefiles. 2011-07-11 22:12:32 -07:00
CleanSpec.mk init.rc: have hwservicemanager start the HAL class 2016-09-26 00:23:51 -07:00
MODULE_LICENSE_APACHE2 auto import from //depot/cupcake/@135843 2013-07-30 13:56:49 -07:00
NOTICE Fix omission in NOTICE file. 2013-07-30 13:56:55 -07:00
platform_tools_tool_version.mk Fix warning on the build servers 2017-05-25 12:35:40 -07:00
PREUPLOAD.cfg Add a PREUPLOAD.cfg file to run git-clang-format on every commit 2017-03-08 16:51:26 +08:00