// SPDX-License-Identifier: GPL-3.0-or-later package probe import ( "bytes" "encoding/json" "errors" "fmt" "os" "path/filepath" "regexp" "sort" "time" ) const stampLayout = "20060102T150405Z" // Save writes /-.json and -latest.json (both 0600, dir 0700) and returns the dated path. // The dated file is never overwritten; latest is replaced atomically. func Save(dir, host string, raw []byte, now time.Time) (string, error) { if !validHost(host) { return "", fmt.Errorf("bad host name %q", host) } var pretty bytes.Buffer if err := json.Indent(&pretty, raw, "", " "); err != nil { return "", errors.New("profile is not valid JSON") } pretty.WriteByte('\n') if err := os.MkdirAll(dir, 0o700); err != nil { return "", fmt.Errorf("cannot create %s", dir) } dated := filepath.Join(dir, fmt.Sprintf("%s-%s.json", host, now.UTC().Format(stampLayout))) f, err := os.OpenFile(dated, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o600) if err != nil { return "", fmt.Errorf("cannot create %s (exists?)", dated) } if _, err := f.Write(pretty.Bytes()); err != nil { f.Close() return "", err } if err := f.Close(); err != nil { return "", err } tmp, err := os.CreateTemp(dir, ".latest-*") if err != nil { return "", err } defer os.Remove(tmp.Name()) if err := tmp.Chmod(0o600); err != nil { tmp.Close() return "", err } if _, err := tmp.Write(pretty.Bytes()); err != nil { tmp.Close() return "", err } if err := tmp.Close(); err != nil { return "", err } if err := os.Rename(tmp.Name(), filepath.Join(dir, host+"-latest.json")); err != nil { return "", err } return dated, nil } func validHost(h string) bool { return regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9._-]*$`).MatchString(h) } // ProfileFile is one stored dated profile. type ProfileFile struct { Path string Stamp string // 20060102T150405Z } // List returns the dated profiles of host, oldest first. The -latest copy is not included. func List(dir, host string) ([]ProfileFile, error) { if !validHost(host) { return nil, fmt.Errorf("bad host name %q", host) } entries, err := os.ReadDir(dir) if err != nil { return nil, fmt.Errorf("no profiles in %s", dir) } re := regexp.MustCompile(`^` + regexp.QuoteMeta(host) + `-(\d{8}T\d{6}Z)\.json$`) var out []ProfileFile for _, e := range entries { if m := re.FindStringSubmatch(e.Name()); m != nil && e.Type().IsRegular() { out = append(out, ProfileFile{Path: filepath.Join(dir, e.Name()), Stamp: m[1]}) } } sort.Slice(out, func(i, j int) bool { return out[i].Stamp < out[j].Stamp }) return out, nil } // Load reads and validates a stored profile. func Load(path string) (*Profile, error) { raw, err := os.ReadFile(path) if err != nil { return nil, fmt.Errorf("profile unreadable: %s", path) } p, err := Parse(raw) if err != nil { return nil, fmt.Errorf("%s: %v", path, err) } return p, nil } // Latest loads /-latest.json. func Latest(dir, host string) (*Profile, string, error) { if !validHost(host) { return nil, "", fmt.Errorf("bad host name %q", host) } path := filepath.Join(dir, host+"-latest.json") p, err := Load(path) return p, path, err }