Coordinate workspace cleanup with active operations
This commit is contained in:
@@ -172,6 +172,18 @@ func initializeSession(ctx context.Context, request *sessionInitializationInput,
|
||||
return report, err
|
||||
}
|
||||
if !remote {
|
||||
managed, err := pathInsideManagedWork(base.Pipeline.Workspace.Root, output)
|
||||
if err != nil {
|
||||
return report, fmt.Errorf("session init: resolve output: %w", err)
|
||||
}
|
||||
if managed {
|
||||
store := deps.newWorkspaceStore(base.Pipeline.Workspace.Root)
|
||||
lock, lockErr := store.AcquireWorkspaceLockContext(ctx)
|
||||
if lockErr != nil {
|
||||
return report, fmt.Errorf("session init: acquire workspace lock: %w", lockErr)
|
||||
}
|
||||
defer func() { err = errors.Join(err, store.ReleaseWorkspaceLock(lock)) }()
|
||||
}
|
||||
if err := writeLocalFile(output, data, force); err != nil {
|
||||
return report, fmt.Errorf("session init: %w", err)
|
||||
}
|
||||
@@ -210,3 +222,60 @@ func initializeSession(ctx context.Context, request *sessionInitializationInput,
|
||||
report = &sessionInitializationReport{Campaign: config.CampaignID(base.Campaign), SessionID: sessionID, Destination: "s3://" + s3BucketName(base.Pipeline) + "/" + key, Remote: true, Written: true}
|
||||
return report, nil
|
||||
}
|
||||
|
||||
// pathInsideManagedWork resolves existing ancestors so aliases to the work tree
|
||||
// participate in the same coordination as paths written directly beneath it.
|
||||
func pathInsideManagedWork(workspace, output string) (bool, error) {
|
||||
within := func(work, target string) (bool, error) {
|
||||
relative, err := filepath.Rel(work, target)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return relative == "." || (relative != ".." && !strings.HasPrefix(relative, ".."+string(filepath.Separator))), nil
|
||||
}
|
||||
workPath, err := filepath.Abs(filepath.Join(workspace, config.PathWorkDirSegment))
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
outputPath, err := filepath.Abs(output)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if inside, err := within(workPath, outputPath); inside || err != nil {
|
||||
return inside, err
|
||||
}
|
||||
resolve := func(path string) (string, error) {
|
||||
absolute, err := filepath.Abs(path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
candidate := absolute
|
||||
for {
|
||||
resolved, err := filepath.EvalSymlinks(candidate)
|
||||
if err == nil {
|
||||
rest, relErr := filepath.Rel(candidate, absolute)
|
||||
if relErr != nil {
|
||||
return "", relErr
|
||||
}
|
||||
return filepath.Join(resolved, rest), nil
|
||||
}
|
||||
if !errors.Is(err, os.ErrNotExist) {
|
||||
return "", err
|
||||
}
|
||||
parent := filepath.Dir(candidate)
|
||||
if parent == candidate {
|
||||
return "", err
|
||||
}
|
||||
candidate = parent
|
||||
}
|
||||
}
|
||||
work, err := resolve(filepath.Join(workspace, config.PathWorkDirSegment))
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
target, err := resolve(output)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return within(work, target)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user