Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ void matchGlobKeepsSingleStarWithinOnePathSegment() {
.contains("logs/2026/01/app.log", "metrics/cpu.log");
}

@Test
void matchGlobFiltersDirectoryPrefixes() {
assertThat(names(Storage.BlobListOption.currentDirectory(), Storage.BlobListOption.matchGlob("*/")))
.containsExactlyInAnyOrder("logs/", "metrics/");
}

@Test
void includeTrailingDelimiterAddsThePlaceholderObjectToItems() throws Exception {
// Driven raw: google-cloud-storage 2.47.0 exposes no BlobListOption for this parameter,
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/io/floci/gcp/services/gcs/GcsObjectController.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,22 @@ public Response listObjects(@PathParam("bucket") String bucket,
}
// startOffset is inclusive, endOffset exclusive.
GcsObjectGlob.GlobMatcher globMatcher = GcsObjectGlob.matcher(GcsObjectGlob.compile(matchGlob));
String basePrefix = prefix != null ? prefix : "";
if (delimiter != null && !delimiter.isEmpty()) {
String globPrefix = GcsObjectGlob.fixedDirectoryPrefix(matchGlob);
if (globPrefix.startsWith(basePrefix)) {
basePrefix = globPrefix;
}
}
String listingPrefix = basePrefix;
all = all.stream()
.sorted(Comparator.comparing(GcsObjectMeta::getName))
.filter(o -> startOffset == null || o.getName().compareTo(startOffset) >= 0)
.filter(o -> endOffset == null || o.getName().compareTo(endOffset) < 0)
.filter(o -> globMatcher.matches(o.getName()))
.filter(o -> o.getName().startsWith(listingPrefix))
.toList();
Set<String> prefixes = new TreeSet<>();
if (delimiter != null && !delimiter.isEmpty()) {
String basePrefix = prefix != null ? prefix : "";
List<GcsObjectMeta> rolledUp = new ArrayList<>();
for (GcsObjectMeta meta : all) {
String rest = meta.getName().substring(basePrefix.length());
Expand All @@ -110,6 +117,10 @@ public Response listObjects(@PathParam("bucket") String bucket,
}
all = rolledUp;
}
all = all.stream()
.filter(o -> globMatcher.matches(o.getName()))
.toList();
prefixes.removeIf(prefixEntry -> !globMatcher.matches(prefixEntry));
PageToken.Page<GcsObjectMeta> page = PageToken.paginate(all, maxResults, pageToken);
Map<String, Object> response = new LinkedHashMap<>();
response.put("kind", "storage#objects");
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/io/floci/gcp/services/gcs/GcsObjectGlob.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ static Pattern compile(String glob) {
}
}
case ',' -> out.append(depth > 0 ? "|" : "\\,");
case '\\' -> {
if (i + 1 < glob.length()) {
appendLiteral(out, glob.charAt(++i));
} else {
appendLiteral(out, c);
}
}
default -> appendLiteral(out, c);
}
}
Expand Down Expand Up @@ -134,6 +141,29 @@ static GlobMatcher matcher(Pattern pattern) {
return new GlobMatcher(pattern);
}

/** Literal path segments before the first glob token act as a prefix for delimiter roll-up. */
static String fixedDirectoryPrefix(String glob) {
if (glob == null || glob.isEmpty()) {
return "";
}

StringBuilder fixedPrefix = new StringBuilder();
for (int i = 0; i < glob.length(); i++) {
char c = glob.charAt(i);
if (c == '\\' && i + 1 < glob.length()) {
fixedPrefix.append(glob.charAt(++i));
continue;
}
Comment thread
electrum marked this conversation as resolved.
if ("*?[{".indexOf(c) >= 0) {
break;
}
fixedPrefix.append(c);
}

int delimiter = fixedPrefix.lastIndexOf("/");
return delimiter < 0 ? "" : fixedPrefix.substring(0, delimiter + 1);
}

static final class GlobMatcher {
private final Pattern pattern;
private int steps;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,55 @@ void matchGlobThatMatchesNothingReturnsNoItems() {
.body("items", org.hamcrest.Matchers.anyOf(org.hamcrest.Matchers.nullValue(), empty()));
}

@Test
void matchGlobFiltersPrefixesAfterDelimiterRollup() {
seed();
given().queryParam("delimiter", "/").queryParam("matchGlob", "*/")
.when().get("/storage/v1/b/" + BUCKET + "/o")
.then().statusCode(200)
.body("items", org.hamcrest.Matchers.anyOf(org.hamcrest.Matchers.nullValue(), empty()))
.body("prefixes", containsInAnyOrder("a/", "b/", "logs/"));
}

@Test
void matchGlobFixedDirectoryPrefixControlsDelimiterRollup() {
seed();
given().queryParam("delimiter", "/").queryParam("matchGlob", "a/*")
.when().get("/storage/v1/b/" + BUCKET + "/o")
.then().statusCode(200)
.body("items.name", containsInAnyOrder("a/", "a/1.txt", "a/2.txt"))
.body("prefixes", org.hamcrest.Matchers.anyOf(org.hamcrest.Matchers.nullValue(), empty()));

given().queryParam("delimiter", "/").queryParam("matchGlob", "a/**")
.when().get("/storage/v1/b/" + BUCKET + "/o")
.then().statusCode(200)
.body("items.name", containsInAnyOrder("a/", "a/1.txt", "a/2.txt"))
.body("prefixes", contains("a/b/"));
}

@Test
void matchGlobBackslashEscapesTheNextCharacter() {
String bucket = "glob-backslash-bucket";
given().contentType("application/json").body(Map.of("name", bucket))
.when().post("/storage/v1/b?project=test-project");
for (String name : new String[] {"ab/file.txt", "a\\b/file.txt"}) {
given().contentType("text/plain").body("x")
.queryParam("uploadType", "media")
.queryParam("name", name)
.when().post("/upload/storage/v1/b/" + bucket + "/o");
}

given().queryParam("delimiter", "/").queryParam("matchGlob", "a\\b/*")
.when().get("/storage/v1/b/" + bucket + "/o")
.then().statusCode(200)
.body("items.name", contains("ab/file.txt"));

given().queryParam("delimiter", "/").queryParam("matchGlob", "a\\\\b/*")
.when().get("/storage/v1/b/" + bucket + "/o")
.then().statusCode(200)
.body("items.name", contains("a\\b/file.txt"));
}

@Test
void trailingDelimiterPlaceholderIsRolledUpByDefault() {
seed();
Expand Down