53 lines
925 B
Go
53 lines
925 B
Go
package workspace
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
|
|
"git.michelsen.id/phill/chron/chron-note/internal/domain"
|
|
)
|
|
|
|
type SnapshotObject struct {
|
|
ID domain.ObjectID
|
|
Size int64
|
|
ModNS int64
|
|
}
|
|
|
|
func (ws *Workspace) Snapshot(ctx context.Context) ([]SnapshotObject, error) {
|
|
entries, err := os.ReadDir(ws.Dir)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("readdir workspace dir: %w", err)
|
|
}
|
|
|
|
out := make([]SnapshotObject, 0, len(entries))
|
|
for _, e := range entries {
|
|
if ctx.Err() != nil {
|
|
return nil, ctx.Err()
|
|
}
|
|
if e.IsDir() {
|
|
continue
|
|
}
|
|
info, err := e.Info()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("stat workspace entry: %w", err)
|
|
}
|
|
if !info.Mode().IsRegular() {
|
|
continue
|
|
}
|
|
|
|
id, err := domain.ParseObjectID(e.Name())
|
|
if err != nil {
|
|
continue
|
|
}
|
|
|
|
out = append(out, SnapshotObject{
|
|
ID: id,
|
|
Size: info.Size(),
|
|
ModNS: info.ModTime().UnixNano(),
|
|
})
|
|
}
|
|
|
|
return out, nil
|
|
}
|