From ba556bf5fe72908cf4dfe83a04c3d593db30ba16 Mon Sep 17 00:00:00 2001 From: Dominic Luechinger Date: Thu, 6 Apr 2017 00:10:43 +0200 Subject: [PATCH] Fix check if config file exists. Directory was detected as file 'os.Stat' checks if a file or directory exists. By adding an additional check (mode) the distinction could be made. --- config.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/config.go b/config.go index 8a20e54..024e0df 100644 --- a/config.go +++ b/config.go @@ -80,8 +80,12 @@ func (c Config) MkdirAll() error { } func (c Config) Exists(fileName string) bool { - _, err := os.Stat(filepath.Join(c.Path, fileName)) - return !os.IsNotExist(err) + return fileExists(filepath.Join(c.Path, fileName)) +} + +func fileExists(fullPath string) bool { + info, err := os.Stat(fullPath) + return err == nil && info.Mode().IsRegular() } // ConfigDir keeps setting for querying folders. @@ -135,7 +139,7 @@ func (c ConfigDir) QueryFolders(configType ConfigType) []*Config { } var existing []*Config for _, entry := range result { - if _, err := os.Stat(entry.Path); !os.IsNotExist(err) { + if fileExists(entry.Path) { existing = append(existing, entry) } } @@ -145,7 +149,7 @@ func (c ConfigDir) QueryFolders(configType ConfigType) []*Config { func (c ConfigDir) QueryFolderContainsFile(fileName string) *Config { configs := c.QueryFolders(Existing) for _, config := range configs { - if _, err := os.Stat(filepath.Join(config.Path, fileName)); !os.IsNotExist(err) { + if fileExists(filepath.Join(config.Path, fileName)) { return config } }