/
genmeta.go
46 lines (40 loc) · 1012 Bytes
/
genmeta.go
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
package main
import (
"encoding/json"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/gebn/unifibackup/v2/internal/pkg/autobackup"
)
// genmeta generates a autobackup_meta.json file for backups in the provided
// directory. This directory must only contain .unf files.
func genmeta(backupDir string) error {
entries, err := os.ReadDir(backupDir)
if err != nil {
return err
}
infos := []fs.FileInfo{}
for _, entry := range entries {
info, err := entry.Info()
if err != nil {
return err
}
if !strings.HasSuffix(info.Name(), ".unf") {
return fmt.Errorf("%v must only contain backups; %v is not a backup", backupDir, info.Name())
}
infos = append(infos, info)
}
meta, err := autobackup.ParseBackups(infos)
if err != nil {
return err
}
f, err := os.Create(filepath.Join(backupDir, "autobackup_meta.json"))
if err != nil {
return err
}
defer f.Close()
// unlike genuine meta files, this produces a trailing linefeed
return json.NewEncoder(f).Encode(meta)
}