Work With Filesystem Object Extended Attributes
Filesystem path target extended attributes store extra, customizable, small bits of info. For example, author name, file character encoding, short comments, security status, etc. Methods are provided to list, extract and work with these attributes.
The following functions are implemented:
get_xattr
: Retrieve the contents of the named xattrget_xattr_df
: Retrieve a data frame of xattr names, sizes and (raw) contents for a target pathget_xattr_raw
: Retrieve the (raw) contents of the named xattrget_xattr_size
: Retrieve the size (bytes) of the named xattrhas_xattrs
: Test if a target path has xattrsis_bplist
: Tests whether a raw vector is really a binary plistlist_xattrs
: List extended attribute names of a target pathread_bplist
: Convert binary plist to something usable in RExtended attributes seem to get stripped when R builds pkgs so until I can figure out an easy way not to do that, just find any file on your system that has an @
next to the permissions string in an ls -l
directory listing.
sample_file <- "~/Downloads/Elementary-Lunch-Menu.pdf"
list_xattrs(sample_file)
## character(0)
get_xattr_size(sample_file, "com.apple.metadata:kMDItemWhereFroms")
## [1] 0
Extended attributes can be anything so it makes alot of sense to work with the contents as a raw vector:
There is a “string” version of the function, but it may return “nothing” if there are embedded NULLs or other breaking characters in the contents:
You are really better off doing this if you really want a raw string conversion:
More often than not (on macOS) extended attributes are “binary property lists” (or “binary plist” for short). You can test to see if the returned raw vector is likely a binary plist:
If it is, you can get the data out of it. For now, this makes a system call to plutil
on macOS and plistutil
on other systems. You’ll be given a hint on how to install plistutil
if it’s not found.
This is R, so you should really consider doing this instead of any of the above #rectanglesrule:
get_xattr_df(sample_file)
## # A tibble: 0 x 3
## # ... with 3 variables: name <chr>, size <dbl>, contents <list>
you can live dangerously even with data frames, tho:
We can process a whole directory of files to see what extended attributes various path targets have:
fils <- list.files("~/Downloads", full.names = TRUE)
xdf <- map_df(set_names(fils, fils), get_xattr_df, .id="path")
count(xdf, name, sort=TRUE)
## # A tibble: 5 x 2
## name n
## <chr> <int>
## 1 com.apple.metadata:kMDItemWhereFroms 9
## 2 com.apple.quarantine 9
## 3 com.apple.lastuseddate#PS 4
## 4 com.apple.diskimages.fsck 1
## 5 com.apple.diskimages.recentcksum 1
And we can work with com.apple.metadata:kMDItemWhereFroms
binary plist data in bulk:
filter(xdf, name == "com.apple.metadata:kMDItemWhereFroms") %>%
filter(map_lgl(contents, is_bplist)) %>%
mutate(converted = map(contents, read_bplist)) %>%
select(size, converted) %>%
mutate(converted = map(converted, ~flatten_chr(.x$plist$array$string))) %>%
unnest() %>%
mutate(converted = urltools::domain(converted)) # you don't rly need to see the full URLs for this example
## # A tibble: 9 x 2
## size converted
## <dbl> <chr>
## 1 110 arxiv.org
## 2 116 128.84.21.199
## 3 86 arxiv.org
## 4 104 arxiv.org
## 5 86 arxiv.org
## 6 132 docs.aws.amazon.com
## 7 161 nms.lcs.mit.edu
## 8 161 osquery-packages.s3.amazonaws.com
## 9 172 s3.amazonaws.com
# Create a temp file for the example
tf <- tempfile(fileext = ".csv")
write.csv(mtcars, tf)
# has attributes? (shld be FALSE)
has_xattrs(tf)
## [1] FALSE
get_xattr(tf, "is.rud.setting")
## character(0)
# set an attribute
set_xattr(tf, "is.rud.setting.a", "first attribut")
get_xattr(tf, "is.rud.setting.a")
## [1] "first attribut"
get_xattr_size(tf, "is.rud.setting.a")
## [1] 14
# shld be TRUE
has_xattrs(tf)
## [1] TRUE
set_xattr(tf, "is.rud.setting.b", "second attribute")
get_xattr(tf, "is.rud.setting.b")
## [1] "second attribute"
get_xattr_size(tf, "is.rud.setting.b")
## [1] 16
# overwrite an attribute
set_xattr(tf, "is.rud.setting.a", "first attribute")
get_xattr(tf, "is.rud.setting.a")
## [1] "first attribute"
get_xattr_size(tf, "is.rud.setting.a")
## [1] 15
# see all the attributes
list_xattrs(tf)
## [1] "is.rud.setting.a" "is.rud.setting.b"
# data frame vs individual functions
get_xattr_df(tf)
## # A tibble: 2 x 3
## name size contents
## <chr> <dbl> <list>
## 1 is.rud.setting.a 15 <raw [15]>
## 2 is.rud.setting.b 16 <raw [16]>
# remove attribute
rm_xattr(tf, "is.rud.setting")
get_xattr(tf, "is.rud.setting")
## character(0)
# cleanup
unlink(tf)
Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.