Skip to content

API for File Locking #1464

Open
Enhancement
@yschimke

Description

@yschimke

From square/okhttp#8209

Want to be able to use this without making assumptions about the FileSystem

  fun fileSystemLock(
    directory: Path,
  ): Closeable {
    val lockFile = directory / "lock"
    lockFile.toFile().createNewFile()
    val channel = FileChannel.open(lockFile.toNioPath(), StandardOpenOption.APPEND)

    checkNotNull(channel.tryLock()) {
      "Cache already open at '$directory' in another process"
    }

    return okio.Closeable {
      channel.close()
    }
  }

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions