file

package module
v2.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 10, 2019 License: MIT Imports: 5 Imported by: 4

README

go-file

Package file is a Go library to open files with file locking depending on the system.

Build Status GoDoc

Install

go get github.com/mithrandie/go-file

Supported Systems

Currently file locking on the following systems are supported.

darwin dragonfly freebsd linux netbsd openbsd solaris

Advisory Lock

windows

Mandatory Lock

android nacl plan9 zos

Not Supported

Example

package main

import (
	"bufio"
	"context"
	"fmt"
	"time"
	 
	"github.com/mithrandie/go-file/v2"
)

func main() {
	// Try to lock and open the file with shared lock
	fp, err := file.TryOpenToRead("/path/to/file")
	if err != nil {
		panic(err)
	}
	defer func() {
		if e := file.Close(fp); e != nil {
			println(e.Error())
		}
	}()

	s := bufio.NewScanner(fp)
	for s.Scan() {
		fmt.Println(s.Text())
	}

	// Open the file with shared lock.
	// If the file is already locked, tries to lock repeatedly until the conditions is met.
	ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
	cfp, err := file.OpenToReadContext(ctx, 50*time.Millisecond, "/path/to/file2")
	if err != nil {
		panic(err)
	}
	defer func() {
		cancel()
		if e := file.Close(cfp); e != nil {
			println(e.Error())
		}
	}()

	cs := bufio.NewScanner(cfp)
	for cs.Scan() {
		fmt.Println(cs.Text())
	}
}

Documentation

Overview

Package file is a Go library to open files with file locking depending on the system.

Currently file locking on the following systems are supported.

darwin dragonfly freebsd linux netbsd openbsd solaris:
        Advisory Lock

windows:
        Mandatory Lock

android nacl plan9 zos:
        Not Supported

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Close

func Close(fp *os.File) (err error)

Unlocks and closes the file

func Create

func Create(path string) (*os.File, error)

Opens the file with exclusive locking. This function is the same as Open(path, os.O_CREATE|os.O_EXCL|os.O_RDWR, TryLock)

func Lock

func Lock(fp *os.File) error

Places the exclusive lock on the file. If the file is already locked, waits until the file is released.

func LockContext

func LockContext(ctx context.Context, retryDelay time.Duration, fp *os.File) error

Places the exclusive lock on the file. If the file is already locked, tries to lock repeatedly until the conditions is met.

func LockEX

func LockEX(fp *os.File) error

Places an exclusive lock on the file. If the file is already locked, waits until the file is released.

func LockSH

func LockSH(fp *os.File) error

Places a shared lock on the file. If the file is already locked, waits until the file is released.

func NewContextCanceled

func NewContextCanceled(message string) error

func NewContextDone

func NewContextDone(message string) error

func NewIOError

func NewIOError(message string) error

func NewLockError

func NewLockError(message string) error

func NewTimeoutError

func NewTimeoutError(path string) error

func Open

func Open(path string, flag int, fn func(*os.File) error) (*os.File, error)

Opens the file with passed locking function.

func OpenContext

func OpenContext(ctx context.Context, retryDelay time.Duration, path string, flag int, fn func(context.Context, time.Duration, *os.File) error) (*os.File, error)

Opens the file with passed locking function. If failed, try to lock repeatedly until the conditions is met.

func OpenToRead

func OpenToRead(path string) (*os.File, error)

Opens the file with shared lock. This function is the same as Open(path, os.O_RDONLY, RLock)

func OpenToReadContext

func OpenToReadContext(ctx context.Context, retryDelay time.Duration, path string) (*os.File, error)

Opens the file with shared lock. If the file is already locked, tries to lock repeatedly until the conditions is met. This function is the same as OpenContext(ctx, retryDelay, path, RLockContext)

func OpenToUpdate

func OpenToUpdate(path string) (*os.File, error)

Opens the file with exclusive lock. This function is the same as Open(path, os.O_RDWR, Lock)

func OpenToUpdateContext

func OpenToUpdateContext(ctx context.Context, retryDelay time.Duration, path string) (*os.File, error)

Opens the file with exclusive lock. If the file is already locked, tries to lock repeatedly until the conditions is met. This function is the same as OpenContext(ctx, retryDelay, path, LockContext)

func RLock

func RLock(fp *os.File) error

Places the shared lock on the file. If the file is already locked, waits until the file is released.

func RLockContext

func RLockContext(ctx context.Context, retryDelay time.Duration, fp *os.File) error

Places the shared lock on the file. If the file is already locked, tries to lock repeatedly until the conditions is met.

func TryLock

func TryLock(fp *os.File) error

Places the exclusive lock on the file. If the file is already locked, returns an error immediately.

func TryLockEX

func TryLockEX(fp *os.File) error

Places an exclusive lock on the file. If the file is already locked, returns an error immediately.

func TryLockSH

func TryLockSH(fp *os.File) error

Places a shared lock on the file. If the file is already locked, returns an error immediately.

func TryOpenToRead

func TryOpenToRead(path string) (*os.File, error)

Tries to lock and opens the file with shared lock. This function is the same as Open(path, os.O_RDONLY, TryRLock)

func TryOpenToUpdate

func TryOpenToUpdate(path string) (*os.File, error)

Tries to lock and opens the file with exclusive lock. This function is the same as Open(path, os.O_RDWR, TryLock)

func TryRLock

func TryRLock(fp *os.File) error

Places the shared lock on the file. If the file is already locked, returns an error immediately.

func Unlock

func Unlock(fp *os.File) error

Unlock the file.

Types

type ContextCanceled

type ContextCanceled struct {
	// contains filtered or unexported fields
}

func (ContextCanceled) Error

func (e ContextCanceled) Error() string

type ContextDone

type ContextDone struct {
	// contains filtered or unexported fields
}

func (ContextDone) Error

func (e ContextDone) Error() string

type IOError

type IOError struct {
	// contains filtered or unexported fields
}

func (IOError) Error

func (e IOError) Error() string

type LockError

type LockError struct {
	// contains filtered or unexported fields
}

func (LockError) Error

func (e LockError) Error() string

type TimeoutError

type TimeoutError struct {
	// contains filtered or unexported fields
}

func (TimeoutError) Error

func (e TimeoutError) Error() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL