代码拉取完成,页面将自动刷新
package utils
import (
"fmt"
"os"
"syscall"
)
// Filelock defines an file lock (shared or exclusive).
type Filelock struct {
file *os.File
}
func doLockFile(path string, mode int) (*Filelock, error) {
// A shared or exclusive lock can be placed on a file regardless
// of the mode in which the file was opened.
file, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0640)
if err != nil {
return nil, fmt.Errorf("open %s: %s", path, err)
}
err = syscall.Flock(int(file.Fd()), mode)
if err != nil {
file.Close()
return nil, fmt.Errorf("lock %s: %s", path, err)
}
return &Filelock{file}, nil
}
// LockFileShar imposes a share lock on a file
func LockFileShar(path string) (*Filelock, error) {
return doLockFile(path, syscall.LOCK_SH)
}
// LockFileExcl locks a file exclusively.
func LockFileExcl(path string) (*Filelock, error) {
return doLockFile(path, syscall.LOCK_EX)
}
// Unlock unlocks a lock holding by current process.
func (lck *Filelock) Unlock() error {
file := lck.file
err := syscall.Flock(int(file.Fd()), syscall.LOCK_UN)
file.Close()
os.Remove(file.Name())
return err
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。