Skip to content

Commit

Permalink
Make sure that GetChildrenFromS3 returns correct results
Browse files Browse the repository at this point in the history
Previously GetChildrenFromS3() returned children of the path and all
paths for which the given path was a prefix. This was because we didn't
append '/' to the end of the path, as S3 expects.
  • Loading branch information
igorcanadi committed Mar 15, 2019
1 parent a33f573 commit 8f6a123
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
5 changes: 4 additions & 1 deletion cloud/aws/aws_env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,9 @@ Status AwsEnv::GetChildrenFromS3(const std::string& path,

// S3 paths don't start with '/'
auto prefix = ltrim_if(path, '/');
// S3 paths better end with '/', otherwise we might also get a list of files
// in a directory for which our path is a prefix
prefix = ensure_ends_with_pathsep(std::move(prefix));
// the starting object marker
Aws::String marker;
bool loop = true;
Expand Down Expand Up @@ -884,7 +887,7 @@ Status AwsEnv::GetChildrenFromS3(const std::string& path,
if (keystr.find(prefix) != 0) {
return Status::IOError("Unexpected result from AWS S3: " + keystr);
}
auto fname = ltrim_if(keystr.substr(prefix.size()), '/');
auto fname = keystr.substr(prefix.size());
result->push_back(fname);
}

Expand Down
9 changes: 9 additions & 0 deletions cloud/filename.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ inline std::string dirname(std::string const& pathname) {
return pathname.substr(0, pos);
}

// If s doesn't end with '/', it appends it.
// Special case: if s is empty, we don't append '/'
inline std::string ensure_ends_with_pathsep(std::string s) {
if (!s.empty() && s.back() != '/') {
s += '/';
}
return s;
}

// If the last char of the string is the specified character, then return a
// string that has the last character removed.
inline std::string rtrim_if(std::string s, char c) {
Expand Down

0 comments on commit 8f6a123

Please sign in to comment.