1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.assembly.utils;
20
21 import java.util.List;
22
23 import org.apache.maven.plugins.assembly.format.AssemblyFormattingException;
24 import org.slf4j.Logger;
25
26
27
28
29 public final class TypeConversionUtils {
30
31 private static final int U_R = 256;
32
33 private static final int U_W = 128;
34
35 private static final int U_X = 64;
36
37 private static final int G_R = 32;
38
39 private static final int G_W = 16;
40
41 private static final int G_X = 8;
42
43 private static final int W_R = 4;
44
45 private static final int W_W = 2;
46
47 private static final int W_X = 1;
48
49 private TypeConversionUtils() {}
50
51 public static String[] toStringArray(final List<String> list) {
52 String[] result = null;
53
54 if ((list != null) && !list.isEmpty()) {
55 result = list.toArray(new String[0]);
56 }
57
58 return result;
59 }
60
61 public static int modeToInt(final String mode, final Logger logger) throws AssemblyFormattingException {
62 if (mode == null || mode.trim().length() < 1) {
63 return -1;
64 }
65
66 try {
67 final int value = Integer.parseInt(mode, 8);
68
69
70 verifyModeSanity(value, logger);
71
72 return value;
73 } catch (final NumberFormatException e) {
74 throw new AssemblyFormattingException("Failed to parse mode as an octal number: \'" + mode + "\'.", e);
75 }
76 }
77
78
79
80 public static boolean verifyModeSanity(final int mode, final Logger logger) {
81 final StringBuilder messages = new StringBuilder();
82
83 messages.append("The mode: ").append(Integer.toString(mode, 8)).append(" contains nonsensical permissions:");
84
85 boolean warn = false;
86
87
88 if (((mode & U_R) == 0) && ((mode & G_R) == G_R)) {
89 messages.append("\n- Group has read access, but user does not.");
90 warn = true;
91 }
92
93 if (((mode & U_R) == 0) && ((mode & W_R) == W_R)) {
94 messages.append("\n- World has read access, but user does not.");
95 warn = true;
96 }
97
98 if (((mode & G_R) == 0) && ((mode & W_R) == W_R)) {
99 messages.append("\n- World has read access, but group does not.");
100 warn = true;
101 }
102
103
104
105 if (((mode & U_W) == 0) && ((mode & G_W) == G_W)) {
106 messages.append("\n- Group has write access, but user does not.");
107 warn = true;
108 }
109
110 if (((mode & U_W) == 0) && ((mode & W_W) == W_W)) {
111 messages.append("\n- World has write access, but user does not.");
112 warn = true;
113 }
114
115 if (((mode & G_W) == 0) && ((mode & W_W) == W_W)) {
116 messages.append("\n- World has write access, but group does not.");
117 warn = true;
118 }
119
120
121
122 if (((mode & U_X) == 0) && ((mode & G_X) == G_X)) {
123 messages.append("\n- Group has execute/list access, but user does not.");
124 warn = true;
125 }
126
127 if (((mode & U_X) == 0) && ((mode & W_X) == W_X)) {
128 messages.append("\n- World has execute/list access, but user does not.");
129 warn = true;
130 }
131
132 if (((mode & G_X) == 0) && ((mode & W_X) == W_X)) {
133 messages.append("\n- World has execute/list access, but group does not.");
134 warn = true;
135 }
136
137
138 if (warn && logger != null) {
139 logger.warn(messages.toString());
140 }
141
142 return !warn;
143 }
144 }