AAPT2: Fix file::mkdirs when given absolute paths
file::mkdirs would try to extract the first part of the
path '/path' which would be the empty string ''. Mkdir would
fail creating the empty string directory.
Change-Id: Ice8ba92135f145f52f3663499a2c49eebe797328
diff --git a/tools/aapt2/util/Files.cpp b/tools/aapt2/util/Files.cpp
index 04e8199..6428e98 100644
--- a/tools/aapt2/util/Files.cpp
+++ b/tools/aapt2/util/Files.cpp
@@ -96,7 +96,7 @@
const char* start = path.begin();
const char* end = path.end();
for (const char* current = start; current != end; ++current) {
- if (*current == sDirSep) {
+ if (*current == sDirSep && current != start) {
StringPiece parentPath(start, current - start);
int result = mkdirImpl(parentPath);
if (result < 0 && errno != EEXIST) {
@@ -139,6 +139,20 @@
return {};
}
+void appendPath(std::string* base, StringPiece part) {
+ assert(base);
+ const bool baseHasTrailingSep = (!base->empty() && *(base->end() - 1) == sDirSep);
+ const bool partHasLeadingSep = (!part.empty() && *(part.begin()) == sDirSep);
+ if (baseHasTrailingSep && partHasLeadingSep) {
+ // Remove the part's leading sep
+ part = part.substr(1, part.size() - 1);
+ } else if (!baseHasTrailingSep && !partHasLeadingSep) {
+ // None of the pieces has a separator.
+ *base += sDirSep;
+ }
+ base->append(part.data(), part.size());
+}
+
std::string packageToPath(const StringPiece& package) {
std::string outPath;
for (StringPiece part : util::tokenize<char>(package, '.')) {
diff --git a/tools/aapt2/util/Files.h b/tools/aapt2/util/Files.h
index c58ba5d..c2e6115 100644
--- a/tools/aapt2/util/Files.h
+++ b/tools/aapt2/util/Files.h
@@ -61,14 +61,7 @@
/*
* Appends a path to `base`, separated by the directory separator.
*/
-void appendPath(std::string* base, const StringPiece& part);
-
-/*
- * Appends a series of paths to `base`, separated by the
- * system directory separator.
- */
-template <typename... Ts >
-void appendPath(std::string* base, const StringPiece& part, const Ts&... parts);
+void appendPath(std::string* base, StringPiece part);
/*
* Makes all the directories in `path`. The last element in the path
@@ -139,20 +132,6 @@
std::vector<std::string> mPatternTokens;
};
-inline void appendPath(std::string* base, const StringPiece& part) {
- assert(base);
- *base += sDirSep;
- base->append(part.data(), part.size());
-}
-
-template <typename... Ts >
-void appendPath(std::string* base, const StringPiece& part, const Ts&... parts) {
- assert(base);
- *base += sDirSep;
- base->append(part.data(), part.size());
- appendPath(base, parts...);
-}
-
} // namespace file
} // namespace aapt
diff --git a/tools/aapt2/util/Files_test.cpp b/tools/aapt2/util/Files_test.cpp
new file mode 100644
index 0000000..efb0459
--- /dev/null
+++ b/tools/aapt2/util/Files_test.cpp
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2016 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 "test/Test.h"
+#include "util/Files.h"
+
+#include <sstream>
+
+namespace aapt {
+namespace file {
+
+class FilesTest : public ::testing::Test {
+public:
+ void SetUp() override {
+ std::stringstream builder;
+ builder << "hello" << sDirSep << "there";
+ mExpectedPath = builder.str();
+ }
+
+protected:
+ std::string mExpectedPath;
+};
+
+TEST_F(FilesTest, appendPath) {
+ std::string base = "hello";
+ appendPath(&base, "there");
+ EXPECT_EQ(mExpectedPath, base);
+}
+
+TEST_F(FilesTest, appendPathWithLeadingOrTrailingSeparators) {
+ std::string base = "hello/";
+ appendPath(&base, "there");
+ EXPECT_EQ(mExpectedPath, base);
+
+ base = "hello";
+ appendPath(&base, "/there");
+ EXPECT_EQ(mExpectedPath, base);
+
+ base = "hello/";
+ appendPath(&base, "/there");
+ EXPECT_EQ(mExpectedPath, base);
+}
+
+} // namespace files
+} // namespace aapt