| 440 | } |
| 441 | |
| 442 | Status HadoopFileSystem::DeleteDir(const string& dir) { |
| 443 | hdfsFS fs = nullptr; |
| 444 | TF_RETURN_IF_ERROR(Connect(dir, &fs)); |
| 445 | |
| 446 | // Count the number of entries in the directory, and only delete if it's |
| 447 | // non-empty. This is consistent with the interface, but note that there's |
| 448 | // a race condition where a file may be added after this check, in which |
| 449 | // case the directory will still be deleted. |
| 450 | int entries = 0; |
| 451 | hdfsFileInfo* info = |
| 452 | hdfs_->hdfsListDirectory(fs, TranslateName(dir).c_str(), &entries); |
| 453 | if (info != nullptr) { |
| 454 | hdfs_->hdfsFreeFileInfo(info, entries); |
| 455 | } |
| 456 | // Due to HDFS bug HDFS-8407, we can't distinguish between an error and empty |
| 457 | // folder, expscially for Kerberos enable setup, EAGAIN is quite common when |
| 458 | // the call is actually successful. Check again by Stat. |
| 459 | if (info == nullptr && errno != 0) { |
| 460 | FileStatistics stat; |
| 461 | TF_RETURN_IF_ERROR(Stat(dir, &stat)); |
| 462 | } |
| 463 | |
| 464 | if (entries > 0) { |
| 465 | return errors::FailedPrecondition("Cannot delete a non-empty directory."); |
| 466 | } |
| 467 | if (hdfs_->hdfsDelete(fs, TranslateName(dir).c_str(), |
| 468 | /*recursive=*/1) != 0) { |
| 469 | return IOError(dir, errno); |
| 470 | } |
| 471 | return Status::OK(); |
| 472 | } |
| 473 | |
| 474 | Status HadoopFileSystem::GetFileSize(const string& fname, uint64* size) { |
| 475 | hdfsFS fs = nullptr; |