Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,19 @@ func Run(ctx context.Context, args []string, stdout io.Writer, stderr io.Writer)
fmt.Fprintf(w, "unmounted %s\n", name)
return nil
}),
{
Name: "fsmonitor-hook",
Usage: "internal fsmonitor hook",
Hidden: true,
Flags: []ucli.Flag{ucli.StringFlag{Name: "name", Usage: "repo name (required)"}},
Action: withService(ctx, root, stderr, func(c *ucli.Context, svc *daemon.Service) error {
name := strings.TrimSpace(c.String("name"))
if name == "" {
return fmt.Errorf("--name required")
}
return svc.FSMonitorHook(ctx, name, stdout)
}),
},
{
Name: "set-refresh",
Usage: "update refresh interval",
Expand Down
69 changes: 69 additions & 0 deletions internal/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"context"
"errors"
"fmt"
"io"
"io/fs"
"log/slog"
"os"
"path/filepath"
"sort"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -565,6 +567,8 @@ func (s *Service) mountRepo(ctx context.Context, cfg model.RepoConfig) error {
if err != nil {
s.logger.Error("fuse mount failed, running without FUSE", "repo", cfg.Name, "error", err)
mfs = nil
} else {
s.configureStatusOptimization(ctx, cfg)
}
rt := &repoRuntime{
cfg: cfg,
Expand Down Expand Up @@ -804,6 +808,7 @@ func (s *Service) runPrepare(ctx context.Context, cfg model.RepoConfig) error {
if err := s.completePreparedRuntime(ctx, cfg, headOID, headRef, gen); err != nil {
return fail(err)
}
s.configureStatusOptimization(ctx, cfg)
return nil
}

Expand Down Expand Up @@ -979,6 +984,70 @@ func (s *Service) onHEADChanged(ctx context.Context, rt *repoRuntime) {
s.mu.Lock()
setHeadState(&rt.state, oid, ref, gen)
s.mu.Unlock()
s.configureStatusOptimization(ctx, rt.cfg)
}

func (s *Service) configureStatusOptimization(ctx context.Context, cfg model.RepoConfig) {
if err := s.git.ConfigureStatusOptimization(ctx, cfg, s.root); err != nil {
s.logger.Warn("git status optimization setup failed", "repo", cfg.Name, "error", err)
}
}

func (s *Service) FSMonitorHook(ctx context.Context, name string, w io.Writer) error {
cfg, err := s.registry.GetRepo(ctx, name)
if err != nil {
return err
}
s.fillPaths(&cfg)
ov, err := overlay.New(ctx, cfg)
if err != nil {
return err
}
defer ov.Close()
entries, err := ov.ListAll(ctx)
if err != nil {
return err
}
paths := fsMonitorDirtyPaths(entries)
token := fmt.Sprintf("artifact-fs:%s:%d", cfg.ID, time.Now().UnixNano())
if _, err := io.WriteString(w, token); err != nil {
return err
}
if _, err := w.Write([]byte{0}); err != nil {
return err
}
for _, p := range paths {
if _, err := io.WriteString(w, p); err != nil {
return err
}
if _, err := w.Write([]byte{0}); err != nil {
return err
}
}
return nil
}

func fsMonitorDirtyPaths(entries []model.OverlayEntry) []string {
set := map[string]struct{}{}
add := func(path string) {
path = model.CleanPath(path)
if path == "." {
return
}
set[path] = struct{}{}
}
for _, e := range entries {
add(e.Path)
if e.TargetPath != "" {
add(e.TargetPath)
}
}
out := make([]string, 0, len(set))
for p := range set {
out = append(out, p)
}
sort.Strings(out)
return out
}

func (s *Service) refreshLoop(rt *repoRuntime) {
Expand Down
77 changes: 77 additions & 0 deletions internal/daemon/status_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package daemon

import (
"bytes"
"context"
"io"
"log/slog"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
"time"

"github.com/cloudflare/artifact-fs/internal/model"
"github.com/cloudflare/artifact-fs/internal/overlay"
)

func TestReadPersistedStatusIncludesHydrationStats(t *testing.T) {
Expand Down Expand Up @@ -46,3 +53,73 @@ func TestReadPersistedStatusIncludesHydrationStats(t *testing.T) {
t.Fatalf("HydratedBlobBytes = %d, want 8", st.HydratedBlobBytes)
}
}

func TestFSMonitorDirtyPathsIncludesChangedPathsAndRenameEndpoints(t *testing.T) {
paths := fsMonitorDirtyPaths([]model.OverlayEntry{
{Path: "src/pkg/file.go", Kind: model.OverlayKindModify},
{Path: "new/name.go", Kind: model.OverlayKindRename, TargetPath: "old/name.go"},
{Path: ".", Kind: model.OverlayKindMkdir},
})
want := []string{
"new/name.go",
"old/name.go",
"src/pkg/file.go",
}
if !reflect.DeepEqual(paths, want) {
t.Fatalf("dirty paths = %#v, want %#v", paths, want)
}
}

func TestFSMonitorHookOutputsDirtyOverlayPaths(t *testing.T) {
ctx := context.Background()
root := t.TempDir()
svc, err := New(ctx, root, slog.New(slog.NewTextHandler(io.Discard, nil)))
if err != nil {
t.Fatal(err)
}
defer svc.Close()
cfg := model.RepoConfig{
Name: "repo",
ID: "repo",
Branch: "main",
RefreshInterval: time.Minute,
Enabled: true,
}
svc.fillPaths(&cfg)
if err := svc.registry.AddRepo(ctx, cfg); err != nil {
t.Fatal(err)
}
ov, err := overlay.New(ctx, cfg)
if err != nil {
t.Fatal(err)
}
if _, err := ov.CreateFile(ctx, "new/file.txt", 0o644); err != nil {
t.Fatal(err)
}
if err := ov.Remove(ctx, "deleted.txt"); err != nil {
t.Fatal(err)
}
if err := ov.Close(); err != nil {
t.Fatal(err)
}

var out bytes.Buffer
if err := svc.FSMonitorHook(ctx, "repo", &out); err != nil {
t.Fatal(err)
}
parts := strings.Split(out.String(), "\x00")
if !strings.HasPrefix(parts[0], "artifact-fs:repo:") {
t.Fatalf("token = %q", parts[0])
}
got := map[string]bool{}
for _, p := range parts[1:] {
if p != "" {
got[p] = true
}
}
for _, want := range []string{"deleted.txt", "new/file.txt"} {
if !got[want] {
t.Fatalf("fsmonitor paths missing %q: %v", want, got)
}
}
}
Loading
Loading