From 970b1a424a0e3a2cb42e9b86948236414e96cda6 Mon Sep 17 00:00:00 2001 From: jola5 Date: Sun, 29 Jan 2017 10:38:39 +0100 Subject: [PATCH] Fix bugged implementation --- cmd/api_serve.go | 19 +++++++++++++++++-- cmd/serve.go | 18 ++++++++++++++++-- system/t07_serve/RootDirInaccessible_gold | 2 +- system/t07_serve/__init__.py | 5 ++++- 4 files changed, 38 insertions(+), 6 deletions(-) diff --git a/cmd/api_serve.go b/cmd/api_serve.go index d2eebaed..c008b355 100644 --- a/cmd/api_serve.go +++ b/cmd/api_serve.go @@ -6,6 +6,7 @@ import ( "github.com/smira/commander" "github.com/smira/flag" "net/http" + "os" "golang.org/x/sys/unix" ) @@ -19,8 +20,22 @@ func aptlyAPIServe(cmd *commander.Command, args []string) error { return commander.ErrCommandError } - if unix.Access(context.Config().RootDir, unix.W_OK) != nil { - return fmt.Errorf("Configured rootDir '%s' is inaccessible, check access rights", context.Config().RootDir) + // There are only two working options for aptly's rootDir: + // 1. rootDir does not exist, then we'll create it + // 2. rootDir exists and is writable + // anything else must fail. + // E.g.: Running the service under a different user may lead to a rootDir + // that exists but is not usable due to access permissions. + // Config loads and returns current configuration + _, err = os.Stat(context.Config().RootDir); + if err != nil { + if ! os.IsNotExist(err) { + return fmt.Errorf("Something went wrong, %v", err) + } + } else { + if unix.Access(context.Config().RootDir, unix.W_OK) != nil { + return fmt.Errorf("Configured rootDir '%s' is inaccessible, check access rights", context.Config().RootDir) + } } listen := context.Flags().Lookup("listen").Value.String() diff --git a/cmd/serve.go b/cmd/serve.go index 346fa4e5..f2679204 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -23,8 +23,22 @@ func aptlyServe(cmd *commander.Command, args []string) error { return commander.ErrCommandError } - if unix.Access(context.Config().RootDir, unix.W_OK) != nil { - return fmt.Errorf("Configured rootDir '%s' is inaccessible, check access rights", context.Config().RootDir) + // There are only two working options for aptly's rootDir: + // 1. rootDir does not exist, then we'll create it + // 2. rootDir exists and is writable + // anything else must fail. + // E.g.: Running the service under a different user may lead to a rootDir + // that exists but is not usable due to access permissions. + // Config loads and returns current configuration + _, err = os.Stat(context.Config().RootDir); + if err != nil { + if ! os.IsNotExist(err) { + return fmt.Errorf("Something went wrong, %v", err) + } + } else { + if unix.Access(context.Config().RootDir, unix.W_OK) != nil { + return fmt.Errorf("Configured rootDir '%s' is inaccessible, check access rights", context.Config().RootDir) + } } if context.CollectionFactory().PublishedRepoCollection().Len() == 0 { diff --git a/system/t07_serve/RootDirInaccessible_gold b/system/t07_serve/RootDirInaccessible_gold index 3d57b5c6..be33d2e7 100644 --- a/system/t07_serve/RootDirInaccessible_gold +++ b/system/t07_serve/RootDirInaccessible_gold @@ -1 +1 @@ -ERROR: Configured rootDir '/rootDir/does/not/exist' is inaccessible, check access rights +ERROR: Configured rootDir '/root' is inaccessible, check access rights diff --git a/system/t07_serve/__init__.py b/system/t07_serve/__init__.py index f7513745..9e7bcedc 100644 --- a/system/t07_serve/__init__.py +++ b/system/t07_serve/__init__.py @@ -19,7 +19,10 @@ class RootDirInaccessible(BaseTest): """ fixtureDB = False fixturePool = False - configOverride = { "rootDir": "/rootDir/does/not/exist" } + + configOverride = { + "rootDir": "/root" # any directory that exists but is not writable + } runCmd = "aptly serve -listen=127.0.0.1:8765" def run(self):