Andreas Huber | 1aec397 | 2016-08-26 09:26:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 17 | #include "Coordinator.h" |
| 18 | |
| 19 | #include "AST.h" |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 20 | |
Iliyan Malchev | 5e8bcfa | 2016-09-09 16:19:57 -0700 | [diff] [blame] | 21 | #include <algorithm> |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 22 | #include <android-base/logging.h> |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 23 | #include <iterator> |
Iliyan Malchev | a72e0d2 | 2016-09-09 11:03:08 -0700 | [diff] [blame] | 24 | #include <dirent.h> |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 25 | #include <sys/stat.h> |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 26 | |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 27 | extern android::status_t parseFile(android::AST *ast); |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 28 | |
| 29 | namespace android { |
| 30 | |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 31 | Coordinator::Coordinator( |
| 32 | const std::vector<std::string> &packageRootPaths, |
| 33 | const std::vector<std::string> &packageRoots) |
| 34 | : mPackageRootPaths(packageRootPaths), |
| 35 | mPackageRoots(packageRoots) { |
| 36 | // empty |
Andreas Huber | dc98133 | 2016-07-29 15:46:54 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 39 | Coordinator::~Coordinator() { |
| 40 | // empty |
| 41 | } |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 42 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 43 | AST *Coordinator::parse(const FQName &fqName, std::set<AST *> *parsedASTs) { |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 44 | CHECK(fqName.isFullyQualified()); |
| 45 | |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 46 | auto it = mCache.find(fqName); |
| 47 | if (it != mCache.end()) { |
| 48 | AST *ast = (*it).second; |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 49 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 50 | if (ast != nullptr && parsedASTs != nullptr) { |
| 51 | parsedASTs->insert(ast); |
| 52 | } |
| 53 | |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 54 | return ast; |
| 55 | } |
| 56 | |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 57 | // Add this to the cache immediately, so we can discover circular imports. |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 58 | mCache[fqName] = nullptr; |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 59 | |
| 60 | AST *typesAST = nullptr; |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 61 | |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 62 | if (fqName.name() != "types") { |
| 63 | // Any interface file implicitly imports its package's types.hal. |
| 64 | FQName typesName(fqName.package(), fqName.version(), "types"); |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 65 | typesAST = parse(typesName, parsedASTs); |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 66 | |
| 67 | // fall through. |
| 68 | } |
| 69 | |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 70 | std::string path = getPackagePath(fqName); |
| 71 | |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 72 | path.append(fqName.name()); |
| 73 | path.append(".hal"); |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 74 | |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 75 | AST *ast = new AST(this, path); |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 76 | |
| 77 | if (typesAST != NULL) { |
| 78 | // If types.hal for this AST's package existed, make it's defined |
| 79 | // types available to the (about to be parsed) AST right away. |
| 80 | ast->addImportedAST(typesAST); |
| 81 | } |
| 82 | |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 83 | status_t err = parseFile(ast); |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 84 | |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 85 | if (err != OK) { |
Andreas Huber | 6e584b7 | 2016-07-29 16:14:07 -0700 | [diff] [blame] | 86 | // LOG(ERROR) << "parsing '" << path << "' FAILED."; |
| 87 | |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 88 | delete ast; |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 89 | ast = nullptr; |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 90 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 91 | return nullptr; |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 94 | if (ast->package().package() != fqName.package() |
| 95 | || ast->package().version() != fqName.version()) { |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 96 | fprintf(stderr, |
| 97 | "ERROR: File at '%s' does not match expected package and/or " |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 98 | "version.\n", |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 99 | path.c_str()); |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 100 | |
| 101 | err = UNKNOWN_ERROR; |
| 102 | } else { |
| 103 | std::string ifaceName; |
| 104 | if (ast->isInterface(&ifaceName)) { |
| 105 | if (fqName.name() == "types") { |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 106 | fprintf(stderr, |
| 107 | "ERROR: File at '%s' declares an interface '%s' " |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 108 | "instead of the expected types common to the package.\n", |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 109 | path.c_str(), |
| 110 | ifaceName.c_str()); |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 111 | |
| 112 | err = UNKNOWN_ERROR; |
| 113 | } else if (ifaceName != fqName.name()) { |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 114 | fprintf(stderr, |
| 115 | "ERROR: File at '%s' does not declare interface type " |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 116 | "'%s'.\n", |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 117 | path.c_str(), |
| 118 | fqName.name().c_str()); |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 119 | |
| 120 | err = UNKNOWN_ERROR; |
| 121 | } |
| 122 | } else if (fqName.name() != "types") { |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 123 | fprintf(stderr, |
| 124 | "ERROR: File at '%s' declares types rather than the " |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 125 | "expected interface type '%s'.\n", |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 126 | path.c_str(), |
| 127 | fqName.name().c_str()); |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 128 | |
| 129 | err = UNKNOWN_ERROR; |
Andreas Huber | 7c5ddfb | 2016-09-29 13:45:22 -0700 | [diff] [blame] | 130 | } else if (ast->containsInterfaces()) { |
| 131 | fprintf(stderr, |
| 132 | "ERROR: types.hal file at '%s' declares at least one " |
| 133 | "interface type.\n", |
| 134 | path.c_str()); |
| 135 | |
| 136 | err = UNKNOWN_ERROR; |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
| 140 | if (err != OK) { |
| 141 | delete ast; |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 142 | ast = nullptr; |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 143 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 144 | return nullptr; |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 147 | if (parsedASTs != nullptr) { parsedASTs->insert(ast); } |
| 148 | |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 149 | mCache[fqName] = ast; |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 150 | |
| 151 | return ast; |
| 152 | } |
| 153 | |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 154 | std::vector<std::string>::const_iterator |
| 155 | Coordinator::findPackageRoot(const FQName &fqName) const { |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 156 | CHECK(!fqName.package().empty()); |
| 157 | CHECK(!fqName.version().empty()); |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 158 | |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 159 | // Find the right package prefix and path for this FQName. For |
| 160 | // example, if FQName is "android.hardware.nfc@1.0::INfc", and the |
| 161 | // prefix:root is set to [ "android.hardware:hardware/interfaces", |
| 162 | // "vendor.qcom.hardware:vendor/qcom"], then we will identify the |
| 163 | // prefix "android.hardware" and the package root |
| 164 | // "hardware/interfaces". |
| 165 | |
| 166 | // TODO: This now returns on the first match. Throw an error if |
| 167 | // there are multiple hits. |
| 168 | auto it = mPackageRoots.begin(); |
| 169 | for (; it != mPackageRoots.end(); it++) { |
| 170 | if (fqName.package().find(*it) != std::string::npos) { |
| 171 | break; |
| 172 | } |
| 173 | } |
Andreas Huber | 401cd16 | 2016-08-26 10:40:30 -0700 | [diff] [blame] | 174 | CHECK(it != mPackageRoots.end()) |
| 175 | << "Unable to find package root for " << fqName.string(); |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 176 | |
| 177 | return it; |
| 178 | } |
| 179 | |
| 180 | std::string Coordinator::getPackageRoot(const FQName &fqName) const { |
| 181 | auto it = findPackageRoot(fqName); |
| 182 | auto prefix = *it; |
| 183 | return prefix; |
| 184 | } |
| 185 | |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 186 | std::string Coordinator::getPackageRootPath(const FQName &fqName) const { |
| 187 | auto it = findPackageRoot(fqName); |
| 188 | auto root = mPackageRootPaths[std::distance(mPackageRoots.begin(), it)]; |
| 189 | return root; |
| 190 | } |
| 191 | |
Yifan Hong | c893404 | 2016-11-17 17:10:52 -0800 | [diff] [blame] | 192 | std::string Coordinator::getPackageRootOption(const FQName &fqName) const { |
| 193 | return getPackageRoot(fqName) + ":" + getPackageRootPath(fqName); |
| 194 | } |
| 195 | |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 196 | std::string Coordinator::getPackagePath( |
Yifan Hong | 97288ac | 2016-12-12 16:03:51 -0800 | [diff] [blame] | 197 | const FQName &fqName, bool relative, bool sanitized) const { |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 198 | |
| 199 | auto it = findPackageRoot(fqName); |
| 200 | auto prefix = *it; |
| 201 | auto root = mPackageRootPaths[std::distance(mPackageRoots.begin(), it)]; |
| 202 | |
| 203 | // Make sure the prefix ends on a '.' and the root path on a '/' |
| 204 | if ((*--prefix.end()) != '.') { |
| 205 | prefix += '.'; |
| 206 | } |
| 207 | |
| 208 | if ((*--root.end()) != '/') { |
| 209 | root += '/'; |
| 210 | } |
| 211 | |
| 212 | // Given FQName of "android.hardware.nfc@1.0::IFoo" and a prefix |
| 213 | // "android.hardware.", the suffix is "nfc@1.0::IFoo". |
| 214 | const std::string packageSuffix = fqName.package().substr(prefix.length()); |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 215 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 216 | std::string packagePath; |
| 217 | if (!relative) { |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 218 | packagePath = root; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 219 | } |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 220 | |
| 221 | size_t startPos = 0; |
| 222 | size_t dotPos; |
| 223 | while ((dotPos = packageSuffix.find('.', startPos)) != std::string::npos) { |
| 224 | packagePath.append(packageSuffix.substr(startPos, dotPos - startPos)); |
| 225 | packagePath.append("/"); |
| 226 | |
| 227 | startPos = dotPos + 1; |
| 228 | } |
| 229 | CHECK_LT(startPos + 1, packageSuffix.length()); |
| 230 | packagePath.append(packageSuffix.substr(startPos)); |
| 231 | packagePath.append("/"); |
| 232 | |
Yifan Hong | 97288ac | 2016-12-12 16:03:51 -0800 | [diff] [blame] | 233 | packagePath.append(sanitized ? fqName.sanitizedVersion() : fqName.version()); |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 234 | packagePath.append("/"); |
| 235 | |
| 236 | return packagePath; |
| 237 | } |
| 238 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 239 | status_t Coordinator::getPackageInterfaceFiles( |
| 240 | const FQName &package, |
| 241 | std::vector<std::string> *fileNames) const { |
| 242 | fileNames->clear(); |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 243 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 244 | const std::string packagePath = getPackagePath(package); |
| 245 | |
| 246 | DIR *dir = opendir(packagePath.c_str()); |
| 247 | |
| 248 | if (dir == NULL) { |
Steven Moreland | 261370a | 2016-08-29 15:36:59 -0700 | [diff] [blame] | 249 | LOG(ERROR) << "Could not open package path: " << packagePath; |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 250 | return -errno; |
| 251 | } |
| 252 | |
| 253 | struct dirent *ent; |
| 254 | while ((ent = readdir(dir)) != NULL) { |
| 255 | if (ent->d_type != DT_REG) { |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 256 | continue; |
| 257 | } |
| 258 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 259 | const auto suffix = ".hal"; |
| 260 | const auto suffix_len = std::strlen(suffix); |
| 261 | const auto d_namelen = strlen(ent->d_name); |
Andreas Huber | e61e3f7 | 2016-08-03 10:22:03 -0700 | [diff] [blame] | 262 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 263 | if (d_namelen < suffix_len |
| 264 | || strcmp(ent->d_name + d_namelen - suffix_len, suffix)) { |
| 265 | continue; |
Andreas Huber | e61e3f7 | 2016-08-03 10:22:03 -0700 | [diff] [blame] | 266 | } |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 267 | |
| 268 | fileNames->push_back(std::string(ent->d_name, d_namelen - suffix_len)); |
| 269 | } |
| 270 | |
| 271 | closedir(dir); |
| 272 | dir = NULL; |
| 273 | |
Iliyan Malchev | 5e8bcfa | 2016-09-09 16:19:57 -0700 | [diff] [blame] | 274 | std::sort(fileNames->begin(), fileNames->end(), |
| 275 | [](const std::string& lhs, const std::string& rhs) -> bool { |
| 276 | if (lhs == "types") { |
| 277 | return true; |
| 278 | } |
| 279 | if (rhs == "types") { |
| 280 | return false; |
| 281 | } |
| 282 | return lhs < rhs; |
| 283 | }); |
| 284 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 285 | return OK; |
| 286 | } |
| 287 | |
Steven Moreland | aa18683 | 2016-09-26 13:51:43 -0700 | [diff] [blame] | 288 | status_t Coordinator::appendPackageInterfacesToVector( |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 289 | const FQName &package, |
| 290 | std::vector<FQName> *packageInterfaces) const { |
| 291 | packageInterfaces->clear(); |
| 292 | |
| 293 | std::vector<std::string> fileNames; |
| 294 | status_t err = getPackageInterfaceFiles(package, &fileNames); |
| 295 | |
| 296 | if (err != OK) { |
| 297 | return err; |
| 298 | } |
| 299 | |
| 300 | for (const auto &fileName : fileNames) { |
| 301 | FQName subFQName( |
Yifan Hong | 90ea87f | 2016-11-01 14:25:47 -0700 | [diff] [blame] | 302 | package.package() + package.atVersion() + "::" + fileName); |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 303 | |
| 304 | if (!subFQName.isValid()) { |
| 305 | LOG(WARNING) |
| 306 | << "Whole-package import encountered invalid filename '" |
| 307 | << fileName |
| 308 | << "' in package " |
| 309 | << package.package() |
Yifan Hong | 90ea87f | 2016-11-01 14:25:47 -0700 | [diff] [blame] | 310 | << package.atVersion(); |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 311 | |
| 312 | continue; |
| 313 | } |
| 314 | |
| 315 | packageInterfaces->push_back(subFQName); |
Andreas Huber | e61e3f7 | 2016-08-03 10:22:03 -0700 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | return OK; |
| 319 | } |
| 320 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 321 | std::string Coordinator::convertPackageRootToPath(const FQName &fqName) const { |
| 322 | std::string packageRoot = getPackageRoot(fqName); |
| 323 | |
| 324 | if (*(packageRoot.end()--) != '.') { |
| 325 | packageRoot += '.'; |
| 326 | } |
| 327 | |
| 328 | std::replace(packageRoot.begin(), packageRoot.end(), '.', '/'); |
| 329 | |
| 330 | return packageRoot; // now converted to a path |
| 331 | } |
| 332 | |
| 333 | // static |
| 334 | bool Coordinator::MakeParentHierarchy(const std::string &path) { |
| 335 | static const mode_t kMode = 0755; |
| 336 | |
| 337 | size_t start = 1; // Ignore leading '/' |
| 338 | size_t slashPos; |
| 339 | while ((slashPos = path.find("/", start)) != std::string::npos) { |
| 340 | std::string partial = path.substr(0, slashPos); |
| 341 | |
| 342 | struct stat st; |
| 343 | if (stat(partial.c_str(), &st) < 0) { |
| 344 | if (errno != ENOENT) { |
| 345 | return false; |
| 346 | } |
| 347 | |
| 348 | int res = mkdir(partial.c_str(), kMode); |
| 349 | if (res < 0) { |
| 350 | return false; |
| 351 | } |
| 352 | } else if (!S_ISDIR(st.st_mode)) { |
| 353 | return false; |
| 354 | } |
| 355 | |
| 356 | start = slashPos + 1; |
| 357 | } |
| 358 | |
| 359 | return true; |
| 360 | } |
| 361 | |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 362 | } // namespace android |
| 363 | |