Apache Mesos
mkdtemp.hpp
Go to the documentation of this file.
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #ifndef __STOUT_OS_POSIX_MKDTEMP_HPP__
18 #define __STOUT_OS_POSIX_MKDTEMP_HPP__
19 
20 #include <stdlib.h>
21 #include <string.h>
22 
23 #include <string>
24 
25 #include <stout/error.hpp>
26 #include <stout/path.hpp>
27 #include <stout/try.hpp>
28 
29 #include <stout/os/temp.hpp>
30 
31 
32 namespace os {
33 
34 // Creates a temporary directory using the specified path
35 // template. The template may be any path with _6_ `Xs' appended to
36 // it, for example /tmp/temp.XXXXXX. The trailing `Xs' are replaced
37 // with a unique alphanumeric combination.
39  const std::string& path = path::join(os::temp(), "XXXXXX"))
40 {
41  char* temp = new char[path.size() + 1];
42  ::memcpy(temp, path.c_str(), path.size() + 1);
43 
44  if (::mkdtemp(temp) != nullptr) {
45  std::string result(temp);
46  delete[] temp;
47  return result;
48  } else {
49  delete[] temp;
50  return ErrnoError();
51  }
52 }
53 
54 } // namespace os {
55 
56 
57 #endif // __STOUT_OS_POSIX_MKDTEMP_HPP__
Definition: path.hpp:29
Definition: check.hpp:33
Definition: errorbase.hpp:50
Definition: posix_signalhandler.hpp:23
std::string join(const std::string &path1, const std::string &path2, const char _separator=os::PATH_SEPARATOR)
Definition: path.hpp:116
std::string temp()
Definition: temp.hpp:27
Try< std::string > mkdtemp(const std::string &path=path::join(os::temp(),"XXXXXX"))
Definition: mkdtemp.hpp:38