Create named subdirectories for new repositories
This commit is contained in:
@@ -22,6 +22,7 @@ type MountManager struct {
|
||||
type MountedRepository struct {
|
||||
Repository model.Repository
|
||||
Mounted bool
|
||||
MountPoint string
|
||||
CredentialFile string
|
||||
}
|
||||
|
||||
@@ -32,7 +33,12 @@ func (m *MountManager) Prepare(ctx context.Context, repo model.Repository) (Moun
|
||||
}
|
||||
if repo.Mount == nil || !repo.Mount.Managed {
|
||||
if _, err := os.Stat(repo.Location); err != nil {
|
||||
return result, fmt.Errorf("connectivity: external mount unavailable: %w", err)
|
||||
if !os.IsNotExist(err) {
|
||||
return result, fmt.Errorf("connectivity: external mount unavailable: %w", err)
|
||||
}
|
||||
if _, parentErr := os.Stat(filepath.Dir(repo.Location)); parentErr != nil {
|
||||
return result, fmt.Errorf("connectivity: external mount unavailable: %w", parentErr)
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
@@ -40,6 +46,11 @@ func (m *MountManager) Prepare(ctx context.Context, repo model.Repository) (Moun
|
||||
if !strings.HasPrefix(point, "/mnt/remotes/") && !strings.HasPrefix(point, "/mnt/disks/") {
|
||||
return result, errors.New("validation: managed mountPoint must be below /mnt/remotes or /mnt/disks")
|
||||
}
|
||||
repositoryPath := filepath.Clean(repo.Location)
|
||||
relative, err := filepath.Rel(point, repositoryPath)
|
||||
if err != nil || relative == ".." || strings.HasPrefix(relative, ".."+string(filepath.Separator)) {
|
||||
return result, errors.New("validation: managed repository location must be inside its mountPoint")
|
||||
}
|
||||
if err := os.MkdirAll(point, 0700); err != nil {
|
||||
return result, err
|
||||
}
|
||||
@@ -86,7 +97,7 @@ func (m *MountManager) Prepare(ctx context.Context, repo model.Repository) (Moun
|
||||
return result, fmt.Errorf("connectivity: mount repository: %w", err)
|
||||
}
|
||||
result.Mounted = true
|
||||
result.Repository.Location = point
|
||||
result.MountPoint = point
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@@ -97,5 +108,5 @@ func (m *MountManager) Cleanup(ctx context.Context, mounted MountedRepository) e
|
||||
if !mounted.Mounted {
|
||||
return nil
|
||||
}
|
||||
return exec.CommandContext(ctx, "umount", mounted.Repository.Location).Run()
|
||||
return exec.CommandContext(ctx, "umount", mounted.MountPoint).Run()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package platform
|
||||
|
||||
import (
|
||||
"context"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"git.casaderoll.de/michael/urbm/internal/model"
|
||||
)
|
||||
|
||||
func TestExternalMountAllowsNewRepositorySubdirectory(t *testing.T) {
|
||||
base := t.TempDir()
|
||||
repo := model.Repository{Type: model.RepositorySMB, Location: filepath.Join(base, "Photos")}
|
||||
manager := &MountManager{}
|
||||
|
||||
mounted, err := manager.Prepare(context.Background(), repo)
|
||||
if err != nil {
|
||||
t.Fatalf("new repository subdirectory rejected: %v", err)
|
||||
}
|
||||
if mounted.Repository.Location != repo.Location {
|
||||
t.Fatalf("location = %q, want %q", mounted.Repository.Location, repo.Location)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExternalMountRejectsMissingBaseDirectory(t *testing.T) {
|
||||
repo := model.Repository{Type: model.RepositoryNFS, Location: filepath.Join(t.TempDir(), "missing", "Photos")}
|
||||
manager := &MountManager{}
|
||||
|
||||
if _, err := manager.Prepare(context.Background(), repo); err == nil {
|
||||
t.Fatal("missing external mount base accepted")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user