Skip to content

Commit

Permalink
Use filesystem symlink mapping on non-windows systems.
Browse files Browse the repository at this point in the history
  • Loading branch information
lotodore committed Apr 12, 2023
1 parent b39ddd2 commit 3323bdb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
14 changes: 13 additions & 1 deletion link.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

package localcache

import "os"
import (
"os"
)

// Simply use native symlinks everywhere but on windows.

func Readlink(name string) (string, error) {
return os.Readlink(name)
Expand All @@ -11,3 +15,11 @@ func Readlink(name string) (string, error) {
func Symlink(oldname, newname string) error {
return os.Symlink(oldname, newname)
}

func Openlink(name string) (*os.File, error) {
return os.Open(name)
}

func ReadlinkFile(name string) ([]byte, error) {
return os.ReadFile(name)
}
16 changes: 16 additions & 0 deletions link_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,19 @@ func Readlink(name string) (string, error) {
func Symlink(oldname, newname string) error {
return os.WriteFile(newname, []byte(oldname), 0644)
}

func Openlink(name string) (*os.File, error) {
entry, err := Readlink(name)
if err != nil {
return nil, err
}
return os.Open(entry)
}

func ReadlinkFile(name string) ([]byte, error) {
entry, err := Readlink(name)
if err != nil {
return nil, err
}
return os.ReadFile(entry)
}
13 changes: 2 additions & 11 deletions localcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package localcache
import (
"crypto/sha256"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -240,22 +239,14 @@ func (c *Cache) IfExists(key string) string {
func (c *Cache) Open(key string) (*os.File, error) {
key = hash(key, false)
path := filepath.Join(c.root, key[:2], key)
entry, err := Readlink(path)
if err != nil {
return nil, err
}
return os.Open(entry)
return Openlink(path)
}

// ReadFile identified by key.
func (c *Cache) ReadFile(key string) ([]byte, error) {
key = hash(key, false)
path := filepath.Join(c.root, key[:2], key)
entry, err := Readlink(path)
if err != nil {
return nil, err
}
return ioutil.ReadFile(entry)
return ReadlinkFile(path)
}

// Purge entry for given key if older than given age.
Expand Down

0 comments on commit 3323bdb

Please sign in to comment.