filechampion4j 0.9.8.3
File validation library for Java
Loading...
Searching...
No Matches
dev.filechampion.filechampion4j.FileAclHelper Class Reference
Collaboration diagram for dev.filechampion.filechampion4j.FileAclHelper:
Collaboration graph

Public Member Functions

 FileAclHelper (Path targetFilePath, String newOwnerUsername, String newPermissions) throws IllegalArgumentException
 
String changeFileAcl ()
 

Private Member Functions

void logSevere (String message)
 
void logFine (String message)
 
UserPrincipal getUserPrinciple (Path targetFilePath, String newOwnerUsername)
 
String setNewOwner (UserPrincipal newOwner)
 
String setNewPermissions (UserPrincipal newOwner)
 
String setNewPermissionsWindows (UserPrincipal newOwner)
 
String setNewPermissionsUnix ()
 

Private Attributes

Path targetFilePath
 
String newPermissions
 
String newOwnerUsername
 
StringBuilder errMsg = new StringBuilder()
 

Static Private Attributes

static final Logger LOGGER = Logger.getLogger(FileAclHelper.class.getName())
 

Detailed Description

This class is used to change the owner and permissions of a file. Set Owner uses java.nio.file.Files.setOwner for cross-platform support. Set Permissions attempts to identify the Operating System, using java.nio.file.attribute.AclFileAttributeView for Windows, and java.nio.file.attribute.PosixFileAttributeView for any other.

Definition at line 19 of file FileAclHelper.java.

Constructor & Destructor Documentation

◆ FileAclHelper()

dev.filechampion.filechampion4j.FileAclHelper.FileAclHelper ( Path  targetFilePath,
String  newOwnerUsername,
String  newPermissions 
) throws IllegalArgumentException

Class constructor

