platform_utils.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2016 The Bazel Authors. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "third_party/ijar/platform_utils.h"
  15. #include <limits.h>
  16. #include <stdio.h>
  17. #if defined(_WIN32) || defined(__CYGWIN__)
  18. #include <windows.h>
  19. #else // !(defined(_WIN32) || defined(__CYGWIN__))
  20. #include <sys/stat.h>
  21. #include <sys/types.h>
  22. #include <unistd.h>
  23. #endif // defined(_WIN32) || defined(__CYGWIN__)
  24. #include <string>
  25. namespace devtools_ijar {
  26. using std::string;
  27. bool stat_file(const char* path, Stat* result) {
  28. #if defined(_WIN32) || defined(__CYGWIN__)
  29. std::wstring wpath;
  30. std::string error;
  31. bool success = false;
  32. BY_HANDLE_FILE_INFORMATION info;
  33. HANDLE handle = ::CreateFileW(
  34. /* lpFileName */ wpath.c_str(),
  35. /* dwDesiredAccess */ GENERIC_READ,
  36. /* dwShareMode */ FILE_SHARE_READ,
  37. /* lpSecurityAttributes */ NULL,
  38. /* dwCreationDisposition */ OPEN_EXISTING,
  39. /* dwFlagsAndAttributes */ FILE_ATTRIBUTE_NORMAL,
  40. /* hTemplateFile */ NULL);
  41. if (handle == INVALID_HANDLE_VALUE) {
  42. // Opening it as a file failed, try opening it as a directory.
  43. handle = ::CreateFileW(
  44. /* lpFileName */ wpath.c_str(),
  45. /* dwDesiredAccess */ GENERIC_READ,
  46. /* dwShareMode */ FILE_SHARE_READ,
  47. /* lpSecurityAttributes */ NULL,
  48. /* dwCreationDisposition */ OPEN_EXISTING,
  49. /* dwFlagsAndAttributes */ FILE_FLAG_BACKUP_SEMANTICS,
  50. /* hTemplateFile */ NULL);
  51. }
  52. if (handle != INVALID_HANDLE_VALUE &&
  53. ::GetFileInformationByHandle(handle, &info)) {
  54. success = true;
  55. bool is_dir = (info.dwFileAttributes != INVALID_FILE_ATTRIBUTES) &&
  56. (info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
  57. // TODO(laszlocsomor): use info.nFileSizeHigh after we updated total_size to
  58. // be u8 type.
  59. result->total_size = is_dir ? 0 : info.nFileSizeLow;
  60. // TODO(laszlocsomor): query the actual permissions and write in file_mode.
  61. result->file_mode = 0777;
  62. result->is_directory = is_dir;
  63. }
  64. ::CloseHandle(handle);
  65. return success;
  66. #else // !(defined(_WIN32) || defined(__CYGWIN__))
  67. struct stat statst;
  68. if (stat(path, &statst) < 0) {
  69. return false;
  70. }
  71. result->total_size = statst.st_size;
  72. result->file_mode = statst.st_mode;
  73. result->is_directory = (statst.st_mode & S_IFDIR) != 0;
  74. return true;
  75. #endif
  76. }
  77. } // namespace devtools_ijar