Apache Mesos
mkdir.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use this file except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 
13 #ifndef __STOUT_OS_WINDOWS_MKDIR_HPP__
14 #define __STOUT_OS_WINDOWS_MKDIR_HPP__
15 
16 #include <string>
17 #include <vector>
18 
19 #include <stout/error.hpp>
20 #include <stout/nothing.hpp>
21 #include <stout/strings.hpp>
22 #include <stout/try.hpp>
23 #include <stout/windows.hpp>
24 
25 #include <stout/os/exists.hpp>
26 #include <stout/os/constants.hpp>
27 
29 
30 namespace os {
31 
32 // NOTE: `sync` has no effect on Windows.
33 inline Try<Nothing> mkdir(
34  const std::string& directory,
35  bool recursive = true,
36  bool sync = false)
37 {
38  if (!recursive) {
39  // NOTE: We check for existence because parts of certain directories
40  // like `C:\` will return an error if passed to `CreateDirectory`,
41  // even though the drive may already exist.
42  if (os::exists(directory)) {
43  return Nothing();
44  }
45 
46  const std::wstring longpath = ::internal::windows::longpath(directory);
47  if (::CreateDirectoryW(longpath.data(), nullptr) == 0) {
48  return WindowsError("Failed to create directory: " + directory);
49  }
50  } else {
51  // Remove the long path prefix, if it already exists, otherwise the
52  // tokenizer includes the long path prefix (`\\?\`) as the first part
53  // of the path.
54  const std::vector<std::string> tokens = strings::tokenize(
55  strings::remove(directory, os::LONGPATH_PREFIX, strings::Mode::PREFIX),
57 
58  std::string path;
59 
60  foreach (const std::string& token, tokens) {
61  path += token + os::PATH_SEPARATOR;
62  const Try<Nothing> result = mkdir(path, false);
63  if (result.isError()) {
64  return result;
65  }
66  }
67  }
68 
69  return Nothing();
70 }
71 
72 } // namespace os {
73 
74 #endif // __STOUT_OS_WINDOWS_MKDIR_HPP__
Definition: path.hpp:29
bool exists(const std::string &path)
Definition: exists.hpp:26
Definition: nothing.hpp:16
Definition: check.hpp:33
Definition: error.hpp:108
Definition: posix_signalhandler.hpp:23
std::string remove(const std::string &from, const std::string &substring, Mode mode=ANY)
Definition: strings.hpp:41
std::vector< std::string > tokenize(const std::string &s, const std::string &delims, const Option< size_t > &maxTokens=None())
Definition: strings.hpp:139
Try< Nothing > mkdir(const std::string &directory, bool recursive=true, bool sync=false)
Definition: mkdir.hpp:42
bool isError() const
Definition: try.hpp:78
std::wstring longpath(const std::string &path)
Definition: longpath.hpp:38
std::string stringify(int flags)
constexpr char PATH_SEPARATOR
Definition: constants.hpp:24