| 1 | package hostfloor |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "sort" |
| 10 | "strings" |
| 11 | |
| 12 | "github.com/carlosframework/platform/internal/rootparticipation" |
| 13 | "github.com/carlosframework/platform/internal/safefile" |
| 14 | ) |
| 15 | |
| 16 | func (d Disk) CheckParticipation(ctx context.Context, dataDir string, requireDirectory bool) error { |
| 17 | return d.WithParticipation(ctx, func(ctx context.Context, l rootparticipation.Ledger) error { |
| 18 | if err := l.Idle(ctx); err != nil { |
| 19 | return err |
| 20 | } |
| 21 | return checkLegacyParticipation(dataDir, false) |
| 22 | }, func() error { return checkLegacyParticipation(dataDir, requireDirectory) }) |
| 23 | } |
| 24 | |
| 25 | func checkLegacyParticipation(dataDir string, requireDirectory bool) error { |
| 26 | dir := filepath.Join(dataDir, ".hostexec") |
| 27 | f, err := safefile.OpenDir(dir) |
| 28 | if errors.Is(err, os.ErrNotExist) && !requireDirectory { |
| 29 | return nil |
| 30 | } |
| 31 | if err != nil { |
| 32 | return fmt.Errorf("%s cannot be listed: %v", dir, err) |
| 33 | } |
| 34 | defer f.Close() |
| 35 | entries, err := f.ReadDir(-1) |
| 36 | if err != nil { |
| 37 | return fmt.Errorf("%s cannot be listed: %v", dir, err) |
| 38 | } |
| 39 | if len(entries) == 0 { |
| 40 | return nil |
| 41 | } |
| 42 | names := make([]string, 0, len(entries)) |
| 43 | for _, e := range entries { |
| 44 | names = append(names, e.Name()) |
| 45 | } |
| 46 | sort.Strings(names) |
| 47 | if len(names) > 3 { |
| 48 | names = append(names[:3], fmt.Sprintf("and %d more", len(entries)-3)) |
| 49 | } |
| 50 | return fmt.Errorf("this box takes part in a running claim operation (%s holds %s)", dir, strings.Join(names, ", ")) |
| 51 | } |
| 52 | |