| 1 | package workspace |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "strings" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 10 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 11 | ) |
| 12 | |
| 13 | func (m *Manager) Remove(_ context.Context, a runnerproto.Assignment, path string) error { |
| 14 | m.mu.Lock() |
| 15 | defer m.mu.Unlock() |
| 16 | var at attempt |
| 17 | if load(filepath.Join(m.cfg.PrivateRoot, "attempt-"+digest(a.WorkspaceKey), "attempt.json"), &at) != nil || !at.Local || at.AssignmentHash != digest(a) || at.Workspace.Path != path || filepath.Dir(path) != m.cfg.WorkspaceRoot { |
| 18 | return provider.ErrIdentity |
| 19 | } |
| 20 | resolved, err := canonical(path) |
| 21 | if err != nil || resolved != path { |
| 22 | return provider.ErrIdentity |
| 23 | } |
| 24 | return os.RemoveAll(path) |
| 25 | } |
| 26 | |
| 27 | func (m *Manager) localGitdir(at attempt) (string, error) { |
| 28 | dir := filepath.Join(at.Workspace.Path, ".git") |
| 29 | if at.Request.Path != at.Workspace.Path || at.Workspace.WorkspaceID == "" { |
| 30 | return "", provider.ErrIdentity |
| 31 | } |
| 32 | info, err := os.Lstat(dir) |
| 33 | if err != nil || !info.IsDir() { |
| 34 | return "", provider.ErrIdentity |
| 35 | } |
| 36 | config, err := regular(filepath.Join(dir, "config")) |
| 37 | if err != nil || at.ConfigHash == "" || digest(config) != at.ConfigHash { |
| 38 | return "", provider.ErrIdentity |
| 39 | } |
| 40 | err = filepath.WalkDir(dir, func(path string, entry os.DirEntry, err error) error { |
| 41 | if err != nil { |
| 42 | return err |
| 43 | } |
| 44 | rel, err := filepath.Rel(dir, path) |
| 45 | if err != nil || entry.Type()&os.ModeSymlink != 0 || (!entry.IsDir() && !entry.Type().IsRegular()) { |
| 46 | return provider.ErrIdentity |
| 47 | } |
| 48 | if rel == "commondir" || rel == "gitdir" || rel == "config.worktree" || rel == "worktrees" || rel == "objects/info/alternates" || rel == "objects/info/http-alternates" || strings.HasPrefix(rel, "hooks/") { |
| 49 | return provider.ErrIdentity |
| 50 | } |
| 51 | return nil |
| 52 | }) |
| 53 | return dir, err |
| 54 | } |
| 55 | |
| 56 | func (m *Manager) RemoveCheck(work string) error { |
| 57 | root := filepath.Dir(work) |
| 58 | if filepath.Base(work) != "work" || filepath.Dir(root) != m.cfg.WorkspaceRoot || !strings.HasPrefix(filepath.Base(root), "checks-") { |
| 59 | return provider.ErrIdentity |
| 60 | } |
| 61 | resolved, err := canonical(root) |
| 62 | if err != nil || resolved != root { |
| 63 | return provider.ErrIdentity |
| 64 | } |
| 65 | return os.RemoveAll(root) |
| 66 | } |
| 67 | |