Apache Mesos
getcwd.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_GETCWD_HPP__
14 #define __STOUT_OS_WINDOWS_GETCWD_HPP__
15 
16 #include <stout/check.hpp>
17 #include <stout/error.hpp>
18 #include <stout/stringify.hpp>
19 #include <stout/try.hpp>
20 #include <stout/windows.hpp>
21 
23 
24 
25 namespace os {
26 
27 // TODO(josephw): Consider changing the return type to a `Try<std::string>`
28 // so that we do not CHECK-fail upon error.
29 inline std::string getcwd()
30 {
31  // First query for the buffer size required.
32  DWORD length = ::GetCurrentDirectoryW(0, nullptr);
33  CHECK(length != 0) << "Failed to retrieve current directory buffer size";
34 
35  std::vector<wchar_t> buffer;
36  buffer.reserve(static_cast<size_t>(length));
37 
38  DWORD result = ::GetCurrentDirectoryW(length, buffer.data());
39  CHECK(result != 0) << "Failed to determine current directory";
40 
41  return strings::remove(
42  stringify(std::wstring(buffer.data())),
43  os::LONGPATH_PREFIX,
45 }
46 
47 } // namespace os {
48 
49 
50 #endif // __STOUT_OS_WINDOWS_GETCWD_HPP__
std::string getcwd()
Definition: getcwd.hpp:23
std::string remove(const std::string &from, const std::string &substring, Mode mode=ANY)
Definition: strings.hpp:40
std::string stringify(int flags)