mirror of
https://gitea.com/gitea/tea.git
synced 2026-02-22 06:13:32 +01:00
- move filelocker logic into dedicated subpackage - consistent useragent in requests Reviewed-on: https://gitea.com/gitea/tea/pulls/888 Co-authored-by: techknowlogick <techknowlogick@gitea.com> Co-committed-by: techknowlogick <techknowlogick@gitea.com>
40 lines
857 B
Go
40 lines
857 B
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
//go:build unix
|
|
|
|
package filelock
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"syscall"
|
|
"time"
|
|
)
|
|
|
|
// lockFile acquires an exclusive lock on the file using flock.
|
|
// It polls with non-blocking flock until timeout.
|
|
func lockFile(file *os.File, timeout time.Duration) error {
|
|
deadline := time.Now().Add(timeout)
|
|
|
|
for {
|
|
err := syscall.Flock(int(file.Fd()), syscall.LOCK_EX|syscall.LOCK_NB)
|
|
if err == nil {
|
|
return nil
|
|
}
|
|
if err != syscall.EWOULDBLOCK {
|
|
return fmt.Errorf("flock failed: %w", err)
|
|
}
|
|
|
|
if time.Now().After(deadline) {
|
|
return fmt.Errorf("timeout waiting for file lock")
|
|
}
|
|
time.Sleep(FileLockPollInterval)
|
|
}
|
|
}
|
|
|
|
// unlockFile releases the lock on the file.
|
|
func unlockFile(file *os.File) error {
|
|
return syscall.Flock(int(file.Fd()), syscall.LOCK_UN)
|
|
}
|