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 com.mucommander.file.impl.zip.provider;
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     * <p>Short is two bytes and Long is four bytes in big endian byte and
045     * word order, device numbers are currently not supported.</p>
046     *
047     * <p>--------------------------------------------------------------------------------------------------------------<br>
048     * <br>
049     * This class is based off the <code>org.apache.tools.zip</code> package of the <i>Apache Ant</i> project. The Ant
050     * code has been modified under the terms of the Apache License which you can find in the bundled muCommander license
051     * file. It was forked at version 1.7.0 of Ant.</p>
052     * 
053     * @author Apache Ant, Maxence Bernard
054     */
055    public class AsiExtraField implements ZipExtraField, UnixStat, Cloneable {
056    
057        private static final ZipShort HEADER_ID = new ZipShort(0x756E);
058    
059        /**
060         * Standard Unix stat(2) file mode.
061         */
062        private int mode = 0;
063        /**
064         * User ID.
065         */
066        private int uid = 0;
067        /**
068         * Group ID.
069         */
070        private int gid = 0;
071        /**
072         * File this entry points to, if it is a symbolic link.
073         *
074         * <p>empty string - if entry is not a symbolic link.</p>
075         */
076        private String link = "";
077        /**
078         * Is this an entry for a directory?
079         */
080        private boolean dirFlag = false;
081    
082        /**
083         * Instance used to calculate checksums.
084         */
085        private CRC32 crc = new CRC32();
086    
087        /** Constructor for AsiExtraField. */
088        public AsiExtraField() {
089        }
090    
091        /**
092         * The Header-ID.
093         * @return the value for the header id for this extrafield
094         */
095        public ZipShort getHeaderId() {
096            return HEADER_ID;
097        }
098    
099        /**
100         * Length of the extra field in the local file data - without
101         * Header-ID or length specifier.
102         * @return a <code>ZipShort</code> for the length of the data of this extra field
103         */
104        public ZipShort getLocalFileDataLength() {
105            return new ZipShort(4         // CRC
106                              + 2         // Mode
107                              + 4         // SizDev
108                              + 2         // UID
109                              + 2         // GID
110                              + getLinkedFile().getBytes().length);
111        }
112    
113        /**
114         * Delegate to local file data.
115         * @return the centralDirectory length
116         */
117        public ZipShort getCentralDirectoryLength() {
118            return getLocalFileDataLength();
119        }
120    
121        /**
122         * The actual data to put into local file data - without Header-ID
123         * or length specifier.
124         * @return get the data
125         */
126        public byte[] getLocalFileDataData() {
127            // CRC will be added later
128            byte[] data = new byte[getLocalFileDataLength().getValue() - 4];
129            ZipShort.getBytes(getMode(), data, 0);
130    
131            byte[] linkArray = getLinkedFile().getBytes();
132            ZipLong.getBytes(linkArray.length, data, 2);
133            ZipShort.getBytes(getUserId(), data, 6);
134            ZipShort.getBytes(getGroupId(), data, 8);
135            System.arraycopy(linkArray, 0, data, 10, linkArray.length);
136    
137            crc.reset();
138            crc.update(data);
139            long checksum = crc.getValue();
140    
141            byte[] result = new byte[data.length + 4];
142            ZipLong.getBytes(checksum, result, 0);
143            System.arraycopy(data, 0, result, 4, data.length);
144    
145            return result;
146        }
147    
148        /**
149         * Delegate to local file data.
150         * @return the local file data
151         */
152        public byte[] getCentralDirectoryData() {
153            return getLocalFileDataData();
154        }
155    
156        /**
157         * Set the user id.
158         * @param uid the user id
159         */
160        public void setUserId(int uid) {
161            this.uid = uid;
162        }
163    
164        /**
165         * Get the user id.
166         * @return the user id
167         */
168        public int getUserId() {
169            return uid;
170        }
171    
172        /**
173         * Set the group id.
174         * @param gid the group id
175         */
176        public void setGroupId(int gid) {
177            this.gid = gid;
178        }
179    
180        /**
181         * Get the group id.
182         * @return the group id
183         */
184        public int getGroupId() {
185            return gid;
186        }
187    
188        /**
189         * Indicate that this entry is a symbolic link to the given filename.
190         *
191         * @param name Name of the file this entry links to, empty String
192         *             if it is not a symbolic link.
193         */
194        public void setLinkedFile(String name) {
195            link = name;
196            mode = getMode(mode);
197        }
198    
199        /**
200         * Name of linked file
201         *
202         * @return name of the file this entry links to if it is a
203         *         symbolic link, the empty string otherwise.
204         */
205        public String getLinkedFile() {
206            return link;
207        }
208    
209        /**
210         * Is this entry a symbolic link?
211         * @return true if this is a symbolic link
212         */
213        public boolean isLink() {
214            return getLinkedFile().length() != 0;
215        }
216    
217        /**
218         * File mode of this file.
219         * @param mode the file mode
220         */
221        public void setMode(int mode) {
222            this.mode = getMode(mode);
223        }
224    
225        /**
226         * File mode of this file.
227         * @return the file mode
228         */
229        public int getMode() {
230            return mode;
231        }
232    
233        /**
234         * Indicate whether this entry is a directory.
235         * @param dirFlag if true, this entry is a directory
236         */
237        public void setDirectory(boolean dirFlag) {
238            this.dirFlag = dirFlag;
239            mode = getMode(mode);
240        }
241    
242        /**
243         * Is this entry a directory?
244         * @return true if this entry is a directory
245         */
246        public boolean isDirectory() {
247            return dirFlag && !isLink();
248        }
249    
250        /**
251         * Populate data from this array as if it was in local file data.
252         * @param data an array of bytes
253         * @param offset the start offset
254         * @param length the number of bytes in the array from offset
255         * @throws ZipException on error
256         */
257        public void parseFromLocalFileData(byte[] data, int offset, int length)
258            throws ZipException {
259    
260            long givenChecksum = ZipLong.getValue(data, offset);
261            byte[] tmp = new byte[length - 4];
262            System.arraycopy(data, offset + 4, tmp, 0, length - 4);
263            crc.reset();
264            crc.update(tmp);
265            long realChecksum = crc.getValue();
266            if (givenChecksum != realChecksum) {
267                throw new ZipException("bad CRC checksum "
268                                       + Long.toHexString(givenChecksum)
269                                       + " instead of "
270                                       + Long.toHexString(realChecksum));
271            }
272    
273            int newMode = ZipShort.getValue(tmp, 0);
274            byte[] linkArray = new byte[(int) ZipLong.getValue(tmp, 2)];
275            uid = ZipShort.getValue(tmp, 6);
276            gid = ZipShort.getValue(tmp, 8);
277    
278            if (linkArray.length == 0) {
279                link = "";
280            } else {
281                System.arraycopy(tmp, 10, linkArray, 0, linkArray.length);
282                link = new String(linkArray);
283            }
284            setDirectory((newMode & DIR_FLAG) != 0);
285            setMode(newMode);
286        }
287    
288        /**
289         * Get the file mode for given permissions with the correct file type.
290         * @param mode the mode
291         * @return the type with the mode
292         */
293        protected int getMode(int mode) {
294            int type = FILE_FLAG;
295            if (isLink()) {
296                type = LINK_FLAG;
297            } else if (isDirectory()) {
298                type = DIR_FLAG;
299            }
300            return type | (mode & PERM_MASK);
301        }
302    
303    }