Skip to content

Commit

Permalink
Copy then atomic move for proto download to prevent clashes between s…
Browse files Browse the repository at this point in the history
…imultaneous downloads
  • Loading branch information
thesamet committed Jan 20, 2024
1 parent cbbb48e commit 86f2a94
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions bridge/src/main/scala/protocbridge/FileCache.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import scala.concurrent.Promise
import java.io.File
import scala.concurrent.ExecutionContext.Implicits.global
import scala.util.Try
import java.nio.file.{Files, Path}
import java.nio.file.{Files, Path, StandardCopyOption}

/** Cache for files that performs a single concurrent get per key upon miss. */
final class FileCache[K](
Expand Down Expand Up @@ -46,8 +46,15 @@ final class FileCache[K](
}

private[protocbridge] def copyToCache(src: File, dst: File): File = {
val tmp = File.createTempFile("protocbridge", "tmp", dst.getParentFile())
val dstPath = dst.toPath()
Files.copy(src.toPath(), dst.toPath())
Files.copy(src.toPath(), tmp.toPath(), StandardCopyOption.REPLACE_EXISTING)
Files.move(
src.toPath(),
dstPath,
StandardCopyOption.ATOMIC_MOVE,
StandardCopyOption.REPLACE_EXISTING
)
dst.setExecutable(true)
dst
}
Expand Down

0 comments on commit 86f2a94

Please sign in to comment.