diff options
author | Matthew Heon <matthew.heon@pm.me> | 2019-05-21 15:54:26 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-21 15:54:26 -0400 |
commit | cec56b0075eeec6a8cdfba3138dec32fdcd36b47 (patch) | |
tree | 6b129e2b8f58e0a46b8d25cd4459bb6e3c9436f7 /vendor/github.com/etcd-io/bbolt/bolt_unix.go | |
parent | e6277b1a2dedf8a4d80a270ddac0be4c22ed3e6d (diff) | |
parent | 1d505f6875d27dccafb777853a51045f82888f00 (diff) | |
download | podman-cec56b0075eeec6a8cdfba3138dec32fdcd36b47.tar.gz podman-cec56b0075eeec6a8cdfba3138dec32fdcd36b47.tar.bz2 podman-cec56b0075eeec6a8cdfba3138dec32fdcd36b47.zip |
Merge pull request #3168 from rhatdan/vendor
Update vendor of buildah and containers/images
Diffstat (limited to 'vendor/github.com/etcd-io/bbolt/bolt_unix.go')
-rw-r--r-- | vendor/github.com/etcd-io/bbolt/bolt_unix.go | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/vendor/github.com/etcd-io/bbolt/bolt_unix.go b/vendor/github.com/etcd-io/bbolt/bolt_unix.go new file mode 100644 index 000000000..5f2bb5145 --- /dev/null +++ b/vendor/github.com/etcd-io/bbolt/bolt_unix.go @@ -0,0 +1,93 @@ +// +build !windows,!plan9,!solaris + +package bbolt + +import ( + "fmt" + "syscall" + "time" + "unsafe" +) + +// flock acquires an advisory lock on a file descriptor. +func flock(db *DB, exclusive bool, timeout time.Duration) error { + var t time.Time + if timeout != 0 { + t = time.Now() + } + fd := db.file.Fd() + flag := syscall.LOCK_NB + if exclusive { + flag |= syscall.LOCK_EX + } else { + flag |= syscall.LOCK_SH + } + for { + // Attempt to obtain an exclusive lock. + err := syscall.Flock(int(fd), flag) + if err == nil { + return nil + } else if err != syscall.EWOULDBLOCK { + return err + } + + // If we timed out then return an error. + if timeout != 0 && time.Since(t) > timeout-flockRetryTimeout { + return ErrTimeout + } + + // Wait for a bit and try again. + time.Sleep(flockRetryTimeout) + } +} + +// funlock releases an advisory lock on a file descriptor. +func funlock(db *DB) error { + return syscall.Flock(int(db.file.Fd()), syscall.LOCK_UN) +} + +// mmap memory maps a DB's data file. +func mmap(db *DB, sz int) error { + // Map the data file to memory. + b, err := syscall.Mmap(int(db.file.Fd()), 0, sz, syscall.PROT_READ, syscall.MAP_SHARED|db.MmapFlags) + if err != nil { + return err + } + + // Advise the kernel that the mmap is accessed randomly. + err = madvise(b, syscall.MADV_RANDOM) + if err != nil && err != syscall.ENOSYS { + // Ignore not implemented error in kernel because it still works. + return fmt.Errorf("madvise: %s", err) + } + + // Save the original byte slice and convert to a byte array pointer. + db.dataref = b + db.data = (*[maxMapSize]byte)(unsafe.Pointer(&b[0])) + db.datasz = sz + return nil +} + +// munmap unmaps a DB's data file from memory. +func munmap(db *DB) error { + // Ignore the unmap if we have no mapped data. + if db.dataref == nil { + return nil + } + + // Unmap using the original byte slice. + err := syscall.Munmap(db.dataref) + db.dataref = nil + db.data = nil + db.datasz = 0 + return err +} + +// NOTE: This function is copied from stdlib because it is not available on darwin. +func madvise(b []byte, advice int) (err error) { + _, _, e1 := syscall.Syscall(syscall.SYS_MADVISE, uintptr(unsafe.Pointer(&b[0])), uintptr(len(b)), uintptr(advice)) + if e1 != 0 { + err = e1 + } + return +} |