| 1 | package workspacestate |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "os" |
| 7 | "strings" |
| 8 | |
| 9 | "reasonix/internal/pathidentity" |
| 10 | ) |
| 11 | |
| 12 | var ( |
| 13 | ErrCreationWorkspaceUnavailable = errors.New("creation workspace is unavailable") |
| 14 | ErrCreationWorkspaceChanged = errors.New("creation workspace names a different directory") |
| 15 | ErrCreationSessionInactive = errors.New("creation session was archived or removed") |
| 16 | ) |
| 17 | |
| 18 | // ResolveCreationWorkspace reconciles a journal's workspace ID without changing |
| 19 | // its operation/session identity or registering a removed workspace. |
| 20 | func ResolveCreationWorkspace(state State, workspaceID, root string) (Workspace, error) { |
| 21 | info, err := os.Stat(root) |
| 22 | if err != nil || !info.IsDir() { |
| 23 | return Workspace{}, ErrCreationWorkspaceUnavailable |
| 24 | } |
| 25 | if previous, ok := state.Workspaces[workspaceID]; ok { |
| 26 | same, err := pathidentity.Same(previous.Root, root, pathidentity.Options{FollowLeaf: true}) |
| 27 | if err != nil { |
| 28 | return Workspace{}, ErrCreationWorkspaceUnavailable |
| 29 | } |
| 30 | if !same { |
| 31 | return Workspace{}, ErrCreationWorkspaceChanged |
| 32 | } |
| 33 | } |
| 34 | id, ok, err := ResolveWorkspaceID(state, root) |
| 35 | if err != nil { |
| 36 | return Workspace{}, ErrCreationWorkspaceUnavailable |
| 37 | } |
| 38 | if !ok { |
| 39 | return Workspace{}, ErrWorkspaceNotFound |
| 40 | } |
| 41 | return state.Workspaces[id], nil |
| 42 | } |
| 43 | |
| 44 | func resolveCreationWorkspace(state State, workspaceID, root, sessionID string) (Workspace, error) { |
| 45 | if lifecycle := state.SessionStates[sessionID].Lifecycle; lifecycle == Archived || lifecycle == Deleted { |
| 46 | return Workspace{}, ErrCreationSessionInactive |
| 47 | } |
| 48 | return ResolveCreationWorkspace(state, workspaceID, root) |
| 49 | } |
| 50 | |
| 51 | // These operations resolve under the registry transaction lock: another |
| 52 | // process may merge workspace IDs between reservation and publication. |
| 53 | func (s *Store) BeginCreateAtRoot(ctx context.Context, pending PendingCreate, root string) (Workspace, error) { |
| 54 | if strings.TrimSpace(root) == "" { |
| 55 | return Workspace{}, ErrCreationWorkspaceUnavailable |
| 56 | } |
| 57 | return s.beginCreate(ctx, pending, root) |
| 58 | } |
| 59 | |
| 60 | func (s *Store) AttachCreatedSessionAtRoot(ctx context.Context, operationID, workspaceID, sessionID, root string) error { |
| 61 | return s.attachSession(ctx, operationID, workspaceID, sessionID, "", "", nil, root) |
| 62 | } |
| 63 |