diff --git a/Readme.adoc b/Readme.adoc index 61bb1bad..faf9bfbb 100644 --- a/Readme.adoc +++ b/Readme.adoc @@ -1555,6 +1555,51 @@ os.isLink(wd / "misc/folder-symlink") ==> true os.isLink(wd / "folder1") ==> false ---- +==== `os.isReadable` + +[source,scala] +---- +os.isReadable(p: Path): Boolean +---- + +Returns `true` if the given path is readable. + +[source,scala] +---- +os.isReadable(wd / "misc/file1") ==> false +os.isReadable(wd / "misc/file2") ==> true +---- + +==== `os.isWritable` + +[source,scala] +---- +os.isWritable(p: Path): Boolean +---- + +Returns `true` if the given path is writable. + +[source,scala] +---- +os.isWritable(wd / "misc/file1") ==> false +os.isWritable(wd / "misc/file2") ==> true +---- + +==== `os.isExecutable` + +[source,scala] +---- +os.isExecutable(p: Path): Boolean +---- + +Returns `true` if the given path is executable. + +[source,scala] +---- +os.isExecutable(wd / "misc/file1") ==> false +os.isExecutable(wd / "misc/file2.sh") ==> true +---- + ==== `os.size` [source,scala] diff --git a/os/src/StatOps.scala b/os/src/StatOps.scala index cf8695ef..b85db779 100644 --- a/os/src/StatOps.scala +++ b/os/src/StatOps.scala @@ -40,6 +40,33 @@ object isDir extends Function1[Path, Boolean] { } } +/** + * Checks whether the given path is readable + * + * Returns `false` if the path does not exist + */ +object isReadable extends Function1[Path, Boolean] { + def apply(p: Path): Boolean = Files.isReadable(p.wrapped) +} + +/** + * Checks whether the given path is writable + * + * Returns `false` if the path does not exist + */ +object isWritable extends Function1[Path, Boolean] { + def apply(p: Path): Boolean = Files.isWritable(p.wrapped) +} + +/** + * Checks whether the given path is executable + * + * Returns `false` if the path does not exist + */ +object isExecutable extends Function1[Path, Boolean] { + def apply(p: Path): Boolean = Files.isExecutable(p.wrapped) +} + /** * Gets the size of the given file or folder * diff --git a/os/test/src/FilesystemMetadataTests.scala b/os/test/src/FilesystemMetadataTests.scala index f5d654db..a340cadd 100644 --- a/os/test/src/FilesystemMetadataTests.scala +++ b/os/test/src/FilesystemMetadataTests.scala @@ -50,6 +50,27 @@ object FilesystemMetadataTests extends TestSuite { os.isLink(wd / "folder1") ==> false } } + test("isReadable") { + test - prep { wd => + os.isReadable(wd / "File.txt") ==> true + os.isReadable(wd / "folder1") ==> true + } + } + test("isWritable") { + test - prep { wd => + os.isWritable(wd / "File.txt") ==> true + os.isWritable(wd / "folder1") ==> true + } + } + test("isExecutable") { + test - prep { wd => + if (Unix()) { + os.perms.set(wd / "File.txt", "rw-rw-rw-") + os.isExecutable(wd / "File.txt") ==> false + os.isExecutable(wd / "misc/echo") ==> true + } + } + } test("size") { test - prep { wd => os.size(wd / "File.txt") ==> 8