Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
pjcdawkins committed Jan 13, 2025
1 parent a925f87 commit caa330b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 40 deletions.
74 changes: 39 additions & 35 deletions internal/config/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand Down Expand Up @@ -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()
}
2 changes: 1 addition & 1 deletion internal/config/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:"-"`
}

Expand Down
11 changes: 7 additions & 4 deletions internal/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit caa330b

Please sign in to comment.