blob: a2dc1c3b344ceb1a782a0a5b9efdee1d8e51a92a [file] [log] [blame]
Torsten Curdtca165392008-07-10 10:17:44 +00001/*
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 */
19package org.apache.commons.compress.archivers.zip;
20
21import java.util.zip.CRC32;
22import java.util.zip.ZipException;
23
24/**
Torsten Curdtab9ebfc2009-01-12 11:15:34 +000025 * Adds Unix file permission and UID/GID fields as well as symbolic
26 * link handling.
Torsten Curdtca165392008-07-10 10:17:44 +000027 *
Stefan Bodewig45e51c22013-12-22 07:03:43 +000028 * <p>This class uses the ASi extra field in the format:</p>
Torsten Curdtab9ebfc2009-01-12 11:15:34 +000029 * <pre>
Torsten Curdtca165392008-07-10 10:17:44 +000030 * Value Size Description
31 * ----- ---- -----------
32 * (Unix3) 0x756e Short tag for this extra block type
33 * TSize Short total data size for this block
34 * CRC Long CRC-32 of the remaining data
35 * Mode Short file permissions
36 * SizDev Long symlink'd size OR major/minor dev num
37 * UID Short user ID
38 * GID Short group ID
39 * (var.) variable symbolic link filename
Torsten Curdtab9ebfc2009-01-12 11:15:34 +000040 * </pre>
Stefan Bodewig45e51c22013-12-22 07:03:43 +000041 * <p>taken from appnote.iz (Info-ZIP note, 981119) found at <a
Torsten Curdtab9ebfc2009-01-12 11:15:34 +000042 * href="ftp://ftp.uu.net/pub/archiving/zip/doc/">ftp://ftp.uu.net/pub/archiving/zip/doc/</a></p>
Torsten Curdtab9ebfc2009-01-12 11:15:34 +000043 *
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>
Sebastian Bazley99870ef2009-03-28 00:04:36 +000046 * @NotThreadSafe
Stefan Bodewig673fd9b2009-04-15 09:38:47 +000047 *
48 * <p>Since the documentation this class is based upon doesn't mention
49 * the character encoding of the file name at all, it is assumed that
50 * it uses the current platform's default encoding.</p>
Torsten Curdtab9ebfc2009-01-12 11:15:34 +000051 */
52public class AsiExtraField implements ZipExtraField, UnixStat, Cloneable {
53
54 private static final ZipShort HEADER_ID = new ZipShort(0x756E);
55 private static final int WORD = 4;
Torsten Curdtca165392008-07-10 10:17:44 +000056 /**
57 * Standard Unix stat(2) file mode.
Torsten Curdtca165392008-07-10 10:17:44 +000058 */
Torsten Curdtab9ebfc2009-01-12 11:15:34 +000059 private int mode = 0;
Torsten Curdtca165392008-07-10 10:17:44 +000060 /**
61 * User ID.
Torsten Curdtca165392008-07-10 10:17:44 +000062 */
Torsten Curdtab9ebfc2009-01-12 11:15:34 +000063 private int uid = 0;
Torsten Curdtca165392008-07-10 10:17:44 +000064 /**
65 * Group ID.
Torsten Curdtca165392008-07-10 10:17:44 +000066 */
Torsten Curdtab9ebfc2009-01-12 11:15:34 +000067 private int gid = 0;
Torsten Curdtca165392008-07-10 10:17:44 +000068 /**
Torsten Curdtab9ebfc2009-01-12 11:15:34 +000069 * File this entry points to, if it is a symbolic link.
Torsten Curdtca165392008-07-10 10:17:44 +000070 *
Torsten Curdtab9ebfc2009-01-12 11:15:34 +000071 * <p>empty string - if entry is not a symbolic link.</p>
Torsten Curdtca165392008-07-10 10:17:44 +000072 */
Torsten Curdtab9ebfc2009-01-12 11:15:34 +000073 private String link = "";
Torsten Curdtca165392008-07-10 10:17:44 +000074 /**
75 * Is this an entry for a directory?
Torsten Curdtca165392008-07-10 10:17:44 +000076 */
Torsten Curdtab9ebfc2009-01-12 11:15:34 +000077 private boolean dirFlag = false;
Torsten Curdtca165392008-07-10 10:17:44 +000078
79 /**
80 * Instance used to calculate checksums.
Torsten Curdtca165392008-07-10 10:17:44 +000081 */
Torsten Curdtab9ebfc2009-01-12 11:15:34 +000082 private CRC32 crc = new CRC32();
83
84 /** Constructor for AsiExtraField. */
85 public AsiExtraField() {
86 }
Torsten Curdtca165392008-07-10 10:17:44 +000087
88 /**
Torsten Curdtab9ebfc2009-01-12 11:15:34 +000089 * The Header-ID.
90 * @return the value for the header id for this extrafield
Torsten Curdtca165392008-07-10 10:17:44 +000091 */
Torsten Curdtab9ebfc2009-01-12 11:15:34 +000092 public ZipShort getHeaderId() {
93 return HEADER_ID;
94 }
95
96 /**
97 * Length of the extra field in the local file data - without
98 * Header-ID or length specifier.
99 * @return a <code>ZipShort</code> for the length of the data of this extra field
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000100 */
101 public ZipShort getLocalFileDataLength() {
102 return new ZipShort(WORD // CRC
103 + 2 // Mode
104 + WORD // SizDev
105 + 2 // UID
106 + 2 // GID
Stefan Bodewig673fd9b2009-04-15 09:38:47 +0000107 + getLinkedFile().getBytes().length);
Sebastian Bazley325f0022009-04-15 11:23:48 +0000108 // Uses default charset - see class Javadoc
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000109 }
110
111 /**
112 * Delegate to local file data.
113 * @return the centralDirectory length
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000114 */
115 public ZipShort getCentralDirectoryLength() {
116 return getLocalFileDataLength();
117 }
118
119 /**
120 * The actual data to put into local file data - without Header-ID
121 * or length specifier.
122 * @return get the data
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000123 */
124 public byte[] getLocalFileDataData() {
125 // CRC will be added later
126 byte[] data = new byte[getLocalFileDataLength().getValue() - WORD];
127 System.arraycopy(ZipShort.getBytes(getMode()), 0, data, 0, 2);
128
Sebastian Bazley325f0022009-04-15 11:23:48 +0000129 byte[] linkArray = getLinkedFile().getBytes(); // Uses default charset - see class Javadoc
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000130 // CheckStyle:MagicNumber OFF
131 System.arraycopy(ZipLong.getBytes(linkArray.length),
132 0, data, 2, WORD);
133
134 System.arraycopy(ZipShort.getBytes(getUserId()),
135 0, data, 6, 2);
136 System.arraycopy(ZipShort.getBytes(getGroupId()),
137 0, data, 8, 2);
138
139 System.arraycopy(linkArray, 0, data, 10, linkArray.length);
140 // CheckStyle:MagicNumber ON
141
142 crc.reset();
143 crc.update(data);
144 long checksum = crc.getValue();
145
146 byte[] result = new byte[data.length + WORD];
147 System.arraycopy(ZipLong.getBytes(checksum), 0, result, 0, WORD);
148 System.arraycopy(data, 0, result, WORD, data.length);
149 return result;
150 }
151
152 /**
153 * Delegate to local file data.
154 * @return the local file data
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000155 */
156 public byte[] getCentralDirectoryData() {
157 return getLocalFileDataData();
158 }
159
160 /**
161 * Set the user id.
162 * @param uid the user id
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000163 */
164 public void setUserId(int uid) {
165 this.uid = uid;
166 }
167
168 /**
169 * Get the user id.
170 * @return the user id
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000171 */
172 public int getUserId() {
173 return uid;
Torsten Curdtca165392008-07-10 10:17:44 +0000174 }
175
176 /**
177 * Set the group id.
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000178 * @param gid the group id
Torsten Curdtca165392008-07-10 10:17:44 +0000179 */
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000180 public void setGroupId(int gid) {
181 this.gid = gid;
182 }
183
184 /**
185 * Get the group id.
186 * @return the group id
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000187 */
188 public int getGroupId() {
189 return gid;
Torsten Curdtca165392008-07-10 10:17:44 +0000190 }
191
192 /**
193 * Indicate that this entry is a symbolic link to the given filename.
194 *
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000195 * @param name Name of the file this entry links to, empty String
196 * if it is not a symbolic link.
Torsten Curdtca165392008-07-10 10:17:44 +0000197 */
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000198 public void setLinkedFile(String name) {
199 link = name;
200 mode = getMode(mode);
Torsten Curdtca165392008-07-10 10:17:44 +0000201 }
202
203 /**
204 * Name of linked file
205 *
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000206 * @return name of the file this entry links to if it is a
207 * symbolic link, the empty string otherwise.
Torsten Curdtca165392008-07-10 10:17:44 +0000208 */
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000209 public String getLinkedFile() {
210 return link;
Torsten Curdtca165392008-07-10 10:17:44 +0000211 }
212
213 /**
214 * Is this entry a symbolic link?
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000215 * @return true if this is a symbolic link
Torsten Curdtca165392008-07-10 10:17:44 +0000216 */
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000217 public boolean isLink() {
Torsten Curdtca165392008-07-10 10:17:44 +0000218 return getLinkedFile().length() != 0;
219 }
220
221 /**
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000222 * File mode of this file.
223 * @param mode the file mode
Torsten Curdtca165392008-07-10 10:17:44 +0000224 */
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000225 public void setMode(int mode) {
226 this.mode = getMode(mode);
227 }
Torsten Curdtca165392008-07-10 10:17:44 +0000228
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000229 /**
230 * File mode of this file.
231 * @return the file mode
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000232 */
233 public int getMode() {
234 return mode;
235 }
236
237 /**
238 * Indicate whether this entry is a directory.
239 * @param dirFlag if true, this entry is a directory
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000240 */
241 public void setDirectory(boolean dirFlag) {
242 this.dirFlag = dirFlag;
243 mode = getMode(mode);
244 }
245
246 /**
247 * Is this entry a directory?
248 * @return true if this entry is a directory
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000249 */
250 public boolean isDirectory() {
251 return dirFlag && !isLink();
252 }
253
254 /**
255 * Populate data from this array as if it was in local file data.
256 * @param data an array of bytes
257 * @param offset the start offset
258 * @param length the number of bytes in the array from offset
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000259 * @throws ZipException on error
260 */
261 public void parseFromLocalFileData(byte[] data, int offset, int length)
262 throws ZipException {
263
264 long givenChecksum = ZipLong.getValue(data, offset);
265 byte[] tmp = new byte[length - WORD];
266 System.arraycopy(data, offset + WORD, tmp, 0, length - WORD);
267 crc.reset();
268 crc.update(tmp);
269 long realChecksum = crc.getValue();
270 if (givenChecksum != realChecksum) {
271 throw new ZipException("bad CRC checksum "
272 + Long.toHexString(givenChecksum)
273 + " instead of "
274 + Long.toHexString(realChecksum));
Torsten Curdtca165392008-07-10 10:17:44 +0000275 }
276
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000277 int newMode = ZipShort.getValue(tmp, 0);
278 // CheckStyle:MagicNumber OFF
279 byte[] linkArray = new byte[(int) ZipLong.getValue(tmp, 2)];
280 uid = ZipShort.getValue(tmp, 6);
281 gid = ZipShort.getValue(tmp, 8);
Torsten Curdtca165392008-07-10 10:17:44 +0000282
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000283 if (linkArray.length == 0) {
284 link = "";
285 } else {
286 System.arraycopy(tmp, 10, linkArray, 0, linkArray.length);
Sebastian Bazley325f0022009-04-15 11:23:48 +0000287 link = new String(linkArray); // Uses default charset - see class Javadoc
Torsten Curdtca165392008-07-10 10:17:44 +0000288 }
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000289 // CheckStyle:MagicNumber ON
290 setDirectory((newMode & DIR_FLAG) != 0);
291 setMode(newMode);
Torsten Curdtca165392008-07-10 10:17:44 +0000292 }
293
294 /**
Stefan Bodewigdfbdfee2009-03-03 12:42:28 +0000295 * Doesn't do anything special since this class always uses the
296 * same data in central directory and local file data.
297 */
298 public void parseFromCentralDirectoryData(byte[] buffer, int offset,
299 int length)
300 throws ZipException {
301 parseFromLocalFileData(buffer, offset, length);
302 }
303
304 /**
Torsten Curdtca165392008-07-10 10:17:44 +0000305 * Get the file mode for given permissions with the correct file type.
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000306 * @param mode the mode
307 * @return the type with the mode
Torsten Curdtca165392008-07-10 10:17:44 +0000308 */
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000309 protected int getMode(int mode) {
Torsten Curdtca165392008-07-10 10:17:44 +0000310 int type = FILE_FLAG;
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000311 if (isLink()) {
Torsten Curdtca165392008-07-10 10:17:44 +0000312 type = LINK_FLAG;
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000313 } else if (isDirectory()) {
Torsten Curdtca165392008-07-10 10:17:44 +0000314 type = DIR_FLAG;
315 }
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000316 return type | (mode & PERM_MASK);
Torsten Curdtca165392008-07-10 10:17:44 +0000317 }
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000318
Sebastian Bazleyfb06e3a2011-08-15 15:10:46 +0000319 @Override
Stefan Bodewig5e5804c2009-02-05 12:45:23 +0000320 public Object clone() {
321 try {
322 AsiExtraField cloned = (AsiExtraField) super.clone();
323 cloned.crc = new CRC32();
324 return cloned;
325 } catch (CloneNotSupportedException cnfe) {
326 // impossible
327 throw new RuntimeException(cnfe);
328 }
329 }
Torsten Curdtca165392008-07-10 10:17:44 +0000330}