诸暨麻将添加redis
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

471 рядки
14 KiB

  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. // Author: laszlocsomor@google.com (Laszlo Csomor)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. // Implementation for long-path-aware open/mkdir/access/etc. on Windows, as well
  34. // as for the supporting utility functions.
  35. //
  36. // These functions convert the input path to an absolute Windows path
  37. // with "\\?\" prefix, then pass that to _wopen/_wmkdir/_waccess/etc.
  38. // (declared in <io.h>) respectively. This allows working with files/directories
  39. // whose paths are longer than MAX_PATH (260 chars).
  40. //
  41. // This file is only used on Windows, it's empty on other platforms.
  42. #if defined(_WIN32) && !defined(_XBOX_ONE)
  43. // Comment this out to fall back to using the ANSI versions (open, mkdir, ...)
  44. // instead of the Unicode ones (_wopen, _wmkdir, ...). Doing so can be useful to
  45. // debug failing tests if that's caused by the long path support.
  46. #define SUPPORT_LONGPATHS
  47. #include <google/protobuf/io/io_win32.h>
  48. #include <ctype.h>
  49. #include <direct.h>
  50. #include <errno.h>
  51. #include <fcntl.h>
  52. #include <io.h>
  53. #include <sys/stat.h>
  54. #include <sys/types.h>
  55. #include <wctype.h>
  56. #ifndef WIN32_LEAN_AND_MEAN
  57. #define WIN32_LEAN_AND_MEAN 1
  58. #endif
  59. #include <windows.h>
  60. #include <memory>
  61. #include <sstream>
  62. #include <string>
  63. #include <vector>
  64. namespace google {
  65. namespace protobuf {
  66. namespace io {
  67. namespace win32 {
  68. namespace {
  69. using std::string;
  70. using std::wstring;
  71. template <typename char_type>
  72. struct CharTraits {
  73. static bool is_alpha(char_type ch);
  74. };
  75. template <>
  76. struct CharTraits<char> {
  77. static bool is_alpha(char ch) { return isalpha(ch); }
  78. };
  79. template <>
  80. struct CharTraits<wchar_t> {
  81. static bool is_alpha(wchar_t ch) { return iswalpha(ch); }
  82. };
  83. template <typename char_type>
  84. bool null_or_empty(const char_type* s) {
  85. return s == nullptr || *s == 0;
  86. }
  87. // Returns true if the path starts with a drive letter, e.g. "c:".
  88. // Note that this won't check for the "\" after the drive letter, so this also
  89. // returns true for "c:foo" (which is "c:\${PWD}\foo").
  90. // This check requires that a path not have a longpath prefix ("\\?\").
  91. template <typename char_type>
  92. bool has_drive_letter(const char_type* ch) {
  93. return CharTraits<char_type>::is_alpha(ch[0]) && ch[1] == ':';
  94. }
  95. // Returns true if the path starts with a longpath prefix ("\\?\").
  96. template <typename char_type>
  97. bool has_longpath_prefix(const char_type* path) {
  98. return path[0] == '\\' && path[1] == '\\' && path[2] == '?' &&
  99. path[3] == '\\';
  100. }
  101. template <typename char_type>
  102. bool is_separator(char_type c) {
  103. return c == '/' || c == '\\';
  104. }
  105. // Returns true if the path starts with a drive specifier (e.g. "c:\").
  106. template <typename char_type>
  107. bool is_path_absolute(const char_type* path) {
  108. return has_drive_letter(path) && is_separator(path[2]);
  109. }
  110. template <typename char_type>
  111. bool is_drive_relative(const char_type* path) {
  112. return has_drive_letter(path) && (path[2] == 0 || !is_separator(path[2]));
  113. }
  114. wstring join_paths(const wstring& path1, const wstring& path2) {
  115. if (path1.empty() || is_path_absolute(path2.c_str()) ||
  116. has_longpath_prefix(path2.c_str())) {
  117. return path2;
  118. }
  119. if (path2.empty()) {
  120. return path1;
  121. }
  122. if (is_separator(path1[path1.size() - 1])) {
  123. return is_separator(path2[0]) ? (path1 + path2.substr(1))
  124. : (path1 + path2);
  125. } else {
  126. return is_separator(path2[0]) ? (path1 + path2)
  127. : (path1 + L'\\' + path2);
  128. }
  129. }
  130. wstring normalize(wstring path) {
  131. if (has_longpath_prefix(path.c_str())) {
  132. path = path.substr(4);
  133. }
  134. static const wstring dot(L".");
  135. static const wstring dotdot(L"..");
  136. const WCHAR* p = path.c_str();
  137. std::vector<wstring> segments;
  138. int segment_start = -1;
  139. // Find the path segments in `path` (separated by "/").
  140. for (int i = 0;; ++i) {
  141. if (!is_separator(p[i]) && p[i] != L'\0') {
  142. // The current character does not end a segment, so start one unless it's
  143. // already started.
  144. if (segment_start < 0) {
  145. segment_start = i;
  146. }
  147. } else if (segment_start >= 0 && i > segment_start) {
  148. // The current character is "/" or "\0", so this ends a segment.
  149. // Add that to `segments` if there's anything to add; handle "." and "..".
  150. wstring segment(p, segment_start, i - segment_start);
  151. segment_start = -1;
  152. if (segment == dotdot) {
  153. if (!segments.empty() &&
  154. (!has_drive_letter(segments[0].c_str()) || segments.size() > 1)) {
  155. segments.pop_back();
  156. }
  157. } else if (segment != dot && !segment.empty()) {
  158. segments.push_back(segment);
  159. }
  160. }
  161. if (p[i] == L'\0') {
  162. break;
  163. }
  164. }
  165. // Handle the case when `path` is just a drive specifier (or some degenerate
  166. // form of it, e.g. "c:\..").
  167. if (segments.size() == 1 && segments[0].size() == 2 &&
  168. has_drive_letter(segments[0].c_str())) {
  169. return segments[0] + L'\\';
  170. }
  171. // Join all segments.
  172. bool first = true;
  173. std::wstringstream result;
  174. for (int i = 0; i < segments.size(); ++i) {
  175. if (!first) {
  176. result << L'\\';
  177. }
  178. first = false;
  179. result << segments[i];
  180. }
  181. // Preserve trailing separator if the input contained it.
  182. if (!path.empty() && is_separator(p[path.size() - 1])) {
  183. result << L'\\';
  184. }
  185. return result.str();
  186. }
  187. bool as_windows_path(const char* path, wstring* result) {
  188. if (null_or_empty(path)) {
  189. result->clear();
  190. return true;
  191. }
  192. wstring wpath;
  193. if (!strings::utf8_to_wcs(path, &wpath)) {
  194. return false;
  195. }
  196. if (has_longpath_prefix(wpath.c_str())) {
  197. *result = wpath;
  198. return true;
  199. }
  200. if (is_separator(path[0]) || is_drive_relative(path)) {
  201. return false;
  202. }
  203. if (!is_path_absolute(wpath.c_str())) {
  204. int size = ::GetCurrentDirectoryW(0, nullptr);
  205. if (size == 0 && GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
  206. return false;
  207. }
  208. std::unique_ptr<WCHAR[]> wcwd(new WCHAR[size]);
  209. ::GetCurrentDirectoryW(size, wcwd.get());
  210. wpath = join_paths(wcwd.get(), wpath);
  211. }
  212. wpath = normalize(wpath);
  213. if (!has_longpath_prefix(wpath.c_str())) {
  214. // Add the "\\?\" prefix unconditionally. This way we prevent the Win32 API
  215. // from processing the path and "helpfully" removing trailing dots from the
  216. // path, for example.
  217. // See https://github.com/bazelbuild/bazel/issues/2935
  218. wpath = wstring(L"\\\\?\\") + wpath;
  219. }
  220. *result = wpath;
  221. return true;
  222. }
  223. } // namespace
  224. int open(const char* path, int flags, int mode) {
  225. #ifdef SUPPORT_LONGPATHS
  226. wstring wpath;
  227. if (!as_windows_path(path, &wpath)) {
  228. errno = ENOENT;
  229. return -1;
  230. }
  231. return ::_wopen(wpath.c_str(), flags, mode);
  232. #else
  233. return ::_open(path, flags, mode);
  234. #endif
  235. }
  236. int mkdir(const char* path, int _mode) {
  237. #ifdef SUPPORT_LONGPATHS
  238. wstring wpath;
  239. if (!as_windows_path(path, &wpath)) {
  240. errno = ENOENT;
  241. return -1;
  242. }
  243. return ::_wmkdir(wpath.c_str());
  244. #else // not SUPPORT_LONGPATHS
  245. return ::_mkdir(path);
  246. #endif // not SUPPORT_LONGPATHS
  247. }
  248. int access(const char* path, int mode) {
  249. #ifdef SUPPORT_LONGPATHS
  250. wstring wpath;
  251. if (!as_windows_path(path, &wpath)) {
  252. errno = ENOENT;
  253. return -1;
  254. }
  255. return ::_waccess(wpath.c_str(), mode);
  256. #else
  257. return ::_access(path, mode);
  258. #endif
  259. }
  260. int chdir(const char* path) {
  261. #ifdef SUPPORT_LONGPATHS
  262. wstring wpath;
  263. if (!as_windows_path(path, &wpath)) {
  264. errno = ENOENT;
  265. return -1;
  266. }
  267. return ::_wchdir(wpath.c_str());
  268. #else
  269. return ::_chdir(path);
  270. #endif
  271. }
  272. int stat(const char* path, struct _stat* buffer) {
  273. #ifdef SUPPORT_LONGPATHS
  274. wstring wpath;
  275. if (!as_windows_path(path, &wpath)) {
  276. errno = ENOENT;
  277. return -1;
  278. }
  279. return ::_wstat(wpath.c_str(), buffer);
  280. #else // not SUPPORT_LONGPATHS
  281. return ::_stat(path, buffer);
  282. #endif // not SUPPORT_LONGPATHS
  283. }
  284. FILE* fopen(const char* path, const char* mode) {
  285. #ifdef SUPPORT_LONGPATHS
  286. if (null_or_empty(path)) {
  287. errno = EINVAL;
  288. return nullptr;
  289. }
  290. wstring wpath;
  291. if (!as_windows_path(path, &wpath)) {
  292. errno = ENOENT;
  293. return nullptr;
  294. }
  295. wstring wmode;
  296. if (!strings::utf8_to_wcs(mode, &wmode)) {
  297. errno = EINVAL;
  298. return nullptr;
  299. }
  300. return ::_wfopen(wpath.c_str(), wmode.c_str());
  301. #else
  302. return ::fopen(path, mode);
  303. #endif
  304. }
  305. int close(int fd) { return ::_close(fd); }
  306. int dup(int fd) { return ::_dup(fd); }
  307. int dup2(int fd1, int fd2) { return ::_dup2(fd1, fd2); }
  308. int read(int fd, void* buffer, size_t size) {
  309. return ::_read(fd, buffer, size);
  310. }
  311. int setmode(int fd, int mode) { return ::_setmode(fd, mode); }
  312. int write(int fd, const void* buffer, size_t size) {
  313. return ::_write(fd, buffer, size);
  314. }
  315. wstring testonly_utf8_to_winpath(const char* path) {
  316. wstring wpath;
  317. return as_windows_path(path, &wpath) ? wpath : wstring();
  318. }
  319. ExpandWildcardsResult ExpandWildcards(
  320. const string& path, std::function<void(const string&)> consume) {
  321. if (path.find_first_of("*?") == string::npos) {
  322. // There are no wildcards in the path, we don't need to expand it.
  323. consume(path);
  324. return ExpandWildcardsResult::kSuccess;
  325. }
  326. wstring wpath;
  327. if (!as_windows_path(path.c_str(), &wpath)) {
  328. return ExpandWildcardsResult::kErrorInputPathConversion;
  329. }
  330. static const wstring kDot = L".";
  331. static const wstring kDotDot = L"..";
  332. WIN32_FIND_DATAW metadata;
  333. HANDLE handle = ::FindFirstFileW(wpath.c_str(), &metadata);
  334. if (handle == INVALID_HANDLE_VALUE) {
  335. // The pattern does not match any files (or directories).
  336. return ExpandWildcardsResult::kErrorNoMatchingFile;
  337. }
  338. string::size_type pos = path.find_last_of("\\/");
  339. string dirname;
  340. if (pos != string::npos) {
  341. dirname = path.substr(0, pos + 1);
  342. }
  343. ExpandWildcardsResult matched = ExpandWildcardsResult::kErrorNoMatchingFile;
  344. do {
  345. // Ignore ".", "..", and directories.
  346. if ((metadata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0 &&
  347. kDot != metadata.cFileName && kDotDot != metadata.cFileName) {
  348. matched = ExpandWildcardsResult::kSuccess;
  349. string filename;
  350. if (!strings::wcs_to_utf8(metadata.cFileName, &filename)) {
  351. return ExpandWildcardsResult::kErrorOutputPathConversion;
  352. }
  353. if (dirname.empty()) {
  354. consume(filename);
  355. } else {
  356. consume(dirname + filename);
  357. }
  358. }
  359. } while (::FindNextFileW(handle, &metadata));
  360. FindClose(handle);
  361. return matched;
  362. }
  363. namespace strings {
  364. bool wcs_to_mbs(const WCHAR* s, string* out, bool outUtf8) {
  365. if (null_or_empty(s)) {
  366. out->clear();
  367. return true;
  368. }
  369. BOOL usedDefaultChar = FALSE;
  370. SetLastError(0);
  371. int size = WideCharToMultiByte(
  372. outUtf8 ? CP_UTF8 : CP_ACP, 0, s, -1, nullptr, 0, nullptr,
  373. outUtf8 ? nullptr : &usedDefaultChar);
  374. if ((size == 0 && GetLastError() != ERROR_INSUFFICIENT_BUFFER)
  375. || usedDefaultChar) {
  376. return false;
  377. }
  378. std::unique_ptr<CHAR[]> astr(new CHAR[size]);
  379. WideCharToMultiByte(
  380. outUtf8 ? CP_UTF8 : CP_ACP, 0, s, -1, astr.get(), size, nullptr, nullptr);
  381. out->assign(astr.get());
  382. return true;
  383. }
  384. bool mbs_to_wcs(const char* s, wstring* out, bool inUtf8) {
  385. if (null_or_empty(s)) {
  386. out->clear();
  387. return true;
  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. bool utf8_to_wcs(const char* input, wstring* out) {
  402. return mbs_to_wcs(input, out, true);
  403. }
  404. bool wcs_to_utf8(const wchar_t* input, string* out) {
  405. return wcs_to_mbs(input, out, true);
  406. }
  407. } // namespace strings
  408. } // namespace win32
  409. } // namespace io
  410. } // namespace protobuf
  411. } // namespace google
  412. #endif // defined(_WIN32)