blob: d50b63971c69071705231484185d312e1ce4c0ef [file] [log] [blame]
Andreas Huber1aec3972016-08-26 09:26:32 -07001/*
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 Huber5345ec22016-07-29 13:33:27 -070017#include "Coordinator.h"
18
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070019#include <dirent.h>
Andreas Huberd2943e12016-08-05 11:59:31 -070020#include <sys/stat.h>
Andreas Huber5345ec22016-07-29 13:33:27 -070021
Yifan Hong78b38d12017-02-13 18:14:46 +000022#include <algorithm>
23#include <iterator>
24
25#include <android-base/logging.h>
26#include <hidl-util/StringHelper.h>
27
28#include "AST.h"
29#include "Interface.h"
30
Andreas Huber0d0f9a22016-08-17 10:26:11 -070031extern android::status_t parseFile(android::AST *ast);
Andreas Huber5345ec22016-07-29 13:33:27 -070032
Yifan Hong78b38d12017-02-13 18:14:46 +000033static bool existdir(const char *name) {
34 DIR *dir = opendir(name);
35 if (dir == NULL) {
36 return false;
37 }
38 closedir(dir);
39 return true;
40}
41
Andreas Huber5345ec22016-07-29 13:33:27 -070042namespace android {
43
Andreas Huberdca261f2016-08-04 13:47:51 -070044Coordinator::Coordinator(
45 const std::vector<std::string> &packageRootPaths,
46 const std::vector<std::string> &packageRoots)
47 : mPackageRootPaths(packageRootPaths),
48 mPackageRoots(packageRoots) {
49 // empty
Andreas Huberdc981332016-07-29 15:46:54 -070050}
51
Andreas Huberdca261f2016-08-04 13:47:51 -070052Coordinator::~Coordinator() {
53 // empty
54}
Andreas Huber5345ec22016-07-29 13:33:27 -070055
Andreas Huber39fa7182016-08-19 14:27:33 -070056AST *Coordinator::parse(const FQName &fqName, std::set<AST *> *parsedASTs) {
Andreas Huber68f24592016-07-29 14:53:48 -070057 CHECK(fqName.isFullyQualified());
58
Steven Morelandd537ab02016-09-12 10:32:01 -070059 auto it = mCache.find(fqName);
60 if (it != mCache.end()) {
61 AST *ast = (*it).second;
Andreas Huber5345ec22016-07-29 13:33:27 -070062
Andreas Huber39fa7182016-08-19 14:27:33 -070063 if (ast != nullptr && parsedASTs != nullptr) {
64 parsedASTs->insert(ast);
65 }
66
Andreas Huber5345ec22016-07-29 13:33:27 -070067 return ast;
68 }
69
Andreas Huber68f24592016-07-29 14:53:48 -070070 // Add this to the cache immediately, so we can discover circular imports.
Steven Morelandd537ab02016-09-12 10:32:01 -070071 mCache[fqName] = nullptr;
Andreas Huber39fa7182016-08-19 14:27:33 -070072
73 AST *typesAST = nullptr;
Andreas Huber68f24592016-07-29 14:53:48 -070074
Andreas Huber68f24592016-07-29 14:53:48 -070075 if (fqName.name() != "types") {
76 // Any interface file implicitly imports its package's types.hal.
Yifan Hongfece6ec2017-01-12 17:04:04 -080077 FQName typesName = fqName.getTypesForPackage();
Andreas Huber39fa7182016-08-19 14:27:33 -070078 typesAST = parse(typesName, parsedASTs);
Andreas Huber68f24592016-07-29 14:53:48 -070079
80 // fall through.
81 }
82
Andreas Huberdca261f2016-08-04 13:47:51 -070083 std::string path = getPackagePath(fqName);
84
Andreas Huber68f24592016-07-29 14:53:48 -070085 path.append(fqName.name());
86 path.append(".hal");
Andreas Huber5345ec22016-07-29 13:33:27 -070087
Andreas Huber0d0f9a22016-08-17 10:26:11 -070088 AST *ast = new AST(this, path);
Andreas Huber39fa7182016-08-19 14:27:33 -070089
90 if (typesAST != NULL) {
91 // If types.hal for this AST's package existed, make it's defined
92 // types available to the (about to be parsed) AST right away.
93 ast->addImportedAST(typesAST);
94 }
95
Andreas Huber0d0f9a22016-08-17 10:26:11 -070096 status_t err = parseFile(ast);
Andreas Huber5345ec22016-07-29 13:33:27 -070097
Andreas Huber68f24592016-07-29 14:53:48 -070098 if (err != OK) {
Andreas Huber6e584b72016-07-29 16:14:07 -070099 // LOG(ERROR) << "parsing '" << path << "' FAILED.";
100
Andreas Huber68f24592016-07-29 14:53:48 -0700101 delete ast;
Andreas Huber39fa7182016-08-19 14:27:33 -0700102 ast = nullptr;
Andreas Huber68f24592016-07-29 14:53:48 -0700103
Andreas Huber39fa7182016-08-19 14:27:33 -0700104 return nullptr;
Andreas Huber68f24592016-07-29 14:53:48 -0700105 }
106
Andreas Hubera2723d22016-07-29 15:36:07 -0700107 if (ast->package().package() != fqName.package()
108 || ast->package().version() != fqName.version()) {
Andreas Huber70a59e12016-08-16 12:57:01 -0700109 fprintf(stderr,
110 "ERROR: File at '%s' does not match expected package and/or "
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700111 "version.\n",
Andreas Huber70a59e12016-08-16 12:57:01 -0700112 path.c_str());
Andreas Hubera2723d22016-07-29 15:36:07 -0700113
114 err = UNKNOWN_ERROR;
115 } else {
116 std::string ifaceName;
117 if (ast->isInterface(&ifaceName)) {
118 if (fqName.name() == "types") {
Andreas Huber70a59e12016-08-16 12:57:01 -0700119 fprintf(stderr,
120 "ERROR: File at '%s' declares an interface '%s' "
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700121 "instead of the expected types common to the package.\n",
Andreas Huber70a59e12016-08-16 12:57:01 -0700122 path.c_str(),
123 ifaceName.c_str());
Andreas Hubera2723d22016-07-29 15:36:07 -0700124
125 err = UNKNOWN_ERROR;
126 } else if (ifaceName != fqName.name()) {
Andreas Huber70a59e12016-08-16 12:57:01 -0700127 fprintf(stderr,
128 "ERROR: File at '%s' does not declare interface type "
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700129 "'%s'.\n",
Andreas Huber70a59e12016-08-16 12:57:01 -0700130 path.c_str(),
131 fqName.name().c_str());
Andreas Hubera2723d22016-07-29 15:36:07 -0700132
133 err = UNKNOWN_ERROR;
134 }
135 } else if (fqName.name() != "types") {
Andreas Huber70a59e12016-08-16 12:57:01 -0700136 fprintf(stderr,
137 "ERROR: File at '%s' declares types rather than the "
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700138 "expected interface type '%s'.\n",
Andreas Huber70a59e12016-08-16 12:57:01 -0700139 path.c_str(),
140 fqName.name().c_str());
Andreas Hubera2723d22016-07-29 15:36:07 -0700141
142 err = UNKNOWN_ERROR;
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700143 } else if (ast->containsInterfaces()) {
144 fprintf(stderr,
145 "ERROR: types.hal file at '%s' declares at least one "
146 "interface type.\n",
147 path.c_str());
148
149 err = UNKNOWN_ERROR;
Andreas Hubera2723d22016-07-29 15:36:07 -0700150 }
151 }
152
153 if (err != OK) {
154 delete ast;
Andreas Huber39fa7182016-08-19 14:27:33 -0700155 ast = nullptr;
Andreas Hubera2723d22016-07-29 15:36:07 -0700156
Andreas Huber39fa7182016-08-19 14:27:33 -0700157 return nullptr;
Andreas Hubera2723d22016-07-29 15:36:07 -0700158 }
159
Andreas Huber39fa7182016-08-19 14:27:33 -0700160 if (parsedASTs != nullptr) { parsedASTs->insert(ast); }
161
Yifan Hongfe07bff2017-02-15 14:55:48 -0800162 // put it into the cache now, so that enforceRestrictionsOnPackage can
163 // parse fqName.
Steven Morelandd537ab02016-09-12 10:32:01 -0700164 mCache[fqName] = ast;
Andreas Huber5345ec22016-07-29 13:33:27 -0700165
Yifan Hong78b38d12017-02-13 18:14:46 +0000166 // For each .hal file that hidl-gen parses, the whole package will be checked.
167 err = enforceRestrictionsOnPackage(fqName);
168 if (err != OK) {
Yifan Hongfe07bff2017-02-15 14:55:48 -0800169 mCache[fqName] = nullptr;
Yifan Hong78b38d12017-02-13 18:14:46 +0000170 delete ast;
171 ast = nullptr;
172 return nullptr;
173 }
174
Andreas Huber5345ec22016-07-29 13:33:27 -0700175 return ast;
176}
177
Andreas Huberdca261f2016-08-04 13:47:51 -0700178std::vector<std::string>::const_iterator
179Coordinator::findPackageRoot(const FQName &fqName) const {
Andreas Huber5345ec22016-07-29 13:33:27 -0700180 CHECK(!fqName.package().empty());
181 CHECK(!fqName.version().empty());
Andreas Huber5345ec22016-07-29 13:33:27 -0700182
Andreas Huberdca261f2016-08-04 13:47:51 -0700183 // Find the right package prefix and path for this FQName. For
184 // example, if FQName is "android.hardware.nfc@1.0::INfc", and the
185 // prefix:root is set to [ "android.hardware:hardware/interfaces",
186 // "vendor.qcom.hardware:vendor/qcom"], then we will identify the
187 // prefix "android.hardware" and the package root
188 // "hardware/interfaces".
189
Andreas Huberdca261f2016-08-04 13:47:51 -0700190 auto it = mPackageRoots.begin();
Steven Moreland62709d72017-01-18 10:14:51 -0800191 auto ret = mPackageRoots.end();
Andreas Huberdca261f2016-08-04 13:47:51 -0700192 for (; it != mPackageRoots.end(); it++) {
Steven Moreland62709d72017-01-18 10:14:51 -0800193 if (!fqName.inPackage(*it)) {
194 continue;
Andreas Huberdca261f2016-08-04 13:47:51 -0700195 }
Steven Moreland62709d72017-01-18 10:14:51 -0800196
197 CHECK(ret == mPackageRoots.end())
198 << "Multiple package roots found for " << fqName.string()
199 << " (" << *it << " and " << *ret << ")";
200
201 ret = it;
Andreas Huberdca261f2016-08-04 13:47:51 -0700202 }
Steven Moreland62709d72017-01-18 10:14:51 -0800203 CHECK(ret != mPackageRoots.end())
Andreas Huber401cd162016-08-26 10:40:30 -0700204 << "Unable to find package root for " << fqName.string();
Andreas Huberdca261f2016-08-04 13:47:51 -0700205
Steven Moreland62709d72017-01-18 10:14:51 -0800206 return ret;
Andreas Huberdca261f2016-08-04 13:47:51 -0700207}
208
209std::string Coordinator::getPackageRoot(const FQName &fqName) const {
210 auto it = findPackageRoot(fqName);
211 auto prefix = *it;
212 return prefix;
213}
214
Iliyan Malchev5bb14022016-08-09 15:04:39 -0700215std::string Coordinator::getPackageRootPath(const FQName &fqName) const {
216 auto it = findPackageRoot(fqName);
217 auto root = mPackageRootPaths[std::distance(mPackageRoots.begin(), it)];
218 return root;
219}
220
Yifan Hongc8934042016-11-17 17:10:52 -0800221std::string Coordinator::getPackageRootOption(const FQName &fqName) const {
222 return getPackageRoot(fqName) + ":" + getPackageRootPath(fqName);
223}
224
Andreas Huberdca261f2016-08-04 13:47:51 -0700225std::string Coordinator::getPackagePath(
Yifan Hong97288ac2016-12-12 16:03:51 -0800226 const FQName &fqName, bool relative, bool sanitized) const {
Andreas Huberdca261f2016-08-04 13:47:51 -0700227
228 auto it = findPackageRoot(fqName);
229 auto prefix = *it;
230 auto root = mPackageRootPaths[std::distance(mPackageRoots.begin(), it)];
231
232 // Make sure the prefix ends on a '.' and the root path on a '/'
233 if ((*--prefix.end()) != '.') {
234 prefix += '.';
235 }
236
237 if ((*--root.end()) != '/') {
238 root += '/';
239 }
240
241 // Given FQName of "android.hardware.nfc@1.0::IFoo" and a prefix
242 // "android.hardware.", the suffix is "nfc@1.0::IFoo".
243 const std::string packageSuffix = fqName.package().substr(prefix.length());
Andreas Huber5345ec22016-07-29 13:33:27 -0700244
Andreas Huber881227d2016-08-02 14:20:21 -0700245 std::string packagePath;
246 if (!relative) {
Andreas Huberdca261f2016-08-04 13:47:51 -0700247 packagePath = root;
Andreas Huber881227d2016-08-02 14:20:21 -0700248 }
Andreas Huber5345ec22016-07-29 13:33:27 -0700249
250 size_t startPos = 0;
251 size_t dotPos;
252 while ((dotPos = packageSuffix.find('.', startPos)) != std::string::npos) {
253 packagePath.append(packageSuffix.substr(startPos, dotPos - startPos));
254 packagePath.append("/");
255
256 startPos = dotPos + 1;
257 }
258 CHECK_LT(startPos + 1, packageSuffix.length());
259 packagePath.append(packageSuffix.substr(startPos));
260 packagePath.append("/");
261
Yifan Hong97288ac2016-12-12 16:03:51 -0800262 packagePath.append(sanitized ? fqName.sanitizedVersion() : fqName.version());
Andreas Huber5345ec22016-07-29 13:33:27 -0700263 packagePath.append("/");
264
265 return packagePath;
266}
267
Andreas Huberd2943e12016-08-05 11:59:31 -0700268status_t Coordinator::getPackageInterfaceFiles(
269 const FQName &package,
270 std::vector<std::string> *fileNames) const {
271 fileNames->clear();
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700272
Andreas Huberd2943e12016-08-05 11:59:31 -0700273 const std::string packagePath = getPackagePath(package);
274
275 DIR *dir = opendir(packagePath.c_str());
276
277 if (dir == NULL) {
Steven Moreland261370a2016-08-29 15:36:59 -0700278 LOG(ERROR) << "Could not open package path: " << packagePath;
Andreas Huberd2943e12016-08-05 11:59:31 -0700279 return -errno;
280 }
281
282 struct dirent *ent;
283 while ((ent = readdir(dir)) != NULL) {
284 if (ent->d_type != DT_REG) {
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700285 continue;
286 }
287
Andreas Huberd2943e12016-08-05 11:59:31 -0700288 const auto suffix = ".hal";
289 const auto suffix_len = std::strlen(suffix);
290 const auto d_namelen = strlen(ent->d_name);
Andreas Hubere61e3f72016-08-03 10:22:03 -0700291
Andreas Huberd2943e12016-08-05 11:59:31 -0700292 if (d_namelen < suffix_len
293 || strcmp(ent->d_name + d_namelen - suffix_len, suffix)) {
294 continue;
Andreas Hubere61e3f72016-08-03 10:22:03 -0700295 }
Andreas Huberd2943e12016-08-05 11:59:31 -0700296
297 fileNames->push_back(std::string(ent->d_name, d_namelen - suffix_len));
298 }
299
300 closedir(dir);
301 dir = NULL;
302
Iliyan Malchev5e8bcfa2016-09-09 16:19:57 -0700303 std::sort(fileNames->begin(), fileNames->end(),
304 [](const std::string& lhs, const std::string& rhs) -> bool {
305 if (lhs == "types") {
306 return true;
307 }
308 if (rhs == "types") {
309 return false;
310 }
311 return lhs < rhs;
312 });
313
Andreas Huberd2943e12016-08-05 11:59:31 -0700314 return OK;
315}
316
Steven Morelandaa186832016-09-26 13:51:43 -0700317status_t Coordinator::appendPackageInterfacesToVector(
Andreas Huberd2943e12016-08-05 11:59:31 -0700318 const FQName &package,
319 std::vector<FQName> *packageInterfaces) const {
320 packageInterfaces->clear();
321
322 std::vector<std::string> fileNames;
323 status_t err = getPackageInterfaceFiles(package, &fileNames);
324
325 if (err != OK) {
326 return err;
327 }
328
329 for (const auto &fileName : fileNames) {
330 FQName subFQName(
Yifan Hong90ea87f2016-11-01 14:25:47 -0700331 package.package() + package.atVersion() + "::" + fileName);
Andreas Huberd2943e12016-08-05 11:59:31 -0700332
333 if (!subFQName.isValid()) {
334 LOG(WARNING)
335 << "Whole-package import encountered invalid filename '"
336 << fileName
337 << "' in package "
338 << package.package()
Yifan Hong90ea87f2016-11-01 14:25:47 -0700339 << package.atVersion();
Andreas Huberd2943e12016-08-05 11:59:31 -0700340
341 continue;
342 }
343
344 packageInterfaces->push_back(subFQName);
Andreas Hubere61e3f72016-08-03 10:22:03 -0700345 }
346
347 return OK;
348}
349
Andreas Huberd2943e12016-08-05 11:59:31 -0700350std::string Coordinator::convertPackageRootToPath(const FQName &fqName) const {
351 std::string packageRoot = getPackageRoot(fqName);
352
353 if (*(packageRoot.end()--) != '.') {
354 packageRoot += '.';
355 }
356
357 std::replace(packageRoot.begin(), packageRoot.end(), '.', '/');
358
359 return packageRoot; // now converted to a path
360}
361
Yifan Hong78b38d12017-02-13 18:14:46 +0000362
363status_t Coordinator::enforceRestrictionsOnPackage(const FQName &fqName) {
364 // need fqName to be something like android.hardware.foo@1.0.
365 // name and valueName is ignored.
366 if (fqName.package().empty() || fqName.version().empty()) {
367 LOG(ERROR) << "Cannot enforce restrictions on package " << fqName.string()
368 << ": package or version is missing.";
369 return BAD_VALUE;
370 }
371 FQName package = fqName.getPackageAndVersion();
372 // look up cache.
373 if (mPackagesEnforced.find(package) != mPackagesEnforced.end()) {
374 return OK;
375 }
376
377 // enforce all rules.
378 status_t err = enforceMinorVersionUprevs(package);
379 if (err != OK) {
380 return err;
381 }
382
383 // cache it so that it won't need to be enforced again.
384 mPackagesEnforced.insert(package);
385 return OK;
386}
387
388status_t Coordinator::enforceMinorVersionUprevs(const FQName &currentPackage) {
389 if(!currentPackage.hasVersion()) {
390 LOG(ERROR) << "Cannot enforce minor version uprevs for " << currentPackage.string()
391 << ": missing version.";
392 return UNKNOWN_ERROR;
393 }
394
395 if (currentPackage.getPackageMinorVersion() == 0) {
396 return OK; // ignore for @x.0
397 }
398
399 bool hasPrevPackage = false;
400 FQName prevPacakge = currentPackage;
401 while (prevPacakge.getPackageMinorVersion() > 0) {
402 prevPacakge = prevPacakge.downRev();
403 if (existdir(getPackagePath(prevPacakge).c_str())) {
404 hasPrevPackage = true;
405 break;
406 }
407 }
408 if (!hasPrevPackage) {
409 // no @x.z, where z < y, exist.
410 return OK;
411 }
412
413 if (prevPacakge != currentPackage.downRev()) {
414 LOG(ERROR) << "Cannot enforce minor version uprevs for " << currentPackage.string()
415 << ": Found package " << prevPacakge.string() << " but missing "
416 << currentPackage.downRev().string() << "; you cannot skip a minor version.";
417 return UNKNOWN_ERROR;
418 }
419
420 status_t err;
421 std::vector<FQName> packageInterfaces;
422 err = appendPackageInterfacesToVector(currentPackage, &packageInterfaces);
423 if (err != OK) {
424 return err;
425 }
426
Yifan Hongfe07bff2017-02-15 14:55:48 -0800427 bool extendedInterface = false;
Yifan Hong78b38d12017-02-13 18:14:46 +0000428 for (const FQName &currentFQName : packageInterfaces) {
429 if (currentFQName.name() == "types") {
430 continue; // ignore types.hal
431 }
432 // Assume that currentFQName == android.hardware.foo@2.2::IFoo.
433 // Then prevFQName == android.hardware.foo@2.1::IFoo.
434 const Interface *iface = nullptr;
435 AST *currentAST = parse(currentFQName);
436 if (currentAST != nullptr) {
437 iface = currentAST->getInterface();
438 }
439 if (iface == nullptr) {
Yifan Hongfe07bff2017-02-15 14:55:48 -0800440 if (currentAST == nullptr) {
441 LOG(WARNING) << "Warning: Skipping " << currentFQName.string()
442 << " because it could not be found or parsed"
443 << " or " << currentPackage.string()
444 << " doesn't pass all requirements.";
445 } else {
446 LOG(WARNING) << "Warning: Skipping " << currentFQName.string()
447 << " because the file might contain more than one interface.";
448 }
Yifan Hong78b38d12017-02-13 18:14:46 +0000449 continue;
450 }
451
452 // android.hardware.foo@2.2::IFoo exists. Now make sure
453 // @2.2::IFoo extends @2.1::IFoo. If any interface IFoo in @2.2
454 // ensures this, @2.2 passes the enforcement.
455 FQName prevFQName(prevPacakge.package(), prevPacakge.version(),
456 currentFQName.name());
457 if (iface->superType() == nullptr) {
458 // @2.2::IFoo doesn't extend anything. (This is probably IBase.)
459 continue;
460 }
461 if (iface->superType()->fqName() != prevFQName) {
462 // @2.2::IFoo doesn't extend @2.1::IFoo.
Yifan Hongfe07bff2017-02-15 14:55:48 -0800463 if (iface->superType()->fqName().getPackageAndVersion() ==
464 prevPacakge.getPackageAndVersion()) {
Yifan Hong78b38d12017-02-13 18:14:46 +0000465 LOG(ERROR) << "Cannot enforce minor version uprevs for " << currentPackage.string()
466 << ": " << iface->fqName().string() << " extends "
467 << iface->superType()->fqName().string() << ", which is not allowed.";
468 return UNKNOWN_ERROR;
469 }
470 // @2.2::IFoo extends something from a package with a different package name.
471 // Check the next interface.
472 continue;
473 }
474
Yifan Hongfe07bff2017-02-15 14:55:48 -0800475 // @2.2::IFoo passes. Check next interface.
476 extendedInterface = true;
Yifan Hong78b38d12017-02-13 18:14:46 +0000477 LOG(VERBOSE) << "enforceMinorVersionUprevs: " << currentFQName.string() << " passes.";
Yifan Hong78b38d12017-02-13 18:14:46 +0000478 }
479
Yifan Hongfe07bff2017-02-15 14:55:48 -0800480 if (!extendedInterface) {
481 // No interface extends the interface with the same name in @x.(y-1).
482 LOG(ERROR) << currentPackage.string() << " doesn't pass minor version uprev requirement. "
483 << "Requires at least one interface to extend an interface with the same name "
484 << "from " << prevPacakge.string() << ".";
485 return UNKNOWN_ERROR;
486 }
487
488 return OK;
Yifan Hong78b38d12017-02-13 18:14:46 +0000489}
490
Andreas Huberd2943e12016-08-05 11:59:31 -0700491// static
492bool Coordinator::MakeParentHierarchy(const std::string &path) {
493 static const mode_t kMode = 0755;
494
495 size_t start = 1; // Ignore leading '/'
496 size_t slashPos;
497 while ((slashPos = path.find("/", start)) != std::string::npos) {
498 std::string partial = path.substr(0, slashPos);
499
500 struct stat st;
501 if (stat(partial.c_str(), &st) < 0) {
502 if (errno != ENOENT) {
503 return false;
504 }
505
506 int res = mkdir(partial.c_str(), kMode);
507 if (res < 0) {
508 return false;
509 }
510 } else if (!S_ISDIR(st.st_mode)) {
511 return false;
512 }
513
514 start = slashPos + 1;
515 }
516
517 return true;
518}
519
Andreas Huber5345ec22016-07-29 13:33:27 -0700520} // namespace android
521