• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 //     * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //     * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 //     * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 // Author: laszlocsomor@google.com (Laszlo Csomor)
32 //  Based on original Protocol Buffers design by
33 //  Sanjay Ghemawat, Jeff Dean, and others.
34 
35 // Implementation for long-path-aware open/mkdir/access/etc. on Windows, as well
36 // as for the supporting utility functions.
37 //
38 // These functions convert the input path to an absolute Windows path
39 // with "\\?\" prefix, then pass that to _wopen/_wmkdir/_waccess/etc.
40 // (declared in <io.h>) respectively. This allows working with files/directories
41 // whose paths are longer than MAX_PATH (260 chars).
42 //
43 // This file is only used on Windows, it's empty on other platforms.
44 
45 #if defined(_WIN32) && !defined(_XBOX_ONE)
46 
47 // Comment this out to fall back to using the ANSI versions (open, mkdir, ...)
48 // instead of the Unicode ones (_wopen, _wmkdir, ...). Doing so can be useful to
49 // debug failing tests if that's caused by the long path support.
50 #define SUPPORT_LONGPATHS
51 
52 #include <ctype.h>
53 #include <direct.h>
54 #include <errno.h>
55 #include <fcntl.h>
56 #include <io.h>
57 #include <memory>
58 #include <sys/stat.h>
59 #include <sys/types.h>
60 #include <wctype.h>
61 #include <windows.h>
62 
63 #include <google/protobuf/io/io_win32.h>
64 
65 #include <memory>
66 #include <sstream>
67 #include <string>
68 #include <vector>
69 
70 namespace google {
71 namespace protobuf {
72 namespace io {
73 namespace win32 {
74 namespace {
75 
76 using std::string;
77 using std::wstring;
78 
79 template <typename char_type>
80 struct CharTraits {
81   static bool is_alpha(char_type ch);
82 };
83 
84 template <>
85 struct CharTraits<char> {
is_alphagoogle::protobuf::io::win32::__anonef1ba9c40111::CharTraits86   static bool is_alpha(char ch) { return isalpha(ch); }
87 };
88 
89 template <>
90 struct CharTraits<wchar_t> {
is_alphagoogle::protobuf::io::win32::__anonef1ba9c40111::CharTraits91   static bool is_alpha(wchar_t ch) { return iswalpha(ch); }
92 };
93 
94 template <typename char_type>
null_or_empty(const char_type * s)95 bool null_or_empty(const char_type* s) {
96   return s == nullptr || *s == 0;
97 }
98 
99 // Returns true if the path starts with a drive letter, e.g. "c:".
100 // Note that this won't check for the "\" after the drive letter, so this also
101 // returns true for "c:foo" (which is "c:\${PWD}\foo").
102 // This check requires that a path not have a longpath prefix ("\\?\").
103 template <typename char_type>
has_drive_letter(const char_type * ch)104 bool has_drive_letter(const char_type* ch) {
105   return CharTraits<char_type>::is_alpha(ch[0]) && ch[1] == ':';
106 }
107 
108 // Returns true if the path starts with a longpath prefix ("\\?\").
109 template <typename char_type>
has_longpath_prefix(const char_type * path)110 bool has_longpath_prefix(const char_type* path) {
111   return path[0] == '\\' && path[1] == '\\' && path[2] == '?' &&
112          path[3] == '\\';
113 }
114 
115 template <typename char_type>
is_separator(char_type c)116 bool is_separator(char_type c) {
117   return c == '/' || c == '\\';
118 }
119 
120 // Returns true if the path starts with a drive specifier (e.g. "c:\").
121 template <typename char_type>
is_path_absolute(const char_type * path)122 bool is_path_absolute(const char_type* path) {
123   return has_drive_letter(path) && is_separator(path[2]);
124 }
125 
126 template <typename char_type>
is_drive_relative(const char_type * path)127 bool is_drive_relative(const char_type* path) {
128   return has_drive_letter(path) && (path[2] == 0 || !is_separator(path[2]));
129 }
130 
join_paths(const wstring & path1,const wstring & path2)131 wstring join_paths(const wstring& path1, const wstring& path2) {
132   if (path1.empty() || is_path_absolute(path2.c_str()) ||
133       has_longpath_prefix(path2.c_str())) {
134     return path2;
135   }
136   if (path2.empty()) {
137     return path1;
138   }
139 
140   if (is_separator(path1[path1.size() - 1])) {
141     return is_separator(path2[0]) ? (path1 + path2.substr(1))
142                                        : (path1 + path2);
143   } else {
144     return is_separator(path2[0]) ? (path1 + path2)
145                                        : (path1 + L'\\' + path2);
146   }
147 }
148 
normalize(wstring path)149 wstring normalize(wstring path) {
150   if (has_longpath_prefix(path.c_str())) {
151     path = path.substr(4);
152   }
153 
154   static const wstring dot(L".");
155   static const wstring dotdot(L"..");
156   const WCHAR* p = path.c_str();
157 
158   std::vector<wstring> segments;
159   int segment_start = -1;
160   // Find the path segments in `path` (separated by "/").
161   for (int i = 0;; ++i) {
162     if (!is_separator(p[i]) && p[i] != L'\0') {
163       // The current character does not end a segment, so start one unless it's
164       // already started.
165       if (segment_start < 0) {
166         segment_start = i;
167       }
168     } else if (segment_start >= 0 && i > segment_start) {
169       // The current character is "/" or "\0", so this ends a segment.
170       // Add that to `segments` if there's anything to add; handle "." and "..".
171       wstring segment(p, segment_start, i - segment_start);
172       segment_start = -1;
173       if (segment == dotdot) {
174         if (!segments.empty() &&
175             (!has_drive_letter(segments[0].c_str()) || segments.size() > 1)) {
176           segments.pop_back();
177         }
178       } else if (segment != dot && !segment.empty()) {
179         segments.push_back(segment);
180       }
181     }
182     if (p[i] == L'\0') {
183       break;
184     }
185   }
186 
187   // Handle the case when `path` is just a drive specifier (or some degenerate
188   // form of it, e.g. "c:\..").
189   if (segments.size() == 1 && segments[0].size() == 2 &&
190       has_drive_letter(segments[0].c_str())) {
191     return segments[0] + L'\\';
192   }
193 
194   // Join all segments.
195   bool first = true;
196   std::wstringstream result;
197   for (int i = 0; i < segments.size(); ++i) {
198     if (!first) {
199       result << L'\\';
200     }
201     first = false;
202     result << segments[i];
203   }
204   // Preserve trailing separator if the input contained it.
205   if (!path.empty() && is_separator(p[path.size() - 1])) {
206     result << L'\\';
207   }
208   return result.str();
209 }
210 
as_windows_path(const char * path,wstring * result)211 bool as_windows_path(const char* path, wstring* result) {
212   if (null_or_empty(path)) {
213     result->clear();
214     return true;
215   }
216   wstring wpath;
217   if (!strings::utf8_to_wcs(path, &wpath)) {
218     return false;
219   }
220   if (has_longpath_prefix(wpath.c_str())) {
221     *result = wpath;
222     return true;
223   }
224   if (is_separator(path[0]) || is_drive_relative(path)) {
225     return false;
226   }
227 
228 
229   if (!is_path_absolute(wpath.c_str())) {
230     int size = ::GetCurrentDirectoryW(0, nullptr);
231     if (size == 0 && GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
232       return false;
233     }
234     std::unique_ptr<WCHAR[]> wcwd(new WCHAR[size]);
235     ::GetCurrentDirectoryW(size, wcwd.get());
236     wpath = join_paths(wcwd.get(), wpath);
237   }
238   wpath = normalize(wpath);
239   if (!has_longpath_prefix(wpath.c_str())) {
240     // Add the "\\?\" prefix unconditionally. This way we prevent the Win32 API
241     // from processing the path and "helpfully" removing trailing dots from the
242     // path, for example.
243     // See https://github.com/bazelbuild/bazel/issues/2935
244     wpath = wstring(L"\\\\?\\") + wpath;
245   }
246   *result = wpath;
247   return true;
248 }
249 
250 }  // namespace
251 
open(const char * path,int flags,int mode)252 int open(const char* path, int flags, int mode) {
253 #ifdef SUPPORT_LONGPATHS
254   wstring wpath;
255   if (!as_windows_path(path, &wpath)) {
256     errno = ENOENT;
257     return -1;
258   }
259   return ::_wopen(wpath.c_str(), flags, mode);
260 #else
261   return ::_open(path, flags, mode);
262 #endif
263 }
264 
mkdir(const char * path,int _mode)265 int mkdir(const char* path, int _mode) {
266 #ifdef SUPPORT_LONGPATHS
267   wstring wpath;
268   if (!as_windows_path(path, &wpath)) {
269     errno = ENOENT;
270     return -1;
271   }
272   return ::_wmkdir(wpath.c_str());
273 #else   // not SUPPORT_LONGPATHS
274   return ::_mkdir(path);
275 #endif  // not SUPPORT_LONGPATHS
276 }
277 
access(const char * path,int mode)278 int access(const char* path, int mode) {
279 #ifdef SUPPORT_LONGPATHS
280   wstring wpath;
281   if (!as_windows_path(path, &wpath)) {
282     errno = ENOENT;
283     return -1;
284   }
285   return ::_waccess(wpath.c_str(), mode);
286 #else
287   return ::_access(path, mode);
288 #endif
289 }
290 
chdir(const char * path)291 int chdir(const char* path) {
292 #ifdef SUPPORT_LONGPATHS
293   wstring wpath;
294   if (!as_windows_path(path, &wpath)) {
295     errno = ENOENT;
296     return -1;
297   }
298   return ::_wchdir(wpath.c_str());
299 #else
300   return ::_chdir(path);
301 #endif
302 }
303 
stat(const char * path,struct _stat * buffer)304 int stat(const char* path, struct _stat* buffer) {
305 #ifdef SUPPORT_LONGPATHS
306   wstring wpath;
307   if (!as_windows_path(path, &wpath)) {
308     errno = ENOENT;
309     return -1;
310   }
311   return ::_wstat(wpath.c_str(), buffer);
312 #else   // not SUPPORT_LONGPATHS
313   return ::_stat(path, buffer);
314 #endif  // not SUPPORT_LONGPATHS
315 }
316 
fopen(const char * path,const char * mode)317 FILE* fopen(const char* path, const char* mode) {
318 #ifdef SUPPORT_LONGPATHS
319   if (null_or_empty(path)) {
320     errno = EINVAL;
321     return nullptr;
322   }
323   wstring wpath;
324   if (!as_windows_path(path, &wpath)) {
325     errno = ENOENT;
326     return nullptr;
327   }
328   wstring wmode;
329   if (!strings::utf8_to_wcs(mode, &wmode)) {
330     errno = EINVAL;
331     return nullptr;
332   }
333   return ::_wfopen(wpath.c_str(), wmode.c_str());
334 #else
335   return ::fopen(path, mode);
336 #endif
337 }
338 
close(int fd)339 int close(int fd) { return ::_close(fd); }
340 
dup(int fd)341 int dup(int fd) { return ::_dup(fd); }
342 
dup2(int fd1,int fd2)343 int dup2(int fd1, int fd2) { return ::_dup2(fd1, fd2); }
344 
read(int fd,void * buffer,size_t size)345 int read(int fd, void* buffer, size_t size) {
346   return ::_read(fd, buffer, size);
347 }
348 
setmode(int fd,int mode)349 int setmode(int fd, int mode) { return ::_setmode(fd, mode); }
350 
write(int fd,const void * buffer,size_t size)351 int write(int fd, const void* buffer, size_t size) {
352   return ::_write(fd, buffer, size);
353 }
354 
testonly_utf8_to_winpath(const char * path)355 wstring testonly_utf8_to_winpath(const char* path) {
356   wstring wpath;
357   return as_windows_path(path, &wpath) ? wpath : wstring();
358 }
359 
360 namespace strings {
361 
wcs_to_mbs(const WCHAR * s,string * out,bool outUtf8)362 bool wcs_to_mbs(const WCHAR* s, string* out, bool outUtf8) {
363   if (null_or_empty(s)) {
364     out->clear();
365     return true;
366   }
367   BOOL usedDefaultChar = FALSE;
368   SetLastError(0);
369   int size = WideCharToMultiByte(
370       outUtf8 ? CP_UTF8 : CP_ACP, 0, s, -1, nullptr, 0, nullptr,
371       outUtf8 ? nullptr : &usedDefaultChar);
372   if ((size == 0 && GetLastError() != ERROR_INSUFFICIENT_BUFFER)
373       || usedDefaultChar) {
374     return false;
375   }
376   std::unique_ptr<CHAR[]> astr(new CHAR[size]);
377   WideCharToMultiByte(
378       outUtf8 ? CP_UTF8 : CP_ACP, 0, s, -1, astr.get(), size, nullptr, nullptr);
379   out->assign(astr.get());
380   return true;
381 }
382 
mbs_to_wcs(const char * s,wstring * out,bool inUtf8)383 bool mbs_to_wcs(const char* s, wstring* out, bool inUtf8) {
384   if (null_or_empty(s)) {
385     out->clear();
386     return true;
387   }
388 
389   SetLastError(0);
390   int size =
391       MultiByteToWideChar(inUtf8 ? CP_UTF8 : CP_ACP, 0, s, -1, nullptr, 0);
392   if (size == 0 && GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
393     return false;
394   }
395   std::unique_ptr<WCHAR[]> wstr(new WCHAR[size]);
396   MultiByteToWideChar(
397       inUtf8 ? CP_UTF8 : CP_ACP, 0, s, -1, wstr.get(), size + 1);
398   out->assign(wstr.get());
399   return true;
400 }
401 
utf8_to_wcs(const char * input,wstring * out)402 bool utf8_to_wcs(const char* input, wstring* out) {
403   return mbs_to_wcs(input, out, true);
404 }
405 
wcs_to_utf8(const wchar_t * input,string * out)406 bool wcs_to_utf8(const wchar_t* input, string* out) {
407   return wcs_to_mbs(input, out, true);
408 }
409 
410 }  // namespace strings
411 }  // namespace win32
412 }  // namespace io
413 }  // namespace protobuf
414 }  // namespace google
415 
416 #endif  // defined(_WIN32)
417