carlos / platform Public

Clone
git clone https://amadan.net/carlos/platform

Plain git — no account needed to clone.

Download

Download this file

1package hostbox
2
3import (
4 "github.com/carlosframework/platform/internal/rootstate"
5 "github.com/carlosframework/platform/internal/safefile"
6 "os"
7)
8
9func sameInventoryIdentity(a, b rootstate.FileIdentity) bool {
10 if a.Boot == "" || b.Boot == "" {
11 return false
12 }
13 if a.Handle == "" || b.Handle == "" {
14 return a.Boot == b.Boot && a == b
15 }
16 return a.Same(b)
17}
18
19func VerifyInventoryFile(f *os.File, e StopInventoryEntry) error {
20 info, err := f.Stat()
21 if err != nil {
22 return err
23 }
24 if !info.IsDir() && !info.Mode().IsRegular() {
25 return ErrTypedInventory
26 }
27 now, err := rootstate.ObjectIdentity(f)
28 if err != nil {
29 return err
30 }
31 if e.Identity.Device != e.Device || e.Identity.Inode != e.Inode || e.Identity.Mode&0170000 != e.Type || !sameInventoryIdentity(e.Identity, now) {
32 return ErrChanged
33 }
34 return nil
35}
36
37func OpenInventoryFile(path string) (*os.File, error) {
38 f, err := safefile.OpenDir(path)
39 if err != nil {
40 return safefile.OpenFile(path, os.O_RDONLY, 0)
41 }
42 return f, nil
43}
44