"fmt"
"os"
"path/filepath"
- "syscall"
"time"
"unicode/utf8"
)
-// errLocked tells that another process owns the chat.
-var errLocked = errors.New("another process owns the chat")
-
// kinds holds the kinds that a message can have.
var kinds = map[string]bool{
"user": true,
return nil
}
-
-// lockFile opens the file at path and takes an exclusive lock on it. The lock
-// stays until the file closes or the process ends.
-func lockFile(path string) (*os.File, error) {
- file, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0o600)
- if err != nil {
- return nil, fmt.Errorf("open lock file: %w", err)
- }
-
- err = syscall.Flock(int(file.Fd()), syscall.LOCK_EX|syscall.LOCK_NB)
- if errors.Is(err, syscall.EWOULDBLOCK) {
- err = errLocked
- }
-
- if err != nil {
- return nil, errors.Join(fmt.Errorf("lock chat: %w", err), file.Close())
- }
-
- return file, nil
-}