Skip to content

Commit 0099bef

Browse files
committed
[refactor] add safeSetExecutable to AbstractUnixStylePermission
This method will add the executable flag to the given mode for - the owner - the group, if the group has read or write access - others, if they have read or write access
1 parent ac2c7e1 commit 0099bef

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

exist-core/src/main/java/org/exist/security/AbstractUnixStylePermission.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,4 +417,36 @@ public static String typesToString(final int types) {
417417

418418
return builder.toString();
419419
}
420+
421+
static int ownerExecute = EXECUTE << 6;
422+
423+
static int groupRead = READ << 3;
424+
static int groupWrite = WRITE << 3;
425+
static int groupExecute = EXECUTE << 3;
426+
427+
static int otherRead = READ;
428+
static int otherWrite = WRITE;
429+
static int otherExecute = EXECUTE;
430+
431+
static int noop = 0;
432+
433+
private static int setExecutableIfOtherCanReadOrWrite (final int mode) {
434+
final boolean canReadOrWrite = (mode & otherRead) + (mode & otherWrite) > 0;
435+
436+
return canReadOrWrite ? otherExecute : noop;
437+
}
438+
439+
private static int setExecutableIfGroupCanReadOrWrite (final int mode) {
440+
final boolean canReadOrWrite = (mode & groupRead) + (mode & groupWrite) > 0;
441+
442+
return canReadOrWrite ? groupExecute : noop;
443+
}
444+
445+
public static int safeSetExecutable (final int mode) {
446+
return mode
447+
| ownerExecute
448+
| setExecutableIfGroupCanReadOrWrite(mode)
449+
| setExecutableIfOtherCanReadOrWrite(mode)
450+
;
451+
}
420452
}

0 commit comments

Comments
 (0)