Commit 76cbf39c authored by Boris Mühmer's avatar Boris Mühmer
Browse files

preparations for information caches - NOT WORKING YET

parent d2cae16c
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -39,16 +39,18 @@ type Status struct {
	NumberOfDirectories int64
	NumberOfFiles       int64
	Contents            map[HashArray]FileDatas
	Files               map[string]HashArray
}

var m sync.Mutex

// FindFiles searches for files at the specific path.
func FindFiles(path string) (*Status, error) {
func FindFiles(path string, files map[string]HashArray, contents map[HashArray]FileDatas) (*Status, error) {

	var status Status

	status.Contents = make(map[HashArray]FileDatas, 0)
	status.Files = make(map[string]HashArray, 0)

	err := walk.Walk(path, func(path string, info os.FileInfo, err error) error {
		if err != nil {
@@ -76,6 +78,7 @@ func FindFiles(path string) (*Status, error) {
		}

		m.Lock()
		status.Files[path] = hash
		entries := status.Contents[hash]
		if entries == nil {
			entries = make(FileDatas, 0)
+7 −2
Original line number Diff line number Diff line
package filediscovery

import "testing"
import (
	"testing"
)

func TestFindFiles(t *testing.T) {
	_, err := FindFiles("")
	var files map[string]HashArray
	var contents map[HashArray]FileDatas

	_, err := FindFiles("", files, contents)
	if err != errorNotImplemented {
		t.Error(err)
	}
+62 −0
Original line number Diff line number Diff line
// based on: https://medium.com/@matryer/golang-advent-calendar-day-eleven-persisting-go-objects-to-disk-7caf1ee3d11d

package filediscovery

import (
	"bytes"
	"encoding/json"
	"io"
	"os"
	"sync"
)

var lock sync.Mutex

// Marshal is a function that marshals the object into an
// io.Reader.
// By default, it uses the JSON marshaller.
var Marshal = func(v interface{}) (io.Reader, error) {
	b, err := json.MarshalIndent(v, "", "\t")
	if err != nil {
		return nil, err
	}
	return bytes.NewReader(b), nil
}

// Unmarshal is a function that unmarshals the data from the
// reader into the specified value.
// By default, it uses the JSON unmarshaller.
var Unmarshal = func(r io.Reader, v interface{}) error {
	return json.NewDecoder(r).Decode(v)
}

// Save saves a representation of v to the file at path.
func Save(path string, v interface{}) error {
	lock.Lock()
	defer lock.Unlock()
	f, err := os.Create(path)
	if err != nil {
		return err
	}
	defer f.Close()
	r, err := Marshal(v)
	if err != nil {
		return err
	}
	_, err = io.Copy(f, r)
	return err
}

// Load loads the file at path into v.
// Use os.IsNotExist() to see if the returned error is due
// to the file being missing.
func Load(path string, v interface{}) error {
	lock.Lock()
	defer lock.Unlock()
	f, err := os.Open(path)
	if err != nil {
		return err
	}
	defer f.Close()
	return Unmarshal(f, v)
}
+28 −1
Original line number Diff line number Diff line
@@ -8,6 +8,11 @@ import (
	"repositories.muehmer.net/bsmrgo/filediscovery/internal/pkg/filediscovery"
)

const (
	pathDatFiles    = "$HOME/.cache/filediscovery/Files.dat"
	pathDatContents = "$HOME/.cache/filediscovery/Contents.dat"
)

func main() {
	var wastedSpace int64

@@ -17,7 +22,21 @@ func main() {

	fmt.Printf("Working on %s...\n", path)

	res, err := filediscovery.FindFiles(path)
	// TODO: ensure pathes exist

	// load data from file
	var files map[string]filediscovery.HashArray
	var contents map[filediscovery.HashArray]filediscovery.FileDatas

	if err := filediscovery.Load(pathDatFiles, files); err != nil {
		log.Fatal(err)
	}
	if err := filediscovery.Load(pathDatContents, contents); err != nil {
		log.Fatal(err)
	}

	// work on directory
	res, err := filediscovery.FindFiles(path, files, contents)
	if err != nil {
		log.Fatal(err)
	}
@@ -39,5 +58,13 @@ func main() {
		}
	}

	// dump results to file
	if err := filediscovery.Save(pathDatFiles, res.Files); err != nil {
		log.Fatal(err)
	}
	if err := filediscovery.Save(pathDatContents, res.Contents); err != nil {
		log.Fatal(err)
	}

	fmt.Printf("Wasted Space: %d bytes\n", wastedSpace)
}