/thirdparty/breakpad/third_party/protobuf/protobuf/src/google/protobuf/testing/file.cc

http://github.com/tomahawk-player/tomahawk · C++ · 176 lines · 113 code · 23 blank · 40 comment · 38 complexity · 4db818e16e701a0be6d573e83ad33c52 MD5 · raw file

  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // http://code.google.com/p/protobuf/
  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 <io.h>
  40. #include <direct.h>
  41. #else
  42. #include <dirent.h>
  43. #include <unistd.h>
  44. #endif
  45. #include <errno.h>
  46. namespace google {
  47. namespace protobuf {
  48. #ifdef _WIN32
  49. #define mkdir(name, mode) mkdir(name)
  50. // Windows doesn't have symbolic links.
  51. #define lstat stat
  52. #ifndef F_OK
  53. #define F_OK 00 // not defined by MSVC for whatever reason
  54. #endif
  55. #endif
  56. bool File::Exists(const string& name) {
  57. return access(name.c_str(), F_OK) == 0;
  58. }
  59. bool File::ReadFileToString(const string& name, string* output) {
  60. char buffer[1024];
  61. FILE* file = fopen(name.c_str(), "rb");
  62. if (file == NULL) return false;
  63. while (true) {
  64. size_t n = fread(buffer, 1, sizeof(buffer), file);
  65. if (n <= 0) break;
  66. output->append(buffer, n);
  67. }
  68. int error = ferror(file);
  69. if (fclose(file) != 0) return false;
  70. return error == 0;
  71. }
  72. void File::ReadFileToStringOrDie(const string& name, string* output) {
  73. GOOGLE_CHECK(ReadFileToString(name, output)) << "Could not read: " << name;
  74. }
  75. void File::WriteStringToFileOrDie(const string& contents, const string& name) {
  76. FILE* file = fopen(name.c_str(), "wb");
  77. GOOGLE_CHECK(file != NULL)
  78. << "fopen(" << name << ", \"wb\"): " << strerror(errno);
  79. GOOGLE_CHECK_EQ(fwrite(contents.data(), 1, contents.size(), file),
  80. contents.size())
  81. << "fwrite(" << name << "): " << strerror(errno);
  82. GOOGLE_CHECK(fclose(file) == 0)
  83. << "fclose(" << name << "): " << strerror(errno);
  84. }
  85. bool File::CreateDir(const string& name, int mode) {
  86. return mkdir(name.c_str(), mode) == 0;
  87. }
  88. bool File::RecursivelyCreateDir(const string& path, int mode) {
  89. if (CreateDir(path, mode)) return true;
  90. if (Exists(path)) return false;
  91. // Try creating the parent.
  92. string::size_type slashpos = path.find_last_of('/');
  93. if (slashpos == string::npos) {
  94. // No parent given.
  95. return false;
  96. }
  97. return RecursivelyCreateDir(path.substr(0, slashpos), mode) &&
  98. CreateDir(path, mode);
  99. }
  100. void File::DeleteRecursively(const string& name,
  101. void* dummy1, void* dummy2) {
  102. // We don't care too much about error checking here since this is only used
  103. // in tests to delete temporary directories that are under /tmp anyway.
  104. #ifdef _MSC_VER
  105. // This interface is so weird.
  106. WIN32_FIND_DATA find_data;
  107. HANDLE find_handle = FindFirstFile((name + "/*").c_str(), &find_data);
  108. if (find_handle == INVALID_HANDLE_VALUE) {
  109. // Just delete it, whatever it is.
  110. DeleteFile(name.c_str());
  111. RemoveDirectory(name.c_str());
  112. return;
  113. }
  114. do {
  115. string entry_name = find_data.cFileName;
  116. if (entry_name != "." && entry_name != "..") {
  117. string path = name + "/" + entry_name;
  118. if (find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
  119. DeleteRecursively(path, NULL, NULL);
  120. RemoveDirectory(path.c_str());
  121. } else {
  122. DeleteFile(path.c_str());
  123. }
  124. }
  125. } while(FindNextFile(find_handle, &find_data));
  126. FindClose(find_handle);
  127. RemoveDirectory(name.c_str());
  128. #else
  129. // Use opendir()! Yay!
  130. // lstat = Don't follow symbolic links.
  131. struct stat stats;
  132. if (lstat(name.c_str(), &stats) != 0) return;
  133. if (S_ISDIR(stats.st_mode)) {
  134. DIR* dir = opendir(name.c_str());
  135. if (dir != NULL) {
  136. while (true) {
  137. struct dirent* entry = readdir(dir);
  138. if (entry == NULL) break;
  139. string entry_name = entry->d_name;
  140. if (entry_name != "." && entry_name != "..") {
  141. DeleteRecursively(name + "/" + entry_name, NULL, NULL);
  142. }
  143. }
  144. }
  145. closedir(dir);
  146. rmdir(name.c_str());
  147. } else if (S_ISREG(stats.st_mode)) {
  148. remove(name.c_str());
  149. }
  150. #endif
  151. }
  152. } // namespace protobuf
  153. } // namespace google