aboutsummaryrefslogtreecommitdiffstats
path: root/internal/notify/files.go
blob: e4da3369dc820be5c192fc5e749fb3a705f230ad (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
// Copyright (C) 2026 Danilo M. <danix@danix.xyz>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

package notify

import (
	"encoding/json"
	"os"
	"path/filepath"
	"strconv"
)

// RuntimeDir is where the daemon publishes. It is tmpfs, so a reboot clears
// every file and there is no cleanup code.
func RuntimeDir() string {
	if d := os.Getenv("XDG_RUNTIME_DIR"); d != "" {
		return filepath.Join(d, "notifyd")
	}
	return filepath.Join(os.TempDir(), "notifyd")
}

// Publish writes the live queue and the history ring, each whole, through a
// temporary file and a rename. A reader never sees a half-written value, the
// same atomic write the status registry relies on.
func Publish(dir string, live, history []Popup) error {
	if err := os.MkdirAll(dir, 0o700); err != nil {
		return err
	}
	if live == nil {
		live = []Popup{}
	}
	if history == nil {
		history = []Popup{}
	}
	if err := writeJSON(filepath.Join(dir, "queue.json"), live); err != nil {
		return err
	}
	return writeJSON(filepath.Join(dir, "history.json"), history)
}

// ImagesDir is where the daemon writes decoded image-data.
func ImagesDir(dir string) string {
	return filepath.Join(dir, "img")
}

// WriteImage writes a decoded image as <id>.png under the image directory.
func WriteImage(dir string, id uint32, data []byte) (string, error) {
	imgDir := ImagesDir(dir)
	if err := os.MkdirAll(imgDir, 0o700); err != nil {
		return "", err
	}
	path := filepath.Join(imgDir, strconv.FormatUint(uint64(id), 10)+".png")
	tmp, err := os.CreateTemp(imgDir, ".img-*")
	if err != nil {
		return "", err
	}
	tmpName := tmp.Name()
	if _, err := tmp.Write(data); err != nil {
		tmp.Close()
		os.Remove(tmpName)
		return "", err
	}
	if err := tmp.Close(); err != nil {
		os.Remove(tmpName)
		return "", err
	}
	if err := os.Rename(tmpName, path); err != nil {
		os.Remove(tmpName)
		return "", err
	}
	return path, nil
}

func writeJSON(path string, v any) error {
	data, err := json.Marshal(v)
	if err != nil {
		return err
	}
	tmp, err := os.CreateTemp(filepath.Dir(path), ".tmp-*")
	if err != nil {
		return err
	}
	tmpName := tmp.Name()
	renamed := false
	defer func() {
		if !renamed {
			os.Remove(tmpName)
		}
	}()
	if _, err := tmp.Write(data); err != nil {
		tmp.Close()
		return err
	}
	if err := tmp.Close(); err != nil {
		return err
	}
	if err := os.Rename(tmpName, path); err != nil {
		return err
	}
	renamed = true
	return nil
}