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