{"id":17272561,"url":"https://github.com/rubyist/lockfile","last_synced_at":"2025-04-14T08:20:54.342Z","repository":{"id":18668052,"uuid":"21876337","full_name":"rubyist/lockfile","owner":"rubyist","description":"Easy advisory file locking for Go","archived":false,"fork":false,"pushed_at":"2014-08-18T01:43:03.000Z","size":176,"stargazers_count":8,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-27T21:52:06.813Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rubyist.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-07-15T21:21:06.000Z","updated_at":"2023-10-02T14:05:38.000Z","dependencies_parsed_at":"2022-08-28T19:41:11.610Z","dependency_job_id":null,"html_url":"https://github.com/rubyist/lockfile","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rubyist%2Flockfile","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rubyist%2Flockfile/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rubyist%2Flockfile/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rubyist%2Flockfile/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rubyist","download_url":"https://codeload.github.com/rubyist/lockfile/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248844036,"owners_count":21170505,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-15T08:48:55.535Z","updated_at":"2025-04-14T08:20:54.313Z","avatar_url":"https://github.com/rubyist.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# lockfile\n\nLockfile provides a simple wrapper for `fcntl` and `flock` based advisory file locking.\n\n## Installation\n\n```\ngo get github.com/rubyist/lockfile\n```\n\n## Examples\n\n`lockfile` provides a function `NewLockfile()` which will use an `fcntl` based lock, if\nit is available on the system. `fcntl` based locks are available in Go 1.3 and later. If\n`fcntl` is not available it will fall back to an `flock` based lock.\n\n```go\nlock := lockfile.NewLockfile(\"myfile\")\n```\n\nA `fcntl` based lock can be built explicitly:\n\n```go\nlock := lockfile.NewFcntlLockfile(\"myfile\")\n```\n\nA `flock` based lock can be built explicitly:\n\n```go\nlock := lockfile.NewFLockfile(\"myfile\")\n```\n\nAn `os.File` can also be used.\n\n```go\nfile, _ := os.Open(\"myfile\")\nlock := lockfile.NewLockfileFromFile(file)\nlock2 := lockfile.NewFcntlLockfileFromFile(file)\nlock3 := lockfile.NewFLockfileFromFile(file)\n```\n\n### Read Locks\n\nThe `LockRead()` function will lock a file for reading. When a file is locked for reading,\nother processes will be able to obtain read locks on the file. Write locks cannot be\nobtained on files locked for reading. If the lock cannot be obtained, `LockRead()` will\nreturn an error immediately\n\nThe `LockReadB()` function provides the same read locking functionality but will block\nuntil the lock can be obtained.\n\n```go\nerr := lock.LockRead()\nif err != nil {\n  // Lock not obtained\n}\n\nlock.LockReadB() // blocks until lock can be obtained\n```\n\n### Write Locks\n\nThe `LockWrite()` function will lock a file for writing. When a file is locked for writing,\nother processes will not be able to obtain read or write locks on the file. If the lock\ncannot be obtained, `LockWrite()` will return an error immediately.\n\nThe `LockWriteB()` function provides the same write locking functionality but will block\nuntil the lock can be obtained.\n\n```go\nerr := lock.LockWrite()\nif err != nil {\n  // Lock not obtained\n}\n\nlock.LockWriteB() // blocks until the lock can be obtained\n```\n\n### Releasing locks\n\nThe `Unlock()` function will release a lock. Closing a file or exiting the process\nwill also release any locks held on the file.\n\n*Caveat*: If the locking process opens a locked file and then closes it, all locks\non that file will be released.\n\n```go\nlock := lockfile.NewLockfile(\"foobar.lock\")\nlock.LockWrite() // lock obtained\n\nf, _ := os.Open(\"foobar.lock\")\nf.Close() // lock released\n```\n\n### Locking Byte Ranges\n\nWhen using an fcntl based lock, byte ranges within the file can be locked and unlocked.\nThe functions availble to do this are:\n\n```go\nLockReadRange(offset int64, whence int, len int64) error\nLockWriteRange(offset int64, whence int, len int64) error\nLockReadRangeB(offset int64, whence int, len int64) error\nLockWriteRangeB(offset int64, whence int, len int64) error\nUnlockRange(offset int64, whence int, len int64)\n```\n\n- whence: 0, 1, or 2 where:\n  - 0: relative to the origin of the file\n  - 1: relative to the current offset of the file\n  - 2: relative to the end of the file\n- offset: The offset, in bytes, from whence\n- len: The number of bytes to lock\n\n## Bugs and Issues\n\nIssues and pull requests on [GitHub](https://github.com/rubyist/lockfile)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frubyist%2Flockfile","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frubyist%2Flockfile","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frubyist%2Flockfile/lists"}