Skip to content

Commit

Permalink
bump non-throwing read methods
Browse files Browse the repository at this point in the history
  • Loading branch information
marschall committed Dec 29, 2023
1 parent b7ba680 commit 3072a92
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ interface AccessCheck {

void checkAccess(AccessMode mode) throws AccessDeniedException;

boolean canRead();

}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ void checkAccess(AccessMode... modes) throws AccessDeniedException {
this.attributes.checkAccess(modes);
}

boolean canRead() {
return this.attributes.canRead();
}

void checkAccess(AccessMode mode) throws AccessDeniedException {
this.attributes.checkAccess(mode);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,21 @@ void checkAccess(AccessMode mode) throws AccessDeniedException {
}
}

boolean canRead() {
try (AutoRelease lock = this.readLock()) {
for (Object attributeView : this.additionalViews.values()) {
if (attributeView instanceof AccessCheck) {
AccessCheck accessCheck = (AccessCheck) attributeView;
if (!accessCheck.canRead()) {
return false;
}
}
}
}
// no access check views -> default allow
return true;
}

private AccessMode getUnsupported(AccessMode... modes) {
for (AccessMode mode : modes) {
if (!(mode == AccessMode.READ || mode == AccessMode.WRITE || mode == AccessMode.EXECUTE)) {
Expand Down Expand Up @@ -456,6 +471,17 @@ public List<AclEntry> getAcl() throws IOException {
}

public void checkAccess(AclEntryPermission mode) throws AccessDeniedException {
if (!this.canAccess(mode)) {
throw new AccessDeniedException(this.path.toString());
}
}

@Override
public boolean canRead() {
return this.canAccess(READ_DATA);
}

private boolean canAccess(AclEntryPermission mode) {
// TODO "OWNER@", "GROUP@", and "EVERYONE@"
UserPrincipal currentUser = this.attributes.getCurrentUser();
GroupPrincipal currentGroup = this.attributes.getCurrentGroup();
Expand All @@ -467,14 +493,15 @@ public void checkAccess(AclEntryPermission mode) throws AccessDeniedException {
AclEntryType type = entry.type();
if (applies) {
if (type == ALLOW) {
return;
return true;
}
if (type == DENY) {
throw new AccessDeniedException(this.path.toString());
return false;
}
}
}
}
return true;
}

@Override
Expand Down Expand Up @@ -600,6 +627,12 @@ public void checkAccess(AccessMode mode) throws AccessDeniedException {
}
}

@Override
public boolean canRead() {
// always fine
return true;
}

@Override
public void checkAccess(AccessMode[] modes) throws AccessDeniedException {
for (AccessMode mode : modes) {
Expand Down Expand Up @@ -827,6 +860,19 @@ public void setPermissions(Set<PosixFilePermission> perms) throws IOException {

@Override
public void checkAccess(AccessMode mode) throws AccessDeniedException {
int flag = this.computeAccessFlag(mode);
if (flag == 0) {
throw new AccessDeniedException(this.path.toString());
}
}

@Override
public boolean canRead() {
int flag = this.computeAccessFlag(AccessMode.READ);
return flag != 0;
}

private int computeAccessFlag(AccessMode mode) {
UserPrincipal user = this.attributes.getCurrentUser();
PosixFilePermission permission;
if (user == this.getOwner()) {
Expand All @@ -839,10 +885,7 @@ public void checkAccess(AccessMode mode) throws AccessDeniedException {
permission = this.translateOthersMode(mode);
}
}
int flag = 1 << permission.ordinal() & this.permissions;
if (flag == 0) {
throw new AccessDeniedException(this.path.toString());
}
return 1 << permission.ordinal() & this.permissions;
}

void assertOwner() throws AccessDeniedException {
Expand Down

0 comments on commit 3072a92

Please sign in to comment.