-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
fjn
committed
Feb 19, 2021
1 parent
70dd7e7
commit 6a93fb6
Showing
2 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
118 changes: 118 additions & 0 deletions
118
langx-java/src/main/java/com/jn/langx/util/io/file/FilePermission.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package com.jn.langx.util.io.file; | ||
|
||
|
||
import java.util.HashSet; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
public enum FilePermission { | ||
|
||
/*** | ||
* user group other | ||
* RWX RWX RWX | ||
* 二进制 110 100 100 | ||
* | ||
* 以0开始的数字,代表了是 8进制的。 | ||
* 以 0x 开始的数字,代表是 16进制的 | ||
*/ | ||
|
||
/** | ||
* read permission, owner | ||
*/ | ||
USR_R(00400), | ||
/** | ||
* write permission, owner | ||
*/ | ||
USR_W(00200), | ||
/** | ||
* execute/search permission, owner | ||
*/ | ||
USR_X(00100), | ||
/** | ||
* read permission, group | ||
*/ | ||
GRP_R(00040), | ||
/** | ||
* write permission, group | ||
*/ | ||
GRP_W(00020), | ||
/** | ||
* execute/search permission, group | ||
*/ | ||
GRP_X(00010), | ||
/** | ||
* read permission, others | ||
*/ | ||
OTH_R(00004), | ||
/** | ||
* write permission, others | ||
*/ | ||
OTH_W(00002), | ||
/** | ||
* execute/search permission, group | ||
*/ | ||
OTH_X(00001), | ||
/** | ||
* set-user-ID on execution | ||
*/ | ||
SUID(04000), | ||
/** | ||
* set-group-ID on execution | ||
*/ | ||
SGID(02000), | ||
/** | ||
* on directories, restricted deletion flag | ||
*/ | ||
STICKY(01000), | ||
// Composite: | ||
/** | ||
* read, write, execute/search by user | ||
*/ | ||
USR_RWX(USR_R, USR_W, USR_X), | ||
/** | ||
* read, write, execute/search by group | ||
*/ | ||
GRP_RWX(GRP_R, GRP_W, GRP_X), | ||
/** | ||
* read, write, execute/search by other | ||
*/ | ||
OTH_RWX(OTH_R, OTH_W, OTH_X); | ||
|
||
private final int val; | ||
|
||
FilePermission(int val) { | ||
this.val = val; | ||
} | ||
|
||
FilePermission(FilePermission... perms) { | ||
int val = 0; | ||
for (FilePermission perm : perms) { | ||
val |= perm.val; | ||
} | ||
this.val = val; | ||
} | ||
|
||
public boolean isIn(int mask) { | ||
return (mask & val) == val; | ||
} | ||
|
||
public static Set<FilePermission> fromMask(int mask) { | ||
final List<FilePermission> perms = new LinkedList<FilePermission>(); | ||
for (FilePermission p : FilePermission.values()) { | ||
if (p.isIn(mask)) { | ||
perms.add(p); | ||
} | ||
} | ||
return new HashSet<FilePermission>(perms); | ||
} | ||
|
||
public static int toMask(Set<FilePermission> perms) { | ||
int mask = 0; | ||
for (FilePermission p : perms) { | ||
mask |= p.val; | ||
} | ||
return mask; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters