1 | /*
|
---|
2 | * Copyright 2001-2002,2004-2005 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 |
|
---|
18 | package org.apache.tools.zip;
|
---|
19 |
|
---|
20 | import java.util.zip.CRC32;
|
---|
21 | import java.util.zip.ZipException;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Adds Unix file permission and UID/GID fields as well as symbolic
|
---|
25 | * link handling.
|
---|
26 | *
|
---|
27 | * <p>This class uses the ASi extra field in the format:
|
---|
28 | * <pre>
|
---|
29 | * Value Size Description
|
---|
30 | * ----- ---- -----------
|
---|
31 | * (Unix3) 0x756e Short tag for this extra block type
|
---|
32 | * TSize Short total data size for this block
|
---|
33 | * CRC Long CRC-32 of the remaining data
|
---|
34 | * Mode Short file permissions
|
---|
35 | * SizDev Long symlink'd size OR major/minor dev num
|
---|
36 | * UID Short user ID
|
---|
37 | * GID Short group ID
|
---|
38 | * (var.) variable symbolic link filename
|
---|
39 | * </pre>
|
---|
40 | * taken from appnote.iz (Info-ZIP note, 981119) found at <a
|
---|
41 | * href="ftp://ftp.uu.net/pub/archiving/zip/doc/">ftp://ftp.uu.net/pub/archiving/zip/doc/</a></p>
|
---|
42 |
|
---|
43 | *
|
---|
44 | * <p>Short is two bytes and Long is four bytes in big endian byte and
|
---|
45 | * word order, device numbers are currently not supported.</p>
|
---|
46 | *
|
---|
47 | */
|
---|
48 | public class AsiExtraField implements ZipExtraField, UnixStat, Cloneable {
|
---|
49 |
|
---|
50 | private static final ZipShort HEADER_ID = new ZipShort(0x756E);
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Standard Unix stat(2) file mode.
|
---|
54 | *
|
---|
55 | * @since 1.1
|
---|
56 | */
|
---|
57 | private int mode = 0;
|
---|
58 | /**
|
---|
59 | * User ID.
|
---|
60 | *
|
---|
61 | * @since 1.1
|
---|
62 | */
|
---|
63 | private int uid = 0;
|
---|
64 | /**
|
---|
65 | * Group ID.
|
---|
66 | *
|
---|
67 | * @since 1.1
|
---|
68 | */
|
---|
69 | private int gid = 0;
|
---|
70 | /**
|
---|
71 | * File this entry points to, if it is a symbolic link.
|
---|
72 | *
|
---|
73 | * <p>empty string - if entry is not a symbolic link.</p>
|
---|
74 | *
|
---|
75 | * @since 1.1
|
---|
76 | */
|
---|
77 | private String link = "";
|
---|
78 | /**
|
---|
79 | * Is this an entry for a directory?
|
---|
80 | *
|
---|
81 | * @since 1.1
|
---|
82 | */
|
---|
83 | private boolean dirFlag = false;
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Instance used to calculate checksums.
|
---|
87 | *
|
---|
88 | * @since 1.1
|
---|
89 | */
|
---|
90 | private CRC32 crc = new CRC32();
|
---|
91 |
|
---|
92 | public AsiExtraField() {
|
---|
93 | }
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * The Header-ID.
|
---|
97 | *
|
---|
98 | * @since 1.1
|
---|
99 | */
|
---|
100 | public ZipShort getHeaderId() {
|
---|
101 | return HEADER_ID;
|
---|
102 | }
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * Length of the extra field in the local file data - without
|
---|
106 | * Header-ID or length specifier.
|
---|
107 | *
|
---|
108 | * @since 1.1
|
---|
109 | */
|
---|
110 | public ZipShort getLocalFileDataLength() {
|
---|
111 | return new ZipShort(4 // CRC
|
---|
112 | + 2 // Mode
|
---|
113 | + 4 // SizDev
|
---|
114 | + 2 // UID
|
---|
115 | + 2 // GID
|
---|
116 | + getLinkedFile().getBytes().length);
|
---|
117 | }
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Delegate to local file data.
|
---|
121 | *
|
---|
122 | * @since 1.1
|
---|
123 | */
|
---|
124 | public ZipShort getCentralDirectoryLength() {
|
---|
125 | return getLocalFileDataLength();
|
---|
126 | }
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * The actual data to put into local file data - without Header-ID
|
---|
130 | * or length specifier.
|
---|
131 | *
|
---|
132 | * @since 1.1
|
---|
133 | */
|
---|
134 | public byte[] getLocalFileDataData() {
|
---|
135 | // CRC will be added later
|
---|
136 | byte[] data = new byte[getLocalFileDataLength().getValue() - 4];
|
---|
137 | System.arraycopy((new ZipShort(getMode())).getBytes(), 0, data, 0, 2);
|
---|
138 |
|
---|
139 | byte[] linkArray = getLinkedFile().getBytes();
|
---|
140 | System.arraycopy((new ZipLong(linkArray.length)).getBytes(),
|
---|
141 | 0, data, 2, 4);
|
---|
142 |
|
---|
143 | System.arraycopy((new ZipShort(getUserId())).getBytes(),
|
---|
144 | 0, data, 6, 2);
|
---|
145 | System.arraycopy((new ZipShort(getGroupId())).getBytes(),
|
---|
146 | 0, data, 8, 2);
|
---|
147 |
|
---|
148 | System.arraycopy(linkArray, 0, data, 10, linkArray.length);
|
---|
149 |
|
---|
150 | crc.reset();
|
---|
151 | crc.update(data);
|
---|
152 | long checksum = crc.getValue();
|
---|
153 |
|
---|
154 | byte[] result = new byte[data.length + 4];
|
---|
155 | System.arraycopy((new ZipLong(checksum)).getBytes(), 0, result, 0, 4);
|
---|
156 | System.arraycopy(data, 0, result, 4, data.length);
|
---|
157 | return result;
|
---|
158 | }
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * Delegate to local file data.
|
---|
162 | *
|
---|
163 | * @since 1.1
|
---|
164 | */
|
---|
165 | public byte[] getCentralDirectoryData() {
|
---|
166 | return getLocalFileDataData();
|
---|
167 | }
|
---|
168 |
|
---|
169 | /**
|
---|
170 | * Set the user id.
|
---|
171 | *
|
---|
172 | * @since 1.1
|
---|
173 | */
|
---|
174 | public void setUserId(int uid) {
|
---|
175 | this.uid = uid;
|
---|
176 | }
|
---|
177 |
|
---|
178 | /**
|
---|
179 | * Get the user id.
|
---|
180 | *
|
---|
181 | * @since 1.1
|
---|
182 | */
|
---|
183 | public int getUserId() {
|
---|
184 | return uid;
|
---|
185 | }
|
---|
186 |
|
---|
187 | /**
|
---|
188 | * Set the group id.
|
---|
189 | *
|
---|
190 | * @since 1.1
|
---|
191 | */
|
---|
192 | public void setGroupId(int gid) {
|
---|
193 | this.gid = gid;
|
---|
194 | }
|
---|
195 |
|
---|
196 | /**
|
---|
197 | * Get the group id.
|
---|
198 | *
|
---|
199 | * @since 1.1
|
---|
200 | */
|
---|
201 | public int getGroupId() {
|
---|
202 | return gid;
|
---|
203 | }
|
---|
204 |
|
---|
205 | /**
|
---|
206 | * Indicate that this entry is a symbolic link to the given filename.
|
---|
207 | *
|
---|
208 | * @param name Name of the file this entry links to, empty String
|
---|
209 | * if it is not a symbolic link.
|
---|
210 | *
|
---|
211 | * @since 1.1
|
---|
212 | */
|
---|
213 | public void setLinkedFile(String name) {
|
---|
214 | link = name;
|
---|
215 | mode = getMode(mode);
|
---|
216 | }
|
---|
217 |
|
---|
218 | /**
|
---|
219 | * Name of linked file
|
---|
220 | *
|
---|
221 | * @return name of the file this entry links to if it is a
|
---|
222 | * symbolic link, the empty string otherwise.
|
---|
223 | *
|
---|
224 | * @since 1.1
|
---|
225 | */
|
---|
226 | public String getLinkedFile() {
|
---|
227 | return link;
|
---|
228 | }
|
---|
229 |
|
---|
230 | /**
|
---|
231 | * Is this entry a symbolic link?
|
---|
232 | *
|
---|
233 | * @since 1.1
|
---|
234 | */
|
---|
235 | public boolean isLink() {
|
---|
236 | return getLinkedFile().length() != 0;
|
---|
237 | }
|
---|
238 |
|
---|
239 | /**
|
---|
240 | * File mode of this file.
|
---|
241 | *
|
---|
242 | * @since 1.1
|
---|
243 | */
|
---|
244 | public void setMode(int mode) {
|
---|
245 | this.mode = getMode(mode);
|
---|
246 | }
|
---|
247 |
|
---|
248 | /**
|
---|
249 | * File mode of this file.
|
---|
250 | *
|
---|
251 | * @since 1.1
|
---|
252 | */
|
---|
253 | public int getMode() {
|
---|
254 | return mode;
|
---|
255 | }
|
---|
256 |
|
---|
257 | /**
|
---|
258 | * Indicate whether this entry is a directory.
|
---|
259 | *
|
---|
260 | * @since 1.1
|
---|
261 | */
|
---|
262 | public void setDirectory(boolean dirFlag) {
|
---|
263 | this.dirFlag = dirFlag;
|
---|
264 | mode = getMode(mode);
|
---|
265 | }
|
---|
266 |
|
---|
267 | /**
|
---|
268 | * Is this entry a directory?
|
---|
269 | *
|
---|
270 | * @since 1.1
|
---|
271 | */
|
---|
272 | public boolean isDirectory() {
|
---|
273 | return dirFlag && !isLink();
|
---|
274 | }
|
---|
275 |
|
---|
276 | /**
|
---|
277 | * Populate data from this array as if it was in local file data.
|
---|
278 | *
|
---|
279 | * @since 1.1
|
---|
280 | */
|
---|
281 | public void parseFromLocalFileData(byte[] data, int offset, int length)
|
---|
282 | throws ZipException {
|
---|
283 |
|
---|
284 | long givenChecksum = (new ZipLong(data, offset)).getValue();
|
---|
285 | byte[] tmp = new byte[length - 4];
|
---|
286 | System.arraycopy(data, offset + 4, tmp, 0, length - 4);
|
---|
287 | crc.reset();
|
---|
288 | crc.update(tmp);
|
---|
289 | long realChecksum = crc.getValue();
|
---|
290 | if (givenChecksum != realChecksum) {
|
---|
291 | throw new ZipException("bad CRC checksum "
|
---|
292 | + Long.toHexString(givenChecksum)
|
---|
293 | + " instead of "
|
---|
294 | + Long.toHexString(realChecksum));
|
---|
295 | }
|
---|
296 |
|
---|
297 | int newMode = (new ZipShort(tmp, 0)).getValue();
|
---|
298 | byte[] linkArray = new byte[(int) (new ZipLong(tmp, 2)).getValue()];
|
---|
299 | uid = (new ZipShort(tmp, 6)).getValue();
|
---|
300 | gid = (new ZipShort(tmp, 8)).getValue();
|
---|
301 |
|
---|
302 | if (linkArray.length == 0) {
|
---|
303 | link = "";
|
---|
304 | } else {
|
---|
305 | System.arraycopy(tmp, 10, linkArray, 0, linkArray.length);
|
---|
306 | link = new String(linkArray);
|
---|
307 | }
|
---|
308 | setDirectory((newMode & DIR_FLAG) != 0);
|
---|
309 | setMode(newMode);
|
---|
310 | }
|
---|
311 |
|
---|
312 | /**
|
---|
313 | * Get the file mode for given permissions with the correct file type.
|
---|
314 | *
|
---|
315 | * @since 1.1
|
---|
316 | */
|
---|
317 | protected int getMode(int mode) {
|
---|
318 | int type = FILE_FLAG;
|
---|
319 | if (isLink()) {
|
---|
320 | type = LINK_FLAG;
|
---|
321 | } else if (isDirectory()) {
|
---|
322 | type = DIR_FLAG;
|
---|
323 | }
|
---|
324 | return type | (mode & PERM_MASK);
|
---|
325 | }
|
---|
326 |
|
---|
327 | }
|
---|