goldsmith/cache.go
Alex Yatskov e408fa9335 Squashed commit of the following:
commit 106991bfd59bb95977045399f7b4e5f4e7addc56
Author: Alex Yatskov <alex@foosoft.net>
Date:   Sat Jan 8 11:19:21 2022 -0800

    Rename property functions

commit 07f6033d4e86df257806af16de002ce8fad3d67f
Author: Alex Yatskov <alex@foosoft.net>
Date:   Sat Jan 8 11:16:46 2022 -0800

    Update rewrite

commit ea2dc0b4d223d54832752f0efd3eb46d81d17b50
Author: Alex Yatskov <alex@foosoft.net>
Date:   Fri Jan 7 22:47:17 2022 -0800

    Add more property methods

commit e2f7f8dc7ebc5b668539e8233e323a7c256eced2
Author: Alex Yatskov <alex@foosoft.net>
Date:   Fri Jan 7 22:39:23 2022 -0800

    Add step indexing

commit a7ed2a0b1c95ed9fbb0bbd87c779cf51a92f0b5f
Author: Alex Yatskov <alex@foosoft.net>
Date:   Fri Jan 7 20:50:19 2022 -0800

    Use self

commit 7ecc01e508c06680dcb6b6ecd4265a7f72e2c933
Author: Alex Yatskov <alex@foosoft.net>
Date:   Sun Aug 22 12:33:09 2021 -0700

    Cleanup

commit 87dd28a4c14b88bea061ca913f68e272cca787f9
Author: Alex Yatskov <alex@foosoft.net>
Date:   Sun Aug 22 12:09:41 2021 -0700

    Cleanup

commit 129130128d2a50f119c46f70e7cb70ef421892ff
Merge: 629fce0 e751d70
Author: Alex Yatskov <alex@foosoft.net>
Date:   Sat Aug 21 12:18:09 2021 -0700

    Merge branch 'master' into dev

commit 629fce06a8fca6810ec772f558d9ffabda016d01
Author: Alex Yatskov <alex@foosoft.net>
Date:   Sun Jun 27 19:34:53 2021 -0700

    Abstract metadata
2022-01-08 18:08:36 -08:00

91 lines
1.8 KiB
Go

package goldsmith
import (
"encoding/binary"
"fmt"
"hash/crc32"
"os"
"path/filepath"
"sort"
)
type cache struct {
baseDir string
}
func (self *cache) retrieveFile(context *Context, outputPath string, inputFiles []*File) (*File, error) {
cachePath, err := self.buildCachePath(context, outputPath, inputFiles)
if err != nil {
return nil, err
}
outputFile, err := context.CreateFileFromAsset(outputPath, cachePath)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, err
}
return outputFile, nil
}
func (self *cache) storeFile(context *Context, outputFile *File, inputFiles []*File) error {
cachePath, err := self.buildCachePath(context, outputFile.Path(), inputFiles)
if err != nil {
return err
}
if err := os.MkdirAll(self.baseDir, 0755); err != nil {
return err
}
fp, err := os.Create(cachePath)
if err != nil {
return err
}
defer fp.Close()
offset, err := outputFile.Seek(0, os.SEEK_CUR)
if err != nil {
return err
}
if _, err := outputFile.Seek(0, os.SEEK_SET); err != nil {
return err
}
if _, err := outputFile.WriteTo(fp); err != nil {
return err
}
if _, err := outputFile.Seek(offset, os.SEEK_SET); err != nil {
return err
}
return nil
}
func (self *cache) buildCachePath(context *Context, outputPath string, inputFiles []*File) (string, error) {
hasher := crc32.NewIEEE()
hasher.Write([]byte(outputPath))
sort.Sort(filesByPath(inputFiles))
for _, inputFile := range inputFiles {
modTimeBuff := make([]byte, 8)
binary.LittleEndian.PutUint64(modTimeBuff, uint64(inputFile.ModTime().UnixNano()))
hasher.Write([]byte(inputFile.Path()))
hasher.Write(modTimeBuff)
}
cachePath := filepath.Join(self.baseDir, fmt.Sprintf(
"gs_%.8x%s",
hasher.Sum32(),
filepath.Ext(outputPath),
))
return cachePath, nil
}