Compare commits

...

1 commit

Author SHA1 Message Date
Michiel Borkent
f8c4be9d70 wip [skip ci] 2024-12-20 12:41:50 +01:00
2 changed files with 49 additions and 1 deletions

View file

@ -394,6 +394,7 @@
java.nio.file.FileAlreadyExistsException
java.nio.file.FileSystem
java.nio.file.FileSystems
java.nio.file.FileStore
java.nio.file.FileVisitor
java.nio.file.FileVisitOption
java.nio.file.FileVisitResult
@ -410,7 +411,8 @@
java.nio.file.attribute.FileTime
java.nio.file.attribute.PosixFileAttributes
java.nio.file.attribute.PosixFilePermission
java.nio.file.attribute.PosixFilePermissions])
java.nio.file.attribute.PosixFilePermissions
java.nio.file.attribute.UserDefinedFileAttributeView])
java.security.DigestInputStream
java.security.KeyFactory
java.security.KeyPairGenerator
@ -728,6 +730,8 @@
java.nio.file.Path
(instance? java.nio.file.FileSystem v)
java.nio.file.FileSystem
(instance? java.nio.file.FileStore v)
java.nio.file.FileStore
(instance? java.nio.file.PathMatcher v)
java.nio.file.PathMatcher
(instance? java.util.stream.Stream v)

View file

@ -0,0 +1,44 @@
(ns user-defined-attribute-view
(:import [java.nio.file Files Paths]
[java.nio.file.attribute UserDefinedFileAttributeView FileAttribute]
[java.nio.charset StandardCharsets])
(:require [babashka.fs :as fs]))
(defn set-user-defined-attribute
"Sets a user-defined attribute on the specified file."
[file-path attribute-name attribute-value]
(let [path file-path
view (.getFileAttributeView (Files/getFileStore path)
UserDefinedFileAttributeView)]
(when view
(let [bytes (.getBytes attribute-value StandardCharsets/UTF_8)]
(.write view attribute-name bytes 0 (count bytes)))
(println "Attribute set successfully."))))
(defn get-user-defined-attribute
"Gets a user-defined attribute from the specified file."
[file-path attribute-name]
(let [path file-path
view (.getFileAttributeView (Files/getFileStore path)
UserDefinedFileAttributeView)]
(when view
(let [size (.size view attribute-name)
buffer (byte-array size)]
(.read view attribute-name buffer 0 size)
(String. buffer StandardCharsets/UTF_8)))))
;; Example usage
(defn -main []
(let [tmp-dir (fs/temp-dir)
file-path (fs/path tmp-dir "example.txt")
_ (fs/delete-on-exit file-path)
attribute-name "user.comment"
attribute-value "This is a test comment."]
;; Create an example file
(Files/createFile file-path (into-array FileAttribute []))
;; Set and get the user-defined attribute
(set-user-defined-attribute file-path attribute-name attribute-value)
(println "Retrieved attribute:"
(get-user-defined-attribute file-path attribute-name))))
(-main)