You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
3.1 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "rtc_base/system/file_wrapper.h"
  11. #include "rtc_base/numerics/safe_conversions.h"
  12. #include <cerrno>
  13. #ifdef _WIN32
  14. #include <Windows.h>
  15. #else
  16. #include <string.h>
  17. #endif
  18. #include <utility>
  19. namespace webrtc {
  20. namespace {
  21. FILE* FileOpen(const char* file_name_utf8, bool read_only, int* error) {
  22. #if defined(_WIN32)
  23. int len = MultiByteToWideChar(CP_UTF8, 0, file_name_utf8, -1, nullptr, 0);
  24. std::wstring wstr(len, 0);
  25. MultiByteToWideChar(CP_UTF8, 0, file_name_utf8, -1, &wstr[0], len);
  26. FILE* file = _wfopen(wstr.c_str(), read_only ? L"rb" : L"wb");
  27. #else
  28. FILE* file = fopen(file_name_utf8, read_only ? "rb" : "wb");
  29. #endif
  30. if (!file && error) {
  31. *error = errno;
  32. }
  33. return file;
  34. }
  35. const char* GetCstrCheckNoEmbeddedNul(const std::string& s) {
  36. const char* p = s.c_str();
  37. RTC_CHECK_EQ(strlen(p), s.size())
  38. << "Invalid filename, containing NUL character";
  39. return p;
  40. }
  41. } // namespace
  42. // static
  43. FileWrapper FileWrapper::OpenReadOnly(const char* file_name_utf8) {
  44. return FileWrapper(FileOpen(file_name_utf8, true, nullptr));
  45. }
  46. // static
  47. FileWrapper FileWrapper::OpenReadOnly(const std::string& file_name_utf8) {
  48. return OpenReadOnly(GetCstrCheckNoEmbeddedNul(file_name_utf8));
  49. }
  50. // static
  51. FileWrapper FileWrapper::OpenWriteOnly(const char* file_name_utf8,
  52. int* error /*=nullptr*/) {
  53. return FileWrapper(FileOpen(file_name_utf8, false, error));
  54. }
  55. // static
  56. FileWrapper FileWrapper::OpenWriteOnly(const std::string& file_name_utf8,
  57. int* error /*=nullptr*/) {
  58. return OpenWriteOnly(GetCstrCheckNoEmbeddedNul(file_name_utf8), error);
  59. }
  60. FileWrapper::FileWrapper(FileWrapper&& other) {
  61. operator=(std::move(other));
  62. }
  63. FileWrapper& FileWrapper::operator=(FileWrapper&& other) {
  64. Close();
  65. file_ = other.file_;
  66. other.file_ = nullptr;
  67. return *this;
  68. }
  69. bool FileWrapper::SeekRelative(int64_t offset) {
  70. RTC_DCHECK(file_);
  71. return fseek(file_, rtc::checked_cast<long>(offset), SEEK_CUR) == 0;
  72. }
  73. bool FileWrapper::SeekTo(int64_t position) {
  74. RTC_DCHECK(file_);
  75. return fseek(file_, rtc::checked_cast<long>(position), SEEK_SET) == 0;
  76. }
  77. bool FileWrapper::Flush() {
  78. RTC_DCHECK(file_);
  79. return fflush(file_) == 0;
  80. }
  81. size_t FileWrapper::Read(void* buf, size_t length) {
  82. RTC_DCHECK(file_);
  83. return fread(buf, 1, length, file_);
  84. }
  85. bool FileWrapper::ReadEof() const {
  86. RTC_DCHECK(file_);
  87. return feof(file_);
  88. }
  89. bool FileWrapper::Write(const void* buf, size_t length) {
  90. RTC_DCHECK(file_);
  91. return fwrite(buf, 1, length, file_) == length;
  92. }
  93. bool FileWrapper::Close() {
  94. if (file_ == nullptr)
  95. return true;
  96. bool success = fclose(file_) == 0;
  97. file_ = nullptr;
  98. return success;
  99. }
  100. } // namespace webrtc