诸暨麻将添加redis
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.
 
 
 
 
 
 

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