diff --git a/internal/config/dir.go b/internal/config/dir.go index b6b797c..949c1a2 100644 --- a/internal/config/dir.go +++ b/internal/config/dir.go @@ -9,44 +9,20 @@ import ( "syscall" ) -// HomeDir returns the user's home directory, which can be overridden with the {ENV_PREFIX}HOME variable. -func (c *Config) HomeDir() (string, error) { - d := os.Getenv(c.Application.EnvPrefix + "HOME") - if d != "" { - return d, nil - } - return os.UserHomeDir() -} - -// WritableUserDir returns the path to a writable user-level directory. -func (c *Config) WritableUserDir() (string, error) { - if c.writableUserDir != "" { - return c.writableUserDir, nil - } - hd, err := c.HomeDir() - if err != nil { - return "", err - } - path := filepath.Join(hd, c.Application.WritableUserDir) - if err := os.MkdirAll(path, 0o700); err != nil { - return "", err - } - c.writableUserDir = path - - return path, nil -} - -// TempDir returns the path to a user-specific temporary directory. +// TempDir returns the path to a user-specific temporary directory, suitable for caches. // // It creates the temporary directory if it does not already exist. // -// It does not use os.TempDir on Linux as that usually returns /tmp which could -// conflict with other users. It also does not use os.MkdirTemp as the CLI -// usually needs a stable (not random) directory path. It therefore uses -// os.UserCacheDir which in turn will use XDG_CACHE_HOME or the home directory. +// The directory can be specified in the {ENV_PREFIX}TMP environment variable. +// +// This does not use os.TempDir, as on Linux/Unix systems that usually returns a +// global /tmp directory, which could conflict with other users. It also does not +// use os.MkdirTemp, as the CLI usually needs a stable (not random) directory +// path. It therefore uses os.UserCacheDir which in turn will use XDG_CACHE_HOME +// or the home directory. func (c *Config) TempDir() (string, error) { - if c.cacheDir != "" { - return c.cacheDir, nil + if c.tempDir != "" { + return c.tempDir, nil } d := os.Getenv(c.Application.EnvPrefix + "TMP") if d == "" { @@ -77,7 +53,35 @@ func (c *Config) TempDir() (string, error) { return "", err } } - c.cacheDir = path + c.tempDir = path return path, nil } + +// WritableUserDir returns the path to a writable user-level directory. +// Deprecated: unless backwards compatibility is desired, TempDir is preferable. +func (c *Config) WritableUserDir() (string, error) { + if c.writableUserDir != "" { + return c.writableUserDir, nil + } + hd, err := c.HomeDir() + if err != nil { + return "", err + } + path := filepath.Join(hd, c.Application.WritableUserDir) + if err := os.MkdirAll(path, 0o700); err != nil { + return "", err + } + c.writableUserDir = path + + return path, nil +} + +// HomeDir returns the user's home directory, which can be overridden with the {ENV_PREFIX}HOME variable. +func (c *Config) HomeDir() (string, error) { + d := os.Getenv(c.Application.EnvPrefix + "HOME") + if d != "" { + return d, nil + } + return os.UserHomeDir() +} diff --git a/internal/config/schema.go b/internal/config/schema.go index ff6eef9..e282370 100644 --- a/internal/config/schema.go +++ b/internal/config/schema.go @@ -56,7 +56,7 @@ type Config struct { DomainWildcards []string `validate:"required" yaml:"domain_wildcards"` // e.g. ["*.platform.sh"] } `validate:"required"` - cacheDir string `yaml:"-"` + tempDir string `yaml:"-"` writableUserDir string `yaml:"-"` } diff --git a/internal/file/file.go b/internal/file/file.go index b57dad8..44e840a 100644 --- a/internal/file/file.go +++ b/internal/file/file.go @@ -47,13 +47,16 @@ func probablyMatches(filename string, data []byte) (bool, error) { return false, nil } - // Read the end of the file (up to 32 KB). - buf := make([]byte, min(32*1024, len(data))) - offset := max(0, len(data)-32*1024) + return matchEndOfFile(f, data, 32*1024) +} + +func matchEndOfFile(f *os.File, b []byte, size int) (bool, error) { + buf := make([]byte, min(size, len(b))) + offset := max(0, len(b)-size) n, err := f.ReadAt(buf, int64(offset)) if err != nil && err != io.EOF { return false, err } - return bytes.Equal(data[offset:], buf[:n]), nil + return bytes.Equal(b[offset:], buf[:n]), nil }