am dd581695
: Merge "Revert "Create libbase.""
* commit 'dd5816953de6aebc51aa4cc6f69945fbb9cd4b2a': Revert "Create libbase."
This commit is contained in:
commit
90a968f0e3
17 changed files with 83 additions and 213 deletions
|
@ -90,7 +90,7 @@ LOCAL_MODULE := adbd_test
|
|||
LOCAL_CFLAGS := -DADB_HOST=0 $(LIBADB_CFLAGS)
|
||||
LOCAL_SRC_FILES := $(LIBADB_TEST_SRCS)
|
||||
LOCAL_STATIC_LIBRARIES := libadbd
|
||||
LOCAL_SHARED_LIBRARIES := liblog libbase libcutils
|
||||
LOCAL_SHARED_LIBRARIES := liblog libcutils libutils
|
||||
include $(BUILD_NATIVE_TEST)
|
||||
|
||||
include $(CLEAR_VARS)
|
||||
|
@ -98,11 +98,12 @@ LOCAL_CLANG := $(ADB_CLANG)
|
|||
LOCAL_MODULE := adb_test
|
||||
LOCAL_CFLAGS := -DADB_HOST=1 $(LIBADB_CFLAGS)
|
||||
LOCAL_SRC_FILES := $(LIBADB_TEST_SRCS) services.cpp
|
||||
LOCAL_SHARED_LIBRARIES := liblog libbase
|
||||
LOCAL_SHARED_LIBRARIES := liblog
|
||||
LOCAL_STATIC_LIBRARIES := \
|
||||
libadb \
|
||||
libcrypto_static \
|
||||
libcutils \
|
||||
libutils \
|
||||
|
||||
ifeq ($(HOST_OS),linux)
|
||||
LOCAL_LDLIBS += -lrt -ldl -lpthread
|
||||
|
|
|
@ -24,7 +24,7 @@
|
|||
|
||||
#include <string>
|
||||
|
||||
#include "base/file.h"
|
||||
#include "utils/file.h"
|
||||
|
||||
class TemporaryFile {
|
||||
public:
|
||||
|
@ -55,7 +55,7 @@ TEST(io, ReadFdExactly_whole) {
|
|||
TemporaryFile tf;
|
||||
ASSERT_NE(-1, tf.fd);
|
||||
|
||||
ASSERT_TRUE(android::base::WriteStringToFd(expected, tf.fd)) << strerror(errno);
|
||||
ASSERT_TRUE(android::WriteStringToFd(expected, tf.fd)) << strerror(errno);
|
||||
ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
|
||||
|
||||
// Test reading the whole file.
|
||||
|
@ -69,7 +69,7 @@ TEST(io, ReadFdExactly_eof) {
|
|||
TemporaryFile tf;
|
||||
ASSERT_NE(-1, tf.fd);
|
||||
|
||||
ASSERT_TRUE(android::base::WriteStringToFd(expected, tf.fd)) << strerror(errno);
|
||||
ASSERT_TRUE(android::WriteStringToFd(expected, tf.fd)) << strerror(errno);
|
||||
ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
|
||||
|
||||
// Test that not having enough data will fail.
|
||||
|
@ -83,7 +83,7 @@ TEST(io, ReadFdExactly_partial) {
|
|||
TemporaryFile tf;
|
||||
ASSERT_NE(-1, tf.fd);
|
||||
|
||||
ASSERT_TRUE(android::base::WriteStringToFd(input, tf.fd)) << strerror(errno);
|
||||
ASSERT_TRUE(android::WriteStringToFd(input, tf.fd)) << strerror(errno);
|
||||
ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
|
||||
|
||||
// Test reading a partial file.
|
||||
|
@ -106,7 +106,7 @@ TEST(io, WriteFdExactly_whole) {
|
|||
ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
|
||||
|
||||
std::string s;
|
||||
ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s));
|
||||
ASSERT_TRUE(android::ReadFdToString(tf.fd, &s));
|
||||
EXPECT_STREQ(expected, s.c_str());
|
||||
}
|
||||
|
||||
|
@ -123,7 +123,7 @@ TEST(io, WriteFdExactly_partial) {
|
|||
expected.pop_back();
|
||||
|
||||
std::string s;
|
||||
ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s));
|
||||
ASSERT_TRUE(android::ReadFdToString(tf.fd, &s));
|
||||
EXPECT_EQ(expected, s);
|
||||
}
|
||||
|
||||
|
@ -137,6 +137,6 @@ TEST(io, WriteStringFully) {
|
|||
ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
|
||||
|
||||
std::string s;
|
||||
ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s));
|
||||
ASSERT_TRUE(android::ReadFdToString(tf.fd, &s));
|
||||
EXPECT_STREQ(str, s.c_str());
|
||||
}
|
||||
|
|
|
@ -1,11 +0,0 @@
|
|||
BasedOnStyle: Google
|
||||
AllowShortBlocksOnASingleLine: false
|
||||
AllowShortFunctionsOnASingleLine: false
|
||||
|
||||
CommentPragmas: NOLINT:.*
|
||||
DerivePointerAlignment: false
|
||||
IndentWidth: 2
|
||||
PointerAlignment: Left
|
||||
TabWidth: 2
|
||||
UseTab: Never
|
||||
PenaltyExcessCharacter: 32
|
|
@ -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)
|
|
@ -1,2 +0,0 @@
|
|||
set noparent
|
||||
filter=-build/header_guard
|
|
@ -14,14 +14,13 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef BASE_FILE_H
|
||||
#define BASE_FILE_H
|
||||
#ifndef UTILS_FILE_H
|
||||
#define UTILS_FILE_H
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <string>
|
||||
#include <sys/stat.h>
|
||||
|
||||
namespace android {
|
||||
namespace base {
|
||||
|
||||
bool ReadFdToString(int fd, std::string* content);
|
||||
bool ReadFileToString(const std::string& path, std::string* content);
|
||||
|
@ -34,7 +33,6 @@ bool WriteStringToFile(const std::string& content, const std::string& path,
|
|||
mode_t mode, uid_t owner, gid_t group);
|
||||
#endif
|
||||
|
||||
} // namespace base
|
||||
} // namespace android
|
||||
} // namespace android
|
||||
|
||||
#endif // BASE_FILE_H
|
||||
#endif
|
|
@ -14,27 +14,25 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef BASE_STRINGPRINTF_H
|
||||
#define BASE_STRINGPRINTF_H
|
||||
#ifndef UTILS_STRINGPRINTF_H_
|
||||
#define UTILS_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)));
|
||||
__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)));
|
||||
__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
|
||||
#endif
|
|
@ -27,7 +27,6 @@ LOCAL_SRC_FILES:= \
|
|||
parser.cpp \
|
||||
util.cpp \
|
||||
|
||||
LOCAL_STATIC_LIBRARIES := libbase
|
||||
LOCAL_MODULE := libinit
|
||||
include $(BUILD_STATIC_LIBRARY)
|
||||
|
||||
|
@ -56,7 +55,7 @@ LOCAL_STATIC_LIBRARIES := \
|
|||
libfs_mgr \
|
||||
liblogwrap \
|
||||
libcutils \
|
||||
libbase \
|
||||
libutils \
|
||||
liblog \
|
||||
libc \
|
||||
libselinux \
|
||||
|
@ -81,7 +80,7 @@ LOCAL_SRC_FILES := \
|
|||
|
||||
LOCAL_SHARED_LIBRARIES += \
|
||||
libcutils \
|
||||
libbase \
|
||||
libutils \
|
||||
|
||||
LOCAL_STATIC_LIBRARIES := libinit
|
||||
include $(BUILD_NATIVE_TEST)
|
||||
|
|
|
@ -32,7 +32,7 @@
|
|||
|
||||
#include <string>
|
||||
|
||||
#include <base/file.h>
|
||||
#include <utils/file.h>
|
||||
|
||||
#define LOG_ROOT "/data/bootchart"
|
||||
#define LOG_STAT LOG_ROOT"/proc_stat.log"
|
||||
|
@ -59,7 +59,7 @@ static FILE* log_disks;
|
|||
|
||||
static long long get_uptime_jiffies() {
|
||||
std::string uptime;
|
||||
if (!android::base::ReadFileToString("/proc/uptime", &uptime)) {
|
||||
if (!android::ReadFileToString("/proc/uptime", &uptime)) {
|
||||
return 0;
|
||||
}
|
||||
return 100LL * strtod(uptime.c_str(), NULL);
|
||||
|
@ -82,7 +82,7 @@ static void log_header() {
|
|||
}
|
||||
|
||||
std::string kernel_cmdline;
|
||||
android::base::ReadFileToString("/proc/cmdline", &kernel_cmdline);
|
||||
android::ReadFileToString("/proc/cmdline", &kernel_cmdline);
|
||||
|
||||
FILE* out = fopen(LOG_HEADER, "we");
|
||||
if (out == NULL) {
|
||||
|
@ -106,7 +106,7 @@ static void do_log_file(FILE* log, const char* procfile) {
|
|||
do_log_uptime(log);
|
||||
|
||||
std::string content;
|
||||
if (android::base::ReadFileToString(procfile, &content)) {
|
||||
if (android::ReadFileToString(procfile, &content)) {
|
||||
fprintf(log, "%s\n", content.c_str());
|
||||
}
|
||||
}
|
||||
|
@ -127,13 +127,13 @@ static void do_log_procs(FILE* log) {
|
|||
// name from /proc/<pid>/cmdline.
|
||||
snprintf(filename, sizeof(filename), "/proc/%d/cmdline", pid);
|
||||
std::string cmdline;
|
||||
android::base::ReadFileToString(filename, &cmdline);
|
||||
android::ReadFileToString(filename, &cmdline);
|
||||
const char* full_name = cmdline.c_str(); // So we stop at the first NUL.
|
||||
|
||||
// Read process stat line.
|
||||
snprintf(filename, sizeof(filename), "/proc/%d/stat", pid);
|
||||
std::string stat;
|
||||
if (android::base::ReadFileToString(filename, &stat)) {
|
||||
if (android::ReadFileToString(filename, &stat)) {
|
||||
if (!cmdline.empty()) {
|
||||
// Substitute the process name with its real name.
|
||||
size_t open = stat.find('(');
|
||||
|
@ -155,7 +155,7 @@ static int bootchart_init() {
|
|||
int timeout = 0;
|
||||
|
||||
std::string start;
|
||||
android::base::ReadFileToString(LOG_STARTFILE, &start);
|
||||
android::ReadFileToString(LOG_STARTFILE, &start);
|
||||
if (!start.empty()) {
|
||||
timeout = atoi(start.c_str());
|
||||
} else {
|
||||
|
@ -164,7 +164,7 @@ static int bootchart_init() {
|
|||
// timeout. this is useful when using -wipe-data since the /data
|
||||
// partition is fresh.
|
||||
std::string cmdline;
|
||||
android::base::ReadFileToString("/proc/cmdline", &cmdline);
|
||||
android::ReadFileToString("/proc/cmdline", &cmdline);
|
||||
#define KERNEL_OPTION "androidboot.bootchart="
|
||||
if (strstr(cmdline.c_str(), KERNEL_OPTION) != NULL) {
|
||||
timeout = atoi(cmdline.c_str() + sizeof(KERNEL_OPTION) - 1);
|
||||
|
@ -226,7 +226,7 @@ static int bootchart_step() {
|
|||
|
||||
// Stop if /data/bootchart/stop contains 1.
|
||||
std::string stop;
|
||||
if (android::base::ReadFileToString(LOG_STOPFILE, &stop) && stop == "1") {
|
||||
if (android::ReadFileToString(LOG_STOPFILE, &stop) && stop == "1") {
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
|
|
@ -30,6 +30,8 @@
|
|||
#include <cutils/sockets.h>
|
||||
#include <cutils/multiuser.h>
|
||||
|
||||
#include <utils/file.h>
|
||||
|
||||
#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
|
||||
#include <sys/_system_properties.h>
|
||||
|
||||
|
|
|
@ -32,11 +32,11 @@
|
|||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
|
||||
#include <base/file.h>
|
||||
|
||||
/* for ANDROID_SOCKET_* */
|
||||
#include <cutils/sockets.h>
|
||||
|
||||
#include <utils/file.h>
|
||||
|
||||
#include <private/android_filesystem_config.h>
|
||||
|
||||
#include "init.h"
|
||||
|
@ -168,7 +168,7 @@ bool read_file(const char* path, std::string* content) {
|
|||
return false;
|
||||
}
|
||||
|
||||
bool okay = android::base::ReadFdToString(fd, content);
|
||||
bool okay = android::ReadFdToString(fd, content);
|
||||
TEMP_FAILURE_RETRY(close(fd));
|
||||
if (okay) {
|
||||
content->append("\n", 1);
|
||||
|
@ -181,7 +181,7 @@ int write_file(const char* path, const char* content) {
|
|||
if (fd == -1) {
|
||||
return -errno;
|
||||
}
|
||||
int result = android::base::WriteStringToFd(content, fd) ? 0 : -errno;
|
||||
int result = android::WriteStringToFd(content, fd) ? 0 : -errno;
|
||||
TEMP_FAILURE_RETRY(close(fd));
|
||||
return result;
|
||||
}
|
||||
|
|
|
@ -39,7 +39,9 @@ commonSources:= \
|
|||
Tokenizer.cpp \
|
||||
Unicode.cpp \
|
||||
VectorImpl.cpp \
|
||||
file.cpp \
|
||||
misc.cpp \
|
||||
stringprintf.cpp \
|
||||
|
||||
host_commonCflags := -DLIBUTILS_NATIVE=1 $(TOOL_CFLAGS) -Werror
|
||||
|
||||
|
|
|
@ -14,23 +14,19 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "base/file.h"
|
||||
#define LOG_TAG "utils.file"
|
||||
#include <cutils/log.h>
|
||||
|
||||
#include "utils/file.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <string>
|
||||
#include <utils/Compat.h> // For TEMP_FAILURE_RETRY on Darwin.
|
||||
|
||||
#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) {
|
||||
bool android::ReadFdToString(int fd, std::string* content) {
|
||||
content->clear();
|
||||
|
||||
char buf[BUFSIZ];
|
||||
|
@ -41,11 +37,10 @@ bool ReadFdToString(int fd, std::string* content) {
|
|||
return (n == 0) ? true : false;
|
||||
}
|
||||
|
||||
bool ReadFileToString(const std::string& path, std::string* content) {
|
||||
bool android::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));
|
||||
int fd = TEMP_FAILURE_RETRY(open(path.c_str(), O_RDONLY | O_CLOEXEC | O_NOFOLLOW));
|
||||
if (fd == -1) {
|
||||
return false;
|
||||
}
|
||||
|
@ -54,7 +49,7 @@ bool ReadFileToString(const std::string& path, std::string* content) {
|
|||
return result;
|
||||
}
|
||||
|
||||
bool WriteStringToFd(const std::string& content, int fd) {
|
||||
bool android::WriteStringToFd(const std::string& content, int fd) {
|
||||
const char* p = content.data();
|
||||
size_t left = content.size();
|
||||
while (left > 0) {
|
||||
|
@ -77,18 +72,18 @@ static bool CleanUpAfterFailedWrite(const std::string& path) {
|
|||
}
|
||||
|
||||
#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));
|
||||
bool android::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.
|
||||
// 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);
|
||||
|
@ -106,10 +101,10 @@ bool WriteStringToFile(const std::string& content, const std::string& path,
|
|||
}
|
||||
#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));
|
||||
bool android::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;
|
||||
}
|
||||
|
@ -118,6 +113,3 @@ bool WriteStringToFile(const std::string& content, const std::string& path) {
|
|||
TEMP_FAILURE_RETRY(close(fd));
|
||||
return result || CleanUpAfterFailedWrite(path);
|
||||
}
|
||||
|
||||
} // namespace base
|
||||
} // namespace android
|
|
@ -14,16 +14,11 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "base/stringprintf.h"
|
||||
#include <utils/stringprintf.h>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace android {
|
||||
namespace base {
|
||||
|
||||
void StringAppendV(std::string* dst, const char* format, va_list ap) {
|
||||
void android::StringAppendV(std::string* dst, const char* format, va_list ap) {
|
||||
// First try with a small fixed size buffer
|
||||
char space[1024];
|
||||
|
||||
|
@ -50,7 +45,7 @@ void StringAppendV(std::string* dst, const char* format, va_list ap) {
|
|||
|
||||
// Increase the buffer size to the size requested by vsnprintf,
|
||||
// plus one for the closing \0.
|
||||
int length = result + 1;
|
||||
int length = result+1;
|
||||
char* buf = new char[length];
|
||||
|
||||
// Restore the va_list before we use it again
|
||||
|
@ -65,7 +60,7 @@ void StringAppendV(std::string* dst, const char* format, va_list ap) {
|
|||
delete[] buf;
|
||||
}
|
||||
|
||||
std::string StringPrintf(const char* fmt, ...) {
|
||||
std::string android::StringPrintf(const char* fmt, ...) {
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
std::string result;
|
||||
|
@ -74,12 +69,9 @@ std::string StringPrintf(const char* fmt, ...) {
|
|||
return result;
|
||||
}
|
||||
|
||||
void StringAppendF(std::string* dst, const char* format, ...) {
|
||||
void android::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
|
|
@ -26,9 +26,11 @@ LOCAL_SRC_FILES := \
|
|||
BasicHashtable_test.cpp \
|
||||
BlobCache_test.cpp \
|
||||
BitSet_test.cpp \
|
||||
file_test.cpp \
|
||||
Looper_test.cpp \
|
||||
LruCache_test.cpp \
|
||||
String8_test.cpp \
|
||||
stringprintf_test.cpp \
|
||||
Unicode_test.cpp \
|
||||
Vector_test.cpp \
|
||||
|
||||
|
|
|
@ -14,15 +14,12 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "base/file.h"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include "utils/file.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <string>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
class TemporaryFile {
|
||||
public:
|
||||
|
@ -51,14 +48,14 @@ class TemporaryFile {
|
|||
TEST(file, ReadFileToString_ENOENT) {
|
||||
std::string s("hello");
|
||||
errno = 0;
|
||||
ASSERT_FALSE(android::base::ReadFileToString("/proc/does-not-exist", &s));
|
||||
ASSERT_FALSE(android::ReadFileToString("/proc/does-not-exist", &s));
|
||||
EXPECT_EQ(ENOENT, errno);
|
||||
EXPECT_EQ("", s); // s was cleared.
|
||||
EXPECT_EQ("", s); // s was cleared.
|
||||
}
|
||||
|
||||
TEST(file, ReadFileToString_success) {
|
||||
std::string s("hello");
|
||||
ASSERT_TRUE(android::base::ReadFileToString("/proc/version", &s)) << errno;
|
||||
ASSERT_TRUE(android::ReadFileToString("/proc/version", &s)) << errno;
|
||||
EXPECT_GT(s.length(), 6U);
|
||||
EXPECT_EQ('\n', s[s.length() - 1]);
|
||||
s[5] = 0;
|
||||
|
@ -68,36 +65,34 @@ TEST(file, ReadFileToString_success) {
|
|||
TEST(file, WriteStringToFile) {
|
||||
TemporaryFile tf;
|
||||
ASSERT_TRUE(tf.fd != -1);
|
||||
ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.filename)) << errno;
|
||||
ASSERT_TRUE(android::WriteStringToFile("abc", tf.filename)) << errno;
|
||||
std::string s;
|
||||
ASSERT_TRUE(android::base::ReadFileToString(tf.filename, &s)) << errno;
|
||||
ASSERT_TRUE(android::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;
|
||||
ASSERT_TRUE(android::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;
|
||||
ASSERT_TRUE(android::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_TRUE(android::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;
|
||||
ASSERT_TRUE(android::ReadFdToString(tf.fd, &s)) << errno;
|
||||
EXPECT_EQ("abc", s);
|
||||
}
|
|
@ -14,27 +14,25 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "base/stringprintf.h"
|
||||
#include <utils/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));
|
||||
EXPECT_EQ("00107e59", android::StringPrintf("%08zx", size));
|
||||
EXPECT_EQ("0x00107e59", android::StringPrintf("0x%08zx", size));
|
||||
}
|
||||
|
||||
TEST(StringPrintfTest, StringAppendF) {
|
||||
std::string s("a");
|
||||
android::base::StringAppendF(&s, "b");
|
||||
android::StringAppendF(&s, "b");
|
||||
EXPECT_EQ("ab", s);
|
||||
}
|
||||
|
||||
TEST(StringPrintfTest, Errno) {
|
||||
errno = 123;
|
||||
android::base::StringPrintf("hello %s", "world");
|
||||
android::StringPrintf("hello %s", "world");
|
||||
EXPECT_EQ(123, errno);
|
||||
}
|
||||
|
||||
|
@ -42,7 +40,7 @@ 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));
|
||||
std::string s(android::StringPrintf("%s", buf));
|
||||
EXPECT_EQ(buf, s);
|
||||
delete[] buf;
|
||||
}
|
Loading…
Reference in a new issue