TryLock acquires an exclusive non-blocking lock; auto-released on process exit.
()
| 46 | |
| 47 | // TryLock acquires an exclusive non-blocking lock; auto-released on process exit. |
| 48 | func (l *LockFile) TryLock() error { |
| 49 | if l.file != nil { |
| 50 | return fmt.Errorf("%w: %s", ErrHeld, l.path) |
| 51 | } |
| 52 | f, err := vfs.OpenFile(l.path, os.O_CREATE|os.O_RDWR, 0600) |
| 53 | if err != nil { |
| 54 | return fmt.Errorf("open lock file: %w", err) |
| 55 | } |
| 56 | if err := tryLockFile(f); err != nil { |
| 57 | f.Close() |
| 58 | return err |
| 59 | } |
| 60 | l.file = f |
| 61 | return nil |
| 62 | } |
| 63 | |
| 64 | // Unlock keeps the file on disk to avoid inode-reuse races between unlock and competing open+flock. |
| 65 | func (l *LockFile) Unlock() error { |