Apache Mesos
temp.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_TEMP_HPP__
14 #define __STOUT_OS_WINDOWS_TEMP_HPP__
15 
16 #include <string>
17 #include <vector>
18 
19 #include <stout/stringify.hpp>
20 #include <stout/windows.hpp>
21 
22 
23 namespace os {
24 
25 // Attempts to resolve the system-designated temporary directory before
26 // falling back to a sensible default. On Windows, this involves checking
27 // (in this order) environment variables for `TMP`, `TEMP`, and `USERPROFILE`
28 // followed by the Windows directory (`::GetTimePath`). In the unlikely event
29 // where none of these are found, this function returns the current directory.
30 inline std::string temp()
31 {
32  size_t size = static_cast<size_t>(MAX_PATH) + 2;
33  std::vector<wchar_t> buffer;
34  buffer.reserve(size);
35  if (::GetTempPathW(static_cast<DWORD>(size), buffer.data()) == 0) {
36  // Failed, use current directory.
37  if (::GetCurrentDirectoryW(static_cast<DWORD>(size), buffer.data()) == 0) {
38  // Failed, use relative path.
39  return ".";
40  }
41  }
42 
43  return stringify(std::wstring(buffer.data()));
44 }
45 
46 } // namespace os {
47 
48 #endif // __STOUT_OS_WINDOWS_TEMP_HPP__
Try< Bytes > size(const std::string &path, const FollowSymlink follow=FollowSymlink::FOLLOW_SYMLINK)
Definition: stat.hpp:100
Definition: posix_signalhandler.hpp:23
std::string temp()
Definition: temp.hpp:27
std::string stringify(int flags)