diff options
author | Christopher Wiley <wiley@google.com> | 2016-06-03 04:04:14 +0000 |
---|---|---|
committer | android-build-merger <android-build-merger@google.com> | 2016-06-03 04:04:14 +0000 |
commit | 23fb35162bc8b22bc88662eb53d66d0d4247763a (patch) | |
tree | 456fee935af2cb2d2b1db533079316117271c76a | |
parent | a4ed950c254ccfd5ee9a55d6de3516c5e850738e (diff) | |
parent | 8decf95ffeffcc8aa14aa743ae398bc38d54b0ac (diff) | |
download | aidl-nougat-mr2-pixel-release.tar.gz |
Always build absolute paths the same wayandroid-cts_7.1_r1android-cts-7.1_r9android-cts-7.1_r8android-cts-7.1_r7android-cts-7.1_r6android-cts-7.1_r5android-cts-7.1_r4android-cts-7.1_r3android-cts-7.1_r29android-cts-7.1_r28android-cts-7.1_r27android-cts-7.1_r26android-cts-7.1_r25android-cts-7.1_r24android-cts-7.1_r23android-cts-7.1_r22android-cts-7.1_r21android-cts-7.1_r20android-cts-7.1_r2android-cts-7.1_r19android-cts-7.1_r18android-cts-7.1_r17android-cts-7.1_r16android-cts-7.1_r15android-cts-7.1_r14android-cts-7.1_r13android-cts-7.1_r12android-cts-7.1_r11android-cts-7.1_r10android-cts-7.1_r1android-7.1.2_r9android-7.1.2_r8android-7.1.2_r6android-7.1.2_r5android-7.1.2_r4android-7.1.2_r39android-7.1.2_r38android-7.1.2_r37android-7.1.2_r36android-7.1.2_r33android-7.1.2_r32android-7.1.2_r30android-7.1.2_r3android-7.1.2_r29android-7.1.2_r28android-7.1.2_r27android-7.1.2_r25android-7.1.2_r24android-7.1.2_r23android-7.1.2_r2android-7.1.2_r19android-7.1.2_r18android-7.1.2_r17android-7.1.2_r16android-7.1.2_r15android-7.1.2_r14android-7.1.2_r13android-7.1.2_r12android-7.1.2_r11android-7.1.2_r10android-7.1.2_r1android-7.1.1_r9android-7.1.1_r8android-7.1.1_r7android-7.1.1_r61android-7.1.1_r60android-7.1.1_r6android-7.1.1_r59android-7.1.1_r58android-7.1.1_r57android-7.1.1_r56android-7.1.1_r55android-7.1.1_r54android-7.1.1_r53android-7.1.1_r52android-7.1.1_r51android-7.1.1_r50android-7.1.1_r49android-7.1.1_r48android-7.1.1_r47android-7.1.1_r46android-7.1.1_r45android-7.1.1_r44android-7.1.1_r43android-7.1.1_r42android-7.1.1_r41android-7.1.1_r40android-7.1.1_r4android-7.1.1_r39android-7.1.1_r38android-7.1.1_r35android-7.1.1_r33android-7.1.1_r32android-7.1.1_r31android-7.1.1_r3android-7.1.1_r28android-7.1.1_r27android-7.1.1_r26android-7.1.1_r25android-7.1.1_r24android-7.1.1_r23android-7.1.1_r22android-7.1.1_r21android-7.1.1_r20android-7.1.1_r2android-7.1.1_r17android-7.1.1_r16android-7.1.1_r15android-7.1.1_r14android-7.1.1_r13android-7.1.1_r12android-7.1.1_r11android-7.1.1_r10android-7.1.1_r1android-7.1.0_r7android-7.1.0_r6android-7.1.0_r5android-7.1.0_r4android-7.1.0_r3android-7.1.0_r2android-7.1.0_r1nougat-mr2.3-releasenougat-mr2.2-releasenougat-mr2.1-releasenougat-mr2-security-releasenougat-mr2-releasenougat-mr2-pixel-releasenougat-mr2-devnougat-mr1.8-releasenougat-mr1.7-releasenougat-mr1.6-releasenougat-mr1.5-releasenougat-mr1.4-releasenougat-mr1.3-releasenougat-mr1.2-releasenougat-mr1.1-releasenougat-mr1-volantis-releasenougat-mr1-security-releasenougat-mr1-releasenougat-mr1-flounder-releasenougat-mr1-devnougat-mr1-cts-releasenougat-dr1-release
am: 8decf95ffe
* commit '8decf95ffeffcc8aa14aa743ae398bc38d54b0ac':
Always build absolute paths the same way
Change-Id: Ic25016aed1f1ee251288e204d32a1fdbcd413dca
-rw-r--r-- | Android.mk | 1 | ||||
-rw-r--r-- | aidl.cpp | 17 | ||||
-rw-r--r-- | io_delegate.cpp | 60 | ||||
-rw-r--r-- | io_delegate.h | 6 | ||||
-rw-r--r-- | io_delegate_unittest.cpp | 46 |
5 files changed, 110 insertions, 20 deletions
@@ -99,6 +99,7 @@ LOCAL_SRC_FILES := \ ast_cpp_unittest.cpp \ ast_java_unittest.cpp \ generate_cpp_unittest.cpp \ + io_delegate_unittest.cpp \ options_unittest.cpp \ tests/end_to_end_tests.cpp \ tests/fake_io_delegate.cpp \ @@ -77,23 +77,10 @@ bool check_filename(const std::string& filename, string expected; string fn; size_t len; - char cwd[MAXPATHLEN]; bool valid = false; -#ifdef _WIN32 - if (isalpha(filename[0]) && filename[1] == ':' - && filename[2] == OS_PATH_SEPARATOR) { -#else - if (filename[0] == OS_PATH_SEPARATOR) { -#endif - fn = filename; - } else { - fn = getcwd(cwd, sizeof(cwd)); - len = fn.length(); - if (fn[len-1] != OS_PATH_SEPARATOR) { - fn += OS_PATH_SEPARATOR; - } - fn += filename; + if (!IoDelegate::GetAbsolutePath(filename, &fn)) { + return false; } if (!package.empty()) { diff --git a/io_delegate.cpp b/io_delegate.cpp index 66891dd1..62be3f5c 100644 --- a/io_delegate.cpp +++ b/io_delegate.cpp @@ -41,6 +41,45 @@ using android::base::Split; namespace android { namespace aidl { +bool IoDelegate::GetAbsolutePath(const string& path, string* absolute_path) { +#ifdef _WIN32 + + char buf[4096]; + DWORD path_len = GetFullPathName(path.c_str(), sizeof(buf), buf, nullptr); + if (path_len <= 0 || path_len >= sizeof(buf)) { + LOG(ERROR) << "Failed to GetFullPathName(" << path << ")"; + return false; + } + *absolute_path = buf; + + return true; + +#else + + if (path.empty()) { + LOG(ERROR) << "Giving up on finding an absolute path to represent the " + "empty string."; + return false; + } + if (path[0] == OS_PATH_SEPARATOR) { + *absolute_path = path; + return true; + } + + char buf[4096]; + if (getcwd(buf, sizeof(buf)) == nullptr) { + LOG(ERROR) << "Path of current working directory does not fit in " + << sizeof(buf) << " bytes"; + return false; + } + + *absolute_path = buf; + *absolute_path += OS_PATH_SEPARATOR; + *absolute_path += path; + return true; +#endif +} + unique_ptr<string> IoDelegate::GetFileContents( const string& filename, const string& content_suffix) const { @@ -112,15 +151,26 @@ bool IoDelegate::CreatePathForFile(const string& path) const { return true; } - string base = "."; - if (path[0] == OS_PATH_SEPARATOR) { + string absolute_path; + if (!GetAbsolutePath(path, &absolute_path)) { + return false; + } + + auto directories = Split(absolute_path, string{1u, OS_PATH_SEPARATOR}); + + // The "base" directory is just the root of the file system. On Windows, + // this will look like "C:\" but on Unix style file systems we get an empty + // string after splitting "/foo" with "/" + string base = directories[0]; + if (base.empty()) { base = "/"; } + directories.erase(directories.begin()); - auto split = Split(path, string{1u, OS_PATH_SEPARATOR}); - split.pop_back(); + // Remove the actual file in question, we're just creating the directory path. + directories.pop_back(); - return CreatedNestedDirs(base, split); + return CreatedNestedDirs(base, directories); } unique_ptr<CodeWriter> IoDelegate::GetCodeWriter( diff --git a/io_delegate.h b/io_delegate.h index b0a703b3..dc9a3e3e 100644 --- a/io_delegate.h +++ b/io_delegate.h @@ -34,6 +34,12 @@ class IoDelegate { IoDelegate() = default; virtual ~IoDelegate() = default; + // Stores an absolute version of |path| to |*absolute_path|, + // possibly prefixing it with the current working directory. + // Returns false and does not set |*absolute_path| on error. + static bool GetAbsolutePath(const std::string& path, + std::string* absolute_path); + // Returns a unique_ptr to the contents of |filename|. // Will append the optional |content_suffix| to the returned contents. virtual std::unique_ptr<std::string> GetFileContents( diff --git a/io_delegate_unittest.cpp b/io_delegate_unittest.cpp new file mode 100644 index 00000000..5227d0bf --- /dev/null +++ b/io_delegate_unittest.cpp @@ -0,0 +1,46 @@ +/* + * 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 <string> + +#include <gtest/gtest.h> + +#include "io_delegate.h" + +using std::string; + +namespace android { +namespace aidl { + +TEST(IoDelegateTest, CannotGetAbsolutePathFromEmptyString) { + string absolute_path; + EXPECT_FALSE(IoDelegate::GetAbsolutePath("", &absolute_path)); + EXPECT_TRUE(absolute_path.empty()); +} + +TEST(IoDelegateTest, CurrentlyInfersLinuxAbsolutePath) { + string absolute_path; + EXPECT_TRUE(IoDelegate::GetAbsolutePath("foo", &absolute_path)); + ASSERT_FALSE(absolute_path.empty()); + // Should find our desired file at the end of |absolute_path| + // But we don't know the prefix, since it's the current working directory + EXPECT_TRUE(absolute_path.rfind("/foo") == absolute_path.length() - 4); + // Whatever our current working directory, the path is absolute. + EXPECT_EQ(absolute_path[0], '/'); +} + +} // namespace android +} // namespace aidl |