Parameters
targetFilePath(Path) the path of the file to change
newOwnerUsername(String) the new owner of the file
newPermissions(String the new permissions of the file (e.g. "rwx")

Definition at line 42 of file FileAclHelper.java.

42 {
43 this.targetFilePath = targetFilePath;
44 this.newOwnerUsername = newOwnerUsername;
45 if (!newPermissions.matches("[rwx]+")) {
46 errMsg.replace(0, errMsg.length(), "Error: Invalid permissions:").append(newPermissions);
47 throw new IllegalArgumentException(errMsg.toString());
48 }
49 this.newPermissions = newPermissions;
50 }

References dev.filechampion.filechampion4j.FileAclHelper.errMsg, dev.filechampion.filechampion4j.FileAclHelper.newOwnerUsername, dev.filechampion.filechampion4j.FileAclHelper.newPermissions, and dev.filechampion.filechampion4j.FileAclHelper.targetFilePath.

Member Function Documentation

◆ changeFileAcl()

String dev.filechampion.filechampion4j.FileAclHelper.changeFileAcl ( )

changeFileAcl is the main method of this class. It attempts to change the owner and permissions of a file.

Returns
(String) a String containing the result of the operation

Definition at line 57 of file FileAclHelper.java.

57 {
58 if(!newPermissions.matches("[rwx]+")) {
59 errMsg.replace(0, errMsg.length(), "Error: Invalid permissions:").append(newPermissions);
60 return errMsg.toString();
61 }
62 UserPrincipal newOwner = getUserPrinciple(targetFilePath, newOwnerUsername);
63 if (newOwner == null) {
64 errMsg.replace(0, errMsg.length(), "Error: Could not get user principal for ").append(newOwnerUsername);
65 return errMsg.toString();
66 }
67
68 // Try to change the owner of the file
69 String newOwnerChangeResults = setNewOwner(newOwner);
70 if (newOwnerChangeResults.contains("Error")) {
71 return newOwnerChangeResults;
72 }
73
74 // Try to change the permissions of the file
75 String newPermissionsChangeResults = setNewPermissions(newOwner);
76 if (newPermissionsChangeResults.contains("Error")) {
77 return newPermissionsChangeResults;
78 }
79
80 // If we get here, both the owner and permissions were changed successfully
81 String successMessage = String.format("Success: Changed owner and permissions of file %s", targetFilePath.toAbsolutePath());
82 logFine(successMessage);
83 return successMessage;
84 }
String setNewPermissions(UserPrincipal newOwner)
String setNewOwner(UserPrincipal newOwner)
UserPrincipal getUserPrinciple(Path targetFilePath, String newOwnerUsername)

References dev.filechampion.filechampion4j.FileAclHelper.errMsg, dev.filechampion.filechampion4j.FileAclHelper.getUserPrinciple(), dev.filechampion.filechampion4j.FileAclHelper.logFine(), dev.filechampion.filechampion4j.FileAclHelper.newOwnerUsername, dev.filechampion.filechampion4j.FileAclHelper.newPermissions, dev.filechampion.filechampion4j.FileAclHelper.setNewOwner(), dev.filechampion.filechampion4j.FileAclHelper.setNewPermissions(), and dev.filechampion.filechampion4j.FileAclHelper.targetFilePath.

Referenced by dev.filechampion.filechampion4j.FileValidator.setFileAttributes().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getUserPrinciple()

UserPrincipal dev.filechampion.filechampion4j.FileAclHelper.getUserPrinciple ( Path  targetFilePath,
String  newOwnerUsername 
)
private

Definition at line 87 of file FileAclHelper.java.

87 {
88 try {
89 return targetFilePath.getFileSystem()
90 .getUserPrincipalLookupService()
91 .lookupPrincipalByName(newOwnerUsername);
92 } catch (Exception e) {
93 errMsg.replace(0, errMsg.length(), "Error: Exception getting user principal: ").append(e.getMessage());
94 logSevere(errMsg.toString());
95 return null;
96 }
97 }

References dev.filechampion.filechampion4j.FileAclHelper.errMsg, dev.filechampion.filechampion4j.FileAclHelper.logSevere(), dev.filechampion.filechampion4j.FileAclHelper.newOwnerUsername, and dev.filechampion.filechampion4j.FileAclHelper.targetFilePath.

Referenced by dev.filechampion.filechampion4j.FileAclHelper.changeFileAcl().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ logFine()

void dev.filechampion.filechampion4j.FileAclHelper.logFine ( String  message)
private

◆ logSevere()

void dev.filechampion.filechampion4j.FileAclHelper.logSevere ( String  message)
private

◆ setNewOwner()

String dev.filechampion.filechampion4j.FileAclHelper.setNewOwner ( UserPrincipal  newOwner)
private

Definition at line 100 of file FileAclHelper.java.

100 {
101 try {
102 // Change the owner of the file
103 Files.setOwner(targetFilePath, newOwner);
104 errMsg.replace(0, errMsg.length(), "Success: Changed owner of file").append(targetFilePath.toAbsolutePath()).append(" to ").append(newOwnerUsername);
105 logFine(errMsg.toString());
106 return errMsg.toString();
107 } catch (AccessDeniedException e) {
108 errMsg.replace(0, errMsg.length(), "Error: Access denied while changing file owner: ").append(e.getMessage());
109 logSevere(errMsg.toString());
110 return errMsg.toString();
111 } catch (FileSystemException e) {
112 errMsg.replace(0, errMsg.length(), "Error: File system error while changing file owner: ").append(e.getMessage());
113 logSevere(errMsg.toString());
114 return errMsg.toString();
115 }
116 catch (Exception e) {
117 errMsg.replace(0, errMsg.length(), "Error: Exception while changing file owner: ").append(e.getMessage());
118 logSevere(errMsg.toString());
119 return errMsg.toString();
120 }
121 }

References dev.filechampion.filechampion4j.FileAclHelper.errMsg, dev.filechampion.filechampion4j.FileAclHelper.logFine(), dev.filechampion.filechampion4j.FileAclHelper.logSevere(), dev.filechampion.filechampion4j.FileAclHelper.newOwnerUsername, and dev.filechampion.filechampion4j.FileAclHelper.targetFilePath.

Referenced by dev.filechampion.filechampion4j.FileAclHelper.changeFileAcl().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setNewPermissions()

String dev.filechampion.filechampion4j.FileAclHelper.setNewPermissions ( UserPrincipal  newOwner)
private

Definition at line 124 of file FileAclHelper.java.

124 {
125 String os = System.getProperty("os.name");
126 if (os.startsWith("Windows")) {
127 return setNewPermissionsWindows(newOwner);
128 } else {
129 return setNewPermissionsUnix();
130 }
131 }
String setNewPermissionsWindows(UserPrincipal newOwner)

References dev.filechampion.filechampion4j.FileAclHelper.setNewPermissionsUnix(), and dev.filechampion.filechampion4j.FileAclHelper.setNewPermissionsWindows().

Referenced by dev.filechampion.filechampion4j.FileAclHelper.changeFileAcl().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setNewPermissionsUnix()

String dev.filechampion.filechampion4j.FileAclHelper.setNewPermissionsUnix ( )
private

Definition at line 186 of file FileAclHelper.java.

186 {
187 String statusMessage;
188
189 // Build the new permissions
190 Set<PosixFilePermission> permissions = new HashSet<>();
191 if (newPermissions.contains("r")) {
192 permissions.add(PosixFilePermission.OWNER_READ);
193 }
194 if (newPermissions.contains("w")) {
195 permissions.add(PosixFilePermission.OWNER_WRITE);
196 }
197 if (newPermissions.contains("x")) {
198 permissions.add(PosixFilePermission.OWNER_EXECUTE);
199 }
200
201 // Change the permissions of the file using POSIX
202 try {
203 statusMessage = String.format("Attempting to change permissions to %s", permissions);
204 logFine(statusMessage);
205 Files.setPosixFilePermissions(targetFilePath.toAbsolutePath(), permissions);
206 errMsg.replace(0, errMsg.length(), "Success: Changed permissions of file").append(targetFilePath.toAbsolutePath()).append(" to ").append(permissions);
207 logFine(errMsg.toString());
208 return errMsg.toString();
209 } catch (Exception e) {
210 errMsg.replace(0, errMsg.length(), "Error: Exception setting permissions on file with POSIX: ")
211 .append(targetFilePath.toAbsolutePath())
212 .append(", ")
213 .append(e.getMessage());
214 logSevere(errMsg.toString());
215 return errMsg.toString();
216 }
217 }

References dev.filechampion.filechampion4j.FileAclHelper.errMsg, dev.filechampion.filechampion4j.FileAclHelper.logFine(), dev.filechampion.filechampion4j.FileAclHelper.logSevere(), dev.filechampion.filechampion4j.FileAclHelper.newPermissions, and dev.filechampion.filechampion4j.FileAclHelper.targetFilePath.

Referenced by dev.filechampion.filechampion4j.FileAclHelper.setNewPermissions().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setNewPermissionsWindows()

String dev.filechampion.filechampion4j.FileAclHelper.setNewPermissionsWindows ( UserPrincipal  newOwner)
private

Definition at line 134 of file FileAclHelper.java.

134 {
135 try {
136 String statusMessage;
137 // Change the permissions of the file using ACLs
138 AclFileAttributeView aclView = Files.getFileAttributeView(targetFilePath, AclFileAttributeView.class);
139 AclEntryPermission readPermission = AclEntryPermission.READ_DATA;
140 AclEntryPermission writePermission = AclEntryPermission.WRITE_DATA;
141 AclEntryPermission executePermission = AclEntryPermission.EXECUTE;
142
143 AclEntryType allow = AclEntryType.ALLOW;
144
145 EnumSet<AclEntryPermission> actualPermissions = EnumSet.noneOf(AclEntryPermission.class);
146 if (newPermissions.contains("r")) {
147 actualPermissions.add(readPermission);
148 }
149 if (newPermissions.contains("w")) {
150 actualPermissions.add(writePermission);
151 }
152 if (newPermissions.contains("x")) {
153 actualPermissions.add(executePermission);
154 }
155
156 statusMessage = String.format("Attempting to change permissions to %s", actualPermissions);
157 logFine(statusMessage);
158
159 // Create the new ACL entry
160 AclEntry aclEntry = AclEntry.newBuilder()
161 .setType(allow)
162 .setPrincipal(newOwner)
163 .setPermissions(actualPermissions)
164 .build();
165
166 // Get the existing ACL and add the new ACL entries
167 List<AclEntry> acl = new ArrayList<>();
168 acl.add(aclEntry);
169
170 // Set the new ACL
171 aclView.setAcl(acl);
172 errMsg.replace(0, errMsg.length(), "Success: Changed permissions of file").append(targetFilePath.toAbsolutePath()).append(" to ").append(actualPermissions);
173 logFine(errMsg.toString());
174 return errMsg.toString();
175 } catch (Exception e) {
176 errMsg.replace(0, errMsg.length(), "Error: Exception setting permissions on file with ACL: ")
177 .append(targetFilePath.toAbsolutePath())
178 .append(", ")
179 .append(e.getMessage());
180 logSevere(errMsg.toString());
181 return errMsg.toString();
182 }
183 }

References dev.filechampion.filechampion4j.FileAclHelper.errMsg, dev.filechampion.filechampion4j.FileAclHelper.logFine(), dev.filechampion.filechampion4j.FileAclHelper.logSevere(), dev.filechampion.filechampion4j.FileAclHelper.newPermissions, and dev.filechampion.filechampion4j.FileAclHelper.targetFilePath.

Referenced by dev.filechampion.filechampion4j.FileAclHelper.setNewPermissions().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ errMsg

◆ LOGGER

final Logger dev.filechampion.filechampion4j.FileAclHelper.LOGGER = Logger.getLogger(FileAclHelper.class.getName())
staticprivate

◆ newOwnerUsername

◆ newPermissions

◆ targetFilePath


The documentation for this class was generated from the following file: