1 /*
2  * Copyright (C) 2007 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 
17 #include "otautil/dirutil.h"
18 
19 #include <dirent.h>
20 #include <errno.h>
21 #include <stdlib.h>
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 #include <unistd.h>
25 
26 #include <string>
27 
28 #include <selinux/label.h>
29 #include <selinux/selinux.h>
30 
31 enum class DirStatus { DMISSING, DDIR, DILLEGAL };
32 
dir_status(const std::string & path)33 static DirStatus dir_status(const std::string& path) {
34   struct stat sb;
35   if (stat(path.c_str(), &sb) == 0) {
36     // Something's there; make sure it's a directory.
37     if (S_ISDIR(sb.st_mode)) {
38       return DirStatus::DDIR;
39     }
40     errno = ENOTDIR;
41     return DirStatus::DILLEGAL;
42   } else if (errno != ENOENT) {
43     // Something went wrong, or something in the path is bad. Can't do anything in this situation.
44     return DirStatus::DILLEGAL;
45   }
46   return DirStatus::DMISSING;
47 }
48 
mkdir_recursively(const std::string & input_path,mode_t mode,bool strip_filename,const selabel_handle * sehnd)49 int mkdir_recursively(const std::string& input_path, mode_t mode, bool strip_filename,
50                       const selabel_handle* sehnd) {
51   // Check for an empty string before we bother making any syscalls.
52   if (input_path.empty()) {
53     errno = ENOENT;
54     return -1;
55   }
56 
57   // Allocate a path that we can modify; stick a slash on the end to make things easier.
58   std::string path = input_path;
59   if (strip_filename) {
60     // Strip everything after the last slash.
61     size_t pos = path.rfind('/');
62     if (pos == std::string::npos) {
63       errno = ENOENT;
64       return -1;
65     }
66     path.resize(pos + 1);
67   } else {
68     // Make sure that the path ends in a slash.
69     path.push_back('/');
70   }
71 
72   // See if it already exists.
73   DirStatus ds = dir_status(path);
74   if (ds == DirStatus::DDIR) {
75     return 0;
76   } else if (ds == DirStatus::DILLEGAL) {
77     return -1;
78   }
79 
80   // Walk up the path from the root and make each level.
81   size_t prev_end = 0;
82   while (prev_end < path.size()) {
83     size_t next_end = path.find('/', prev_end + 1);
84     if (next_end == std::string::npos) {
85       break;
86     }
87     std::string dir_path = path.substr(0, next_end);
88     // Check this part of the path and make a new directory if necessary.
89     switch (dir_status(dir_path)) {
90       case DirStatus::DILLEGAL:
91         // Could happen if some other process/thread is messing with the filesystem.
92         return -1;
93       case DirStatus::DMISSING: {
94         char* secontext = nullptr;
95         if (sehnd) {
96           selabel_lookup(const_cast<selabel_handle*>(sehnd), &secontext, dir_path.c_str(), mode);
97           setfscreatecon(secontext);
98         }
99         int err = mkdir(dir_path.c_str(), mode);
100         if (secontext) {
101           freecon(secontext);
102           setfscreatecon(nullptr);
103         }
104         if (err != 0) {
105           return -1;
106         }
107         break;
108       }
109       default:
110         // Already exists.
111         break;
112     }
113     prev_end = next_end;
114   }
115   return 0;
116 }
117