| 542 | } |
| 543 | |
| 544 | Status S3FileSystem::DeleteDir(const string& dirname) { |
| 545 | string bucket, object; |
| 546 | TF_RETURN_IF_ERROR(ParseS3Path(dirname, false, &bucket, &object)); |
| 547 | |
| 548 | string prefix = object; |
| 549 | if (prefix.back() != '/') { |
| 550 | prefix.push_back('/'); |
| 551 | } |
| 552 | Aws::S3::Model::ListObjectsRequest listObjectsRequest; |
| 553 | listObjectsRequest.WithBucket(bucket.c_str()) |
| 554 | .WithPrefix(prefix.c_str()) |
| 555 | .WithMaxKeys(2); |
| 556 | listObjectsRequest.SetResponseStreamFactory( |
| 557 | []() { return Aws::New<Aws::StringStream>(kS3FileSystemAllocationTag); }); |
| 558 | auto listObjectsOutcome = |
| 559 | this->GetS3Client()->ListObjects(listObjectsRequest); |
| 560 | if (listObjectsOutcome.IsSuccess()) { |
| 561 | auto contents = listObjectsOutcome.GetResult().GetContents(); |
| 562 | if (contents.size() > 1 || |
| 563 | (contents.size() == 1 && contents[0].GetKey() != prefix.c_str())) { |
| 564 | return errors::FailedPrecondition("Cannot delete a non-empty directory."); |
| 565 | } |
| 566 | if (contents.size() == 1 && contents[0].GetKey() == prefix.c_str()) { |
| 567 | string filename = dirname; |
| 568 | if (filename.back() != '/') { |
| 569 | filename.push_back('/'); |
| 570 | } |
| 571 | return DeleteFile(filename); |
| 572 | } |
| 573 | } |
| 574 | return Status::OK(); |
| 575 | } |
| 576 | |
| 577 | Status S3FileSystem::GetFileSize(const string& fname, uint64* file_size) { |
| 578 | FileStatistics stats; |