summaryrefslogtreecommitdiff
path: root/internal/recordsdir/recordsdir.go
blob: 6ebf491ce5e29a8fcce159f89e4a53960cc97734 (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
package recordsdir

import (
	"io/fs"
	"os"
	"path"
	"path/filepath"
	"strings"
	"time"
)

type Entry struct {
	Path    string
	Host    string
	ModTime time.Time
}

func HostFromFileName(name string) string {
	host := strings.TrimSuffix(name, filepath.Ext(name))
	if idx := strings.Index(host, "."); idx > 0 {
		host = host[:idx]
	}
	return host
}

func listRecordsFileNames(fsys fs.FS, root string) ([]Entry, error) {
	entries, err := fs.ReadDir(fsys, root)
	if err != nil {
		return nil, err
	}
	var out []Entry
	for _, e := range entries {
		if e.IsDir() || !strings.HasSuffix(e.Name(), ".records") {
			continue
		}
		rel := path.Join(root, e.Name())
		info, err := fs.Stat(fsys, rel)
		if err != nil || info.Size() == 0 {
			continue
		}
		out = append(out, Entry{
			Path:    rel,
			Host:    HostFromFileName(e.Name()),
			ModTime: info.ModTime(),
		})
	}
	return out, nil
}

// ListNonEmptyFilesFS returns non-empty .records files under root within fsys.
func ListNonEmptyFilesFS(fsys fs.FS, root string) ([]Entry, error) {
	return listRecordsFileNames(fsys, root)
}

func ListNonEmptyFiles(dir string) ([]Entry, error) {
	entries, err := listRecordsFileNames(os.DirFS(dir), ".")
	if err != nil {
		return nil, err
	}
	for i := range entries {
		entries[i].Path = filepath.Join(dir, filepath.Base(entries[i].Path))
	}
	return entries, nil
}