Skip to content

Commit

Permalink
trim prefix: list-objects; bucket-summary; multi-obj operations
Browse files Browse the repository at this point in the history
* rtrim(prefix, '*') to satisfy one common expectation
* proxy only; leaving rest of the code and CLI intact

Signed-off-by: Alex Aizman <alex.aizman@gmail.com>
  • Loading branch information
alex-aizman committed Sep 30, 2024
1 parent 5789273 commit 7cf1546
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
5 changes: 5 additions & 0 deletions ais/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,7 @@ func (p *proxy) httpbckget(w http.ResponseWriter, r *http.Request, dpq *dpq) {
p.writeErrf(w, r, cmn.FmtErrMorphUnmarshal, p.si, msg.Action, msg.Value, err)
return
}
summMsg.Prefix = cos.TrimPrefix(summMsg.Prefix)
if qbck.IsBucket() {
bck := (*meta.Bck)(qbck)
bckArgs := bctx{p: p, w: w, r: r, msg: msg, perms: apc.AceBckHEAD, bck: bck, dpq: dpq}
Expand Down Expand Up @@ -685,6 +686,7 @@ func (p *proxy) httpbckget(w http.ResponseWriter, r *http.Request, dpq *dpq) {
p.writeErrf(w, r, cmn.FmtErrMorphUnmarshal, p.si, msg.Action, msg.Value, err)
return
}
lsmsg.Prefix = cos.TrimPrefix(lsmsg.Prefix)
if err := cmn.ValidatePrefix("bad list-objects request", lsmsg.Prefix); err != nil {
p.statsT.IncErr(stats.ErrListCount)
p.writeErr(w, r, err)
Expand Down Expand Up @@ -1348,6 +1350,7 @@ func (p *proxy) _bckpost(w http.ResponseWriter, r *http.Request, msg *apc.ActMsg
p.writeErr(w, r, err)
return
}
tcbmsg.Prefix = cos.TrimPrefix(tcbmsg.Prefix)
if bckFrom.Equal(bckTo, true, true) {
if !bckFrom.IsRemote() {
p.writeErrf(w, r, "cannot %s bucket %q onto itself", msg.Action, bckFrom)
Expand Down Expand Up @@ -1408,6 +1411,7 @@ func (p *proxy) _bckpost(w http.ResponseWriter, r *http.Request, msg *apc.ActMsg
p.writeErrf(w, r, errPrependSync, tcomsg.Prepend)
return
}
tcomsg.Prefix = cos.TrimPrefix(tcomsg.Prefix)
bckTo = meta.CloneBck(&tcomsg.ToBck)

if bck.Equal(bckTo, true, true) {
Expand Down Expand Up @@ -1932,6 +1936,7 @@ func (p *proxy) httpbckhead(w http.ResponseWriter, r *http.Request, apireq *apiR
for _, s := range items[2:] {
prefix += "/" + s
}
prefix = cos.TrimPrefix(prefix)
apireq.after = 2
}
}
Expand Down
11 changes: 11 additions & 0 deletions cmn/cos/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ func TrimLastB(s string, b byte) string {
return s
}

// [NOTE] common *nix expectation in re: `ls aaa/bbb*` and similar
// - `?` not supported
// - `\*` not supported
// see also: cmn.ObjHasPrefix and friends
func TrimPrefix(s string) string {
if l := len(s); l > 0 && s[l-1] == WildcardMatchAll[0] {
return s[:l-1]
}
return s
}

// left if non-empty; otherwise right
func Left(left, right string) string {
if left != "" {
Expand Down
2 changes: 1 addition & 1 deletion cmn/cos/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func NewParsedTemplate(template string) (parsed ParsedTemplate, err error) {
}

// "pure" prefix w/ no ranges
return ParsedTemplate{Prefix: template}, nil
return ParsedTemplate{Prefix: TrimPrefix(template)}, nil
}

func (pt *ParsedTemplate) Clone() *ParsedTemplate {
Expand Down
6 changes: 4 additions & 2 deletions cmn/objlist_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,14 +219,16 @@ func MergeLso(lists []*LsoRes, lsmsg *apc.LsoMsg, maxSize int) *LsoRes {
// already listed and must be skipped). Note that string `>=` is lexicographic.
func TokenGreaterEQ(token, objName string) bool { return token >= objName }

// Directory has to either:
// directory has to either:
// - include (or match) prefix, or
// - be contained in prefix - motivation: don't SkipDir a/b when looking for a/b/c
// An alternative name for this function could be smth. like SameBranch()
// an alternative name for this function could be smth. like SameBranch()
// see also: cos.TrimPrefix
func DirHasOrIsPrefix(dirPath, prefix string) bool {
return prefix == "" || (strings.HasPrefix(prefix, dirPath) || strings.HasPrefix(dirPath, prefix))
}

// see also: cos.TrimPrefix
func ObjHasPrefix(objName, prefix string) bool {
return prefix == "" || strings.HasPrefix(objName, prefix)
}
Expand Down

0 comments on commit 7cf1546

Please sign in to comment.