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_CFLAGS := -DADB_HOST=0 $(LIBADB_CFLAGS)
|
||||||
LOCAL_SRC_FILES := $(LIBADB_TEST_SRCS)
|
LOCAL_SRC_FILES := $(LIBADB_TEST_SRCS)
|
||||||
LOCAL_STATIC_LIBRARIES := libadbd
|
LOCAL_STATIC_LIBRARIES := libadbd
|
||||||
LOCAL_SHARED_LIBRARIES := liblog libbase libcutils
|
LOCAL_SHARED_LIBRARIES := liblog libcutils libutils
|
||||||
include $(BUILD_NATIVE_TEST)
|
include $(BUILD_NATIVE_TEST)
|
||||||
|
|
||||||
include $(CLEAR_VARS)
|
include $(CLEAR_VARS)
|
||||||
|
|
@ -98,11 +98,12 @@ LOCAL_CLANG := $(ADB_CLANG)
|
||||||
LOCAL_MODULE := adb_test
|
LOCAL_MODULE := adb_test
|
||||||
LOCAL_CFLAGS := -DADB_HOST=1 $(LIBADB_CFLAGS)
|
LOCAL_CFLAGS := -DADB_HOST=1 $(LIBADB_CFLAGS)
|
||||||
LOCAL_SRC_FILES := $(LIBADB_TEST_SRCS) services.cpp
|
LOCAL_SRC_FILES := $(LIBADB_TEST_SRCS) services.cpp
|
||||||
LOCAL_SHARED_LIBRARIES := liblog libbase
|
LOCAL_SHARED_LIBRARIES := liblog
|
||||||
LOCAL_STATIC_LIBRARIES := \
|
LOCAL_STATIC_LIBRARIES := \
|
||||||
libadb \
|
libadb \
|
||||||
libcrypto_static \
|
libcrypto_static \
|
||||||
libcutils \
|
libcutils \
|
||||||
|
libutils \
|
||||||
|
|
||||||
ifeq ($(HOST_OS),linux)
|
ifeq ($(HOST_OS),linux)
|
||||||
LOCAL_LDLIBS += -lrt -ldl -lpthread
|
LOCAL_LDLIBS += -lrt -ldl -lpthread
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "base/file.h"
|
#include "utils/file.h"
|
||||||
|
|
||||||
class TemporaryFile {
|
class TemporaryFile {
|
||||||
public:
|
public:
|
||||||
|
|
@ -55,7 +55,7 @@ TEST(io, ReadFdExactly_whole) {
|
||||||
TemporaryFile tf;
|
TemporaryFile tf;
|
||||||
ASSERT_NE(-1, tf.fd);
|
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));
|
ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
|
||||||
|
|
||||||
// Test reading the whole file.
|
// Test reading the whole file.
|
||||||
|
|
@ -69,7 +69,7 @@ TEST(io, ReadFdExactly_eof) {
|
||||||
TemporaryFile tf;
|
TemporaryFile tf;
|
||||||
ASSERT_NE(-1, tf.fd);
|
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));
|
ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
|
||||||
|
|
||||||
// Test that not having enough data will fail.
|
// Test that not having enough data will fail.
|
||||||
|
|
@ -83,7 +83,7 @@ TEST(io, ReadFdExactly_partial) {
|
||||||
TemporaryFile tf;
|
TemporaryFile tf;
|
||||||
ASSERT_NE(-1, tf.fd);
|
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));
|
ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
|
||||||
|
|
||||||
// Test reading a partial file.
|
// Test reading a partial file.
|
||||||
|
|
@ -106,7 +106,7 @@ TEST(io, WriteFdExactly_whole) {
|
||||||
ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
|
ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
|
||||||
|
|
||||||
std::string s;
|
std::string s;
|
||||||
ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s));
|
ASSERT_TRUE(android::ReadFdToString(tf.fd, &s));
|
||||||
EXPECT_STREQ(expected, s.c_str());
|
EXPECT_STREQ(expected, s.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -123,7 +123,7 @@ TEST(io, WriteFdExactly_partial) {
|
||||||
expected.pop_back();
|
expected.pop_back();
|
||||||
|
|
||||||
std::string s;
|
std::string s;
|
||||||
ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s));
|
ASSERT_TRUE(android::ReadFdToString(tf.fd, &s));
|
||||||
EXPECT_EQ(expected, s);
|
EXPECT_EQ(expected, s);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -137,6 +137,6 @@ TEST(io, WriteStringFully) {
|
||||||
ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
|
ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
|
||||||
|
|
||||||
std::string s;
|
std::string s;
|
||||||
ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s));
|
ASSERT_TRUE(android::ReadFdToString(tf.fd, &s));
|
||||||
EXPECT_STREQ(str, s.c_str());
|
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.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef BASE_FILE_H
|
#ifndef UTILS_FILE_H
|
||||||
#define BASE_FILE_H
|
#define UTILS_FILE_H
|
||||||
|
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
namespace android {
|
namespace android {
|
||||||
namespace base {
|
|
||||||
|
|
||||||
bool ReadFdToString(int fd, std::string* content);
|
bool ReadFdToString(int fd, std::string* content);
|
||||||
bool ReadFileToString(const std::string& path, 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);
|
mode_t mode, uid_t owner, gid_t group);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
} // namespace base
|
} // namespace android
|
||||||
} // namespace android
|
|
||||||
|
|
||||||
#endif // BASE_FILE_H
|
#endif
|
||||||
|
|
@ -14,27 +14,25 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef BASE_STRINGPRINTF_H
|
#ifndef UTILS_STRINGPRINTF_H_
|
||||||
#define BASE_STRINGPRINTF_H
|
#define UTILS_STRINGPRINTF_H_
|
||||||
|
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
namespace android {
|
namespace android {
|
||||||
namespace base {
|
|
||||||
|
|
||||||
// Returns a string corresponding to printf-like formatting of the arguments.
|
// Returns a string corresponding to printf-like formatting of the arguments.
|
||||||
std::string StringPrintf(const char* fmt, ...)
|
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'.
|
// Appends a printf-like formatting of the arguments to 'dst'.
|
||||||
void StringAppendF(std::string* dst, const char* fmt, ...)
|
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'.
|
// Appends a printf-like formatting of the arguments to 'dst'.
|
||||||
void StringAppendV(std::string* dst, const char* format, va_list ap);
|
void StringAppendV(std::string* dst, const char* format, va_list ap);
|
||||||
|
|
||||||
} // namespace base
|
|
||||||
} // namespace android
|
} // namespace android
|
||||||
|
|
||||||
#endif // BASE_STRINGPRINTF_H
|
#endif
|
||||||
|
|
@ -27,7 +27,6 @@ LOCAL_SRC_FILES:= \
|
||||||
parser.cpp \
|
parser.cpp \
|
||||||
util.cpp \
|
util.cpp \
|
||||||
|
|
||||||
LOCAL_STATIC_LIBRARIES := libbase
|
|
||||||
LOCAL_MODULE := libinit
|
LOCAL_MODULE := libinit
|
||||||
include $(BUILD_STATIC_LIBRARY)
|
include $(BUILD_STATIC_LIBRARY)
|
||||||
|
|
||||||
|
|
@ -56,7 +55,7 @@ LOCAL_STATIC_LIBRARIES := \
|
||||||
libfs_mgr \
|
libfs_mgr \
|
||||||
liblogwrap \
|
liblogwrap \
|
||||||
libcutils \
|
libcutils \
|
||||||
libbase \
|
libutils \
|
||||||
liblog \
|
liblog \
|
||||||
libc \
|
libc \
|
||||||
libselinux \
|
libselinux \
|
||||||
|
|
@ -81,7 +80,7 @@ LOCAL_SRC_FILES := \
|
||||||
|
|
||||||
LOCAL_SHARED_LIBRARIES += \
|
LOCAL_SHARED_LIBRARIES += \
|
||||||
libcutils \
|
libcutils \
|
||||||
libbase \
|
libutils \
|
||||||
|
|
||||||
LOCAL_STATIC_LIBRARIES := libinit
|
LOCAL_STATIC_LIBRARIES := libinit
|
||||||
include $(BUILD_NATIVE_TEST)
|
include $(BUILD_NATIVE_TEST)
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,7 @@
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include <base/file.h>
|
#include <utils/file.h>
|
||||||
|
|
||||||
#define LOG_ROOT "/data/bootchart"
|
#define LOG_ROOT "/data/bootchart"
|
||||||
#define LOG_STAT LOG_ROOT"/proc_stat.log"
|
#define LOG_STAT LOG_ROOT"/proc_stat.log"
|
||||||
|
|
@ -59,7 +59,7 @@ static FILE* log_disks;
|
||||||
|
|
||||||
static long long get_uptime_jiffies() {
|
static long long get_uptime_jiffies() {
|
||||||
std::string uptime;
|
std::string uptime;
|
||||||
if (!android::base::ReadFileToString("/proc/uptime", &uptime)) {
|
if (!android::ReadFileToString("/proc/uptime", &uptime)) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
return 100LL * strtod(uptime.c_str(), NULL);
|
return 100LL * strtod(uptime.c_str(), NULL);
|
||||||
|
|
@ -82,7 +82,7 @@ static void log_header() {
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string kernel_cmdline;
|
std::string kernel_cmdline;
|
||||||
android::base::ReadFileToString("/proc/cmdline", &kernel_cmdline);
|
android::ReadFileToString("/proc/cmdline", &kernel_cmdline);
|
||||||
|
|
||||||
FILE* out = fopen(LOG_HEADER, "we");
|
FILE* out = fopen(LOG_HEADER, "we");
|
||||||
if (out == NULL) {
|
if (out == NULL) {
|
||||||
|
|
@ -106,7 +106,7 @@ static void do_log_file(FILE* log, const char* procfile) {
|
||||||
do_log_uptime(log);
|
do_log_uptime(log);
|
||||||
|
|
||||||
std::string content;
|
std::string content;
|
||||||
if (android::base::ReadFileToString(procfile, &content)) {
|
if (android::ReadFileToString(procfile, &content)) {
|
||||||
fprintf(log, "%s\n", content.c_str());
|
fprintf(log, "%s\n", content.c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -127,13 +127,13 @@ static void do_log_procs(FILE* log) {
|
||||||
// name from /proc/<pid>/cmdline.
|
// name from /proc/<pid>/cmdline.
|
||||||
snprintf(filename, sizeof(filename), "/proc/%d/cmdline", pid);
|
snprintf(filename, sizeof(filename), "/proc/%d/cmdline", pid);
|
||||||
std::string cmdline;
|
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.
|
const char* full_name = cmdline.c_str(); // So we stop at the first NUL.
|
||||||
|
|
||||||
// Read process stat line.
|
// Read process stat line.
|
||||||
snprintf(filename, sizeof(filename), "/proc/%d/stat", pid);
|
snprintf(filename, sizeof(filename), "/proc/%d/stat", pid);
|
||||||
std::string stat;
|
std::string stat;
|
||||||
if (android::base::ReadFileToString(filename, &stat)) {
|
if (android::ReadFileToString(filename, &stat)) {
|
||||||
if (!cmdline.empty()) {
|
if (!cmdline.empty()) {
|
||||||
// Substitute the process name with its real name.
|
// Substitute the process name with its real name.
|
||||||
size_t open = stat.find('(');
|
size_t open = stat.find('(');
|
||||||
|
|
@ -155,7 +155,7 @@ static int bootchart_init() {
|
||||||
int timeout = 0;
|
int timeout = 0;
|
||||||
|
|
||||||
std::string start;
|
std::string start;
|
||||||
android::base::ReadFileToString(LOG_STARTFILE, &start);
|
android::ReadFileToString(LOG_STARTFILE, &start);
|
||||||
if (!start.empty()) {
|
if (!start.empty()) {
|
||||||
timeout = atoi(start.c_str());
|
timeout = atoi(start.c_str());
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -164,7 +164,7 @@ static int bootchart_init() {
|
||||||
// timeout. this is useful when using -wipe-data since the /data
|
// timeout. this is useful when using -wipe-data since the /data
|
||||||
// partition is fresh.
|
// partition is fresh.
|
||||||
std::string cmdline;
|
std::string cmdline;
|
||||||
android::base::ReadFileToString("/proc/cmdline", &cmdline);
|
android::ReadFileToString("/proc/cmdline", &cmdline);
|
||||||
#define KERNEL_OPTION "androidboot.bootchart="
|
#define KERNEL_OPTION "androidboot.bootchart="
|
||||||
if (strstr(cmdline.c_str(), KERNEL_OPTION) != NULL) {
|
if (strstr(cmdline.c_str(), KERNEL_OPTION) != NULL) {
|
||||||
timeout = atoi(cmdline.c_str() + sizeof(KERNEL_OPTION) - 1);
|
timeout = atoi(cmdline.c_str() + sizeof(KERNEL_OPTION) - 1);
|
||||||
|
|
@ -226,7 +226,7 @@ static int bootchart_step() {
|
||||||
|
|
||||||
// Stop if /data/bootchart/stop contains 1.
|
// Stop if /data/bootchart/stop contains 1.
|
||||||
std::string stop;
|
std::string stop;
|
||||||
if (android::base::ReadFileToString(LOG_STOPFILE, &stop) && stop == "1") {
|
if (android::ReadFileToString(LOG_STOPFILE, &stop) && stop == "1") {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,8 @@
|
||||||
#include <cutils/sockets.h>
|
#include <cutils/sockets.h>
|
||||||
#include <cutils/multiuser.h>
|
#include <cutils/multiuser.h>
|
||||||
|
|
||||||
|
#include <utils/file.h>
|
||||||
|
|
||||||
#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
|
#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
|
||||||
#include <sys/_system_properties.h>
|
#include <sys/_system_properties.h>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -32,11 +32,11 @@
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <sys/un.h>
|
#include <sys/un.h>
|
||||||
|
|
||||||
#include <base/file.h>
|
|
||||||
|
|
||||||
/* for ANDROID_SOCKET_* */
|
/* for ANDROID_SOCKET_* */
|
||||||
#include <cutils/sockets.h>
|
#include <cutils/sockets.h>
|
||||||
|
|
||||||
|
#include <utils/file.h>
|
||||||
|
|
||||||
#include <private/android_filesystem_config.h>
|
#include <private/android_filesystem_config.h>
|
||||||
|
|
||||||
#include "init.h"
|
#include "init.h"
|
||||||
|
|
@ -168,7 +168,7 @@ bool read_file(const char* path, std::string* content) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool okay = android::base::ReadFdToString(fd, content);
|
bool okay = android::ReadFdToString(fd, content);
|
||||||
TEMP_FAILURE_RETRY(close(fd));
|
TEMP_FAILURE_RETRY(close(fd));
|
||||||
if (okay) {
|
if (okay) {
|
||||||
content->append("\n", 1);
|
content->append("\n", 1);
|
||||||
|
|
@ -181,7 +181,7 @@ int write_file(const char* path, const char* content) {
|
||||||
if (fd == -1) {
|
if (fd == -1) {
|
||||||
return -errno;
|
return -errno;
|
||||||
}
|
}
|
||||||
int result = android::base::WriteStringToFd(content, fd) ? 0 : -errno;
|
int result = android::WriteStringToFd(content, fd) ? 0 : -errno;
|
||||||
TEMP_FAILURE_RETRY(close(fd));
|
TEMP_FAILURE_RETRY(close(fd));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -39,7 +39,9 @@ commonSources:= \
|
||||||
Tokenizer.cpp \
|
Tokenizer.cpp \
|
||||||
Unicode.cpp \
|
Unicode.cpp \
|
||||||
VectorImpl.cpp \
|
VectorImpl.cpp \
|
||||||
|
file.cpp \
|
||||||
misc.cpp \
|
misc.cpp \
|
||||||
|
stringprintf.cpp \
|
||||||
|
|
||||||
host_commonCflags := -DLIBUTILS_NATIVE=1 $(TOOL_CFLAGS) -Werror
|
host_commonCflags := -DLIBUTILS_NATIVE=1 $(TOOL_CFLAGS) -Werror
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -14,23 +14,19 @@
|
||||||
* limitations under the License.
|
* 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 <errno.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
#include <string>
|
#include <utils/Compat.h> // For TEMP_FAILURE_RETRY on Darwin.
|
||||||
|
|
||||||
#define LOG_TAG "base.file"
|
bool android::ReadFdToString(int fd, std::string* content) {
|
||||||
#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();
|
content->clear();
|
||||||
|
|
||||||
char buf[BUFSIZ];
|
char buf[BUFSIZ];
|
||||||
|
|
@ -41,11 +37,10 @@ bool ReadFdToString(int fd, std::string* content) {
|
||||||
return (n == 0) ? true : false;
|
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();
|
content->clear();
|
||||||
|
|
||||||
int fd =
|
int fd = TEMP_FAILURE_RETRY(open(path.c_str(), O_RDONLY | O_CLOEXEC | O_NOFOLLOW));
|
||||||
TEMP_FAILURE_RETRY(open(path.c_str(), O_RDONLY | O_CLOEXEC | O_NOFOLLOW));
|
|
||||||
if (fd == -1) {
|
if (fd == -1) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
@ -54,7 +49,7 @@ bool ReadFileToString(const std::string& path, std::string* content) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WriteStringToFd(const std::string& content, int fd) {
|
bool android::WriteStringToFd(const std::string& content, int fd) {
|
||||||
const char* p = content.data();
|
const char* p = content.data();
|
||||||
size_t left = content.size();
|
size_t left = content.size();
|
||||||
while (left > 0) {
|
while (left > 0) {
|
||||||
|
|
@ -77,18 +72,18 @@ static bool CleanUpAfterFailedWrite(const std::string& path) {
|
||||||
}
|
}
|
||||||
|
|
||||||
#if !defined(_WIN32)
|
#if !defined(_WIN32)
|
||||||
bool WriteStringToFile(const std::string& content, const std::string& path,
|
bool android::WriteStringToFile(const std::string& content, const std::string& path,
|
||||||
mode_t mode, uid_t owner, gid_t group) {
|
mode_t mode, uid_t owner, gid_t group) {
|
||||||
int fd = TEMP_FAILURE_RETRY(
|
int fd = TEMP_FAILURE_RETRY(open(path.c_str(),
|
||||||
open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW,
|
O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW,
|
||||||
mode));
|
mode));
|
||||||
if (fd == -1) {
|
if (fd == -1) {
|
||||||
ALOGE("android::WriteStringToFile open failed: %s", strerror(errno));
|
ALOGE("android::WriteStringToFile open failed: %s", strerror(errno));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// We do an explicit fchmod here because we assume that the caller really
|
// We do an explicit fchmod here because we assume that the caller really meant what they
|
||||||
// meant what they said and doesn't want the umask-influenced mode.
|
// said and doesn't want the umask-influenced mode.
|
||||||
if (fchmod(fd, mode) == -1) {
|
if (fchmod(fd, mode) == -1) {
|
||||||
ALOGE("android::WriteStringToFile fchmod failed: %s", strerror(errno));
|
ALOGE("android::WriteStringToFile fchmod failed: %s", strerror(errno));
|
||||||
return CleanUpAfterFailedWrite(path);
|
return CleanUpAfterFailedWrite(path);
|
||||||
|
|
@ -106,10 +101,10 @@ bool WriteStringToFile(const std::string& content, const std::string& path,
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
bool WriteStringToFile(const std::string& content, const std::string& path) {
|
bool android::WriteStringToFile(const std::string& content, const std::string& path) {
|
||||||
int fd = TEMP_FAILURE_RETRY(
|
int fd = TEMP_FAILURE_RETRY(open(path.c_str(),
|
||||||
open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW,
|
O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW,
|
||||||
DEFFILEMODE));
|
DEFFILEMODE));
|
||||||
if (fd == -1) {
|
if (fd == -1) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
@ -118,6 +113,3 @@ bool WriteStringToFile(const std::string& content, const std::string& path) {
|
||||||
TEMP_FAILURE_RETRY(close(fd));
|
TEMP_FAILURE_RETRY(close(fd));
|
||||||
return result || CleanUpAfterFailedWrite(path);
|
return result || CleanUpAfterFailedWrite(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace base
|
|
||||||
} // namespace android
|
|
||||||
|
|
@ -14,16 +14,11 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "base/stringprintf.h"
|
#include <utils/stringprintf.h>
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
#include <string>
|
void android::StringAppendV(std::string* dst, const char* format, va_list ap) {
|
||||||
|
|
||||||
namespace android {
|
|
||||||
namespace base {
|
|
||||||
|
|
||||||
void StringAppendV(std::string* dst, const char* format, va_list ap) {
|
|
||||||
// First try with a small fixed size buffer
|
// First try with a small fixed size buffer
|
||||||
char space[1024];
|
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,
|
// Increase the buffer size to the size requested by vsnprintf,
|
||||||
// plus one for the closing \0.
|
// plus one for the closing \0.
|
||||||
int length = result + 1;
|
int length = result+1;
|
||||||
char* buf = new char[length];
|
char* buf = new char[length];
|
||||||
|
|
||||||
// Restore the va_list before we use it again
|
// 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;
|
delete[] buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string StringPrintf(const char* fmt, ...) {
|
std::string android::StringPrintf(const char* fmt, ...) {
|
||||||
va_list ap;
|
va_list ap;
|
||||||
va_start(ap, fmt);
|
va_start(ap, fmt);
|
||||||
std::string result;
|
std::string result;
|
||||||
|
|
@ -74,12 +69,9 @@ std::string StringPrintf(const char* fmt, ...) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void StringAppendF(std::string* dst, const char* format, ...) {
|
void android::StringAppendF(std::string* dst, const char* format, ...) {
|
||||||
va_list ap;
|
va_list ap;
|
||||||
va_start(ap, format);
|
va_start(ap, format);
|
||||||
StringAppendV(dst, format, ap);
|
StringAppendV(dst, format, ap);
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace base
|
|
||||||
} // namespace android
|
|
||||||
|
|
@ -26,9 +26,11 @@ LOCAL_SRC_FILES := \
|
||||||
BasicHashtable_test.cpp \
|
BasicHashtable_test.cpp \
|
||||||
BlobCache_test.cpp \
|
BlobCache_test.cpp \
|
||||||
BitSet_test.cpp \
|
BitSet_test.cpp \
|
||||||
|
file_test.cpp \
|
||||||
Looper_test.cpp \
|
Looper_test.cpp \
|
||||||
LruCache_test.cpp \
|
LruCache_test.cpp \
|
||||||
String8_test.cpp \
|
String8_test.cpp \
|
||||||
|
stringprintf_test.cpp \
|
||||||
Unicode_test.cpp \
|
Unicode_test.cpp \
|
||||||
Vector_test.cpp \
|
Vector_test.cpp \
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -14,15 +14,12 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "base/file.h"
|
#include "utils/file.h"
|
||||||
|
|
||||||
#include <gtest/gtest.h>
|
|
||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <gtest/gtest.h>
|
||||||
#include <string>
|
|
||||||
|
|
||||||
class TemporaryFile {
|
class TemporaryFile {
|
||||||
public:
|
public:
|
||||||
|
|
@ -51,14 +48,14 @@ class TemporaryFile {
|
||||||
TEST(file, ReadFileToString_ENOENT) {
|
TEST(file, ReadFileToString_ENOENT) {
|
||||||
std::string s("hello");
|
std::string s("hello");
|
||||||
errno = 0;
|
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(ENOENT, errno);
|
||||||
EXPECT_EQ("", s); // s was cleared.
|
EXPECT_EQ("", s); // s was cleared.
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(file, ReadFileToString_success) {
|
TEST(file, ReadFileToString_success) {
|
||||||
std::string s("hello");
|
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_GT(s.length(), 6U);
|
||||||
EXPECT_EQ('\n', s[s.length() - 1]);
|
EXPECT_EQ('\n', s[s.length() - 1]);
|
||||||
s[5] = 0;
|
s[5] = 0;
|
||||||
|
|
@ -68,36 +65,34 @@ TEST(file, ReadFileToString_success) {
|
||||||
TEST(file, WriteStringToFile) {
|
TEST(file, WriteStringToFile) {
|
||||||
TemporaryFile tf;
|
TemporaryFile tf;
|
||||||
ASSERT_TRUE(tf.fd != -1);
|
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;
|
std::string s;
|
||||||
ASSERT_TRUE(android::base::ReadFileToString(tf.filename, &s)) << errno;
|
ASSERT_TRUE(android::ReadFileToString(tf.filename, &s)) << errno;
|
||||||
EXPECT_EQ("abc", s);
|
EXPECT_EQ("abc", s);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(file, WriteStringToFile2) {
|
TEST(file, WriteStringToFile2) {
|
||||||
TemporaryFile tf;
|
TemporaryFile tf;
|
||||||
ASSERT_TRUE(tf.fd != -1);
|
ASSERT_TRUE(tf.fd != -1);
|
||||||
ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.filename, 0660,
|
ASSERT_TRUE(android::WriteStringToFile("abc", tf.filename, 0660, getuid(), getgid())) << errno;
|
||||||
getuid(), getgid()))
|
|
||||||
<< errno;
|
|
||||||
struct stat sb;
|
struct stat sb;
|
||||||
ASSERT_EQ(0, stat(tf.filename, &sb));
|
ASSERT_EQ(0, stat(tf.filename, &sb));
|
||||||
ASSERT_EQ(0660U, (sb.st_mode & ~S_IFMT));
|
ASSERT_EQ(0660U, (sb.st_mode & ~S_IFMT));
|
||||||
ASSERT_EQ(getuid(), sb.st_uid);
|
ASSERT_EQ(getuid(), sb.st_uid);
|
||||||
ASSERT_EQ(getgid(), sb.st_gid);
|
ASSERT_EQ(getgid(), sb.st_gid);
|
||||||
std::string s;
|
std::string s;
|
||||||
ASSERT_TRUE(android::base::ReadFileToString(tf.filename, &s)) << errno;
|
ASSERT_TRUE(android::ReadFileToString(tf.filename, &s)) << errno;
|
||||||
EXPECT_EQ("abc", s);
|
EXPECT_EQ("abc", s);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(file, WriteStringToFd) {
|
TEST(file, WriteStringToFd) {
|
||||||
TemporaryFile tf;
|
TemporaryFile tf;
|
||||||
ASSERT_TRUE(tf.fd != -1);
|
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;
|
ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << errno;
|
||||||
|
|
||||||
std::string s;
|
std::string s;
|
||||||
ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s)) << errno;
|
ASSERT_TRUE(android::ReadFdToString(tf.fd, &s)) << errno;
|
||||||
EXPECT_EQ("abc", s);
|
EXPECT_EQ("abc", s);
|
||||||
}
|
}
|
||||||
|
|
@ -14,27 +14,25 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "base/stringprintf.h"
|
#include <utils/stringprintf.h>
|
||||||
|
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
TEST(StringPrintfTest, HexSizeT) {
|
TEST(StringPrintfTest, HexSizeT) {
|
||||||
size_t size = 0x00107e59;
|
size_t size = 0x00107e59;
|
||||||
EXPECT_EQ("00107e59", android::base::StringPrintf("%08zx", size));
|
EXPECT_EQ("00107e59", android::StringPrintf("%08zx", size));
|
||||||
EXPECT_EQ("0x00107e59", android::base::StringPrintf("0x%08zx", size));
|
EXPECT_EQ("0x00107e59", android::StringPrintf("0x%08zx", size));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(StringPrintfTest, StringAppendF) {
|
TEST(StringPrintfTest, StringAppendF) {
|
||||||
std::string s("a");
|
std::string s("a");
|
||||||
android::base::StringAppendF(&s, "b");
|
android::StringAppendF(&s, "b");
|
||||||
EXPECT_EQ("ab", s);
|
EXPECT_EQ("ab", s);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(StringPrintfTest, Errno) {
|
TEST(StringPrintfTest, Errno) {
|
||||||
errno = 123;
|
errno = 123;
|
||||||
android::base::StringPrintf("hello %s", "world");
|
android::StringPrintf("hello %s", "world");
|
||||||
EXPECT_EQ(123, errno);
|
EXPECT_EQ(123, errno);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -42,7 +40,7 @@ void TestN(size_t n) {
|
||||||
char* buf = new char[n + 1];
|
char* buf = new char[n + 1];
|
||||||
memset(buf, 'x', n);
|
memset(buf, 'x', n);
|
||||||
buf[n] = '\0';
|
buf[n] = '\0';
|
||||||
std::string s(android::base::StringPrintf("%s", buf));
|
std::string s(android::StringPrintf("%s", buf));
|
||||||
EXPECT_EQ(buf, s);
|
EXPECT_EQ(buf, s);
|
||||||
delete[] buf;
|
delete[] buf;
|
||||||
}
|
}
|
||||||
Loading…
Add table
Reference in a new issue