summaryrefslogtreecommitdiff
path: root/memex/repo.go
blob: 2321a50589ffa5f4470d1788f72696ec93fa6286 (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main

import (
	"bytes"
	"encoding/json"
	"filelock"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"path"
	"sort"
	"strconv"
	"syscall"
	"time"
)

type RepoLock struct{ *os.File }

func GetRepoLock(arch string) (*RepoLock, error) {
	lpath := path.Join(arch, "memex/lock")
	f, err := os.OpenFile(lpath, os.O_RDWR|os.O_CREATE, 0644)
	if err != nil {
		return nil, err
	}

	err = filelock.Lock(f)
	if err != nil {
		defer f.Close()
		if err.(*os.PathError).Err == syscall.EWOULDBLOCK {
			var buf bytes.Buffer
			io.Copy(&buf, f)
			pid, _ := strconv.Atoi(buf.String())
			err = fmt.Errorf("%s: held by process %d", lpath, pid)
			return nil, err
		}
		return nil, err
	}

	f.Truncate(0)
	f.WriteString(strconv.Itoa(os.Getpid()))
	f.Sync()
	return &RepoLock{f}, nil
}

func (l *RepoLock) Unlock() {
	l.Close()
}

func AllRefs(arch string) []string {
	refsdir := path.Join(arch, "memex/refs")
	refs := []string{}
	dirs := []string{"."}
	for len(dirs) != 0 {
		dir := dirs[0]
		dirs = dirs[1:]
		fs, err := ioutil.ReadDir(path.Join(refsdir, dir))
		if err != nil {
			panic(err)
		}
		for _, f := range fs {
			name := path.Join(dir, f.Name())
			if !f.IsDir() {
				refs = append(refs, name)
			} else {
				dirs = append(dirs, name)
			}
		}
	}
	return refs
}

func ReadRef(arch, ref string) (Addr, error) {
	rpath := path.Join(arch, "memex/refs", ref)
	b, err := ioutil.ReadFile(rpath)
	if err != nil {
		return nil, err
	}
	if len(b) > 0 {
		b = b[:len(b)-1]
	}
	if a := DecodeAddr(b); a != nil {
		return a, nil
	}
	return nil, fmt.Errorf("invalid address '%s' in ref/%s", b, ref)
}

func Reflog(arch, ref string, addr Addr) {
	lpath := path.Join(arch, "memex/reflog")
	flags := os.O_WRONLY | os.O_APPEND | os.O_CREATE
	f, err := os.OpenFile(lpath, flags, 0666)
	if err != nil {
		return
	}
	enc := json.NewEncoder(f)
	enc.SetIndent("", "")
	enc.SetEscapeHTML(false)
	enc.Encode(map[string]string{
		"ref":  ref,
		"addr": addr.String(),
		"date": time.Now().Truncate(time.Second).String(),
		"cmd":  CmdLine(),
	})
	f.Close()
}

func NewRef(arch, ref string) error {
	rpath := path.Join(arch, "memex/refs", ref)
	f, err := os.OpenFile(rpath, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0666)
	if err != nil {
		return err
	}
	f.Close()
	return nil
}

func WriteRef(arch, ref string, addr Addr) error {
	Reflog(arch, ref, addr)
	rpath := path.Join(arch, "memex/refs", ref)
	return WriteFile(rpath, addr.String()+"\n")
}

func ReadCommitCache(arch string) []string {
	cpath := path.Join(arch, "memex/commits")
	b, err := ioutil.ReadFile(cpath)
	if err != nil {
		return nil
	}
	var s []string
	for len(b) >= AddrDisplayLen+1 {
		s = append(s, string(b[0:AddrDisplayLen]))
		b = b[AddrDisplayLen+1:]
	}
	sort.Strings(s)
	return s
}

func WriteCommitCache(arch string, cache []string) {
	cpath := path.Join(arch, "memex/commits")
	f, err := os.OpenFile(cpath, os.O_CREATE|os.O_WRONLY, 0600)
	if err != nil {
		return
	}
	f.Truncate(0)
	for _, line := range cache {
		f.WriteString(line + "\n")
	}
	f.Close()
}