Finding the owner of a file with ColdFusion

A reader asked me last night if it was possible to find the owner of a file using ColdFusion. You may think getFileInfo would return this but it does not. Luckily it is relatively simple to get to it from Java.

The first result I found via Google sent me to a Stack Overflow answer that worked fine once I converted it to work within ColdFusion:

pathOb = createObject("java", "java.nio.file.Paths");
//Hard coded path here, but obviously this would be dynamic
path = pathOb.get("/Applications/ColdFusion11/cfusion/wwwroot/test.cfm",[]);
files = createObject("java", "java.nio.file.Files");
fav = createObject("java","java.nio.file.attribute.FileOwnerAttributeView");
oav = files.getFileAttributeView(path, fav.getClass(), []);
owner = oav.getOwner();
writeoutput(owner);

You could easily wrap this up into a UDF so it is easier to call. Ok, Ray, don’t be lazy. Here is a UDF version with an example call:


function getFileOwner(p) {
	var pathOb = createObject("java", "java.nio.file.Paths");
	var path = pathOb.get(p,[]);
	var files = createObject("java", "java.nio.file.Files");
	var fav = createObject("java","java.nio.file.attribute.FileOwnerAttributeView");
	var oav = files.getFileAttributeView(path, fav.getClass(), []);
	return oav.getOwner();
}

writeoutput(getFileOwner(expandPath("./Application.cfc")));

And because I was bored, I rewrote the UDF as one line that would be a pain in the rear to debug.


function getFileOwner(p) {
	return createObject("java", "java.nio.file.Files").getFileAttributeView(createObject("java", "java.nio.file.Paths").get(p,[]), createObject("java","java.nio.file.attribute.FileOwnerAttributeView").getClass(), []).getOwner();
}
Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate looking for his next gig. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support.

Lafayette, LA https://www.raymondcamden.com

Comments