We loop over /proc to iterate through pids in a lot of code, so let's
consolidate this into a single function in libbase.
Test: new unit test
Change-Id: I908fab90b603546d0e3e8b8acdc8dadfc3552d62
Bug: 135683564
Test: Included expected.h from two different files. Builds succesfully
with the fix. Does not build without the fix.
Change-Id: If1962a3b70f7580fa652a043aaf29bf592d1926c
We're already including fmtlib statically in libbase, so let's also
expose its functionality directly.
Also create a small benchmark of the most common operations for
format() and StringPrintf(): int, float, and string arguments. Note
that fmt::format is faster than StringPrintf() in all of these cases.
Test: build, run benchmark
Change-Id: I1e18f13673dd89ea936ea5c51418fad723495b08
String formatting for error message can be done by using Errorf and
ErrnoErrorf. e.g. `return Errorf("{} failures", num_failure);`
The string format follows that of fmtlib, not printf.
Bug: 132145659
Test: libbase_test
Change-Id: I65f45d5f12ed23e2c14d6eb442278f467cc42e83
It is typical to pass error to callers like following;
if (!result) {
return Error() << result.error();
}
To transfer errno(or ResultError#code()), Error defines a specialization
operator<<(const ResultError&).
This change fixes so that ResultError is properly handled
Bug: 132145659
Test: atest libbase_test
Change-Id: Ib35457da2d4b923d8e652c54ac510a75546cf918
I was looking for 'MmapFile' earlier and couldn't find it. After
being pointed to it, we thought that adding this synonym would help
others trying to find this file.
Test: n/a
Change-Id: I7fbfd8e090f21183a5de3603a76f884562b99c4a
The Result, Error, ErrnoError are quite generic. Moving them from init
to libbase so that they can be used from other places.
Bug: 132145659
Test: libbase_test
Change-Id: Id774a587f74380fadd7a0fc88c0aa892c3d9a489
As of Android-P, __system_property_set() is synchronous and only will
return '0' if the property was successfully set.
Test: n/a
Change-Id: Icbc0a6d8be1ae683565f378c534ea8e5c5f25357
This is a follow-up change of 7d89fb164b.
- Some of the missing conditions for SFNAIE were added
- Fixed indentation
- The assignment operator became "= default"
Bug: 132145659
Test: libbase_test
Change-Id: Ib5232a6e5e1d3df67e185d6e8c03374105c1ce94
android::base::expected is an Android implementation of the
std::expected proposal.
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0323r7.html
For usage, refer to the expected.h header file and
expected_test.cpp
Bug: 132145659
Test: libbase_test
Change-Id: I65d3a1ecf8654d9858989755dfd0065c81f7b209
The original commit broke aosp-master-with-phones, because of
vendor libraries the depended on the int versions of libbase functions.
This patch reverts the revert, and also adds ABI-compatibility shims for
the replaced functions.
This reverts commit 2c58e1924a.
Bug: http://b/131312539
Test: treehugger
Test: forrest run of aosp-master-with-phones
Change-Id: I75cc84ec8d963e20862f7662e8e2f409471f41cc
adb was already using ConsumePrefix, and now we have another would-be
user in cutils. (There appears to be one place in adb that should use
ConsumeSuffix, so I'm assuming we'll want that sooner or later.)
I've kept these inline because adb and google3's versions both were, and
I'm easily led.
Test: treehugger
Change-Id: I29d99032f6f6ccbfaefece59725db8afb02a4c87
This reverts commit 93d63c010a.
Reason for revert: b/131312539 - All aosp-master-with-phones phones failing boot / health check.
Change-Id: Id4239f93484b7d6b8d9bcd355a59cbd40d9766e2
unique_fd's implicit conversion to int has led to tons of problems (see
all of the overloads for close, fdopen, fdopendir, etc.). Add a switch
that can turn it off, and reduce the ridiculous amount of work to fix up
callers by introducing a borrowed_fd type that can be constructed from
either int or unique_fd.
Test: treehugger
Change-Id: If77cf5cbcaddacdaec5919a15b3520fb68f51a62
This catches a common mistake where client code checks for errors using
the common idiom that works for std::iostream and other file-like
classes:
unique_fd fd = open(...);
if (!fd) {
}
Test: atest libbase_test
Test: m droid
Change-Id: I9629a7795537ecb3b57be9c741c06f80967e4cc2
Almost all of the uses of cmsg(3) in our source tree are wrong in at
least one of the following ways:
- not aligning the cmsg buffer
- leaking fds if more fds are received than expected
- blindly dereferencing CMSG_DATA without checking the header
- using CMSG_SPACE instead of CMSG_LEN for .cmsg_len
- using CMSG_LEN instead of CMSG_SPACE for .msg_controllen
- using a length specified in number of fds instead of bytes
Implement wrapper functions that implement this dumpster fire of an
API correctly.
Bug: http://b/122047630
Test: libbase_test32
Change-Id: I6ac34d67bbbf1bfa9727ab598248fc178ea19df9
This is a useful helper for anyone that's using thread safety
annotations alongside std::condition_variable, extract it from adb and
move it to libbase.
Test: mma
Change-Id: Ic51e2f2a0e6a16628034b29d8ff32bf2155afccd
unique_fd's destructor potentially mangling errno makes it difficult to
use correctly in code that sets errno (or, in reality, it makes it so
that errno values get randomly stomped upon if close actually sets
errno, because no one accounts for this case).
Preserve errno ourselves to avoid this.
Test: treehugger
Change-Id: Ib06e6f65866d86fff4032b2311021eaf9226a1af
Recursively delete all contents in a temporary directory in the
destructor. If the caller wants the contents preserved, then
call DoNotRemove().
Test: compile
Bug: 119313545
Change-Id: I3601c06334920055a60dd3074b249824f4d6f510
Allow a wider legitimate audience to use TemporaryFile and
TemporaryDir by moving them from android-base/test_utils.h to
android-base/file.h.
Test: compile
Bug: 119313545
Change-Id: Ie558c5873ce5b3937914918b6bfb427e5b61d0da
Using fdopendir with unique_fd correctly is more annoying than it
should be, because fdopendir doesn't close the file descriptor
received upon failure.
Add an android::base::Fdopendir that does that handles the failure
case.
Bug: 118818285
Test: treehugger
Change-Id: I5dbbe0eb82242bc7716f84735ffc7644febcfd65
Given that I can't actually go back in time and just make bionic's
`off_t` be 64-bit.
Bug: N/A
Test: builds
Change-Id: Ic93336b07bffc0977bd0e9caad6a22ce29ca06ed
This allows us to remove libziparchive's dependency on libutils.
Bug: http://b/79112958
Test: ran libbase and libziparchive tests, ran fastboot manually
Change-Id: I95c651976dad222863e5b8c37d4514b778f5dce7
Add FALLTHROUGH_INTENDED for clang compiler.
Bug: 112564944
Test: build with global -Wimplicit-fallthrough.
Change-Id: I40f8bbf94e207c9dd90921e9b762ba51abab5777
Move the fd() function to be private since it should not have been
exposed in the first place.
Fix the way logging_test uses CapturedXXX.
Adding this because the new isolated testing doesn't print errors to
stderr so the ASSERT_ EXPECT_ messages can get swallowed. Also, it's easier
to reuse a CapturedXXX object in a test with these functions.
Test: New unit tests pass.
Change-Id: I38b113fc184146ce434802f80a9b7997fa83e78a
Using fdopen with unique_fd correctly is more annoying than it should
be, because fdopen doesn't close the file descriptor received upon
failure, which means you have to something like the following:
unique_fd ufd = ...;
int fd = ufd.release();
FILE* file = fdopen(fd, "...");
if (!file) {
close(fd);
return;
}
Add an android::base::Fdopen that does that dance for you.
Bug: http://b/113880863
Test: treehugger
Change-Id: I6325acf1ff06484005c1053fe09672c5eeeecaa1
1) ParseInt and ParseUint accept strings that have spaces followed by
a valid number, however the base check for determining hex numbers
does not take this into account currently, but now does this this
change.
2) ParseUint rejects negative numbers except in the case that the
template argument is unsigned long long. That is a mistake; it
should reject negative numbers in all cases and this change does
that by rejecting any string with a - prefix.
Bug: 112668205
Test: new (and old) unit tests
Change-Id: I0179b42e50adc1b169cf0e6d830283b71210a029
I originally accidentally created this file with the execute bit on.
Test: mma
Change-Id: I8075ffdbb114475434ad48912a9dd24f19a9025b
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>