summaryrefslogtreecommitdiffstats
path: root/libutils
diff options
context:
space:
mode:
authorNicolas Geoffray <ngeoffray@google.com>2015-03-16 11:08:29 +0000
committerGerrit Code Review <noreply-gerritcodereview@google.com>2015-03-16 11:08:29 +0000
commitdd5816953de6aebc51aa4cc6f69945fbb9cd4b2a (patch)
tree8db9bc4aa940a8b3a44cf31dacd3a7f932eadc80 /libutils
parentacf0a8b3f632fe90132b9ce457a2237160f8cb9a (diff)
parenta7870d88167f619e758b5bcd15b410d16da7c16b (diff)
downloadsystem_core-dd5816953de6aebc51aa4cc6f69945fbb9cd4b2a.zip
system_core-dd5816953de6aebc51aa4cc6f69945fbb9cd4b2a.tar.gz
system_core-dd5816953de6aebc51aa4cc6f69945fbb9cd4b2a.tar.bz2
Merge "Revert "Create libbase.""
Diffstat (limited to 'libutils')
-rw-r--r--libutils/Android.mk2
-rw-r--r--libutils/file.cpp115
-rw-r--r--libutils/stringprintf.cpp77
-rw-r--r--libutils/tests/Android.mk2
-rw-r--r--libutils/tests/file_test.cpp98
-rw-r--r--libutils/tests/stringprintf_test.cpp58
6 files changed, 352 insertions, 0 deletions
diff --git a/libutils/Android.mk b/libutils/Android.mk
index e9c5f89..7bff14e 100644
--- a/libutils/Android.mk
+++ b/libutils/Android.mk
@@ -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
diff --git a/libutils/file.cpp b/libutils/file.cpp
new file mode 100644
index 0000000..0690bc2
--- /dev/null
+++ b/libutils/file.cpp
@@ -0,0 +1,115 @@
+/*
+ * 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.
+ */
+
+#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 <utils/Compat.h> // For TEMP_FAILURE_RETRY on Darwin.
+
+bool android::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 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));
+ if (fd == -1) {
+ return false;
+ }
+ bool result = ReadFdToString(fd, content);
+ TEMP_FAILURE_RETRY(close(fd));
+ return result;
+}
+
+bool android::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 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.
+ 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 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;
+ }
+
+ bool result = WriteStringToFd(content, fd);
+ TEMP_FAILURE_RETRY(close(fd));
+ return result || CleanUpAfterFailedWrite(path);
+}
diff --git a/libutils/stringprintf.cpp b/libutils/stringprintf.cpp
new file mode 100644
index 0000000..5eaa293
--- /dev/null
+++ b/libutils/stringprintf.cpp
@@ -0,0 +1,77 @@
+/*
+ * 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 <utils/stringprintf.h>
+
+#include <stdio.h>
+
+void android::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 android::StringPrintf(const char* fmt, ...) {
+ va_list ap;
+ va_start(ap, fmt);
+ std::string result;
+ StringAppendV(&result, fmt, ap);
+ va_end(ap);
+ return result;
+}
+
+void android::StringAppendF(std::string* dst, const char* format, ...) {
+ va_list ap;
+ va_start(ap, format);
+ StringAppendV(dst, format, ap);
+ va_end(ap);
+}
diff --git a/libutils/tests/Android.mk b/libutils/tests/Android.mk
index 634f44f..ce288ca 100644
--- a/libutils/tests/Android.mk
+++ b/libutils/tests/Android.mk
@@ -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 \
diff --git a/libutils/tests/file_test.cpp b/libutils/tests/file_test.cpp
new file mode 100644
index 0000000..cea18b6
--- /dev/null
+++ b/libutils/tests/file_test.cpp
@@ -0,0 +1,98 @@
+/*
+ * 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 "utils/file.h"
+
+#include <errno.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <gtest/gtest.h>
+
+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::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::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::WriteStringToFile("abc", tf.filename)) << errno;
+ std::string s;
+ 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::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::ReadFileToString(tf.filename, &s)) << errno;
+ EXPECT_EQ("abc", s);
+}
+
+TEST(file, WriteStringToFd) {
+ TemporaryFile tf;
+ ASSERT_TRUE(tf.fd != -1);
+ ASSERT_TRUE(android::WriteStringToFd("abc", tf.fd));
+
+ ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << errno;
+
+ std::string s;
+ ASSERT_TRUE(android::ReadFdToString(tf.fd, &s)) << errno;
+ EXPECT_EQ("abc", s);
+}
diff --git a/libutils/tests/stringprintf_test.cpp b/libutils/tests/stringprintf_test.cpp
new file mode 100644
index 0000000..f995452
--- /dev/null
+++ b/libutils/tests/stringprintf_test.cpp
@@ -0,0 +1,58 @@
+/*
+ * 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 <utils/stringprintf.h>
+
+#include <gtest/gtest.h>
+
+TEST(StringPrintfTest, HexSizeT) {
+ size_t size = 0x00107e59;
+ EXPECT_EQ("00107e59", android::StringPrintf("%08zx", size));
+ EXPECT_EQ("0x00107e59", android::StringPrintf("0x%08zx", size));
+}
+
+TEST(StringPrintfTest, StringAppendF) {
+ std::string s("a");
+ android::StringAppendF(&s, "b");
+ EXPECT_EQ("ab", s);
+}
+
+TEST(StringPrintfTest, Errno) {
+ errno = 123;
+ android::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::StringPrintf("%s", buf));
+ EXPECT_EQ(buf, s);
+ delete[] buf;
+}
+
+TEST(StringPrintfTest, At1023) {
+ TestN(1023);
+}
+
+TEST(StringPrintfTest, At1024) {
+ TestN(1024);
+}
+
+TEST(StringPrintfTest, At1025) {
+ TestN(1025);
+}