aboutsummaryrefslogtreecommitdiff
path: root/storage/local_storage.go
blob: c080564e9e84dc77dcdfbfd76eaded6b9ef3054b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package storage

import (
	"bytes"
	"code.laria.me/petrific/config"
	"code.laria.me/petrific/objects"
	"encoding/hex"
	"fmt"
	"io"
	"os"
	"strings"
)

func joinPath(parts ...string) string {
	return strings.Join(parts, string(os.PathSeparator))
}

func objectDir(id objects.ObjectId) string {
	return joinPath(string(id.Algo), hex.EncodeToString(id.Sum[0:1]))
}

type LocalStorage struct {
	Path  string
	index Index
}

func LocalStorageFromConfig(conf config.Config, name string) (Storage, error) {
	var path string
	if err := conf.Storage[name].Get("path", &path); err != nil {
		return nil, err
	}

	return OpenLocalStorage(config.ExpandTilde(path))
}

func OpenLocalStorage(path string) (l LocalStorage, err error) {
	l.Path = path
	l.index = NewIndex()

	if fi, err := os.Stat(path); os.IsNotExist(err) {
		if err := os.MkdirAll(path, 0755); err != nil {
			return l, err
		}
	} else if err != nil {
		return l, err
	} else if !fi.Mode().IsDir() {
		return l, fmt.Errorf("%s: Not a directory", path)
	}

	f, err := os.Open(joinPath(path, "index"))
	if err != nil {
		if os.IsNotExist(err) {
			err = nil
		}
		return l, err
	}

	if err == nil {
		defer f.Close()
		err = l.index.Load(f)
	}

	return
}

func objectPath(id objects.ObjectId) string {
	return joinPath(objectDir(id), hex.EncodeToString(id.Sum[1:]))
}

func (l LocalStorage) Get(id objects.ObjectId) ([]byte, error) {
	f, err := os.Open(joinPath(l.Path, objectPath(id)))
	if os.IsNotExist(err) {
		return []byte{}, ObjectNotFound
	} else if err != nil {
		return []byte{}, err
	}
	defer f.Close()

	buf := new(bytes.Buffer)
	_, err = io.Copy(buf, f)
	return buf.Bytes(), err
}

func (l LocalStorage) Has(id objects.ObjectId) (bool, error) {
	_, err := os.Stat(joinPath(l.Path, objectPath(id)))
	if err == nil {
		return true, nil
	} else if os.IsNotExist(err) {
		return false, nil
	} else {
		return false, err
	}
}

func (l LocalStorage) Set(id objects.ObjectId, typ objects.ObjectType, raw []byte) error {
	// First, check if the directory exists
	dir := joinPath(l.Path, objectDir(id))
	_, err := os.Stat(dir)
	if os.IsNotExist(err) {
		if err := os.MkdirAll(dir, 0755); err != nil {
			return err
		}
	} else if err != nil {
		return err
	}

	f, err := os.Create(joinPath(l.Path, objectPath(id)))
	if err != nil {
		return err
	}
	defer f.Close()

	_, err = f.Write(raw)
	l.index.Set(id, typ)
	return err
}

func (l LocalStorage) List(typ objects.ObjectType) ([]objects.ObjectId, error) {
	return l.index.List(typ), nil
}

func (l LocalStorage) Close() error {
	f, err := os.Create(joinPath(l.Path, "index"))
	if err != nil {
		return err
	}
	defer f.Close()

	return l.index.Save(f)
}