lockedfile

Imports

Imports #

"fmt"
"io"
"io/fs"
"os"
"runtime"
"io/fs"
"os"
"cmd/go/internal/lockedfile/internal/filelock"
"io/fs"
"math/rand"
"os"
"strings"
"time"
"fmt"
"os"
"sync"

Constants & Variables

lockedErrStrings var #

Opening an exclusive-use file returns an error. The expected error strings are: - "open/create -- file is locked" (cwfs, kfs) - "exclusive lock" (fossil) - "exclusive use file already open" (ramfs)

var lockedErrStrings = [...]string{...}

Structs

File struct #

A File is a locked *os.File. Closing the file releases the lock. If the program exits while a file is locked, the operating system releases the lock but may not do so promptly: callers must ensure that all locked files are closed before exiting.

type File struct {
osFile
closed bool
}

Mutex struct #

A Mutex provides mutual exclusion within and across processes by locking a well-known file. Such a file generally guards some other part of the filesystem: for example, a Mutex file in a directory might guard access to the entire tree rooted in that directory. Mutex does not implement sync.Locker: unlike a sync.Mutex, a lockedfile.Mutex can fail to lock (e.g. if there is a permission error in the filesystem). Like a sync.Mutex, a Mutex may be included as a field of a larger struct but must not be copied after first use. The Path field must be set before first use and must not be change thereafter.

type Mutex struct {
Path string
mu sync.Mutex
}

osFile struct #

osFile embeds a *os.File while keeping the pointer itself unexported. (When we close a File, it must be the same file descriptor that we opened!)

type osFile struct {
*os.File
}

Functions

Close method #

Close unlocks and closes the underlying file. Close may be called multiple times; all calls after the first will return a non-nil error.

func (f *File) Close() error

Create function #

Create is like os.Create, but returns a write-locked file.

func Create(name string) (*File, error)

Edit function #

Edit creates the named file with mode 0666 (before umask), but does not truncate existing contents. If Edit succeeds, methods on the returned File can be used for I/O. The associated file descriptor has mode O_RDWR and the file is write-locked.

func Edit(name string) (*File, error)

Lock method #

Lock attempts to lock the Mutex. If successful, Lock returns a non-nil unlock function: it is provided as a return-value instead of a separate method to remind the caller to check the accompanying error. (See https://golang.org/issue/20803.)

func (mu *Mutex) Lock() (unlock func(), err error)

MutexAt function #

MutexAt returns a new Mutex with Path set to the given non-empty path.

func MutexAt(path string) *Mutex

Open function #

Open is like os.Open, but returns a read-locked file.

func Open(name string) (*File, error)

OpenFile function #

OpenFile is like os.OpenFile, but returns a locked file. If flag includes os.O_WRONLY or os.O_RDWR, the file is write-locked; otherwise, it is read-locked.

func OpenFile(name string, flag int, perm fs.FileMode) (*File, error)

Read function #

Read opens the named file with a read-lock and returns its contents.

func Read(name string) ([]byte, error)

String method #

func (mu *Mutex) String() string

Transform function #

Transform invokes t with the result of reading the named file, with its lock still held. If t returns a nil error, Transform then writes the returned contents back to the file, making a best effort to preserve existing contents on error. t must not modify the slice passed to it.

func Transform(name string, t func([]byte) ([]byte, error)) (err error)

Write function #

Write opens the named file (creating it with the given permissions if needed), then write-locks it and overwrites it with the given content.

func Write(name string, content io.Reader, perm fs.FileMode) (err error)

closeFile function #

func closeFile(f *os.File) error

closeFile function #

func closeFile(f *os.File) error

isLocked function #

Even though plan9 doesn't support the Lock/RLock/Unlock functions to manipulate already-open files, IsLocked is still meaningful: os.OpenFile itself may return errors that indicate that a file with the ModeExclusive bit set is already open.

func isLocked(err error) bool

openFile function #

func openFile(name string, flag int, perm fs.FileMode) (*os.File, error)

openFile function #

func openFile(name string, flag int, perm fs.FileMode) (*os.File, error)

Generated with Arrow