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); |
94 return false; | 95 return false; |
95 } | 96 } |
96 | 97 |
97 if (fclose(file) != 0) { | 98 if (fclose(file) != 0) { |
98 return false; | 99 return false; |
99 } | 100 } |
100 return true; | 101 return true; |
101 } | 102 } |
102 | 103 |
103 void File::WriteStringToFileOrDie(const string& contents, const string& name) { | 104 void File::WriteStringToFileOrDie(const string& contents, const string& name) { |
(...skipping 29 matching lines...) Expand all Loading... |
133 | 134 |
134 void File::DeleteRecursively(const string& name, | 135 void File::DeleteRecursively(const string& name, |
135 void* dummy1, void* dummy2) { | 136 void* dummy1, void* dummy2) { |
136 if (name.empty()) return; | 137 if (name.empty()) return; |
137 | 138 |
138 // We don't care too much about error checking here since this is only used | 139 // We don't care too much about error checking here since this is only used |
139 // in tests to delete temporary directories that are under /tmp anyway. | 140 // in tests to delete temporary directories that are under /tmp anyway. |
140 | 141 |
141 #ifdef _MSC_VER | 142 #ifdef _MSC_VER |
142 // This interface is so weird. | 143 // This interface is so weird. |
143 WIN32_FIND_DATA find_data; | 144 WIN32_FIND_DATAA find_data; |
144 HANDLE find_handle = FindFirstFile((name + "/*").c_str(), &find_data); | 145 HANDLE find_handle = FindFirstFileA((name + "/*").c_str(), &find_data); |
145 if (find_handle == INVALID_HANDLE_VALUE) { | 146 if (find_handle == INVALID_HANDLE_VALUE) { |
146 // Just delete it, whatever it is. | 147 // Just delete it, whatever it is. |
147 DeleteFile(name.c_str()); | 148 DeleteFileA(name.c_str()); |
148 RemoveDirectory(name.c_str()); | 149 RemoveDirectoryA(name.c_str()); |
149 return; | 150 return; |
150 } | 151 } |
151 | 152 |
152 do { | 153 do { |
153 string entry_name = find_data.cFileName; | 154 string entry_name = find_data.cFileName; |
154 if (entry_name != "." && entry_name != "..") { | 155 if (entry_name != "." && entry_name != "..") { |
155 string path = name + "/" + entry_name; | 156 string path = name + "/" + entry_name; |
156 if (find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { | 157 if (find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { |
157 DeleteRecursively(path, NULL, NULL); | 158 DeleteRecursively(path, NULL, NULL); |
158 RemoveDirectory(path.c_str()); | 159 RemoveDirectoryA(path.c_str()); |
159 } else { | 160 } else { |
160 DeleteFile(path.c_str()); | 161 DeleteFileA(path.c_str()); |
161 } | 162 } |
162 } | 163 } |
163 } while(FindNextFile(find_handle, &find_data)); | 164 } while(FindNextFileA(find_handle, &find_data)); |
164 FindClose(find_handle); | 165 FindClose(find_handle); |
165 | 166 |
166 RemoveDirectory(name.c_str()); | 167 RemoveDirectoryA(name.c_str()); |
167 #else | 168 #else |
168 // Use opendir()! Yay! | 169 // Use opendir()! Yay! |
169 // lstat = Don't follow symbolic links. | 170 // lstat = Don't follow symbolic links. |
170 struct stat stats; | 171 struct stat stats; |
171 if (lstat(name.c_str(), &stats) != 0) return; | 172 if (lstat(name.c_str(), &stats) != 0) return; |
172 | 173 |
173 if (S_ISDIR(stats.st_mode)) { | 174 if (S_ISDIR(stats.st_mode)) { |
174 DIR* dir = opendir(name.c_str()); | 175 DIR* dir = opendir(name.c_str()); |
175 if (dir != NULL) { | 176 if (dir != NULL) { |
176 while (true) { | 177 while (true) { |
(...skipping 14 matching lines...) Expand all Loading... |
191 } | 192 } |
192 #endif | 193 #endif |
193 } | 194 } |
194 | 195 |
195 bool File::ChangeWorkingDirectory(const string& new_working_directory) { | 196 bool File::ChangeWorkingDirectory(const string& new_working_directory) { |
196 return chdir(new_working_directory.c_str()) == 0; | 197 return chdir(new_working_directory.c_str()) == 0; |
197 } | 198 } |
198 | 199 |
199 } // namespace protobuf | 200 } // namespace protobuf |
200 } // namespace google | 201 } // namespace google |
OLD | NEW |