blob: 8aedec1d3fed94e6896109277d85a4e3c395e69b [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 *
Torsten Curdtab9ebfc2009-01-12 11:15:34 +000028 * <p>This class uses the ASi extra field in the format:
29 * <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>
41 * taken from appnote.iz (Info-ZIP note, 981119) found at <a
42 * href="ftp://ftp.uu.net/pub/archiving/zip/doc/">ftp://ftp.uu.net/pub/archiving/zip/doc/</a></p>
Torsten Curdtca165392008-07-10 10:17:44 +000043
Torsten Curdtab9ebfc2009-01-12 11:15:34 +000044 *
45 * <p>Short is two bytes and Long is four bytes in big endian byte and
46 * word order, device numbers are currently not supported.</p>
Sebastian Bazley99870ef2009-03-28 00:04:36 +000047 * @NotThreadSafe
Stefan Bodewig673fd9b2009-04-15 09:38:47 +000048 *
49 * <p>Since the documentation this class is based upon doesn't mention
50 * the character encoding of the file name at all, it is assumed that
51 * it uses the current platform's default encoding.</p>
Torsten Curdtab9ebfc2009-01-12 11:15:34 +000052 */
53public class AsiExtraField implements ZipExtraField, UnixStat, Cloneable {
54
55 private static final ZipShort HEADER_ID = new ZipShort(0x756E);
56 private static final int WORD = 4;
Torsten Curdtca165392008-07-10 10:17:44 +000057 /**
58 * Standard Unix stat(2) file mode.
Torsten Curdtca165392008-07-10 10:17:44 +000059 */
Torsten Curdtab9ebfc2009-01-12 11:15:34 +000060 private int mode = 0;
Torsten Curdtca165392008-07-10 10:17:44 +000061 /**
62 * User ID.
Torsten Curdtca165392008-07-10 10:17:44 +000063 */
Torsten Curdtab9ebfc2009-01-12 11:15:34 +000064 private int uid = 0;
Torsten Curdtca165392008-07-10 10:17:44 +000065 /**
66 * Group ID.
Torsten Curdtca165392008-07-10 10:17:44 +000067 */
Torsten Curdtab9ebfc2009-01-12 11:15:34 +000068 private int gid = 0;
Torsten Curdtca165392008-07-10 10:17:44 +000069 /**
Torsten Curdtab9ebfc2009-01-12 11:15:34 +000070 * File this entry points to, if it is a symbolic link.
Torsten Curdtca165392008-07-10 10:17:44 +000071 *
Torsten Curdtab9ebfc2009-01-12 11:15:34 +000072 * <p>empty string - if entry is not a symbolic link.</p>
Torsten Curdtca165392008-07-10 10:17:44 +000073 */
Torsten Curdtab9ebfc2009-01-12 11:15:34 +000074 private String link = "";
Torsten Curdtca165392008-07-10 10:17:44 +000075 /**
76 * Is this an entry for a directory?
Torsten Curdtca165392008-07-10 10:17:44 +000077 */
Torsten Curdtab9ebfc2009-01-12 11:15:34 +000078 private boolean dirFlag = false;
Torsten Curdtca165392008-07-10 10:17:44 +000079
80 /**
81 * Instance used to calculate checksums.
Torsten Curdtca165392008-07-10 10:17:44 +000082 */
Torsten Curdtab9ebfc2009-01-12 11:15:34 +000083 private CRC32 crc = new CRC32();
84
85 /** Constructor for AsiExtraField. */
86 public AsiExtraField() {
87 }
Torsten Curdtca165392008-07-10 10:17:44 +000088
89 /**
Torsten Curdtab9ebfc2009-01-12 11:15:34 +000090 * The Header-ID.
91 * @return the value for the header id for this extrafield
Torsten Curdtca165392008-07-10 10:17:44 +000092 */
Torsten Curdtab9ebfc2009-01-12 11:15:34 +000093 public ZipShort getHeaderId() {
94 return HEADER_ID;
95 }
96
97 /**
98 * Length of the extra field in the local file data - without
99 * Header-ID or length specifier.
100 * @return a <code>ZipShort</code> for the length of the data of this extra field
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000101 */
102 public ZipShort getLocalFileDataLength() {
103 return new ZipShort(WORD // CRC
104 + 2 // Mode
105 + WORD // SizDev
106 + 2 // UID
107 + 2 // GID
Stefan Bodewig673fd9b2009-04-15 09:38:47 +0000108 + getLinkedFile().getBytes().length);
Sebastian Bazley325f0022009-04-15 11:23:48 +0000109 // Uses default charset - see class Javadoc
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000110 }
111
112 /**
113 * Delegate to local file data.
114 * @return the centralDirectory length
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000115 */
116 public ZipShort getCentralDirectoryLength() {
117 return getLocalFileDataLength();
118 }
119
120 /**
121 * The actual data to put into local file data - without Header-ID
122 * or length specifier.
123 * @return get the data
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000124 */
125 public byte[] getLocalFileDataData() {
126 // CRC will be added later
127 byte[] data = new byte[getLocalFileDataLength().getValue() - WORD];
128 System.arraycopy(ZipShort.getBytes(getMode()), 0, data, 0, 2);
129
Sebastian Bazley325f0022009-04-15 11:23:48 +0000130 byte[] linkArray = getLinkedFile().getBytes(); // Uses default charset - see class Javadoc
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000131 // CheckStyle:MagicNumber OFF
132 System.arraycopy(ZipLong.getBytes(linkArray.length),
133 0, data, 2, WORD);
134
135 System.arraycopy(ZipShort.getBytes(getUserId()),
136 0, data, 6, 2);
137 System.arraycopy(ZipShort.getBytes(getGroupId()),
138 0, data, 8, 2);
139
140 System.arraycopy(linkArray, 0, data, 10, linkArray.length);
141 // CheckStyle:MagicNumber ON
142
143 crc.reset();
144 crc.update(data);
145 long checksum = crc.getValue();
146
147 byte[] result = new byte[data.length + WORD];
148 System.arraycopy(ZipLong.getBytes(checksum), 0, result, 0, WORD);
149 System.arraycopy(data, 0, result, WORD, data.length);
150 return result;
151 }
152
153 /**
154 * Delegate to local file data.
155 * @return the local file data
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000156 */
157 public byte[] getCentralDirectoryData() {
158 return getLocalFileDataData();
159 }
160
161 /**
162 * Set the user id.
163 * @param uid the user id
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000164 */
165 public void setUserId(int uid) {
166 this.uid = uid;
167 }
168
169 /**
170 * Get the user id.
171 * @return the user id
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000172 */
173 public int getUserId() {
174 return uid;
Torsten Curdtca165392008-07-10 10:17:44 +0000175 }
176
177 /**
178 * Set the group id.
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000179 * @param gid the group id
Torsten Curdtca165392008-07-10 10:17:44 +0000180 */
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000181 public void setGroupId(int gid) {
182 this.gid = gid;
183 }
184
185 /**
186 * Get the group id.
187 * @return the group id
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000188 */
189 public int getGroupId() {
190 return gid;
Torsten Curdtca165392008-07-10 10:17:44 +0000191 }
192
193 /**
194 * Indicate that this entry is a symbolic link to the given filename.
195 *
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000196 * @param name Name of the file this entry links to, empty String
197 * if it is not a symbolic link.
Torsten Curdtca165392008-07-10 10:17:44 +0000198 */
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000199 public void setLinkedFile(String name) {
200 link = name;
201 mode = getMode(mode);
Torsten Curdtca165392008-07-10 10:17:44 +0000202 }
203
204 /**
205 * Name of linked file
206 *
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000207 * @return name of the file this entry links to if it is a
208 * symbolic link, the empty string otherwise.
Torsten Curdtca165392008-07-10 10:17:44 +0000209 */
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000210 public String getLinkedFile() {
211 return link;
Torsten Curdtca165392008-07-10 10:17:44 +0000212 }
213
214 /**
215 * Is this entry a symbolic link?
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000216 * @return true if this is a symbolic link
Torsten Curdtca165392008-07-10 10:17:44 +0000217 */
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000218 public boolean isLink() {
Torsten Curdtca165392008-07-10 10:17:44 +0000219 return getLinkedFile().length() != 0;
220 }
221
222 /**
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000223 * File mode of this file.
224 * @param mode the file mode
Torsten Curdtca165392008-07-10 10:17:44 +0000225 */
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000226 public void setMode(int mode) {
227 this.mode = getMode(mode);
228 }
Torsten Curdtca165392008-07-10 10:17:44 +0000229
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000230 /**
231 * File mode of this file.
232 * @return the file mode
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000233 */
234 public int getMode() {
235 return mode;
236 }
237
238 /**
239 * Indicate whether this entry is a directory.
240 * @param dirFlag if true, this entry is a directory
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000241 */
242 public void setDirectory(boolean dirFlag) {
243 this.dirFlag = dirFlag;
244 mode = getMode(mode);
245 }
246
247 /**
248 * Is this entry a directory?
249 * @return true if this entry is a directory
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000250 */
251 public boolean isDirectory() {
252 return dirFlag && !isLink();
253 }
254
255 /**
256 * Populate data from this array as if it was in local file data.
257 * @param data an array of bytes
258 * @param offset the start offset
259 * @param length the number of bytes in the array from offset
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000260 * @throws ZipException on error
261 */
262 public void parseFromLocalFileData(byte[] data, int offset, int length)
263 throws ZipException {
264
265 long givenChecksum = ZipLong.getValue(data, offset);
266 byte[] tmp = new byte[length - WORD];
267 System.arraycopy(data, offset + WORD, tmp, 0, length - WORD);
268 crc.reset();
269 crc.update(tmp);
270 long realChecksum = crc.getValue();
271 if (givenChecksum != realChecksum) {
272 throw new ZipException("bad CRC checksum "
273 + Long.toHexString(givenChecksum)
274 + " instead of "
275 + Long.toHexString(realChecksum));
Torsten Curdtca165392008-07-10 10:17:44 +0000276 }
277
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000278 int newMode = ZipShort.getValue(tmp, 0);
279 // CheckStyle:MagicNumber OFF
280 byte[] linkArray = new byte[(int) ZipLong.getValue(tmp, 2)];
281 uid = ZipShort.getValue(tmp, 6);
282 gid = ZipShort.getValue(tmp, 8);
Torsten Curdtca165392008-07-10 10:17:44 +0000283
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000284 if (linkArray.length == 0) {
285 link = "";
286 } else {
287 System.arraycopy(tmp, 10, linkArray, 0, linkArray.length);
Sebastian Bazley325f0022009-04-15 11:23:48 +0000288 link = new String(linkArray); // Uses default charset - see class Javadoc
Torsten Curdtca165392008-07-10 10:17:44 +0000289 }
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000290 // CheckStyle:MagicNumber ON
291 setDirectory((newMode & DIR_FLAG) != 0);
292 setMode(newMode);
Torsten Curdtca165392008-07-10 10:17:44 +0000293 }
294
295 /**
Stefan Bodewigdfbdfee2009-03-03 12:42:28 +0000296 * Doesn't do anything special since this class always uses the
297 * same data in central directory and local file data.
298 */
299 public void parseFromCentralDirectoryData(byte[] buffer, int offset,
300 int length)
301 throws ZipException {
302 parseFromLocalFileData(buffer, offset, length);
303 }
304
305 /**
Torsten Curdtca165392008-07-10 10:17:44 +0000306 * Get the file mode for given permissions with the correct file type.
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000307 * @param mode the mode
308 * @return the type with the mode
Torsten Curdtca165392008-07-10 10:17:44 +0000309 */
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000310 protected int getMode(int mode) {
Torsten Curdtca165392008-07-10 10:17:44 +0000311 int type = FILE_FLAG;
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000312 if (isLink()) {
Torsten Curdtca165392008-07-10 10:17:44 +0000313 type = LINK_FLAG;
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000314 } else if (isDirectory()) {
Torsten Curdtca165392008-07-10 10:17:44 +0000315 type = DIR_FLAG;
316 }
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000317 return type | (mode & PERM_MASK);
Torsten Curdtca165392008-07-10 10:17:44 +0000318 }
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000319
Sebastian Bazleyfb06e3a2011-08-15 15:10:46 +0000320 @Override
Stefan Bodewig5e5804c2009-02-05 12:45:23 +0000321 public Object clone() {
322 try {
323 AsiExtraField cloned = (AsiExtraField) super.clone();
324 cloned.crc = new CRC32();
325 return cloned;
326 } catch (CloneNotSupportedException cnfe) {
327 // impossible
328 throw new RuntimeException(cnfe);
329 }
330 }
Torsten Curdtca165392008-07-10 10:17:44 +0000331}