返回 DeepSeek-Reasonix
attachment_storage.go
根目录 / internal / control / attachment_storage.go
1 package control
2
3 import (
4 "fmt"
5 "os"
6 "path/filepath"
7 "strings"
8 )
9
10 type ownedAttachmentFile struct {
11 *os.File
12 directory *os.Root
13 name string
14 rel string
15 complete bool
16 }
17
18 // Hold the directory handle through cleanup: re-resolving its path after a
19 // rename or symlink replacement could remove a different file.
20 func createOwnedAttachmentFile(base, ext string) (*ownedAttachmentFile, error) {
21 directory, err := openAttachmentRootIn(base)
22 if err != nil {
23 return nil, err
24 }
25
26 if !safeAttachmentExt.MatchString(ext) {
27 ext = ".bin"
28 }
29 for range maxAttachmentCreateAttempts {
30 name := filepath.Base(attachmentPath(ext))
31 f, err := directory.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o644)
32 if os.IsExist(err) {
33 continue
34 }
35 if err != nil {
36 _ = directory.Close()
37 return nil, err
38 }
39 return &ownedAttachmentFile{File: f, directory: directory, name: name,
40 rel: filepath.Join(".reasonix", "attachments", name)}, nil
41 }
42 _ = directory.Close()
43 return nil, fmt.Errorf("create unique attachment path")
44 }
45
46 func (f *ownedAttachmentFile) finish() error {
47 if err := f.Close(); err != nil {
48 return err
49 }
50 f.complete = true
51 return nil
52 }
53
54 func (f *ownedAttachmentFile) cleanup() {
55 _ = f.Close()
56 if !f.complete {
57 _ = f.directory.Remove(f.name)
58 }
59 _ = f.directory.Close()
60 }
61
62 func openAttachmentRootIn(base string) (*os.Root, error) {
63 if strings.TrimSpace(base) == "" {
64 base = "."
65 }
66 if err := os.MkdirAll(base, 0o755); err != nil {
67 return nil, err
68 }
69 workspace, err := os.OpenRoot(base)
70 if err != nil {
71 return nil, err
72 }
73 defer workspace.Close()
74 for _, name := range []string{".reasonix", filepath.Join(".reasonix", "attachments")} {
75 if err := workspace.Mkdir(name, 0o755); err != nil && !os.IsExist(err) {
76 return nil, err
77 }
78 info, err := workspace.Lstat(name)
79 if err != nil {
80 return nil, err
81 }
82 if info.Mode()&os.ModeSymlink != 0 || !info.IsDir() {
83 return nil, fmt.Errorf("attachment directory must be a directory without symlinks")
84 }
85 }
86 return workspace.OpenRoot(filepath.Join(".reasonix", "attachments"))
87 }
88
89 // RemoveStoredAttachment removes a direct attachment reference without following
90 // a replaced workspace path outside its root.
91 func RemoveStoredAttachment(base, ref string) error {
92 clean := filepath.Clean(filepath.FromSlash(ref))
93 if filepath.Dir(clean) != filepath.Join(".reasonix", "attachments") {
94 return fmt.Errorf("attachment path is outside .reasonix/attachments")
95 }
96 workspace, err := os.OpenRoot(base)
97 if err != nil {
98 return err
99 }
100 defer workspace.Close()
101 directory, err := workspace.OpenRoot(filepath.Join(".reasonix", "attachments"))
102 if err != nil {
103 return err
104 }
105 defer directory.Close()
106 return directory.Remove(filepath.Base(clean))
107 }
108
108 lines GO