Apache Mesos
longpath.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_INTERNAL_WINDOWS_LONGPATH_HPP__
14 #define __STOUT_INTERNAL_WINDOWS_LONGPATH_HPP__
15 
16 #include <string>
17 
18 #include <assert.h>
19 
20 #include <stout/path.hpp>
21 #include <stout/stringify.hpp>
22 
23 #include <stout/os/constants.hpp>
24 
25 
26 namespace internal {
27 namespace windows {
28 
29 // This function idempotently prepends "\\?\" to the given path iff:
30 // (1) The path's length is greater than or equal to 248, the minimum Windows
31 // API limit. This limit is neither `NAME_MAX` nor `PATH_MAX`; it is an
32 // arbitrary limit of `CreateDirectoryW` and is the smallest such limit.
33 // (2) The path is absolute (otherwise the marker is meaningless).
34 // (3) The path does not already have the marker (idempotent).
35 //
36 // It then converts the path to UTF-16, appropriate for use in Unicode versions
37 // of Windows filesystem APIs which support lengths greater than NAME_MAX.
38 inline std::wstring longpath(const std::string& path)
39 {
40  const size_t max_path_length = 248;
41  if (path.size() >= max_path_length &&
42  path::is_absolute(path) &&
43  !strings::startsWith(path, os::LONGPATH_PREFIX)) {
44  return wide_stringify(os::LONGPATH_PREFIX + path);
45  } else {
46  return wide_stringify(path);
47  }
48 }
49 
50 
51 inline std::wstring longpath(const std::wstring& path)
52 {
53  return longpath(stringify(path));
54 }
55 
56 } // namespace windows {
57 } // namespace internal {
58 
59 #endif // __STOUT_INTERNAL_WINDOWS_LONGPATH_HPP__
Definition: path.hpp:29
bool is_absolute(const std::string &path)
Returns whether the given path is an absolute path.
Definition: path.hpp:158
Definition: attributes.hpp:24
std::wstring longpath(const std::string &path)
Definition: longpath.hpp:38
std::string stringify(int flags)
bool startsWith(const std::string &s, const std::string &prefix)
Definition: strings.hpp:381