file.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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: kenton@google.com (Kenton Varda)
  31. // emulates google3/file/base/file.cc
  32. #include <google/protobuf/testing/file.h>
  33. #include <stdio.h>
  34. #include <sys/stat.h>
  35. #include <sys/types.h>
  36. #ifdef _MSC_VER
  37. #define WIN32_LEAN_AND_MEAN // yeah, right
  38. #include <windows.h> // Find*File(). :(
  39. // #include <direct.h>
  40. #else
  41. #include <dirent.h>
  42. #include <unistd.h>
  43. #endif
  44. #include <errno.h>
  45. #include <google/protobuf/stubs/io_win32.h>
  46. namespace google {
  47. namespace protobuf {
  48. #ifdef _WIN32
  49. // Windows doesn't have symbolic links.
  50. #define lstat stat
  51. // DO NOT include <io.h>, instead create functions in io_win32.{h,cc} and import
  52. // them like we do below.
  53. #endif
  54. #ifdef _WIN32
  55. using google::protobuf::internal::win32::access;
  56. using google::protobuf::internal::win32::chdir;
  57. using google::protobuf::internal::win32::fopen;
  58. using google::protobuf::internal::win32::mkdir;
  59. using google::protobuf::internal::win32::stat;
  60. #endif
  61. bool File::Exists(const string& name) {
  62. return access(name.c_str(), F_OK) == 0;
  63. }
  64. bool File::ReadFileToString(const string& name, string* output) {
  65. char buffer[1024];
  66. FILE* file = fopen(name.c_str(), "rb");
  67. if (file == NULL) return false;
  68. while (true) {
  69. size_t n = fread(buffer, 1, sizeof(buffer), file);
  70. if (n <= 0) break;
  71. output->append(buffer, n);
  72. }
  73. int error = ferror(file);
  74. if (fclose(file) != 0) return false;
  75. return error == 0;
  76. }
  77. void File::ReadFileToStringOrDie(const string& name, string* output) {
  78. GOOGLE_CHECK(ReadFileToString(name, output)) << "Could not read: " << name;
  79. }
  80. bool File::WriteStringToFile(const string& contents, const string& name) {
  81. FILE* file = fopen(name.c_str(), "wb");
  82. if (file == NULL) {
  83. GOOGLE_LOG(ERROR) << "fopen(" << name << ", \"wb\"): " << strerror(errno);
  84. return false;
  85. }
  86. if (fwrite(contents.data(), 1, contents.size(), file) != contents.size()) {
  87. GOOGLE_LOG(ERROR) << "fwrite(" << name << "): " << strerror(errno);
  88. fclose(file);
  89. return false;
  90. }
  91. if (fclose(file) != 0) {
  92. return false;
  93. }
  94. return true;
  95. }
  96. void File::WriteStringToFileOrDie(const string& contents, const string& name) {
  97. FILE* file = fopen(name.c_str(), "wb");
  98. GOOGLE_CHECK(file != NULL)
  99. << "fopen(" << name << ", \"wb\"): " << strerror(errno);
  100. GOOGLE_CHECK_EQ(fwrite(contents.data(), 1, contents.size(), file),
  101. contents.size())
  102. << "fwrite(" << name << "): " << strerror(errno);
  103. GOOGLE_CHECK(fclose(file) == 0)
  104. << "fclose(" << name << "): " << strerror(errno);
  105. }
  106. bool File::CreateDir(const string& name, int mode) {
  107. if (!name.empty()) {
  108. GOOGLE_CHECK_OK(name[name.size() - 1] != '.');
  109. }
  110. return mkdir(name.c_str(), mode) == 0;
  111. }
  112. bool File::RecursivelyCreateDir(const string& path, int mode) {
  113. if (CreateDir(path, mode)) return true;
  114. if (Exists(path)) return false;
  115. // Try creating the parent.
  116. string::size_type slashpos = path.find_last_of('/');
  117. if (slashpos == string::npos) {
  118. // No parent given.
  119. return false;
  120. }
  121. return RecursivelyCreateDir(path.substr(0, slashpos), mode) &&
  122. CreateDir(path, mode);
  123. }
  124. void File::DeleteRecursively(const string& name,
  125. void* dummy1, void* dummy2) {
  126. if (name.empty()) return;
  127. // We don't care too much about error checking here since this is only used
  128. // in tests to delete temporary directories that are under /tmp anyway.
  129. #ifdef _MSC_VER
  130. // This interface is so weird.
  131. WIN32_FIND_DATAA find_data;
  132. HANDLE find_handle = FindFirstFileA((name + "/*").c_str(), &find_data);
  133. if (find_handle == INVALID_HANDLE_VALUE) {
  134. // Just delete it, whatever it is.
  135. DeleteFileA(name.c_str());
  136. RemoveDirectoryA(name.c_str());
  137. return;
  138. }
  139. do {
  140. string entry_name = find_data.cFileName;
  141. if (entry_name != "." && entry_name != "..") {
  142. string path = name + "/" + entry_name;
  143. if (find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
  144. DeleteRecursively(path, NULL, NULL);
  145. RemoveDirectoryA(path.c_str());
  146. } else {
  147. DeleteFileA(path.c_str());
  148. }
  149. }
  150. } while(FindNextFileA(find_handle, &find_data));
  151. FindClose(find_handle);
  152. RemoveDirectoryA(name.c_str());
  153. #else
  154. // Use opendir()! Yay!
  155. // lstat = Don't follow symbolic links.
  156. struct stat stats;
  157. if (lstat(name.c_str(), &stats) != 0) return;
  158. if (S_ISDIR(stats.st_mode)) {
  159. DIR* dir = opendir(name.c_str());
  160. if (dir != NULL) {
  161. while (true) {
  162. struct dirent* entry = readdir(dir);
  163. if (entry == NULL) break;
  164. string entry_name = entry->d_name;
  165. if (entry_name != "." && entry_name != "..") {
  166. DeleteRecursively(name + "/" + entry_name, NULL, NULL);
  167. }
  168. }
  169. }
  170. closedir(dir);
  171. rmdir(name.c_str());
  172. } else if (S_ISREG(stats.st_mode)) {
  173. remove(name.c_str());
  174. }
  175. #endif
  176. }
  177. bool File::ChangeWorkingDirectory(const string& new_working_directory) {
  178. return chdir(new_working_directory.c_str()) == 0;
  179. }
  180. } // namespace protobuf
  181. } // namespace google