| 197 | } |
| 198 | |
| 199 | bool RemoveFileIfExists(const std::string& path, std::string* err) { |
| 200 | struct stat st; |
| 201 | #if defined(_WIN32) |
| 202 | //TODO: Windows version can't handle symbol link correctly. |
| 203 | int result = stat(path.c_str(), &st); |
| 204 | bool file_type_removable = (result == 0 && S_ISREG(st.st_mode)); |
| 205 | #else |
| 206 | int result = lstat(path.c_str(), &st); |
| 207 | bool file_type_removable = (result == 0 && (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))); |
| 208 | #endif |
| 209 | if (result == 0) { |
| 210 | if (!file_type_removable) { |
| 211 | if (err != nullptr) { |
| 212 | *err = "is not a regular or symbol link file"; |
| 213 | } |
| 214 | return false; |
| 215 | } |
| 216 | if (unlink(path.c_str()) == -1) { |
| 217 | if (err != nullptr) { |
| 218 | *err = strerror(errno); |
| 219 | } |
| 220 | return false; |
| 221 | } |
| 222 | } |
| 223 | return true; |
| 224 | } |
| 225 | |
| 226 | #if !defined(_WIN32) |
| 227 | bool Readlink(const std::string& path, std::string* result) { |