001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.zip;
020:
021: import java.util.zip.CRC32;
022: import java.util.zip.ZipException;
023:
024: /**
025: * Adds Unix file permission and UID/GID fields as well as symbolic
026: * link handling.
027: *
028: * <p>This class uses the ASi extra field in the format:
029: * <pre>
030: * Value Size Description
031: * ----- ---- -----------
032: * (Unix3) 0x756e Short tag for this extra block type
033: * TSize Short total data size for this block
034: * CRC Long CRC-32 of the remaining data
035: * Mode Short file permissions
036: * SizDev Long symlink'd size OR major/minor dev num
037: * UID Short user ID
038: * GID Short group ID
039: * (var.) variable symbolic link filename
040: * </pre>
041: * taken from appnote.iz (Info-ZIP note, 981119) found at <a
042: * href="ftp://ftp.uu.net/pub/archiving/zip/doc/">ftp://ftp.uu.net/pub/archiving/zip/doc/</a></p>
043:
044: *
045: * <p>Short is two bytes and Long is four bytes in big endian byte and
046: * word order, device numbers are currently not supported.</p>
047: *
048: */
049: public class AsiExtraField implements ZipExtraField, UnixStat,
050: Cloneable {
051:
052: private static final ZipShort HEADER_ID = new ZipShort(0x756E);
053:
054: /**
055: * Standard Unix stat(2) file mode.
056: *
057: * @since 1.1
058: */
059: private int mode = 0;
060: /**
061: * User ID.
062: *
063: * @since 1.1
064: */
065: private int uid = 0;
066: /**
067: * Group ID.
068: *
069: * @since 1.1
070: */
071: private int gid = 0;
072: /**
073: * File this entry points to, if it is a symbolic link.
074: *
075: * <p>empty string - if entry is not a symbolic link.</p>
076: *
077: * @since 1.1
078: */
079: private String link = "";
080: /**
081: * Is this an entry for a directory?
082: *
083: * @since 1.1
084: */
085: private boolean dirFlag = false;
086:
087: /**
088: * Instance used to calculate checksums.
089: *
090: * @since 1.1
091: */
092: private CRC32 crc = new CRC32();
093:
094: /** Constructor for AsiExtraField. */
095: public AsiExtraField() {
096: }
097:
098: /**
099: * The Header-ID.
100: * @return the value for the header id for this extrafield
101: * @since 1.1
102: */
103: public ZipShort getHeaderId() {
104: return HEADER_ID;
105: }
106:
107: /**
108: * Length of the extra field in the local file data - without
109: * Header-ID or length specifier.
110: * @return a <code>ZipShort</code> for the length of the data of this extra field
111: * @since 1.1
112: */
113: public ZipShort getLocalFileDataLength() {
114: return new ZipShort(4 // CRC
115: + 2 // Mode
116: + 4 // SizDev
117: + 2 // UID
118: + 2 // GID
119: + getLinkedFile().getBytes().length);
120: }
121:
122: /**
123: * Delegate to local file data.
124: * @return the centralDirectory length
125: * @since 1.1
126: */
127: public ZipShort getCentralDirectoryLength() {
128: return getLocalFileDataLength();
129: }
130:
131: /**
132: * The actual data to put into local file data - without Header-ID
133: * or length specifier.
134: * @return get the data
135: * @since 1.1
136: */
137: public byte[] getLocalFileDataData() {
138: // CRC will be added later
139: byte[] data = new byte[getLocalFileDataLength().getValue() - 4];
140: System.arraycopy(ZipShort.getBytes(getMode()), 0, data, 0, 2);
141:
142: byte[] linkArray = getLinkedFile().getBytes();
143: System.arraycopy(ZipLong.getBytes(linkArray.length), 0, data,
144: 2, 4);
145:
146: System.arraycopy(ZipShort.getBytes(getUserId()), 0, data, 6, 2);
147: System
148: .arraycopy(ZipShort.getBytes(getGroupId()), 0, data, 8,
149: 2);
150:
151: System.arraycopy(linkArray, 0, data, 10, linkArray.length);
152:
153: crc.reset();
154: crc.update(data);
155: long checksum = crc.getValue();
156:
157: byte[] result = new byte[data.length + 4];
158: System.arraycopy(ZipLong.getBytes(checksum), 0, result, 0, 4);
159: System.arraycopy(data, 0, result, 4, data.length);
160: return result;
161: }
162:
163: /**
164: * Delegate to local file data.
165: * @return the local file data
166: * @since 1.1
167: */
168: public byte[] getCentralDirectoryData() {
169: return getLocalFileDataData();
170: }
171:
172: /**
173: * Set the user id.
174: * @param uid the user id
175: * @since 1.1
176: */
177: public void setUserId(int uid) {
178: this .uid = uid;
179: }
180:
181: /**
182: * Get the user id.
183: * @return the user id
184: * @since 1.1
185: */
186: public int getUserId() {
187: return uid;
188: }
189:
190: /**
191: * Set the group id.
192: * @param gid the group id
193: * @since 1.1
194: */
195: public void setGroupId(int gid) {
196: this .gid = gid;
197: }
198:
199: /**
200: * Get the group id.
201: * @return the group id
202: * @since 1.1
203: */
204: public int getGroupId() {
205: return gid;
206: }
207:
208: /**
209: * Indicate that this entry is a symbolic link to the given filename.
210: *
211: * @param name Name of the file this entry links to, empty String
212: * if it is not a symbolic link.
213: *
214: * @since 1.1
215: */
216: public void setLinkedFile(String name) {
217: link = name;
218: mode = getMode(mode);
219: }
220:
221: /**
222: * Name of linked file
223: *
224: * @return name of the file this entry links to if it is a
225: * symbolic link, the empty string otherwise.
226: *
227: * @since 1.1
228: */
229: public String getLinkedFile() {
230: return link;
231: }
232:
233: /**
234: * Is this entry a symbolic link?
235: * @return true if this is a symbolic link
236: * @since 1.1
237: */
238: public boolean isLink() {
239: return getLinkedFile().length() != 0;
240: }
241:
242: /**
243: * File mode of this file.
244: * @param mode the file mode
245: * @since 1.1
246: */
247: public void setMode(int mode) {
248: this .mode = getMode(mode);
249: }
250:
251: /**
252: * File mode of this file.
253: * @return the file mode
254: * @since 1.1
255: */
256: public int getMode() {
257: return mode;
258: }
259:
260: /**
261: * Indicate whether this entry is a directory.
262: * @param dirFlag if true, this entry is a directory
263: * @since 1.1
264: */
265: public void setDirectory(boolean dirFlag) {
266: this .dirFlag = dirFlag;
267: mode = getMode(mode);
268: }
269:
270: /**
271: * Is this entry a directory?
272: * @return true if this entry is a directory
273: * @since 1.1
274: */
275: public boolean isDirectory() {
276: return dirFlag && !isLink();
277: }
278:
279: /**
280: * Populate data from this array as if it was in local file data.
281: * @param data an array of bytes
282: * @param offset the start offset
283: * @param length the number of bytes in the array from offset
284: * @since 1.1
285: * @throws ZipException on error
286: */
287: public void parseFromLocalFileData(byte[] data, int offset,
288: int length) throws ZipException {
289:
290: long givenChecksum = ZipLong.getValue(data, offset);
291: byte[] tmp = new byte[length - 4];
292: System.arraycopy(data, offset + 4, tmp, 0, length - 4);
293: crc.reset();
294: crc.update(tmp);
295: long realChecksum = crc.getValue();
296: if (givenChecksum != realChecksum) {
297: throw new ZipException("bad CRC checksum "
298: + Long.toHexString(givenChecksum) + " instead of "
299: + Long.toHexString(realChecksum));
300: }
301:
302: int newMode = ZipShort.getValue(tmp, 0);
303: byte[] linkArray = new byte[(int) ZipLong.getValue(tmp, 2)];
304: uid = ZipShort.getValue(tmp, 6);
305: gid = ZipShort.getValue(tmp, 8);
306:
307: if (linkArray.length == 0) {
308: link = "";
309: } else {
310: System.arraycopy(tmp, 10, linkArray, 0, linkArray.length);
311: link = new String(linkArray);
312: }
313: setDirectory((newMode & DIR_FLAG) != 0);
314: setMode(newMode);
315: }
316:
317: /**
318: * Get the file mode for given permissions with the correct file type.
319: * @param mode the mode
320: * @return the type with the mode
321: * @since 1.1
322: */
323: protected int getMode(int mode) {
324: int type = FILE_FLAG;
325: if (isLink()) {
326: type = LINK_FLAG;
327: } else if (isDirectory()) {
328: type = DIR_FLAG;
329: }
330: return type | (mode & PERM_MASK);
331: }
332:
333: }
|