Giter Site home page Giter Site logo

lockfile's Introduction

LockFile

A cross-platform Go library that creates a lock file.

Development

Run make deps to install dependencies.

Run make test to format, test, and check the code for security issues.

Examples

You can either use the full-auto mode or on a self-service basis.

Full-auto example:

fLock, err := NewLockFile(path + ".lock")
if err != nil {
    return err
}
defer fLock.Free()

// Do work

If you want to modify a locked file or prefer a more self-service approach, here's an example for you:

// Open File
file, err := os.Open("/path/to/file")
if err != nil {
	return err
}
// Lock file
if err := lockfile.LockFile(file); err != nil {
	return err
}
// Write data
if _, err := file.Write(data); err != nil {
	_ = lockfile.UnlockFile(file)
	_ = file.Close()
	return err
}
if err := file.Sync(); err != nil {
	_ = lockfile.UnlockFile(file)
	_ = file.Close()
	return err
}
// Free and unlock
_ = lockfile.UnlockFile(file)
return file.Close()

Caveats

  • The locking logic will only work if all entities accessing a given lock file use this API.
  • This will likely not work on shared drives.
  • This library won't work on wasm/js builds.

This library has been tested on:

  • Linux/amd64 (Fedora Linux)
  • windows/amd64 (Windows 10 VM)
  • darwin/arm64 (M1 MacBook)

lockfile's People

Contributors

stefanovazzocell avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.