Skip to content

Commit

Permalink
Merge pull request #3345 from dantb/fix-posix-perms
Browse files Browse the repository at this point in the history
Fix fetching permissions through getPosixFileAttributes and add equality
  • Loading branch information
mpilquist authored Jan 20, 2024
2 parents 352f331 + 4795b63 commit fb78701
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
7 changes: 7 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,13 @@ ThisBuild / mimaBinaryIssueFilters ++= Seq(
// sealed trait: #3349
ProblemFilters.exclude[ReversedMissingMethodProblem](
"fs2.io.net.tls.TLSParameters.withClientAuthType"
),
// equals/hashCode/toString on file attributes: #3345
ProblemFilters.exclude[ReversedMissingMethodProblem](
"fs2.io.file.PosixFileAttributes.fs2$io$file$PosixFileAttributes$$super=uals"
),
ProblemFilters.exclude[ReversedMissingMethodProblem](
"fs2.io.file.PosixFileAttributes.fs2$io$file$PosixFileAttributes$$super#Code"
)
)

Expand Down
3 changes: 2 additions & 1 deletion io/jvm-native/src/main/scala/fs2/io/file/FilesPlatform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ private[file] trait FilesCompanionPlatform {
with PosixFileAttributes.UnsealedPosixFileAttributes {
def owner: Principal = attr.owner
def group: Principal = attr.group
def permissions: PosixPermissions = PosixPermissions.fromString(attr.permissions.toString).get
def permissions: PosixPermissions =
PosixPermissions.fromString(PosixFilePermissions.toString(attr.permissions)).get
}
}
48 changes: 48 additions & 0 deletions io/shared/src/main/scala/fs2/io/file/FileAttributes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,38 @@ sealed trait BasicFileAttributes {
def lastAccessTime: FiniteDuration
def lastModifiedTime: FiniteDuration
def size: Long

override def equals(that: Any): Boolean = that match {
case other: BasicFileAttributes =>
creationTime == other.creationTime &&
fileKey == other.fileKey &&
isDirectory == other.isDirectory &&
isOther == other.isOther &&
isRegularFile == other.isRegularFile &&
isSymbolicLink == other.isSymbolicLink &&
lastAccessTime == other.lastAccessTime &&
lastModifiedTime == other.lastModifiedTime &&
size == other.size
case _ => false
}

override def hashCode: Int = {
import util.hashing.MurmurHash3.{stringHash, mix, finalizeHash}
val h = stringHash("FileAttributes")
mix(h, creationTime.##)
mix(h, fileKey.##)
mix(h, isDirectory.##)
mix(h, isOther.##)
mix(h, isRegularFile.##)
mix(h, isSymbolicLink.##)
mix(h, lastAccessTime.##)
mix(h, lastModifiedTime.##)
mix(h, size.##)
finalizeHash(h, 9)
}

override def toString: String =
s"BasicFileAttributes($creationTime, $fileKey, $isDirectory, $isOther, $isRegularFile, $isSymbolicLink, $lastAccessTime, $lastModifiedTime, $size)"
}

object BasicFileAttributes {
Expand All @@ -54,6 +86,22 @@ object BasicFileAttributes {
// the owner/group operations JVM only.
sealed trait PosixFileAttributes extends BasicFileAttributes {
def permissions: PosixPermissions

final override def equals(that: Any): Boolean = that match {
case other: PosixFileAttributes => super.equals(other) && permissions == other.permissions
case _ => false
}

final override def hashCode: Int = {
import util.hashing.MurmurHash3.{stringHash, mix, finalizeHash}
val h = stringHash("PosixFileAttributes")
mix(h, super.hashCode)
mix(h, permissions.##)
finalizeHash(h, 2)
}

final override def toString: String =
s"PosixFileAttributes($creationTime, $fileKey, $isDirectory, $isOther, $isRegularFile, $isSymbolicLink, $lastAccessTime, $lastModifiedTime, $size, $permissions)"
}

object PosixFileAttributes {
Expand Down
17 changes: 17 additions & 0 deletions io/shared/src/test/scala/fs2/io/file/FilesSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -856,4 +856,21 @@ class FilesSuite extends Fs2IoSuite with BaseFileSuite {
}
}

group("attributes") {

test("basic attributes are consistent for the same file") {
tempFile.use { p =>
val attr = Files[IO].getBasicFileAttributes(p)
(attr, attr).mapN(assertEquals(_, _))
}
}

test("posix attributes are consistent for the same file") {
tempFile.use { p =>
val attr = Files[IO].getPosixFileAttributes(p)
(attr, attr).mapN(assertEquals(_, _))
}
}
}

}

0 comments on commit fb78701

Please sign in to comment.