Files
s3/weed/s3api/s3lifecycle/engine/match.go
zooqueen dd74d3e6d6 Replace AGPL MinIO fork with Apache-2.0 SeaweedFS fork; rebrand CLI weed->s3
hanzoai/s3 was an AGPL-3.0 MinIO fork whose network-copyleft blocks embedding
and reselling as a managed service. Replace it wholesale with a fork of
SeaweedFS (github.com/seaweedfs/seaweedfs, Apache-2.0, Go) which ships a
built-in S3 gateway + Filer.

- License: AGPL-3.0 -> Apache-2.0 (upstream LICENSE kept verbatim, incl.
  "Copyright 2025 Chris Lu"); NOTICE added crediting SeaweedFS. No AGPL/BUSL/
  SSPL/GPL or enterprise ("ee") files remain.
- Module: go.mod path set to github.com/hanzoai/s3 with a self-replace
  (replace github.com/seaweedfs/seaweedfs => ./) so the ~1800 internal Go files
  keep their upstream import paths with zero churn. Internal identifiers and
  import paths are intentionally NOT renamed.
- CLI rebrand: the binary is built as `s3` (was `weed`); root usage, per-command
  help/usage/flag strings, shell autocomplete program name, and the product name
  in CLI output now read "Hanzo S3" / `s3`. Users never see `weed`.
  (e.g. `s3 server`, `s3 filer`, `s3 master`, `s3 volume`, `s3 s3`, `s3 mount`.)
- Build: `make` (or `go build -o s3 ./weed`) builds the `s3` binary green;
  `cmd/weed-db` and `cmd/weed-sql` also build; `go vet ./weed/command/` clean.
- README/LLM.md rewritten for Hanzo S3; AGENTS.md/CLAUDE.md symlinks preserved.
- Dropped a 33MB prebuilt Mach-O `stress` binary upstream had committed; `.gitignore`
  updated to ignore the `s3` build output.
2026-06-18 23:41:25 -07:00

135 lines
3.2 KiB
Go

package engine
import (
"strings"
"time"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3lifecycle"
)
type EventShape int
const (
EventShapeUnknown EventShape = iota
// EventShapeOriginalWrite: any event resetting AWS LastModified
// (fresh PUT, overwrite, MPU init, version flip).
EventShapeOriginalWrite
// EventShapePredicateChange: tags / Extended changed without an mtime
// reset.
EventShapePredicateChange
)
// Event is the routing-relevant slice of a meta-log event. Kept minimal so
// the engine doesn't depend on filer_pb.
type Event struct {
Shape EventShape
Bucket string
Path string
Tags map[string]string
Size int64
IsLatest bool
IsDeleteMarker bool
IsMPUInit bool
EventTime time.Time
}
// MatchOriginalWrite returns active ActionKeys in the given delay group
// whose filter matches the event.
func (s *Snapshot) MatchOriginalWrite(ev *Event, delay time.Duration) []s3lifecycle.ActionKey {
if ev == nil || ev.Shape != EventShapeOriginalWrite {
return nil
}
keys := s.originalDelayGroups[delay]
if len(keys) == 0 {
return nil
}
return s.filterMatching(keys, ev)
}
// MatchPredicateChange returns active predicate-sensitive ActionKeys whose
// filter matches the event.
func (s *Snapshot) MatchPredicateChange(ev *Event) []s3lifecycle.ActionKey {
if ev == nil || ev.Shape != EventShapePredicateChange {
return nil
}
if len(s.predicateActions) == 0 {
return nil
}
return s.filterMatching(s.predicateActions, ev)
}
// MatchPath returns active ActionKeys for a specific bucket+path. ev=nil
// applies prefix matching only; pass a non-nil Event to also gate on tags
// and size.
func (s *Snapshot) MatchPath(bucket, path string, ev *Event) []s3lifecycle.ActionKey {
bi := s.buckets[bucket]
if bi == nil {
return nil
}
out := make([]s3lifecycle.ActionKey, 0, len(bi.actionKeys))
for _, k := range bi.actionKeys {
a := s.actions[k]
if a == nil || !a.IsActive() {
continue
}
if !prefixMatches(a.Rule.Prefix, path) {
continue
}
if ev != nil && !filterAllows(a.Rule, ev) {
continue
}
out = append(out, k)
}
return out
}
func (s *Snapshot) filterMatching(keys []s3lifecycle.ActionKey, ev *Event) []s3lifecycle.ActionKey {
out := make([]s3lifecycle.ActionKey, 0, len(keys))
for _, k := range keys {
a := s.actions[k]
if a == nil || !a.IsActive() || a.Bucket != ev.Bucket {
continue
}
if !prefixMatches(a.Rule.Prefix, ev.Path) {
continue
}
if !filterAllows(a.Rule, ev) {
continue
}
switch k.ActionKind {
case s3lifecycle.ActionKindAbortMPU:
if !ev.IsMPUInit {
continue
}
case s3lifecycle.ActionKindExpiredDeleteMarker:
if !ev.IsDeleteMarker || !ev.IsLatest {
continue
}
}
out = append(out, k)
}
return out
}
func prefixMatches(rulePrefix, path string) bool {
if rulePrefix == "" {
return true
}
return strings.HasPrefix(path, rulePrefix)
}
func filterAllows(rule *s3lifecycle.Rule, ev *Event) bool {
if rule.FilterSizeGreaterThan > 0 && ev.Size <= rule.FilterSizeGreaterThan {
return false
}
if rule.FilterSizeLessThan > 0 && ev.Size >= rule.FilterSizeLessThan {
return false
}
for k, v := range rule.FilterTags {
if got, ok := ev.Tags[k]; !ok || got != v {
return false
}
}
return true
}