View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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              // discard sanity assessment here; we're pushing ahead.
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      // the boolean return type is for people who want to make a decision based on the sanity
79      // assessment.
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          // read-access checks.
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         // end read-access checks.
103 
104         // write-access checks.
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         // end write-access checks.
120 
121         // execute-/list-access checks.
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         // end execute-/list-access checks.
137 
138         if (warn && logger != null) {
139             logger.warn(messages.toString());
140         }
141 
142         return !warn;
143     }
144 }