From 9ee652c81817354c4007352112ab313dbb559116 Mon Sep 17 00:00:00 2001 From: Anthony Lapenna Date: Tue, 3 Jan 2017 08:32:53 +1300 Subject: [PATCH] fix(api): creates the data directory if not exist (#452) --- api/cmd/portainer/main.go | 10 +++++----- api/file/file.go | 37 ++++++++++++++++++++++++++----------- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/api/cmd/portainer/main.go b/api/cmd/portainer/main.go index c11f9b34f..e01c304a9 100644 --- a/api/cmd/portainer/main.go +++ b/api/cmd/portainer/main.go @@ -29,6 +29,11 @@ func main() { Logo: *flags.Logo, } + fileService, err := file.NewService(*flags.Data, "") + if err != nil { + log.Fatal(err) + } + var store = bolt.NewStore(*flags.Data) err = store.Open() if err != nil { @@ -41,11 +46,6 @@ func main() { log.Fatal(err) } - fileService, err := file.NewService(*flags.Data) - if err != nil { - log.Fatal(err) - } - var cryptoService portainer.CryptoService = &crypto.Service{} // Initialize the active endpoint from the CLI only if there is no diff --git a/api/file/file.go b/api/file/file.go index cd76acec9..c9ad71a4a 100644 --- a/api/file/file.go +++ b/api/file/file.go @@ -1,13 +1,12 @@ package file import ( - "strconv" - "github.com/portainer/portainer" "io" "os" "path" + "strconv" ) const ( @@ -21,18 +20,26 @@ const ( TLSKeyFile = "key.pem" ) -// Service represents a service for managing files. +// Service represents a service for managing files and directories. type Service struct { + dataStorePath string fileStorePath string } -// NewService initializes a new service. -func NewService(fileStorePath string) (*Service, error) { +// NewService initializes a new service. It creates a data directory and a directory to store files +// inside this directory if they don't exist. +func NewService(dataStorePath, fileStorePath string) (*Service, error) { service := &Service{ - fileStorePath: fileStorePath, + dataStorePath: dataStorePath, + fileStorePath: path.Join(dataStorePath, fileStorePath), } - err := service.createFolderInStoreIfNotExist(TLSStorePath) + err := createDirectoryIfNotExist(dataStorePath, 0755) + if err != nil { + return nil, err + } + + err = service.createDirectoryInStoreIfNotExist(TLSStorePath) if err != nil { return nil, err } @@ -44,7 +51,7 @@ func NewService(fileStorePath string) (*Service, error) { func (service *Service) StoreTLSFile(endpointID portainer.EndpointID, fileType portainer.TLSFileType, r io.Reader) error { ID := strconv.Itoa(int(endpointID)) endpointStorePath := path.Join(TLSStorePath, ID) - err := service.createFolderInStoreIfNotExist(endpointStorePath) + err := service.createDirectoryInStoreIfNotExist(endpointStorePath) if err != nil { return err } @@ -97,12 +104,20 @@ func (service *Service) DeleteTLSFiles(endpointID portainer.EndpointID) error { return nil } -// createFolderInStoreIfNotExist creates a new folder in the file store if it doesn't exists on the file system. -func (service *Service) createFolderInStoreIfNotExist(name string) error { +// createDirectoryInStoreIfNotExist creates a new directory in the file store if it doesn't exists on the file system. +func (service *Service) createDirectoryInStoreIfNotExist(name string) error { path := path.Join(service.fileStorePath, name) + return createDirectoryIfNotExist(path, 0700) +} + +// createDirectoryIfNotExist creates a directory if it doesn't exists on the file system. +func createDirectoryIfNotExist(path string, mode uint32) error { _, err := os.Stat(path) if os.IsNotExist(err) { - os.Mkdir(path, 0600) + err = os.Mkdir(path, os.FileMode(mode)) + if err != nil { + return err + } } else if err != nil { return err }