Documentation
¶
Overview ¶
Package sharedfile provides a file of data (JSON by default) that is shared by several processes, such as different Cogent Core apps, like the sort orders of directories and the geometry of windows. The data is read again when another process has changed the file, and the file is written with a lock file held, after reading the changes of the other processes, so that they are not lost, and it is written whole and then renamed, so that the other processes never read a partial file.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var LockStale = 2 * time.Second
LockStale is how long a lock file can exist before it is taken as left by a process that ended while holding it (see Lock).
Functions ¶
func Lock ¶
Lock locks the given lock file, by creating it (which only one process can do), waiting for another process to remove it, or taking it over when it is older than LockStale. It gives up with an error after waiting for longer than LockStale (plus a second). It returns the function to unlock it, by removing it.
Types ¶
type File ¶
type File[T any] struct { // Path is the path of the file. If it is empty, the data are only // kept in memory. It can be changed, as when the directory of the // file changes in tests: the data are then those of the new file. Path string // Data is the data of the file, as last read from it or updated. // It is replaced by a new value when the file is read again, so it // must only be changed by [File.Update]. Data T // Encode and Decode, if set, write and read the data in another // format than JSON, which is used for each that is not set, such as // a text file of lines. Decode returns what it could read along with // any error. Encode func(d T) ([]byte, error) Decode func(b []byte) (T, error) // contains filtered or unexported fields }
File is a JSON file of data of type T that is shared by several processes: see File.Reload and File.Update. It is not safe for concurrent use by goroutines: the caller protects it with a mutex. The zero value is ready to use, with only File.Path to set.
func (*File[T]) Changed ¶
Changed returns whether the file exists and has changed since it was last read or written by this File, as another process has written it, or whether File.Path has changed. Another process is detected by the file being a different file (as it is renamed into place when it is written), or by a different modification time or size.
func (*File[T]) Reload ¶
Reload reads the file into File.Data if it has File.Changed, which replaces Data by the new value that is read. If the file does not exist, Data is kept, except when File.Path has changed, when it is reset to the zero value. If the file is not valid JSON, Data is what could be read from it, and an error is returned; the file is then replaced when it is next updated.
func (*File[T]) Update ¶
Update changes File.Data with the given change function (which can be nil to just write it) and writes it to the file, with the file locked (see Lock), after reading the changes of other processes with File.Reload, which the change function must then merge with. A file that is not valid JSON is replaced. If the file cannot be locked or read, the data are only changed in memory, and the error is returned. The directory of the file is made if it does not exist.