OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 | 84 |
85 bool File::WriteStringToFile(const string& contents, const string& name) { | 85 bool File::WriteStringToFile(const string& contents, const string& name) { |
86 FILE* file = fopen(name.c_str(), "wb"); | 86 FILE* file = fopen(name.c_str(), "wb"); |
87 if (file == NULL) { | 87 if (file == NULL) { |
88 GOOGLE_LOG(ERROR) << "fopen(" << name << ", \"wb\"): " << strerror(errno); | 88 GOOGLE_LOG(ERROR) << "fopen(" << name << ", \"wb\"): " << strerror(errno); |
89 return false; | 89 return false; |
90 } | 90 } |
91 | 91 |
92 if (fwrite(contents.data(), 1, contents.size(), file) != contents.size()) { | 92 if (fwrite(contents.data(), 1, contents.size(), file) != contents.size()) { |
93 GOOGLE_LOG(ERROR) << "fwrite(" << name << "): " << strerror(errno); | 93 GOOGLE_LOG(ERROR) << "fwrite(" << name << "): " << strerror(errno); |
94 fclose(file); | |
95 return false; | 94 return false; |
96 } | 95 } |
97 | 96 |
98 if (fclose(file) != 0) { | 97 if (fclose(file) != 0) { |
99 return false; | 98 return false; |
100 } | 99 } |
101 return true; | 100 return true; |
102 } | 101 } |
103 | 102 |
104 void File::WriteStringToFileOrDie(const string& contents, const string& name) { | 103 void File::WriteStringToFileOrDie(const string& contents, const string& name) { |
(...skipping 29 matching lines...) Expand all Loading... |
134 | 133 |
135 void File::DeleteRecursively(const string& name, | 134 void File::DeleteRecursively(const string& name, |
136 void* dummy1, void* dummy2) { | 135 void* dummy1, void* dummy2) { |
137 if (name.empty()) return; | 136 if (name.empty()) return; |
138 | 137 |
139 // We don't care too much about error checking here since this is only used | 138 // We don't care too much about error checking here since this is only used |
140 // in tests to delete temporary directories that are under /tmp anyway. | 139 // in tests to delete temporary directories that are under /tmp anyway. |
141 | 140 |
142 #ifdef _MSC_VER | 141 #ifdef _MSC_VER |
143 // This interface is so weird. | 142 // This interface is so weird. |
144 WIN32_FIND_DATAA find_data; | 143 WIN32_FIND_DATA find_data; |
145 HANDLE find_handle = FindFirstFileA((name + "/*").c_str(), &find_data); | 144 HANDLE find_handle = FindFirstFile((name + "/*").c_str(), &find_data); |
146 if (find_handle == INVALID_HANDLE_VALUE) { | 145 if (find_handle == INVALID_HANDLE_VALUE) { |
147 // Just delete it, whatever it is. | 146 // Just delete it, whatever it is. |
148 DeleteFileA(name.c_str()); | 147 DeleteFile(name.c_str()); |
149 RemoveDirectoryA(name.c_str()); | 148 RemoveDirectory(name.c_str()); |
150 return; | 149 return; |
151 } | 150 } |
152 | 151 |
153 do { | 152 do { |
154 string entry_name = find_data.cFileName; | 153 string entry_name = find_data.cFileName; |
155 if (entry_name != "." && entry_name != "..") { | 154 if (entry_name != "." && entry_name != "..") { |
156 string path = name + "/" + entry_name; | 155 string path = name + "/" + entry_name; |
157 if (find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { | 156 if (find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { |
158 DeleteRecursively(path, NULL, NULL); | 157 DeleteRecursively(path, NULL, NULL); |
159 RemoveDirectoryA(path.c_str()); | 158 RemoveDirectory(path.c_str()); |
160 } else { | 159 } else { |
161 DeleteFileA(path.c_str()); | 160 DeleteFile(path.c_str()); |
162 } | 161 } |
163 } | 162 } |
164 } while(FindNextFileA(find_handle, &find_data)); | 163 } while(FindNextFile(find_handle, &find_data)); |
165 FindClose(find_handle); | 164 FindClose(find_handle); |
166 | 165 |
167 RemoveDirectoryA(name.c_str()); | 166 RemoveDirectory(name.c_str()); |
168 #else | 167 #else |
169 // Use opendir()! Yay! | 168 // Use opendir()! Yay! |
170 // lstat = Don't follow symbolic links. | 169 // lstat = Don't follow symbolic links. |
171 struct stat stats; | 170 struct stat stats; |
172 if (lstat(name.c_str(), &stats) != 0) return; | 171 if (lstat(name.c_str(), &stats) != 0) return; |
173 | 172 |
174 if (S_ISDIR(stats.st_mode)) { | 173 if (S_ISDIR(stats.st_mode)) { |
175 DIR* dir = opendir(name.c_str()); | 174 DIR* dir = opendir(name.c_str()); |
176 if (dir != NULL) { | 175 if (dir != NULL) { |
177 while (true) { | 176 while (true) { |
(...skipping 14 matching lines...) Expand all Loading... |
192 } | 191 } |
193 #endif | 192 #endif |
194 } | 193 } |
195 | 194 |
196 bool File::ChangeWorkingDirectory(const string& new_working_directory) { | 195 bool File::ChangeWorkingDirectory(const string& new_working_directory) { |
197 return chdir(new_working_directory.c_str()) == 0; | 196 return chdir(new_working_directory.c_str()) == 0; |
198 } | 197 } |
199 | 198 |
200 } // namespace protobuf | 199 } // namespace protobuf |
201 } // namespace google | 200 } // namespace google |
OLD | NEW |