Commit graph

352 commits

Author SHA1 Message Date
Daniel Colascione
9e3cbb64ee Expose ParseBool from libbase
Also change the properties implementation to call the new API.  We use
this ParseBool API in the new SystemProperties implementation, with
which we want the libbase property API to be consistent.

Test: included
Change-Id: I89cb3eb4e1203a6bb0da41914dad720e44c00303
2019-11-19 13:31:50 -08:00
Elliott Hughes
908e0dfda5 Add absl-like StringReplace.
Needed for cli-test.

Test: treehugger
Change-Id: Ib1fd01ef7f3e54e5778cc548dd789b5fcfcb7bd9
2019-11-18 16:02:31 -08:00
Elliott Hughes
10dc4ca3cd libbase: add a README.md covering the most frequent question.
Test: N/A
Change-Id: Iebea3e74bb7d59778fa0d2342e51bb4ffc5450a3
2019-11-06 09:38:21 -08:00
Elliott Hughes
2d70aad034 Remove Mac/Windows quick_exit() implementation.
This was added with the intention of using it in adb, but then the
change that would have used it
(https://android-review.googlesource.com/c/platform/system/core/+/273824)
was abandoned.

Remove the corresponding (never used) implementation.

Bug: http://b/31468413
Test: treehugger
Change-Id: I42322d079c175b7c6fbd12940e5bc022bd9ebd1c
2019-10-29 13:30:43 -07:00
Alex Buynytskyy
0c2819fc9d Addressing comments in 1130236.
Use Unicode versions of mktemp function.
Correctly handle unset TMP variable in tests.

Test: atest libbase_test
Change-Id: I5009962d33b224c4d5f39102e03daeb0837f1525
2019-10-02 16:48:22 -07:00
Alex Buynytskyy
43f0b0c4ba Using WideChar->UTF8 versions of Windows API to obtain temp folder.
This will correctly handle non-ascii chars in paths.
+Windows specific tests for non-unicode chars and root of disk as a temp
folder.

Test: atest libbase_test

Change-Id: Ief3ee26df93e122250441bfe41f0440fe62bfadc
2019-10-02 02:22:23 +00:00
Bowgo Tsai
41d8a576ec <android-base/endian.h>: fix interoperability issue
<winsock2.h> has defined htonl(), ntohl(), htons() and ntohs() with
different return type that cannot be replaced by those macro definitions
in <android-base/endian.h>.

Includes <winsock2.h> first to prevent them from being replaced.
Then defines the macro later so we don't need to call into DLL
when using those functions.

Bug: 139639521
Test: m libbase_test && wine out/host/windows-x86/nativetest64/libbase_test/libbase_test64.exe
Test: m checkbuild
Change-Id: I672f23a31c6800df10e04d36695d446bca4c91e9
2019-09-18 13:06:45 +08:00
Paul Crowley
b3236ebab6 Remove -D_FILE_OFFSET_BITS=64 on host builds
This prevents us from using glibc's <fts.h> because of
https://sourceware.org/bugzilla/show_bug.cgi?id=11460,
and we now only support 64-bit hosts.

Bug: 139544611
Test: Treehugger
Change-Id: Iaacc9ea17473201bf3d701e82081264921062d5e
2019-09-12 00:27:38 +00:00
Daniel Colascione
7c706baedb Improve codegen slightly when doing FD validity checks
See https://gcc.godbolt.org/z/hGUZIk

Test: inspection
Bug: 140506797
Change-Id: I6fe87b16e18c93b7fc3ca28edbbc68c245e7e5e5
2019-09-04 13:46:50 -07:00
Haibo Huang
f4c3471d22 Include chrono.h
Upstream merged time.h into chrono.h

Test: build
Change-Id: Iadbdfa4902ddd2dd38ee1dba51ef59def54130e4
2019-08-27 20:38:14 +00:00
Yurii Zubrytskyi
5f0e5e0ecc Merge "[base] Make MappedFile work with OS file handles" 2019-08-15 16:50:31 +00:00
Yurii Zubrytskyi
d77c99ebc3 [base] Make MappedFile work with OS file handles
adb uses its own implementation of Windows HANDLE->int mapping,
and it doesn't play well with _get_osfhandle() function used in
MappedFile::FromFd().

This CL adds another function that accepts raw handle, so adb
can pass it directly

+ make constant functions 'const'
+ make the MappedFile movable, as nothing prevents it from being
  one

Test: libbase_test

Change-Id: Ifde4a4094b910e9c7b431126ecf3fef5aa3bb4a6
2019-08-14 10:04:32 -07:00
Colin Cross
888c1b9de5 Fix more endian.h issues
__BIONIC__ is defined in sys/cdefs.h, __GLIBC__ is defined in
features.h (which is included from sys/cdefs.h).  If sys/cdefs.h
was not included before android-base/endian.h it was always falling
back to the Windows definitions.

mingw defines LITTLE_ENDIAN, BIG_ENDIAN and BYTE_ORDER in
sys/params.h, use those definitions to avoid conflicts.

glibc uses different names for letoh*, add compatibily #defines.

Test: m checkbuild
Change-Id: I0709a964cc8f20dd9fa4f03064cc67d97ae6c525
2019-08-13 15:27:00 -07:00
Elliott Hughes
f86ea02de6 Use as much of the macOS endian support as we can.
Turns out that although there's no <endian.h> or <sys/endian.h>, there
are <machine/endian.h> and <sys/_endian.h>, and they're included by
other system headers such as <dirent.h>.

Reuse the contents of <sys/_endian.h> here for better interop.

Bug: http://b/139203733
Test: treehugger
Change-Id: Ic0e9bfa1a5b56d05e9e542839d237b6ceae4aa8c
2019-08-12 12:11:42 -07:00
Anatol Pomazau
fd41f9a579 Add header that defines strerror()
It helps to fix a compilation issue with host libc:

core/base/errors_unix.cpp:25:10: error: use of undeclared identifier 'strerror'
  return strerror(error_code);
         ^

Change-Id: I71d4221cc1bfa6f0cc764b40b6b28159994b8e04
2019-08-08 23:31:36 +00:00
Tomasz Wasilczyk
c9ec95272c Merge "Implement unique_fd.ok()" 2019-07-24 13:51:43 +00:00
Tomasz Wasilczyk
c3fc9607c2 Implement unique_fd.ok()
While I'm here, I'll fix how unique_fd disallows copy and assignment
constructors (detele instead of marking them private).

Bug: 135918744
Test: WiP change in master
Change-Id: Idefcc685943326c511f59d18790c1c4fa2e04989
2019-07-23 16:35:38 -07:00
Treehugger Robot
8987f061c4 Merge "Add nolint for implicit borrowed_fd constructors" 2019-07-16 04:11:20 +00:00
Tom Cherry
aa55ee6044 Add nolint for implicit borrowed_fd constructors
bpfloader uses both clang-tidy and this header, so the below lint
warnings are generated:

/work/aosp/system/core/base/include/android-base/unique_fd.h:261:18:
  warning: single-argument constructors must be marked explicit to
  avoid unintentional implicit conversions
  [google-explicit-constructor]
  /* implicit */ borrowed_fd(int fd) : fd_(fd) {}
                 ^
                 explicit

/work/aosp/system/core/base/include/android-base/unique_fd.h:263:18:
  warning: single-argument constructors must be marked explicit to
  avoid unintentional implicit conversions
  [google-explicit-constructor]
  /* implicit */ borrowed_fd(const unique_fd_impl<T>& ufd) : fd_(ufd.get()) {}
                 ^
                 explicit

Add NOLINT to quiet them.

Test: build without this lint warning
Change-Id: I5241938c33070b0fa39888289b8ca67d6d94ac73
2019-07-15 15:00:23 -07:00
Tom Cherry
820333dce4 Add noexcept to missing places in expected.h
These move and assignment operations are conditionally noexcept, so
add that to their definitions.  This is a performance issue as well as
correctness, since std containers will copy instead of move during
resize if these operations are not noexcept.

Test: build
Change-Id: I148f7eb3489e7f1dd68cc0fb0e555b56470e42da
2019-07-09 17:06:08 -07:00
Tom Cherry
a93e09f834 Merge "Don't implement our own remove_cvref_t." 2019-07-09 20:10:22 +00:00
Tom Cherry
a2928b52ff Don't implement our own remove_cvref_t.
clang-tidy flagged the anonymous namespace in the header as a warning,
but in any case, it'll be cleaner to implement this in terms of the
existing type traits.

Test: build
Change-Id: I189986d2a855c028e28dd9d62ab9da012feddc9b
2019-07-09 09:55:47 -07:00
Tom Cherry
247ffbf314 Fix a few clang-tidy issues and add NOLINT for others
android-base:
* Add NOLINT for expanding namespace std for std::string* ostream
  overload

libdm:
* Fix missing parentesis around macro parameters

init:
* Fix missing CLOEXEC usage and add NOLINT for the intended
  usages.
* Fix missing parentesis around macro parameters
* Fix erase() / remove_if() idiom
* Correctly specific unsigned char when intended
* 'namespace flags' should be signed, since 'flags' it signed for
  clone()
* Add clear to property restore vector<string> to empty after move
* Explicit comparison against 0 for strcmp

Test: build
Change-Id: I8c31dafda2c43ebc5aa50124cbbd6e23ed2c4101
2019-07-09 16:17:36 +00:00
Tom Cherry
13b1f34279 Add android::base::GetPids() function to return all pids
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
2019-07-01 13:55:46 -07:00
Mohammad Samiul Islam
9f24c38fe3 Prevent definitions of expected.h from being included twice
Bug: 135683564
Test: Included expected.h from two different files. Builds succesfully
    with the fix. Does not build without the fix.

Change-Id: If1962a3b70f7580fa652a043aaf29bf592d1926c
2019-06-20 19:03:32 +01:00
Tom Cherry
b90dcc07d3 Create android-base/format.h that includes fmtlib headers
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
2019-06-14 14:55:27 -07:00
Jiyong Park
20524ed6a2 Add Errorf and ErrnoErrorf
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
2019-06-14 14:29:36 +09:00
Jooyung Han
df7d32753e fix: passing ResultError to Error thru operator<<
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
2019-06-11 13:38:01 +09:00
Tom Cherry
91e6f888d7 Remove Result<Success> in favor of Result<void>
Result<void> is the correct way to do this.

Bug: 132145659
Test: libbase and init unit tests
Change-Id: I9c2e16cee6657a1895f215016df41a8b290383c7
2019-06-10 12:53:15 -07:00
Tom Cherry
383e1418ff base: Add MmapFile as a synonym for MappedFile
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
2019-06-07 13:05:22 -07:00
Treehugger Robot
bb1b83e325 Merge "Add expected<void,E>" 2019-06-07 07:17:47 +00:00
Treehugger Robot
2ba59c5b81 Merge "Move result.h from init to libbase" 2019-06-06 23:27:29 +00:00
Jiyong Park
b2a85c4333 Add expected<void,E>
Result<void> could be used instead of Result<Success>.

Bug: 132145659
Test: libbase_test
Change-Id: I7f079a94d06c5ecb8b0303ced981f0995253dc40
2019-06-07 08:10:32 +09:00
Jiyong Park
8fd64c8af1 Move result.h from init to libbase
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
2019-06-06 08:58:55 +09:00
Treehugger Robot
44cabca1ef Merge "base: fix out of date documentation with PropertySet()" 2019-06-05 21:24:23 +00:00
Tom Cherry
ef2469e547 base: fix out of date documentation with PropertySet()
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
2019-06-05 10:31:33 -07:00
Treehugger Robot
41f690ba2a Merge "Fix leak of mapped zip central directories" 2019-06-04 12:36:52 +00:00
Jiyong Park
5c06e6e17b Minor fixes on android::base::expected
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
2019-05-29 08:37:32 +09:00
Ryan Mitchell
ce1c49da6f Fix leak of mapped zip central directories
Memory mapping the central directory of specific APKs caused memory
mappings to build up over time because they were never unmapped
correctly. This is because MappedFile is not calling munmap with the
size of the data after aligning the mmap offset to a page boundary.

Bug: 133463863
Test: install APKs and verify that the entire mapped CD is unmaped
Test: ran aapt2 as daemon and confirmed that mapped CD is unmapped
Change-Id: I26bda27c83615d1a166d92e43a2327909c453b89
2019-05-28 22:59:53 +00:00
Jiyong Park
7d89fb164b Add android::base::expected
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
2019-05-22 06:39:27 +09:00
Josh Gao
fe1d1e815f Merge "Revert "Revert "Add a way to turn off unique_fd's operator int.""" 2019-05-09 23:09:38 +00:00
Josh Gao
27241a7a96 Revert "Revert "Add a way to turn off unique_fd's operator int.""
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
2019-05-06 16:18:02 -07:00
dimitry
a808b1150a Enable native_bridge_support
Enable native bridge support for libbase, liblog,
libziparchive and libpropertyinfoparser.

This makes it possible to use them in binaries for translated
architectures.

Bug: http://b/77159578
Test: make
Change-Id: If67ce92288b17a052ea1e79a268e284f7d941439
2019-05-06 14:05:05 +02:00
Elliott Hughes
b4dc7be6c5 libbase: add ConsumePrefix/ConsumeSuffix.
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
2019-05-03 12:49:31 -07:00
Ian Kasprzak
2c58e1924a Revert "Add a way to turn off unique_fd's operator int."
This reverts commit 93d63c010a.

Reason for revert: b/131312539 - All aosp-master-with-phones phones failing boot / health check.

Change-Id: Id4239f93484b7d6b8d9bcd355a59cbd40d9766e2
2019-04-25 15:59:28 +00:00
Josh Gao
93d63c010a Add a way to turn off unique_fd's operator int.
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
2019-04-24 12:59:42 -07:00
Bernie Innocenti
7cb72c96b4 Disallow operator!() on unique_fd
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
2019-03-28 15:56:07 +09:00
Yifan Hong
f959fffc1c Merge changes from topic "lpdumpd"
* changes:
  libbase: realpath is wrapped with TEMP_FAILURE_RETRY
  liblp: Replace open with GetControlFileOrOpen
  init: expand prop in 'file'
  libcutils: android_get_control_file uses realpath.
2019-03-26 20:35:36 +00:00
Yifan Hong
5f4cb8a240 libbase: realpath is wrapped with TEMP_FAILURE_RETRY
Although man page for realpath doesn't say so, the bionic
implementation of realpath may exit with error code
EINTR. In such cases, retry.

Test: boots (sanity)
Change-Id: Ic5feb7152374a81bd2f475ad74c4bc8c3afb3a20
2019-03-22 14:26:25 -07:00
Yurii Zubrytskyi
59d876c7a2 [base] Convert Starts/Ends/Equals string functions to string_view
Test: UTs

Change-Id: I3751656d44e7a814488169c53d3e8af0da44f262
2019-03-21 16:29:46 +00:00