Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get an exclusive lock on a file in go

Tags:

go

How can I get an exclusive read access to a file in go? I have tried documentations from docs but I am still able to open the file in notepad and edit it. I want to deny any other process to have access to read and write while the first process has not closed it explicitly. In .NET I could do something as:

File.Open("a.txt", FileMode.Open, FileAccess.ReadWrite, FileShare.None);

How do I do it in go?

like image 952
Vishal Anand Avatar asked Oct 25 '18 09:10

Vishal Anand


People also ask

Can a file be locked?

File locking is a mechanism that restricts access to a computer file. For example, applications will often create a temporary file while it is open to prevent others from editing the same file. These temporary files are usually deleted when you exit your program.

How do you unlock a file locker?

Right-click on the file. In the menu that appears, select Lock File. To unlock, right-click the file and select Unlock File.

What does it mean when a file is locked in box?

If you choose unlimited, the file will be locked until you unlock it manually. You can also choose to prevent users from downloading the file while you are editing it. A lock icon to the left of the file's name will indicate that the file has been locked.

Why would a file be locked?

The file might be locked because: The file is shared and another user is currently editing it. An instance of the Office app is running in the background with the file already opened. The file has been marked as Final and can no longer be updated.

What is the difference between read lock and exclusive lock?

Introduction to File Locks Put simply, an exclusive lock prevents all other operations – including reads – while a write operation completes. In contrast, a shared lock allows more than one process to read at the same time. The point of a read lock is to prevent the acquisition of a write lock by another process.

What is an exclusive lock in C++?

With the Exclusive Lock, a data item can be read as well as written. Also called write lock. An exclusive lock prevents any other locker from obtaining any sort of a lock on the object. They can be owned by only one transaction at a time. It is denoted as Lock-X. X-lock is requested using Lock-X instruction.

What does Windows Backup failed to get an exclusive lock mean?

LimitlessTechnology-2700 answered • Jun 20 2022 at 12:36 AM | JesseFlintoff-4117 commented • Jun 23 2022 at 3:53 PM ACCEPTED ANSWER The error ‘Windows Backup failed to get an exclusive lock on the EFI partition (ESP)’ usually occurs when there’s an application that is denying access of the process.

Can a control manager Grant an exclusive lock to a transaction?

If the transaction T1 is holding a shared lock in data item A, then the control manager can grant the shared lock to transaction T2 as compatibility is TRUE, but it cannot grant the exclusive lock as compatibility is FALSE.


1 Answers

I finally found a go package that can lock a file.

Here is the repo: https://github.com/juju/fslock

go get -u github.com/juju/fslock

this package does exactly what it says

fslock provides a cross-process mutex based on file locks that works on windows and *nix platforms. fslock relies on LockFileEx on Windows and flock on *nix systems. The timeout feature uses overlapped IO on Windows, but on *nix platforms, timing out requires the use of a goroutine that will run until the lock is acquired, regardless of timeout. If you need to avoid this use of goroutines, poll TryLock in a loop.

To use this package, first, create a new lock for the lockfile

func New(filename string) *Lock

This API will create the lockfile if it already doesn't exist.

Then we can use the lockhandle to lock (or try lock) the file

func (l *Lock) Lock() error

There is also a timeout version of the above function that will try to get the lock of the file until timeout

func (l *Lock) LockWithTimeout(timeout time.Duration) error

Finally, if you are done, release the acquired lock by

func (l *Lock) Unlock() error

Very basic implementation

package main

import (
    "time"
    "fmt"
    "github.com/juju/fslock"
)

func main() {
    lock := fslock.New("../lock.txt")
    lockErr := lock.TryLock()
    if lockErr != nil {
        fmt.Println("falied to acquire lock > " + lockErr.Error())
        return
    }

    fmt.Println("got the lock")
    time.Sleep(1 * time.Minute)

    // release the lock
    lock.Unlock()
}
like image 180
Vishal Anand Avatar answered Oct 01 '22 21:10

Vishal Anand