Skip to main content

FileLock

A FileLock instance encapsulates a native whole-file lock and file locking between php-wasm processes.

A FileLock supports php-wasm whole-file locks and byte range locks. Before granting a php-wasm lock, a FileLock ensures that it first holds a compatible native lock. If a compatible native lock cannot be acquired, the php-wasm lock is not granted.

Index

Methods

dispose

  • dispose(): void
  • Close the file descriptor and release the native lock.

    @TODO

    Replace this with a Symbol.dispose property once supported by all JS runtimes.


    Returns void

findFirstConflictingByteRangeLock

  • findFirstConflictingByteRangeLock(desiredLock: Readonly<{ end: bigint; pid: number; start: bigint; type: shared | exclusive | unlocked }>): Readonly<{ end: bigint; pid: number; start: bigint; type: shared | exclusive | unlocked }>
  • Find the first conflicting byte range lock.

    This method corresponds to the fcntl() F_GETLK command.


    Parameters

    • desiredLock: Readonly<{ end: bigint; pid: number; start: bigint; type: shared | exclusive | unlocked }>

      The desired byte range lock.

    Returns Readonly<{ end: bigint; pid: number; start: bigint; type: shared | exclusive | unlocked }>

    The first conflicting byte range lock, or undefined if no conflicting lock exists.

isUnlocked

  • isUnlocked(): boolean
  • Check if the file lock is unlocked.


    Returns boolean

    True if the file lock is unlocked, false otherwise.

lockFileByteRange

  • lockFileByteRange(requestedLock: Readonly<{ end: bigint; pid: number; start: bigint; type: shared | exclusive | unlocked }>): boolean
  • Lock a byte range.

    This method corresponds to the fcntl() F_SETLK command.


    Parameters

    • requestedLock: Readonly<{ end: bigint; pid: number; start: bigint; type: shared | exclusive | unlocked }>

      The byte range lock to perform.

    Returns boolean

    True if the lock was granted, false otherwise.

lockWholeFile

  • Lock the whole file.

    This method corresponds to the flock() function.


    Parameters

    Returns boolean

    True if the lock was granted, false otherwise.

releaseLocksForProcess

  • releaseLocksForProcess(pid: number): void
  • Release all locks for the given process.


    Parameters

    • pid: number

      The process ID to release locks for.

    Returns void

releaseLocksForProcessFd

  • releaseLocksForProcessFd(pid: number, fd: number): void
  • Release all locks for the given process and file descriptor.


    Parameters

    • pid: number

      The process ID to release locks for.

    • fd: number

      The file descriptor to release locks for.

    Returns void

staticmaybeCreate

  • maybeCreate(path: string, mode: shared | exclusive): FileLock
  • Create a new FileLock instance for the given file and mode. Fail if the underlying native file lock cannot be acquired.


    Parameters

    • path: string

      The path to the file to lock

    • mode: shared | exclusive

      The type of lock to acquire

    Returns FileLock

    A FileLock instance if the lock was acquired, undefined otherwise