diff options
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, 0 insertions, 560 deletions
diff --git a/base/.clang-format b/base/.clang-format deleted file mode 100644 index 2b83a1f..0000000 --- a/base/.clang-format +++ /dev/null @@ -1,11 +0,0 @@ -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 deleted file mode 100644 index 3b64ab0..0000000 --- a/base/Android.mk +++ /dev/null @@ -1,96 +0,0 @@ -# -# 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 deleted file mode 100644 index 5ee068e..0000000 --- a/base/CPPLINT.cfg +++ /dev/null @@ -1,2 +0,0 @@ -set noparent -filter=-build/header_guard diff --git a/base/file.cpp b/base/file.cpp deleted file mode 100644 index 773f33b..0000000 --- a/base/file.cpp +++ /dev/null @@ -1,123 +0,0 @@ -/* - * 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 deleted file mode 100644 index 34b8755..0000000 --- a/base/file_test.cpp +++ /dev/null @@ -1,103 +0,0 @@ -/* - * 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 deleted file mode 100644 index ef97742..0000000 --- a/base/include/base/file.h +++ /dev/null @@ -1,40 +0,0 @@ -/* - * 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 deleted file mode 100644 index 195c1de..0000000 --- a/base/include/base/stringprintf.h +++ /dev/null @@ -1,40 +0,0 @@ -/* - * 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 deleted file mode 100644 index d55ff52..0000000 --- a/base/stringprintf.cpp +++ /dev/null @@ -1,85 +0,0 @@ -/* - * 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 deleted file mode 100644 index 5cc2086..0000000 --- a/base/stringprintf_test.cpp +++ /dev/null @@ -1,60 +0,0 @@ -/* - * 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); -} |