Work With Filesystem Object Extended Attributes

Description

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.

NOTE

I don’t think this will work on Windows.

What’s Inside The Tin

The following functions are implemented:

  • get_xattr: Retrieve the contents of the named xattr
  • get_xattr_df: Retrieve a data frame of xattr names, sizes and (raw) contents for a target path
  • get_xattr_raw: Retrieve the (raw) contents of the named xattr
  • get_xattr_size: Retrieve the size (bytes) of the named xattr
  • has_xattrs: Test if a target path has xattrs
  • is_bplist: Tests whether a raw vector is really a binary plist
  • list_xattrs: List extended attribute names of a target path
  • read_bplist: Convert binary plist to something usable in R

Installation

devtools::install_github("hrbrmstr/xattrs")

Usage

Basic Operation

Extended 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:

get_xattr_raw(sample_file, "com.apple.metadata:kMDItemWhereFroms")
## raw(0)

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:

get_xattr(sample_file, "com.apple.metadata:kMDItemWhereFroms")
## character(0)

You are really better off doing this if you really want a raw string conversion:

readBin(get_xattr_raw(sample_file, "com.apple.metadata:kMDItemWhereFroms"), "character")
## [1] ""

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:

is_bplist(get_xattr_raw(sample_file, "com.apple.metadata:kMDItemWhereFroms"))
## [1] FALSE

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.

read_bplist(get_xattr_raw(sample_file, "com.apple.metadata:kMDItemWhereFroms"))
## [[1]]
## [1] NA

This is R, so you should really consider doing this instead of any of the above #rectanglesrule:

you can live dangerously even with data frames, tho:

Code of Conduct

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.