61 lines
1.5 KiB
Go
61 lines
1.5 KiB
Go
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package filesystem
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"connectrpc.com/connect"
|
|
|
|
"git.omukk.dev/wrenn/sandbox/envd/internal/permissions"
|
|
rpc "git.omukk.dev/wrenn/sandbox/envd/internal/services/spec/filesystem"
|
|
)
|
|
|
|
func (s Service) Move(ctx context.Context, req *connect.Request[rpc.MoveRequest]) (*connect.Response[rpc.MoveResponse], error) {
|
|
u, err := permissions.GetAuthUser(ctx, s.defaults.User)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
source, err := permissions.ExpandAndResolve(req.Msg.GetSource(), u, s.defaults.Workdir)
|
|
if err != nil {
|
|
return nil, connect.NewError(connect.CodeInvalidArgument, err)
|
|
}
|
|
|
|
destination, err := permissions.ExpandAndResolve(req.Msg.GetDestination(), u, s.defaults.Workdir)
|
|
if err != nil {
|
|
return nil, connect.NewError(connect.CodeInvalidArgument, err)
|
|
}
|
|
|
|
uid, gid, userErr := permissions.GetUserIdInts(u)
|
|
if userErr != nil {
|
|
return nil, connect.NewError(connect.CodeInternal, userErr)
|
|
}
|
|
|
|
userErr = permissions.EnsureDirs(filepath.Dir(destination), uid, gid)
|
|
if userErr != nil {
|
|
return nil, connect.NewError(connect.CodeInternal, userErr)
|
|
}
|
|
|
|
err = os.Rename(source, destination)
|
|
if err != nil {
|
|
if os.IsNotExist(err) {
|
|
return nil, connect.NewError(connect.CodeNotFound, fmt.Errorf("source file not found: %w", err))
|
|
}
|
|
|
|
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("error renaming: %w", err))
|
|
}
|
|
|
|
entry, err := entryInfo(destination)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return connect.NewResponse(&rpc.MoveResponse{
|
|
Entry: entry,
|
|
}), nil
|
|
}
|