jaulib v1.3.0
Jau Support Library (C++, Java, ..)
FMode.java
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2022 Gothel Software e.K.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24package org.jau.fs;
25
26/**
27 * Generic file type and POSIX protection mode bits as used in file_stats, touch(), mkdir() etc.
28 *
29 * The POSIX protection mode bits reside in the lower 16-bits and are bit-wise POSIX compliant
30 * while the file type bits reside in the upper 16-bits and are platform agnostic.
31 *
32 * This `enum class` type fulfills `C++ named requirements: BitmaskType`.
33 *
34 * @see FileStats
35 * @see FileStats#mode()
36 */
37public class FMode {
38 public enum Bit {
39 /** No mode bit set */
40 none ( 0 ),
41
42 /** Protection bit: POSIX S_ISUID */
43 set_uid ( 04000 ),
44 /** Protection bit: POSIX S_ISGID */
45 set_gid ( 02000 ),
46 /** Protection bit: POSIX S_ISVTX */
47 sticky ( 01000 ),
48 /** Protection bit: POSIX S_ISUID | S_ISGID | S_ISVTX */
49 ugs_set ( 07000 ),
50
51 /** Protection bit: POSIX S_IRUSR */
52 read_usr ( 00400 ),
53 /** Protection bit: POSIX S_IWUSR */
54 write_usr ( 00200 ),
55 /** Protection bit: POSIX S_IXUSR */
56 exec_usr ( 00100 ),
57 /** Protection bit: POSIX S_IRWXU */
58 rwx_usr ( 00700 ),
59
60 /** Protection bit: POSIX S_IRGRP */
61 read_grp ( 00040 ),
62 /** Protection bit: POSIX S_IWGRP */
63 write_grp ( 00020 ),
64 /** Protection bit: POSIX S_IXGRP */
65 exec_grp ( 00010 ),
66 /** Protection bit: POSIX S_IRWXG */
67 rwx_grp ( 00070 ),
68
69 /** Protection bit: POSIX S_IROTH */
70 read_oth ( 00004 ),
71 /** Protection bit: POSIX S_IWOTH */
72 write_oth ( 00002 ),
73 /** Protection bit: POSIX S_IXOTH */
74 exec_oth ( 00001 ),
75 /** Protection bit: POSIX S_IRWXO */
76 rwx_oth ( 00007 ),
77
78 /** Protection bit: POSIX S_IRWXU | S_IRWXG | S_IRWXO or rwx_usr | rwx_grp | rwx_oth */
79 rwx_all ( 00777 ),
80
81 /** Default directory protection bit: Safe default: POSIX S_IRWXU | S_IRGRP | S_IXGRP or rwx_usr | read_grp | exec_grp */
82 def_dir_prot ( 00750 ),
83
84 /** Default file protection bit: Safe default: POSIX S_IRUSR | S_IWUSR | S_IRGRP or read_usr | write_usr | read_grp */
85 def_file_prot ( 00640 ),
86
87 /** 12 bit protection bit mask 07777 for rwx_all | set_uid | set_gid | sticky . */
88 protection_mask ( 0b00000000000000000000111111111111 ),
89
90 /** Type: Entity is a socket, might be in combination with link. */
91 sock ( 0b00000000000000000001000000000000 ),
92 /** Type: Entity is a block device ), might be in combination with link. */
93 blk ( 0b00000000000000000010000000000000 ),
94 /** Type: Entity is a character device ), might be in combination with link. */
95 chr ( 0b00000000000000000100000000000000 ),
96 /** Type: Entity is a fifo/pipe ), might be in combination with link. */
97 fifo ( 0b00000000000000001000000000000000 ),
98 /** Type: Entity is a directory ), might be in combination with link. */
99 dir ( 0b00000000000000010000000000000000 ),
100 /** Type: Entity is a file ), might be in combination with link. */
101 file ( 0b00000000000000100000000000000000 ),
102 /** Type: Entity is a symbolic link ), might be in combination with file or dir ), fifo ), chr ), blk or sock. */
103 link ( 0b00000000000001000000000000000000 ),
104 /** Type: Entity gives no access to user ), exclusive bit. */
105 no_access ( 0b00100000000000000000000000000000 ),
106 /** Type: Entity does not exist ), exclusive bit. */
107 not_existing ( 0b01000000000000000000000000000000 ),
108 /** Type mask for sock | blk | chr | fifo | dir | file | link | no_access | not_existing. */
109 type_mask ( 0b01100000000001111111000000000000 );
110
111 Bit(final int v) { value = v; }
112 public final int value;
113 }
114 public int mask;
115
116 public FMode(final int v) {
117 mask = v;
118 }
119 public FMode() {
120 mask = 0;
121 }
122
123 public boolean isSet(final Bit bit) { return bit.value == ( mask & bit.value ); }
124
125 /**
126 * Sets the given bit and returns this instance for chaining.
127 * @param bit the given Bit value to set
128 * @return this instance for chaining.
129 */
130 public FMode set(final Bit bit) { mask = (short) ( mask | bit.value ); return this; }
131
132 public FMode mask(final int bits) {
133 final int r = mask & bits;
134 if( r == mask ) { return this; }
135 else { return new FMode(r); }
136 }
137
138 private static native String to_string(final int mask, final boolean show_rwx);
139
140 @Override
141 public String toString() {
142 return to_string(mask, false);
143 }
144 public String toString(final boolean show_rwx) {
145 return to_string(mask, show_rwx);
146 }
147
148 @Override
149 public boolean equals(final Object other) {
150 if (this == other) {
151 return true;
152 }
153 return (other instanceof FMode) &&
154 this.mask == ((FMode)other).mask;
155 }
156
157 /** Default directory protection bit: Safe default: POSIX S_IRWXU | S_IRGRP | S_IXGRP or rwx_usr | read_grp | exec_grp */
158 public static final FMode def_dir = new FMode(FMode.Bit.def_dir_prot.value);
159
160 /** Default file protection bit: Safe default: POSIX S_IRUSR | S_IWUSR | S_IRGRP or read_usr | write_usr | read_grp */
161 public static final FMode def_file = new FMode(FMode.Bit.def_file_prot.value);
162}
Generic file type and POSIX protection mode bits as used in file_stats, touch(), mkdir() etc.
Definition: FMode.java:37
static final FMode def_file
Default file protection bit: Safe default: POSIX S_IRUSR | S_IWUSR | S_IRGRP or read_usr | write_usr ...
Definition: FMode.java:161
boolean isSet(final Bit bit)
Definition: FMode.java:123
FMode mask(final int bits)
Definition: FMode.java:132
String toString(final boolean show_rwx)
Definition: FMode.java:144
FMode(final int v)
Definition: FMode.java:116
boolean equals(final Object other)
Definition: FMode.java:149
static final FMode def_dir
Default directory protection bit: Safe default: POSIX S_IRWXU | S_IRGRP | S_IXGRP or rwx_usr | read_g...
Definition: FMode.java:158
String toString()
Definition: FMode.java:141
file
Type: Entity is a file ), might be in combination with link.
Definition: FMode.java:101
write_grp
Protection bit: POSIX S_IWGRP.
Definition: FMode.java:63
rwx_oth
Protection bit: POSIX S_IRWXO.
Definition: FMode.java:76
final int value
Definition: FMode.java:112
protection_mask
12 bit protection bit mask 07777 for rwx_all | set_uid | set_gid | sticky .
Definition: FMode.java:88
sticky
Protection bit: POSIX S_ISVTX.
Definition: FMode.java:47
dir
Type: Entity is a directory ), might be in combination with link.
Definition: FMode.java:99
not_existing
Type: Entity does not exist ), exclusive bit.
Definition: FMode.java:107
read_usr
Protection bit: POSIX S_IRUSR.
Definition: FMode.java:52
write_oth
Protection bit: POSIX S_IWOTH.
Definition: FMode.java:72
fifo
Type: Entity is a fifo/pipe ), might be in combination with link.
Definition: FMode.java:97
def_dir_prot
Default directory protection bit: Safe default: POSIX S_IRWXU | S_IRGRP | S_IXGRP or rwx_usr | read_g...
Definition: FMode.java:82
blk
Type: Entity is a block device ), might be in combination with link.
Definition: FMode.java:93
rwx_grp
Protection bit: POSIX S_IRWXG.
Definition: FMode.java:67
type_mask
Type mask for sock | blk | chr | fifo | dir | file | link | no_access | not_existing.
Definition: FMode.java:109
link
Type: Entity is a symbolic link ), might be in combination with file or dir ), fifo ),...
Definition: FMode.java:103
rwx_all
Protection bit: POSIX S_IRWXU | S_IRWXG | S_IRWXO or rwx_usr | rwx_grp | rwx_oth.
Definition: FMode.java:79
exec_grp
Protection bit: POSIX S_IXGRP.
Definition: FMode.java:65
def_file_prot
Default file protection bit: Safe default: POSIX S_IRUSR | S_IWUSR | S_IRGRP or read_usr | write_usr ...
Definition: FMode.java:85
read_grp
Protection bit: POSIX S_IRGRP.
Definition: FMode.java:61
chr
Type: Entity is a character device ), might be in combination with link.
Definition: FMode.java:95
read_oth
Protection bit: POSIX S_IROTH.
Definition: FMode.java:70
exec_usr
Protection bit: POSIX S_IXUSR.
Definition: FMode.java:56
ugs_set
Protection bit: POSIX S_ISUID | S_ISGID | S_ISVTX.
Definition: FMode.java:49
rwx_usr
Protection bit: POSIX S_IRWXU.
Definition: FMode.java:58
exec_oth
Protection bit: POSIX S_IXOTH.
Definition: FMode.java:74
none
No mode bit set.
Definition: FMode.java:40
Bit(final int v)
Definition: FMode.java:111
no_access
Type: Entity gives no access to user ), exclusive bit.
Definition: FMode.java:105
set_uid
Protection bit: POSIX S_ISUID.
Definition: FMode.java:43
sock
Type: Entity is a socket, might be in combination with link.
Definition: FMode.java:91
set_gid
Protection bit: POSIX S_ISGID.
Definition: FMode.java:45
write_usr
Protection bit: POSIX S_IWUSR.
Definition: FMode.java:54