| 1 | package session |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "io/fs" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | ) |
| 10 | |
| 11 | // Owned artifacts are independent copies, never links to the parent. History |
| 12 | // references remain relative to the session that owns their imported data. |
| 13 | func copyOwnedSessionFiles(ctx context.Context, source, target string) error { |
| 14 | for _, name := range []string{"attachments", "assets", "legacy"} { |
| 15 | root := filepath.Join(source, name) |
| 16 | if _, err := os.Lstat(root); os.IsNotExist(err) { |
| 17 | continue |
| 18 | } else if err != nil { |
| 19 | return err |
| 20 | } |
| 21 | err := filepath.WalkDir(root, func(path string, entry fs.DirEntry, walkErr error) error { |
| 22 | if walkErr != nil { |
| 23 | return walkErr |
| 24 | } |
| 25 | if err := ctx.Err(); err != nil { |
| 26 | return err |
| 27 | } |
| 28 | info, err := entry.Info() |
| 29 | if err != nil { |
| 30 | return err |
| 31 | } |
| 32 | relative, err := filepath.Rel(source, path) |
| 33 | if err != nil { |
| 34 | return err |
| 35 | } |
| 36 | destination := filepath.Join(target, relative) |
| 37 | if entry.IsDir() { |
| 38 | return os.MkdirAll(destination, 0700) |
| 39 | } |
| 40 | if !info.Mode().IsRegular() { |
| 41 | return fmt.Errorf("session: owned artifact is not a regular file: %s", relative) |
| 42 | } |
| 43 | return copySessionFile(ctx, path, destination, 0600) |
| 44 | }) |
| 45 | if err != nil { |
| 46 | return err |
| 47 | } |
| 48 | } |
| 49 | return nil |
| 50 | } |
| 51 |