Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ ij_kotlin_allow_trailing_comma_on_call_site = true

[*.{yml,yaml}]
indent_size = 2
tab_width = 2
tab_width = 2

[*.kt*]
ktlint_code_style = intellij_idea
max_line_length = 120
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ import java.util.UUID
* @param delegate The [FileSystem] to wrap and forward calls to.
* @param provider The [FileSystemProvider] associated with this [FileSystem].
*/
public class AttributeCachingFileSystem(
delegate: FileSystem,
private val provider: FileSystemProvider,
) : ForwardingFileSystem(delegate) {
public class AttributeCachingFileSystem(delegate: FileSystem, private val provider: FileSystemProvider) :
ForwardingFileSystem(delegate) {

override fun provider(): FileSystemProvider = provider

Expand Down Expand Up @@ -61,9 +59,7 @@ public class AttributeCachingFileSystem(
* @throws IOException If an IO error occurs.
*/
@Throws(FileAlreadyExistsException::class, ProviderNotFoundException::class, IOException::class)
public fun wrapping(
fileSystem: FileSystem,
): AttributeCachingFileSystem = FileSystems.newFileSystem(
public fun wrapping(fileSystem: FileSystem): AttributeCachingFileSystem = FileSystems.newFileSystem(
// Need to ensure a unique fileSystem name everytime this is called, hence UUID.randomUUID()
URI.create("cache:///${UUID.randomUUID()}"),
mapOf(Pair("filesystem", fileSystem)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,18 +292,15 @@ internal class AttributeCachingFileSystemProvider : FileSystemProvider() {
* given [type] are not supported.
*/
@Throws(IOException::class, UnsupportedOperationException::class)
override fun <A : BasicFileAttributes?> readAttributes(
path: Path,
type: Class<A>,
vararg options: LinkOption,
): A = if (path is AttributeCachingPath) {
val attributes = path.getAllAttributesMatchingClass(type) ?: throw UnsupportedOperationException(
"Could not read attributes of type $type from the path $path and its delegate filesystem.",
)
attributes
} else {
path.fileSystem.provider().readAttributes(path, type, *options)
}
override fun <A : BasicFileAttributes?> readAttributes(path: Path, type: Class<A>, vararg options: LinkOption): A =
if (path is AttributeCachingPath) {
val attributes = path.getAllAttributesMatchingClass(type) ?: throw UnsupportedOperationException(
"Could not read attributes of type $type from the path $path and its delegate filesystem.",
)
attributes
} else {
path.fileSystem.provider().readAttributes(path, type, *options)
}

/**
* Read filesystem [attributes] from the incoming [path]. If the returned attributes are `null` we then attempt to get
Expand All @@ -324,18 +321,15 @@ internal class AttributeCachingFileSystemProvider : FileSystemProvider() {
* [FileSystemProvider].
*/
@Throws(IOException::class, UnsupportedOperationException::class, IllegalArgumentException::class)
override fun readAttributes(
path: Path,
attributes: String,
vararg options: LinkOption,
): MutableMap<String, Any> = if (path is AttributeCachingPath) {
val attributesMap = path.getAllAttributesMatchingNames(attributes) ?: throw IllegalArgumentException(
"Could not read attributes $attributes of the path $path from the delegate filesystem.",
)
attributesMap
} else {
path.fileSystem.provider().readAttributes(path, attributes, *options)
}
override fun readAttributes(path: Path, attributes: String, vararg options: LinkOption): MutableMap<String, Any> =
if (path is AttributeCachingPath) {
val attributesMap = path.getAllAttributesMatchingNames(attributes) ?: throw IllegalArgumentException(
"Could not read attributes $attributes of the path $path from the delegate filesystem.",
)
attributesMap
} else {
path.fileSystem.provider().readAttributes(path, attributes, *options)
}

/**
* Set a single attribute or attribute class (ie: "dos:*","basic:*","posix:permissions", etc.) for the given [path]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,8 @@ public const val CACHE_KEY_ACL: String = "acl:*"
* @param fileSystem the [FileSystem] associated with this [AttributeCachingPath] instance.
* @param delegate the [Path] to forward calls to if needed.
*/
internal class AttributeCachingPath(
private val fileSystem: FileSystem,
internal val delegate: Path,
) : ForwardingPath(delegate) {
internal class AttributeCachingPath(private val fileSystem: FileSystem, internal val delegate: Path) :
ForwardingPath(delegate) {

private val delegateSupportedFileAttributeViews = delegate.fileSystem.supportedFileAttributeViews()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,8 @@ public abstract class ForwardingFileSystemProvider(private val delegate: FileSys
delegate.delete(path)
}

override fun <A : BasicFileAttributes?> readAttributes(
path: Path,
type: Class<A>,
vararg options: LinkOption,
): A = delegate.readAttributes(path, type, *options)
override fun <A : BasicFileAttributes?> readAttributes(path: Path, type: Class<A>, vararg options: LinkOption): A =
delegate.readAttributes(path, type, *options)

override fun readAttributes(path: Path, attributes: String, vararg options: LinkOption): MutableMap<String, Any> =
delegate.readAttributes(path, attributes, *options)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,12 @@ class AttributeCachingFileSystemTests {

@ParameterizedTest
@MethodSource("allFileSystems")
fun `getName returns a cachingPath`(fileSystem: FileSystem) =
AttributeCachingFileSystem.wrapping(fileSystem).use {
val cachingPath = it.getPath("test.txt")
val closestToRootPathName = cachingPath.getName(0)
assertThat(cachingPath).isInstanceOf(AttributeCachingPath::class.java)
assertThat(closestToRootPathName).isInstanceOf(AttributeCachingPath::class.java)
}
fun `getName returns a cachingPath`(fileSystem: FileSystem) = AttributeCachingFileSystem.wrapping(fileSystem).use {
val cachingPath = it.getPath("test.txt")
val closestToRootPathName = cachingPath.getName(0)
assertThat(cachingPath).isInstanceOf(AttributeCachingPath::class.java)
assertThat(closestToRootPathName).isInstanceOf(AttributeCachingPath::class.java)
}

@ParameterizedTest
@MethodSource("allFileSystems")
Expand Down Expand Up @@ -794,9 +793,7 @@ class AttributeCachingFileSystemTests {

@ParameterizedTest
@MethodSource("allFileSystems")
fun `cached attributes do not get modified by concurrent operation`(
fileSystem: FileSystem,
) {
fun `cached attributes do not get modified by concurrent operation`(fileSystem: FileSystem) {
val tempDirPath = fileSystem.getPath("temp")

AttributeCachingFileSystem.wrapping(fileSystem).use {
Expand Down Expand Up @@ -843,10 +840,7 @@ class AttributeCachingFileSystemTests {

@ParameterizedTest
@MethodSource("allFileSystemsWithCopyOption")
fun `copy file from source to target`(
option: CopyOption,
fileSystem: () -> FileSystem,
) {
fun `copy file from source to target`(option: CopyOption, fileSystem: () -> FileSystem) {
val testFileSystem = fileSystem()
AttributeCachingFileSystem.wrapping(testFileSystem).use {
// get filesystem attribute caching path
Expand Down Expand Up @@ -916,10 +910,7 @@ class AttributeCachingFileSystemTests {

@ParameterizedTest
@MethodSource("allFileSystemsWithMoveOption")
fun `move file from source to target`(
option: CopyOption,
fileSystem: () -> FileSystem,
) {
fun `move file from source to target`(option: CopyOption, fileSystem: () -> FileSystem) {
val testFileSystem = fileSystem()
AttributeCachingFileSystem.wrapping(testFileSystem).use {
// get filesystem attribute caching path
Expand Down Expand Up @@ -1006,16 +997,13 @@ class AttributeCachingFileSystemTests {
@DisabledOnOs(OS.WINDOWS)
@ParameterizedTest
@MethodSource("hiddenTestPathsPosix")
fun `file isHidden on unix and macOS`(
fileName: String,
expectedHidden: Boolean,
fileSystem: () -> FileSystem,
) = AttributeCachingFileSystem.wrapping(fileSystem()).use {
val directoryName = "temp"
Files.createDirectory(it.getPath(directoryName))
val cachingPath = it.getPath(directoryName, fileName)
assertThat(Files.isHidden(cachingPath)).isEqualTo(expectedHidden)
}
fun `file isHidden on unix and macOS`(fileName: String, expectedHidden: Boolean, fileSystem: () -> FileSystem) =
AttributeCachingFileSystem.wrapping(fileSystem()).use {
val directoryName = "temp"
Files.createDirectory(it.getPath(directoryName))
val cachingPath = it.getPath(directoryName, fileName)
assertThat(Files.isHidden(cachingPath)).isEqualTo(expectedHidden)
}

@Test
fun `only commonly supported attribute views are transferred when copying across filesystems`() {
Expand Down Expand Up @@ -1162,10 +1150,8 @@ class AttributeCachingFileSystemTests {
.build(),
)

private fun ComparableSubject<FileTime>.followedFlagRulesComparedTo(
options: CopyOption,
expected: FileTime,
) = if (options == StandardCopyOption.COPY_ATTRIBUTES) isEqualTo(expected) else isNotEqualTo(expected)
private fun ComparableSubject<FileTime>.followedFlagRulesComparedTo(options: CopyOption, expected: FileTime) =
if (options == StandardCopyOption.COPY_ATTRIBUTES) isEqualTo(expected) else isNotEqualTo(expected)

private fun getOsString(fileSystem: FileSystem): String {
val supportedViews = fileSystem.supportedFileAttributeViews()
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[versions]
detektVersion = "1.23.1"
junitVersion = "5.10.3"
ktlintVersion = "11.6.1"
ktlintVersion = "13.0.0"
log4jVersion = "2.25.1"

[libraries]
Expand Down
Loading