Fix /tmp/.sbt/ collision for domain socket

Problem
-------
Fixes https://github.com/sbt/sbt/issues/7013
In a shared environment, multiple users will try to create
`/tmp/.sbt/` directory, and fail.

Solution
--------
Append a deterministic hash like `/tmp/.sbt1234ABCD` based
on the user home, so the same directory is used for the given
user, but each user would have a unique runtime directory.
This commit is contained in:
Eugene Yokota 2022-10-02 12:45:30 -04:00
parent 87bbdd9f9b
commit 2a61fb35f9
1 changed files with 11 additions and 2 deletions

View File

@ -14,6 +14,7 @@ import java.util.Locale
import scala.util.control.NonFatal
import scala.concurrent.duration._
import sbt.internal.inc.HashUtil
import sbt.internal.util.{ Terminal => ITerminal, Util }
import sbt.internal.util.complete.SizeParser
import sbt.io.syntax._
@ -247,7 +248,15 @@ object SysProp {
* Windows, and Docker environment.
* Mostly these directories will be used as throw-away location to extract
* native files etc.
* A deterministic hash is appended in the directory name as "/tmp/.sbt1234ABCD/"
* to avoid collision between multiple users in a shared server environment.
*/
private[this] def runtimeDirectory: Path =
Paths.get(sys.env.getOrElse("XDG_RUNTIME_DIR", sys.props("java.io.tmpdir"))).resolve(".sbt")
private[this] def runtimeDirectory: Path = {
val hashValue =
java.lang.Long.toHexString(HashUtil.farmHash(home.toString.getBytes("UTF-8")))
val halfhash = hashValue.take(8)
Paths
.get(sys.env.getOrElse("XDG_RUNTIME_DIR", sys.props("java.io.tmpdir")))
.resolve(s".sbt$halfhash")
}
}