Skip to main content

dir_unix.go

dir_unix.go - Overview

This file implements directory locking using flock for Unix-like systems (excluding Windows, Plan9, js and wasip1). It ensures only one process can access the Badger database at a time.

directoryLockGuard

  • Purpose: Represents a lock on a directory.
  • Fields:
    • f: *os.File - File handle on the directory, which we've flocked.
    • path: string - The absolute path to the PID file.
    • readOnly: bool - Indicates if this is a shared lock for a read-only database.

acquireDirectoryLock

  • Purpose: Acquires a lock on the directory using flock.
  • Parameters:
    • dirPath: string - The path to the directory to lock.
    • pidFileName: string - The name of the PID file.
    • readOnly: bool - Indicates whether to acquire a read-only lock.
  • Returns:
    • *directoryLockGuard - A pointer to the directoryLockGuard struct on success.
    • error - An error if the lock could not be acquired.

directoryLockGuard.release

  • Purpose: Releases the directory lock by closing the file and removing the PID file.
  • Parameters: None
  • Returns: error - An error, if any.

openDir

  • Purpose: Opens a directory for syncing.
  • Parameters:
    • path: string - The path to the directory.
  • Returns:
    • *os.File - A pointer to the opened file.
    • error - An error, if any.

syncDir

  • Purpose: Ensures the directory entry for the file is synced.
  • Parameters:
    • dir: string - The directory to sync.
  • Returns: error - An error, if any.