From c3d65951d80f09b813e158a9e3a1785c622353b3 Mon Sep 17 00:00:00 2001 From: Pathikrit Bhowmick Date: Mon, 16 Jan 2017 11:57:27 -0500 Subject: [PATCH] File update metadata API --- README.md | 1 + core/src/main/scala/better/files/File.scala | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1ce2938f..f873f679 100644 --- a/README.md +++ b/README.md @@ -307,6 +307,7 @@ file.size // for a directory, computes the directory size file.posixAttributes / file.dosAttributes // see file.attributes file.isEmpty // true if file has no content (or no children if directory) or does not exist file.isParentOf / file.isChildOf / file.isSiblingOf / file.siblings +file("dos:system") = true // set custom meta-data for file (similar to Files.setAttribute) ``` All the above APIs let you specify the [`LinkOption`](http://docs.oracle.com/javase/8/docs/api/java/nio/file/LinkOption.html) either directly: ```scala diff --git a/core/src/main/scala/better/files/File.scala b/core/src/main/scala/better/files/File.scala index dd2aedf9..5af6cd24 100644 --- a/core/src/main/scala/better/files/File.scala +++ b/core/src/main/scala/better/files/File.scala @@ -2,7 +2,7 @@ package better.files import java.io.{File => JFile, _} import java.net.URI -import java.nio.channels.{OverlappingFileLockException, AsynchronousFileChannel, FileChannel, NonWritableChannelException, NonReadableChannelException} +import java.nio.channels._ import java.nio.file._, attribute._ import java.security.MessageDigest import java.time.Instant @@ -388,6 +388,19 @@ class File private(val path: Path) { algorithm.digest() } + /** + * Set a file attribute e.g. file("dos:system") = true + * + * @param attribute + * @param value + * @param linkOptions + * @return + */ + def update(attribute: String, value: Any)(implicit linkOptions: File.LinkOptions = File.LinkOptions.default): this.type = { + Files.setAttribute(path, attribute, value, linkOptions : _*) + this + } + /** * @return checksum of this file (or directory) in hex format */