Skip to content

Commit

Permalink
fix mode LDEV-3506
Browse files Browse the repository at this point in the history
  • Loading branch information
mircea-botez authored and zspitzer committed Jun 26, 2023
1 parent 7a1c578 commit f302df0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
31 changes: 31 additions & 0 deletions core/src/main/java/lucee/commons/io/ModeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
**/
package lucee.commons.io;

import lucee.print;

import java.io.IOException;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.Set;

public final class ModeUtil {
Expand All @@ -45,6 +48,32 @@ public static int toOctalMode(String strMode) throws IOException {
throw new IOException("can't translate [" + strMode + "] to a mode value");
}

public static Set<PosixFilePermission> fromInt(int perms) {
final char[] ds = Integer.toString(perms).toCharArray();
final char[] ss = {'-','-','-','-','-','-','-','-','-'};
for (int i = ds.length-1; i >= 0; i--) {
int n = ds[i] - '0';
if (i == ds.length-1) {
if ((n & 1) != 0) ss[8] = 'x';
if ((n & 2) != 0) ss[7] = 'w';
if ((n & 4) != 0) ss[6] = 'r';
}
else if (i == ds.length-2) {
if ((n & 1) != 0) ss[5] = 'x';
if ((n & 2) != 0) ss[4] = 'w';
if ((n & 4) != 0) ss[3] = 'r';
}
else if (i == ds.length-3) {
if ((n & 1) != 0) ss[2] = 'x';
if ((n & 2) != 0) ss[1] = 'w';
if ((n & 4) != 0) ss[0] = 'r';
}
}
String sperms = new String(ss);
return PosixFilePermissions.fromString(sperms);
}


public static int posixToOctalMode(Set<PosixFilePermission> filePermissions) {
int mode = 0100000;
mode += 0100 * _posixToOctalMode(
Expand Down Expand Up @@ -110,7 +139,9 @@ private static int _toOctalMode(String strMode) {
* @return
*/
public static String toStringMode(int octalMode) {
print.ds("octalModeINT = " + octalMode);
String str = Integer.toString(octalMode, 8);
print.ds("octalModeSTR = " + str);
while (str.length() < 3)
str = "0" + str;
return str;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ public void setMode(int mode) throws IOException {
provider.lock(this);

Path path = FileSystems.getDefault().getPath(getPath());
Files.setPosixFilePermissions(path, PosixFilePermissions.fromString(ModeUtil.toStringMode(mode)));
Files.setPosixFilePermissions(path, ModeUtil.fromInt(mode));

provider.unlock(this);
}
Expand Down

0 comments on commit f302df0

Please sign in to comment.