Files
Agent-System-Backend/internal/blobstore/blobstore.go
T
weichenandCursor 46073d30a5
ci / vet-and-test (pull_request) Successful in 46s
Fall back to local artifacts when the blob store is a typed nil
Why: an unset S3 client passed through the Store interface is not a nil interface, so uploads panic instead of using the local artifact directory.
Evidence: TestIsNil and TestFinishOpenTypedNilS3UsesLocalArtifacts failed before this change and pass after it. The PostgreSQL case skips unless AGENT_PG_TEST_DSN is set.
Impact: only an unset blob store falls back; a constructed S3 or local store is unchanged.
Refs: internal/blobstore/blobstore.go internal/store/store.go
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-13 01:22:57 +00:00

116 lines
3.5 KiB
Go

// Package blobstore 是工件对象存储抽象(阶段 3:多实例前置)。
// 默认本地目录档(单实例,零外部服务);多实例部署切共享卷路径或按本接口补
// 对象存储实现(S3 兼容等)——上层(store 工件方法)只依赖本接口。
package blobstore
import (
"fmt"
"os"
"path/filepath"
"reflect"
"strings"
"sync"
)
// Store 是对象存储接口:Put 原子落盘并返回记录进库的引用(本地档=绝对路径,
// 对象存储档=对象键);Get/Delete 按该引用操作。
type Store interface {
Put(id string, content []byte) (ref string, err error)
Get(ref string) ([]byte, error)
Delete(ref string) error
}
// IsNil 判断对象存储是否未配置。nil 接口,以及放进接口后的带类型空指针
// (例如未设置端点时的 (*S3)(nil)),都算未配置。已构造的实现返回 false。
func IsNil(s Store) bool {
if s == nil {
return true
}
v := reflect.ValueOf(s)
switch v.Kind() {
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Pointer, reflect.Slice:
return v.IsNil()
default:
return false
}
}
// Local 是本地目录实现(data/artifacts;多实例时把目录指向共享卷即可)。
type Local struct {
dir string
mu sync.Mutex
}
func NewLocal(dir string) (*Local, error) {
if err := os.MkdirAll(dir, 0o755); err != nil {
return nil, err
}
return &Local{dir: dir}, nil
}
// OpenLocalExisting 绑定既有目录,不 MkdirAll。目录缺失时仍返回可用句柄
// (账户运维只碰 DB;Put 时若目录不存在会失败)。
func OpenLocalExisting(dir string) (*Local, error) {
return &Local{dir: dir}, nil
}
// validID 只接受纯文件名形态的对象 id:非空、不含路径分隔符、非 . / ..。
// Put 的 id 可能是上层透传的用户输入(如工件路径参数),先在此挡住路径穿越。
func validID(id string) bool {
if id == "" || id == "." || id == ".." || filepath.IsAbs(id) {
return false
}
return !strings.ContainsAny(id, `/\`) && !strings.ContainsRune(id, 0)
}
// contains 判断路径是否落在对象目录内:Get/Delete 收的是入库引用,
// 做包含性检查作为纵深防御,引用被篡改时也不读/删目录外文件。
func (l *Local) contains(p string) bool {
rel, err := filepath.Rel(l.dir, filepath.Clean(p))
if err != nil {
return false
}
return rel != ".." && !strings.HasPrefix(rel, ".."+string(filepath.Separator))
}
// Put 临时文件 → rename 原子写:崩溃只可能残留 .tmp 孤立文件,不产生半写对象。
func (l *Local) Put(id string, content []byte) (string, error) {
if !validID(id) {
return "", fmt.Errorf("blobstore: invalid object id %q", id)
}
final := filepath.Join(l.dir, id)
tmp := final + ".tmp-" + id
if err := os.WriteFile(tmp, content, 0o644); err != nil {
return "", err
}
l.mu.Lock()
defer l.mu.Unlock()
if err := os.Rename(tmp, final); err != nil {
os.Remove(tmp)
return "", err
}
return final, nil
}
func (l *Local) Get(ref string) ([]byte, error) {
if !l.contains(ref) {
return nil, fmt.Errorf("blobstore: ref %s escapes object store", filepath.Base(ref))
}
b, err := os.ReadFile(ref)
if os.IsNotExist(err) {
return nil, fmt.Errorf("blobstore: object %s not found", filepath.Base(ref))
}
return b, err
}
func (l *Local) Delete(ref string) error {
if !l.contains(ref) {
return fmt.Errorf("blobstore: ref %s escapes object store", filepath.Base(ref))
}
err := os.Remove(ref)
if os.IsNotExist(err) {
return nil
}
return err
}