throws error on any failure. */
| 308 | |
| 309 | /* throws error on any failure. */ |
| 310 | void removePath(const std::string& name) { |
| 311 | if (!pathExists(name)) { |
| 312 | return; |
| 313 | } |
| 314 | |
| 315 | if (isDir(name)) { |
| 316 | auto dir = opendir(name.c_str()); |
| 317 | if (!dir) { |
| 318 | throwSystemError(errno, fmt::format("Err removing path={}", name)); |
| 319 | } |
| 320 | SCOPE_EXIT { closedir(dir); }; |
| 321 | struct dirent* entry; |
| 322 | while ((entry = readdir(dir))) { |
| 323 | if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) { |
| 324 | continue; |
| 325 | } |
| 326 | std::string path = name + "/" + std::string(entry->d_name); |
| 327 | removePath(path); |
| 328 | } |
| 329 | auto err = rmdir(name.c_str()); |
| 330 | if (err) { |
| 331 | throwSystemError(errno, fmt::format("Err removing path={}", name)); |
| 332 | } |
| 333 | } else { |
| 334 | auto err = unlink(name.c_str()); |
| 335 | if (err) { |
| 336 | throwSystemError(errno, fmt::format("Err removing path={}", name)); |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | std::string getUniqueTempDir(folly::StringPiece prefix) { |
| 342 | const char* dir = getenv("TMPDIR"); |