diff options
author | Dan Albert <danalbert@google.com> | 2015-03-16 10:08:46 -0700 |
---|---|---|
committer | Dan Albert <danalbert@google.com> | 2015-03-16 13:48:07 -0700 |
commit | a83ba64a238de57fa2d02c4ea20f4c8ff8ab1464 (patch) | |
tree | 0c4bf967c0cc52ccd6eca52564a1275ab2da33f2 /base | |
parent | 778b9c84b81cba056f0b9a3ba05ecd88331df00a (diff) | |
download | system_core-a83ba64a238de57fa2d02c4ea20f4c8ff8ab1464.zip system_core-a83ba64a238de57fa2d02c4ea20f4c8ff8ab1464.tar.gz system_core-a83ba64a238de57fa2d02c4ea20f4c8ff8ab1464.tar.bz2 |
Revert "Revert "Create libbase.""
This reverts commit a7870d88167f619e758b5bcd15b410d16da7c16b.
(cherry picked from commit c007bc3856a4cf86b8f610eb045f26a9dedc2894)
Diffstat (limited to 'base')
-rw-r--r-- | base/.clang-format | 11 | ||||
-rw-r--r-- | base/Android.mk | 96 | ||||
-rw-r--r-- | base/CPPLINT.cfg | 2 | ||||
-rw-r--r-- | base/file.cpp | 123 | ||||
-rw-r--r-- | base/file_test.cpp | 103 | ||||
-rw-r--r-- | base/include/base/file.h | 40 | ||||
-rw-r--r-- | base/include/base/stringprintf.h | 40 | ||||
-rw-r--r-- | base/stringprintf.cpp | 85 | ||||
-rw-r--r-- | base/stringprintf_test.cpp | 60 |
9 files changed, 560 insertions, 0 deletions
diff --git a/base/.clang-format b/base/.clang-format new file mode 100644 index 0000000..2b83a1f --- /dev/null +++ b/base/.clang-format @@ -0,0 +1,11 @@ +BasedOnStyle: Google +AllowShortBlocksOnASingleLine: false +AllowShortFunctionsOnASingleLine: false + +CommentPragmas: NOLINT:.* +DerivePointerAlignment: false +IndentWidth: 2 +PointerAlignment: Left +TabWidth: 2 +UseTab: Never +PenaltyExcessCharacter: 32 diff --git a/base/Android.mk b/base/Android.mk new file mode 100644 index 0000000..3b64ab0 --- /dev/null +++ b/base/Android.mk @@ -0,0 +1,96 @@ +# +# Copyright (C) 2015 The Android Open Source Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +LOCAL_PATH := $(call my-dir) + +libbase_src_files := \ + file.cpp \ + stringprintf.cpp \ + +libbase_test_src_files := \ + file_test.cpp \ + stringprintf_test.cpp \ + +libbase_cppflags := \ + -Wall \ + -Wextra \ + -Werror \ + +# Device +# ------------------------------------------------------------------------------ +include $(CLEAR_VARS) +LOCAL_MODULE := libbase +LOCAL_CLANG := true +LOCAL_SRC_FILES := $(libbase_src_files) +LOCAL_C_INCLUDES := $(LOCAL_PATH)/include +LOCAL_CPPFLAGS := $(libbase_cppflags) +LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include +LOCAL_MULTILIB := both +include $(BUILD_STATIC_LIBRARY) + +include $(CLEAR_VARS) +LOCAL_MODULE := libbase +LOCAL_CLANG := true +LOCAL_WHOLE_STATIC_LIBRARIES := libbase +LOCAL_SHARED_LIBRARIES := liblog +LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include +LOCAL_MULTILIB := both +include $(BUILD_SHARED_LIBRARY) + +# Host +# ------------------------------------------------------------------------------ +include $(CLEAR_VARS) +LOCAL_MODULE := libbase +LOCAL_CLANG := true +LOCAL_SRC_FILES := $(libbase_src_files) +LOCAL_C_INCLUDES := $(LOCAL_PATH)/include +LOCAL_CPPFLAGS := $(libbase_cppflags) +LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include +LOCAL_MULTILIB := both +include $(BUILD_HOST_STATIC_LIBRARY) + +include $(CLEAR_VARS) +LOCAL_MODULE := libbase +LOCAL_CLANG := true +LOCAL_WHOLE_STATIC_LIBRARIES := libbase +LOCAL_SHARED_LIBRARIES := liblog +LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include +LOCAL_MULTILIB := both +include $(BUILD_HOST_SHARED_LIBRARY) + +# Tests +# ------------------------------------------------------------------------------ +include $(CLEAR_VARS) +LOCAL_MODULE := libbase_test +LOCAL_CLANG := true +LOCAL_SRC_FILES := $(libbase_test_src_files) +LOCAL_CPPFLAGS := $(libbase_cppflags) +LOCAL_SHARED_LIBRARIES := libbase +LOCAL_MULTILIB := both +LOCAL_MODULE_STEM_32 := $(LOCAL_MODULE)32 +LOCAL_MODULE_STEM_64 := $(LOCAL_MODULE)64 +include $(BUILD_NATIVE_TEST) + +include $(CLEAR_VARS) +LOCAL_MODULE := libbase_test +LOCAL_CLANG := true +LOCAL_SRC_FILES := $(libbase_test_src_files) +LOCAL_CPPFLAGS := $(libbase_cppflags) +LOCAL_SHARED_LIBRARIES := libbase +LOCAL_MULTILIB := both +LOCAL_MODULE_STEM_32 := $(LOCAL_MODULE)32 +LOCAL_MODULE_STEM_64 := $(LOCAL_MODULE)64 +include $(BUILD_HOST_NATIVE_TEST) diff --git a/base/CPPLINT.cfg b/base/CPPLINT.cfg new file mode 100644 index 0000000..5ee068e --- /dev/null +++ b/base/CPPLINT.cfg @@ -0,0 +1,2 @@ +set noparent +filter=-build/header_guard diff --git a/base/file.cpp b/base/file.cpp new file mode 100644 index 0000000..773f33b --- /dev/null +++ b/base/file.cpp @@ -0,0 +1,123 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "base/file.h" + +#include <errno.h> +#include <fcntl.h> +#include <sys/stat.h> +#include <sys/types.h> + +#include <string> + +#define LOG_TAG "base.file" +#include "cutils/log.h" +#include "utils/Compat.h" // For TEMP_FAILURE_RETRY on Darwin. + +namespace android { +namespace base { + +bool ReadFdToString(int fd, std::string* content) { + content->clear(); + + char buf[BUFSIZ]; + ssize_t n; + while ((n = TEMP_FAILURE_RETRY(read(fd, &buf[0], sizeof(buf)))) > 0) { + content->append(buf, n); + } + return (n == 0) ? true : false; +} + +bool ReadFileToString(const std::string& path, std::string* content) { + content->clear(); + + int fd = + TEMP_FAILURE_RETRY(open(path.c_str(), O_RDONLY | O_CLOEXEC | O_NOFOLLOW)); + if (fd == -1) { + return false; + } + bool result = ReadFdToString(fd, content); + TEMP_FAILURE_RETRY(close(fd)); + return result; +} + +bool WriteStringToFd(const std::string& content, int fd) { + const char* p = content.data(); + size_t left = content.size(); + while (left > 0) { + ssize_t n = TEMP_FAILURE_RETRY(write(fd, p, left)); + if (n == -1) { + return false; + } + p += n; + left -= n; + } + return true; +} + +static bool CleanUpAfterFailedWrite(const std::string& path) { + // Something went wrong. Let's not leave a corrupt file lying around. + int saved_errno = errno; + unlink(path.c_str()); + errno = saved_errno; + return false; +} + +#if !defined(_WIN32) +bool WriteStringToFile(const std::string& content, const std::string& path, + mode_t mode, uid_t owner, gid_t group) { + int fd = TEMP_FAILURE_RETRY( + open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW, + mode)); + if (fd == -1) { + ALOGE("android::WriteStringToFile open failed: %s", strerror(errno)); + return false; + } + + // We do an explicit fchmod here because we assume that the caller really + // meant what they said and doesn't want the umask-influenced mode. + if (fchmod(fd, mode) == -1) { + ALOGE("android::WriteStringToFile fchmod failed: %s", strerror(errno)); + return CleanUpAfterFailedWrite(path); + } + if (fchown(fd, owner, group) == -1) { + ALOGE("android::WriteStringToFile fchown failed: %s", strerror(errno)); + return CleanUpAfterFailedWrite(path); + } + if (!WriteStringToFd(content, fd)) { + ALOGE("android::WriteStringToFile write failed: %s", strerror(errno)); + return CleanUpAfterFailedWrite(path); + } + TEMP_FAILURE_RETRY(close(fd)); + return true; +} +#endif + +bool WriteStringToFile(const std::string& content, const std::string& path) { + int fd = TEMP_FAILURE_RETRY( + open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW, + DEFFILEMODE)); + if (fd == -1) { + return false; + } + + bool result = WriteStringToFd(content, fd); + TEMP_FAILURE_RETRY(close(fd)); + return result || CleanUpAfterFailedWrite(path); +} + +} // namespace base +} // namespace android diff --git a/base/file_test.cpp b/base/file_test.cpp new file mode 100644 index 0000000..34b8755 --- /dev/null +++ b/base/file_test.cpp @@ -0,0 +1,103 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "base/file.h" + +#include <gtest/gtest.h> + +#include <errno.h> +#include <fcntl.h> +#include <unistd.h> + +#include <string> + +class TemporaryFile { + public: + TemporaryFile() { + init("/data/local/tmp"); + if (fd == -1) { + init("/tmp"); + } + } + + ~TemporaryFile() { + close(fd); + unlink(filename); + } + + int fd; + char filename[1024]; + + private: + void init(const char* tmp_dir) { + snprintf(filename, sizeof(filename), "%s/TemporaryFile-XXXXXX", tmp_dir); + fd = mkstemp(filename); + } +}; + +TEST(file, ReadFileToString_ENOENT) { + std::string s("hello"); + errno = 0; + ASSERT_FALSE(android::base::ReadFileToString("/proc/does-not-exist", &s)); + EXPECT_EQ(ENOENT, errno); + EXPECT_EQ("", s); // s was cleared. +} + +TEST(file, ReadFileToString_success) { + std::string s("hello"); + ASSERT_TRUE(android::base::ReadFileToString("/proc/version", &s)) << errno; + EXPECT_GT(s.length(), 6U); + EXPECT_EQ('\n', s[s.length() - 1]); + s[5] = 0; + EXPECT_STREQ("Linux", s.c_str()); +} + +TEST(file, WriteStringToFile) { + TemporaryFile tf; + ASSERT_TRUE(tf.fd != -1); + ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.filename)) << errno; + std::string s; + ASSERT_TRUE(android::base::ReadFileToString(tf.filename, &s)) << errno; + EXPECT_EQ("abc", s); +} + +TEST(file, WriteStringToFile2) { + TemporaryFile tf; + ASSERT_TRUE(tf.fd != -1); + ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.filename, 0660, + getuid(), getgid())) + << errno; + struct stat sb; + ASSERT_EQ(0, stat(tf.filename, &sb)); + ASSERT_EQ(0660U, (sb.st_mode & ~S_IFMT)); + ASSERT_EQ(getuid(), sb.st_uid); + ASSERT_EQ(getgid(), sb.st_gid); + std::string s; + ASSERT_TRUE(android::base::ReadFileToString(tf.filename, &s)) << errno; + EXPECT_EQ("abc", s); +} + +TEST(file, WriteStringToFd) { + TemporaryFile tf; + ASSERT_TRUE(tf.fd != -1); + ASSERT_TRUE(android::base::WriteStringToFd("abc", tf.fd)); + + ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << errno; + + std::string s; + ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s)) << errno; + EXPECT_EQ("abc", s); +} diff --git a/base/include/base/file.h b/base/include/base/file.h new file mode 100644 index 0000000..ef97742 --- /dev/null +++ b/base/include/base/file.h @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef BASE_FILE_H +#define BASE_FILE_H + +#include <sys/stat.h> +#include <string> + +namespace android { +namespace base { + +bool ReadFdToString(int fd, std::string* content); +bool ReadFileToString(const std::string& path, std::string* content); + +bool WriteStringToFile(const std::string& content, const std::string& path); +bool WriteStringToFd(const std::string& content, int fd); + +#if !defined(_WIN32) +bool WriteStringToFile(const std::string& content, const std::string& path, + mode_t mode, uid_t owner, gid_t group); +#endif + +} // namespace base +} // namespace android + +#endif // BASE_FILE_H diff --git a/base/include/base/stringprintf.h b/base/include/base/stringprintf.h new file mode 100644 index 0000000..195c1de --- /dev/null +++ b/base/include/base/stringprintf.h @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef BASE_STRINGPRINTF_H +#define BASE_STRINGPRINTF_H + +#include <stdarg.h> +#include <string> + +namespace android { +namespace base { + +// Returns a string corresponding to printf-like formatting of the arguments. +std::string StringPrintf(const char* fmt, ...) + __attribute__((__format__(__printf__, 1, 2))); + +// Appends a printf-like formatting of the arguments to 'dst'. +void StringAppendF(std::string* dst, const char* fmt, ...) + __attribute__((__format__(__printf__, 2, 3))); + +// Appends a printf-like formatting of the arguments to 'dst'. +void StringAppendV(std::string* dst, const char* format, va_list ap); + +} // namespace base +} // namespace android + +#endif // BASE_STRINGPRINTF_H diff --git a/base/stringprintf.cpp b/base/stringprintf.cpp new file mode 100644 index 0000000..d55ff52 --- /dev/null +++ b/base/stringprintf.cpp @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "base/stringprintf.h" + +#include <stdio.h> + +#include <string> + +namespace android { +namespace base { + +void StringAppendV(std::string* dst, const char* format, va_list ap) { + // First try with a small fixed size buffer + char space[1024]; + + // It's possible for methods that use a va_list to invalidate + // the data in it upon use. The fix is to make a copy + // of the structure before using it and use that copy instead. + va_list backup_ap; + va_copy(backup_ap, ap); + int result = vsnprintf(space, sizeof(space), format, backup_ap); + va_end(backup_ap); + + if (result < static_cast<int>(sizeof(space))) { + if (result >= 0) { + // Normal case -- everything fit. + dst->append(space, result); + return; + } + + if (result < 0) { + // Just an error. + return; + } + } + + // Increase the buffer size to the size requested by vsnprintf, + // plus one for the closing \0. + int length = result + 1; + char* buf = new char[length]; + + // Restore the va_list before we use it again + va_copy(backup_ap, ap); + result = vsnprintf(buf, length, format, backup_ap); + va_end(backup_ap); + + if (result >= 0 && result < length) { + // It fit + dst->append(buf, result); + } + delete[] buf; +} + +std::string StringPrintf(const char* fmt, ...) { + va_list ap; + va_start(ap, fmt); + std::string result; + StringAppendV(&result, fmt, ap); + va_end(ap); + return result; +} + +void StringAppendF(std::string* dst, const char* format, ...) { + va_list ap; + va_start(ap, format); + StringAppendV(dst, format, ap); + va_end(ap); +} + +} // namespace base +} // namespace android diff --git a/base/stringprintf_test.cpp b/base/stringprintf_test.cpp new file mode 100644 index 0000000..5cc2086 --- /dev/null +++ b/base/stringprintf_test.cpp @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "base/stringprintf.h" + +#include <gtest/gtest.h> + +#include <string> + +TEST(StringPrintfTest, HexSizeT) { + size_t size = 0x00107e59; + EXPECT_EQ("00107e59", android::base::StringPrintf("%08zx", size)); + EXPECT_EQ("0x00107e59", android::base::StringPrintf("0x%08zx", size)); +} + +TEST(StringPrintfTest, StringAppendF) { + std::string s("a"); + android::base::StringAppendF(&s, "b"); + EXPECT_EQ("ab", s); +} + +TEST(StringPrintfTest, Errno) { + errno = 123; + android::base::StringPrintf("hello %s", "world"); + EXPECT_EQ(123, errno); +} + +void TestN(size_t n) { + char* buf = new char[n + 1]; + memset(buf, 'x', n); + buf[n] = '\0'; + std::string s(android::base::StringPrintf("%s", buf)); + EXPECT_EQ(buf, s); + delete[] buf; +} + +TEST(StringPrintfTest, At1023) { + TestN(1023); +} + +TEST(StringPrintfTest, At1024) { + TestN(1024); +} + +TEST(StringPrintfTest, At1025) { + TestN(1025); +} |