blob: 1f9695c3d230510207a26dd496b1e827aff73e8c [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.tar;
20
21import java.io.File;
Stefan Bodewigaa9d0bc2011-07-23 05:03:52 +000022import java.nio.ByteBuffer;
Torsten Curdtca165392008-07-10 10:17:44 +000023import java.util.Date;
24import java.util.Locale;
25
26import org.apache.commons.compress.archivers.ArchiveEntry;
27
28/**
Torsten Curdt46ad24d2009-01-08 11:09:25 +000029 * This class represents an entry in a Tar archive. It consists
30 * of the entry's header, as well as the entry's File. Entries
31 * can be instantiated in one of three ways, depending on how
32 * they are to be used.
33 * <p>
34 * TarEntries that are created from the header bytes read from
35 * an archive are instantiated with the TarEntry( byte[] )
36 * constructor. These entries will be used when extracting from
37 * or listing the contents of an archive. These entries have their
38 * header filled in using the header bytes. They also set the File
39 * to null, since they reference an archive entry not a file.
40 * <p>
41 * TarEntries that are created from Files that are to be written
42 * into an archive are instantiated with the TarEntry( File )
43 * constructor. These entries have their header filled in using
44 * the File's information. They also keep a reference to the File
45 * for convenience when writing entries.
46 * <p>
47 * Finally, TarEntries can be constructed from nothing but a name.
48 * This allows the programmer to construct the entry by hand, for
49 * instance when only an InputStream is available for writing to
50 * the archive, and the header information is constructed from
51 * other information. In this case the header fields are set to
52 * defaults and the File is set to null.
Torsten Curdtca165392008-07-10 10:17:44 +000053 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +000054 * <p>
55 * The C structure for a Tar Entry's header is:
56 * <pre>
Torsten Curdtca165392008-07-10 10:17:44 +000057 * struct header {
Sebastian Bazley8118f822009-04-02 23:34:48 +000058 * char name[100]; // TarConstants.NAMELEN - offset 0
59 * char mode[8]; // TarConstants.MODELEN - offset 100
60 * char uid[8]; // TarConstants.UIDLEN - offset 108
61 * char gid[8]; // TarConstants.GIDLEN - offset 116
62 * char size[12]; // TarConstants.SIZELEN - offset 124
63 * char mtime[12]; // TarConstants.MODTIMELEN - offset 136
64 * char chksum[8]; // TarConstants.CHKSUMLEN - offset 148
65 * char linkflag[1]; // - offset 156
66 * char linkname[100]; // TarConstants.NAMELEN - offset 157
Sebastian Bazley24f9c9b2009-04-02 15:19:17 +000067 * The following fields are only present in new-style POSIX tar archives:
Sebastian Bazley8118f822009-04-02 23:34:48 +000068 * char magic[6]; // TarConstants.MAGICLEN - offset 257
69 * char version[2]; // TarConstants.VERSIONLEN - offset 263
70 * char uname[32]; // TarConstants.UNAMELEN - offset 265
71 * char gname[32]; // TarConstants.GNAMELEN - offset 297
72 * char devmajor[8]; // TarConstants.DEVLEN - offset 329
73 * char devminor[8]; // TarConstants.DEVLEN - offset 337
74 * char prefix[155]; // TarConstants.PREFIXLEN - offset 345
75 * // Used if "name" field is not long enough to hold the path
76 * char pad[12]; // NULs - offset 500
Torsten Curdtca165392008-07-10 10:17:44 +000077 * } header;
Sebastian Bazley24f9c9b2009-04-02 15:19:17 +000078 * All unused bytes are set to null.
79 * New-style GNU tar files are slightly different from the above.
Sebastian Bazley16dc21b2012-02-23 00:16:07 +000080 * For values of size larger than 077777777777L (11 7s)
81 * or uid and gid larger than 07777777L (7 7s)
82 * the sign bit of the first byte is set, and the rest of the
83 * field is the binary representation of the number.
84 * See TarUtils.parseOctalOrBinary.
Torsten Curdtca165392008-07-10 10:17:44 +000085 * </pre>
Sebastian Bazley99870ef2009-03-28 00:04:36 +000086 *
Stefan Bodewigaa9d0bc2011-07-23 05:03:52 +000087 * <p>
88 * The C structure for a old GNU Tar Entry's header is:
89 * <pre>
90 * struct oldgnu_header {
91 * char unused_pad1[345]; // TarConstants.PAD1LEN_GNU - offset 0
92 * char atime[12]; // TarConstants.ATIMELEN_GNU - offset 345
93 * char ctime[12]; // TarConstants.CTIMELEN_GNU - offset 357
94 * char offset[12]; // TarConstants.OFFSETLEN_GNU - offset 369
95 * char longnames[4]; // TarConstants.LONGNAMESLEN_GNU - offset 381
96 * char unused_pad2; // TarConstants.PAD2LEN_GNU - offset 385
97 * struct sparse sp[4]; // TarConstants.SPARSELEN_GNU - offset 386
98 * char isextended; // TarConstants.ISEXTENDEDLEN_GNU - offset 482
99 * char realsize[12]; // TarConstants.REALSIZELEN_GNU - offset 483
100 * char unused_pad[17]; // TarConstants.PAD3LEN_GNU - offset 495
101 * };
102 * </pre>
103 * Whereas, "struct sparse" is:
104 * <pre>
105 * struct sparse {
106 * char offset[12]; // offset 0
107 * char numbytes[12]; // offset 12
108 * };
109 * </pre>
110 *
Sebastian Bazley99870ef2009-03-28 00:04:36 +0000111 * @NotThreadSafe
Torsten Curdtca165392008-07-10 10:17:44 +0000112 */
Torsten Curdtca165392008-07-10 10:17:44 +0000113
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000114public class TarArchiveEntry implements TarConstants, ArchiveEntry {
115 /** The entry's name. */
Sebastian Bazley1d556702009-04-02 18:45:02 +0000116 private String name;
Torsten Curdtca165392008-07-10 10:17:44 +0000117
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000118 /** The entry's permission mode. */
119 private int mode;
Torsten Curdtca165392008-07-10 10:17:44 +0000120
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000121 /** The entry's user id. */
122 private int userId;
Torsten Curdtca165392008-07-10 10:17:44 +0000123
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000124 /** The entry's group id. */
125 private int groupId;
Torsten Curdtca165392008-07-10 10:17:44 +0000126
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000127 /** The entry's size. */
128 private long size;
Torsten Curdtca165392008-07-10 10:17:44 +0000129
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000130 /** The entry's modification time. */
131 private long modTime;
Torsten Curdtca165392008-07-10 10:17:44 +0000132
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000133 /** The entry's link flag. */
134 private byte linkFlag;
Torsten Curdtca165392008-07-10 10:17:44 +0000135
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000136 /** The entry's link name. */
Sebastian Bazley1d556702009-04-02 18:45:02 +0000137 private String linkName;
Torsten Curdtca165392008-07-10 10:17:44 +0000138
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000139 /** The entry's magic tag. */
Sebastian Bazley1d556702009-04-02 18:45:02 +0000140 private String magic;
Sebastian Bazley8118f822009-04-02 23:34:48 +0000141 /** The version of the format */
142 private String version;
Torsten Curdtca165392008-07-10 10:17:44 +0000143
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000144 /** The entry's user name. */
Sebastian Bazley1d556702009-04-02 18:45:02 +0000145 private String userName;
Torsten Curdtca165392008-07-10 10:17:44 +0000146
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000147 /** The entry's group name. */
Sebastian Bazley1d556702009-04-02 18:45:02 +0000148 private String groupName;
Torsten Curdtca165392008-07-10 10:17:44 +0000149
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000150 /** The entry's major device number. */
151 private int devMajor;
Torsten Curdtca165392008-07-10 10:17:44 +0000152
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000153 /** The entry's minor device number. */
154 private int devMinor;
Torsten Curdtca165392008-07-10 10:17:44 +0000155
Stefan Bodewigaa9d0bc2011-07-23 05:03:52 +0000156 /** If an extension sparse header follows. */
157 private boolean isExtended;
158
159 /** The entry's real size in case of a sparse file. */
160 private long realSize;
161
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000162 /** The entry's file reference */
163 private File file;
Torsten Curdtca165392008-07-10 10:17:44 +0000164
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000165 /** Maximum length of a user's name in the tar file */
166 public static final int MAX_NAMELEN = 31;
Torsten Curdtca165392008-07-10 10:17:44 +0000167
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000168 /** Default permissions bits for directories */
169 public static final int DEFAULT_DIR_MODE = 040755;
Torsten Curdtca165392008-07-10 10:17:44 +0000170
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000171 /** Default permissions bits for files */
172 public static final int DEFAULT_FILE_MODE = 0100644;
Torsten Curdtca165392008-07-10 10:17:44 +0000173
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000174 /** Convert millis to seconds */
175 public static final int MILLIS_PER_SECOND = 1000;
Torsten Curdtca165392008-07-10 10:17:44 +0000176
177 /**
178 * Construct an empty entry and prepares the header values.
179 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000180 private TarArchiveEntry () {
Sebastian Bazley1d556702009-04-02 18:45:02 +0000181 this.magic = MAGIC_POSIX;
Sebastian Bazley8118f822009-04-02 23:34:48 +0000182 this.version = VERSION_POSIX;
Sebastian Bazley1d556702009-04-02 18:45:02 +0000183 this.name = "";
184 this.linkName = "";
Torsten Curdtca165392008-07-10 10:17:44 +0000185
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000186 String user = System.getProperty("user.name", "");
187
188 if (user.length() > MAX_NAMELEN) {
189 user = user.substring(0, MAX_NAMELEN);
Torsten Curdtca165392008-07-10 10:17:44 +0000190 }
191
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000192 this.userId = 0;
193 this.groupId = 0;
Sebastian Bazley1d556702009-04-02 18:45:02 +0000194 this.userName = user;
195 this.groupName = "";
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000196 this.file = null;
Torsten Curdtca165392008-07-10 10:17:44 +0000197 }
198
199 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000200 * Construct an entry with only a name. This allows the programmer
201 * to construct the entry's header "by hand". File is set to null.
Torsten Curdtca165392008-07-10 10:17:44 +0000202 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000203 * @param name the entry name
Torsten Curdtca165392008-07-10 10:17:44 +0000204 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000205 public TarArchiveEntry(String name) {
Stefan Bodewig3e2ddad2009-08-25 08:11:44 +0000206 this(name, false);
207 }
208
209 /**
210 * Construct an entry with only a name. This allows the programmer
211 * to construct the entry's header "by hand". File is set to null.
212 *
213 * @param name the entry name
214 * @param preserveLeadingSlashes whether to allow leading slashes
215 * in the name.
Sebastian Bazley6209f812010-05-10 17:36:40 +0000216 *
217 * @since Apache Commons Compress 1.1
Stefan Bodewig3e2ddad2009-08-25 08:11:44 +0000218 */
219 public TarArchiveEntry(String name, boolean preserveLeadingSlashes) {
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000220 this();
221
Stefan Bodewig3e2ddad2009-08-25 08:11:44 +0000222 name = normalizeFileName(name, preserveLeadingSlashes);
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000223 boolean isDir = name.endsWith("/");
224
225 this.devMajor = 0;
226 this.devMinor = 0;
Sebastian Bazley1d556702009-04-02 18:45:02 +0000227 this.name = name;
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000228 this.mode = isDir ? DEFAULT_DIR_MODE : DEFAULT_FILE_MODE;
229 this.linkFlag = isDir ? LF_DIR : LF_NORMAL;
230 this.userId = 0;
231 this.groupId = 0;
232 this.size = 0;
233 this.modTime = (new Date()).getTime() / MILLIS_PER_SECOND;
Sebastian Bazley1d556702009-04-02 18:45:02 +0000234 this.linkName = "";
235 this.userName = "";
236 this.groupName = "";
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000237 this.devMajor = 0;
238 this.devMinor = 0;
239
Torsten Curdtca165392008-07-10 10:17:44 +0000240 }
241
242 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000243 * Construct an entry with a name and a link flag.
Torsten Curdtca165392008-07-10 10:17:44 +0000244 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000245 * @param name the entry name
246 * @param linkFlag the entry link flag.
Torsten Curdtca165392008-07-10 10:17:44 +0000247 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000248 public TarArchiveEntry(String name, byte linkFlag) {
249 this(name);
250 this.linkFlag = linkFlag;
Stefan Bodewig7bb5d882009-10-08 12:07:19 +0000251 if (linkFlag == LF_GNUTYPE_LONGNAME) {
252 magic = MAGIC_GNU;
253 version = VERSION_GNU_SPACE;
254 }
Torsten Curdtca165392008-07-10 10:17:44 +0000255 }
256
257 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000258 * Construct an entry for a file. File is set to file, and the
259 * header is constructed from information from the file.
Sebastian Bazleyfec51a12009-03-31 00:35:56 +0000260 * The name is set from the normalized file path.
Torsten Curdtca165392008-07-10 10:17:44 +0000261 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000262 * @param file The file that the entry represents.
Torsten Curdtca165392008-07-10 10:17:44 +0000263 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000264 public TarArchiveEntry(File file) {
Stefan Bodewig3e2ddad2009-08-25 08:11:44 +0000265 this(file, normalizeFileName(file.getPath(), false));
Sebastian Bazleyfec51a12009-03-31 00:35:56 +0000266 }
Stefan Bodewigaa9d0bc2011-07-23 05:03:52 +0000267
Sebastian Bazleyfec51a12009-03-31 00:35:56 +0000268 /**
269 * Construct an entry for a file. File is set to file, and the
270 * header is constructed from information from the file.
271 *
272 * @param file The file that the entry represents.
273 * @param fileName the name to be used for the entry.
274 */
275 public TarArchiveEntry(File file, String fileName) {
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000276 this();
Torsten Curdtca165392008-07-10 10:17:44 +0000277
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000278 this.file = file;
Torsten Curdtca165392008-07-10 10:17:44 +0000279
Sebastian Bazley1d556702009-04-02 18:45:02 +0000280 this.linkName = "";
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000281
282 if (file.isDirectory()) {
283 this.mode = DEFAULT_DIR_MODE;
284 this.linkFlag = LF_DIR;
285
Sebastian Bazley1d556702009-04-02 18:45:02 +0000286 int nameLength = fileName.length();
Christian Grobmeier3bb92822009-05-04 09:15:01 +0000287 if (nameLength == 0 || fileName.charAt(nameLength - 1) != '/') {
Sebastian Bazley1d556702009-04-02 18:45:02 +0000288 this.name = fileName + "/";
289 } else {
Stefan Bodewigaa9d0bc2011-07-23 05:03:52 +0000290 this.name = fileName;
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000291 }
Stefan Bodewigc013e282009-03-18 04:38:47 +0000292 this.size = 0;
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000293 } else {
294 this.mode = DEFAULT_FILE_MODE;
295 this.linkFlag = LF_NORMAL;
Stefan Bodewigc013e282009-03-18 04:38:47 +0000296 this.size = file.length();
Sebastian Bazley1d556702009-04-02 18:45:02 +0000297 this.name = fileName;
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000298 }
299
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000300 this.modTime = file.lastModified() / MILLIS_PER_SECOND;
301 this.devMajor = 0;
302 this.devMinor = 0;
Torsten Curdtca165392008-07-10 10:17:44 +0000303 }
304
305 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000306 * Construct an entry from an archive's header bytes. File is set
307 * to null.
Torsten Curdtca165392008-07-10 10:17:44 +0000308 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000309 * @param headerBuf The header bytes from a tar archive entry.
Torsten Curdtca165392008-07-10 10:17:44 +0000310 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000311 public TarArchiveEntry(byte[] headerBuf) {
312 this();
313 parseTarHeader(headerBuf);
Torsten Curdtca165392008-07-10 10:17:44 +0000314 }
315
316 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000317 * Determine if the two entries are equal. Equality is determined
318 * by the header names being equal.
Torsten Curdtca165392008-07-10 10:17:44 +0000319 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000320 * @param it Entry to be checked for equality.
321 * @return True if the entries are equal.
Torsten Curdtca165392008-07-10 10:17:44 +0000322 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000323 public boolean equals(TarArchiveEntry it) {
324 return getName().equals(it.getName());
Torsten Curdtca165392008-07-10 10:17:44 +0000325 }
326
327 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000328 * Determine if the two entries are equal. Equality is determined
329 * by the header names being equal.
Torsten Curdtca165392008-07-10 10:17:44 +0000330 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000331 * @param it Entry to be checked for equality.
332 * @return True if the entries are equal.
Torsten Curdtca165392008-07-10 10:17:44 +0000333 */
Stefan Bodewig46628ef2011-08-06 16:30:40 +0000334 @Override
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000335 public boolean equals(Object it) {
336 if (it == null || getClass() != it.getClass()) {
337 return false;
338 }
339 return equals((TarArchiveEntry) it);
Torsten Curdtca165392008-07-10 10:17:44 +0000340 }
341
342 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000343 * Hashcodes are based on entry names.
Torsten Curdtca165392008-07-10 10:17:44 +0000344 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000345 * @return the entry hashcode
Torsten Curdtca165392008-07-10 10:17:44 +0000346 */
Stefan Bodewig46628ef2011-08-06 16:30:40 +0000347 @Override
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000348 public int hashCode() {
349 return getName().hashCode();
Torsten Curdtca165392008-07-10 10:17:44 +0000350 }
351
352 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000353 * Determine if the given entry is a descendant of this entry.
354 * Descendancy is determined by the name of the descendant
355 * starting with this entry's name.
Torsten Curdtca165392008-07-10 10:17:44 +0000356 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000357 * @param desc Entry to be checked as a descendent of this.
358 * @return True if entry is a descendant of this.
Torsten Curdtca165392008-07-10 10:17:44 +0000359 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000360 public boolean isDescendent(TarArchiveEntry desc) {
361 return desc.getName().startsWith(getName());
Torsten Curdtca165392008-07-10 10:17:44 +0000362 }
363
364 /**
365 * Get this entry's name.
366 *
367 * @return This entry's name.
368 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000369 public String getName() {
370 return name.toString();
Torsten Curdtca165392008-07-10 10:17:44 +0000371 }
372
373 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000374 * Set this entry's name.
Torsten Curdtca165392008-07-10 10:17:44 +0000375 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000376 * @param name This entry's new name.
Torsten Curdtca165392008-07-10 10:17:44 +0000377 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000378 public void setName(String name) {
Stefan Bodewig3e2ddad2009-08-25 08:11:44 +0000379 this.name = normalizeFileName(name, false);
Torsten Curdtca165392008-07-10 10:17:44 +0000380 }
381
382 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000383 * Set the mode for this entry
Torsten Curdtca165392008-07-10 10:17:44 +0000384 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000385 * @param mode the mode for this entry
Torsten Curdtca165392008-07-10 10:17:44 +0000386 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000387 public void setMode(int mode) {
388 this.mode = mode;
Torsten Curdtca165392008-07-10 10:17:44 +0000389 }
390
391 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000392 * Get this entry's link name.
Torsten Curdtca165392008-07-10 10:17:44 +0000393 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000394 * @return This entry's link name.
Torsten Curdtca165392008-07-10 10:17:44 +0000395 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000396 public String getLinkName() {
397 return linkName.toString();
Torsten Curdtca165392008-07-10 10:17:44 +0000398 }
399
400 /**
Sebastian Bazley5b85bf12010-05-10 10:59:23 +0000401 * Set this entry's link name.
Sebastian Bazley6209f812010-05-10 17:36:40 +0000402 *
403 * @param link the link name to use.
404 *
405 * @since Apache Commons Compress 1.1
Sebastian Bazley5b85bf12010-05-10 10:59:23 +0000406 */
407 public void setLinkName(String link) {
Sebastian Bazley6209f812010-05-10 17:36:40 +0000408 this.linkName = link;
Sebastian Bazley5b85bf12010-05-10 10:59:23 +0000409 }
410
411 /**
Torsten Curdtca165392008-07-10 10:17:44 +0000412 * Get this entry's user id.
413 *
414 * @return This entry's user id.
415 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000416 public int getUserId() {
417 return userId;
418 }
419
420 /**
421 * Set this entry's user id.
422 *
423 * @param userId This entry's new user id.
424 */
425 public void setUserId(int userId) {
426 this.userId = userId;
427 }
428
429 /**
430 * Get this entry's group id.
431 *
432 * @return This entry's group id.
433 */
434 public int getGroupId() {
435 return groupId;
436 }
437
438 /**
439 * Set this entry's group id.
440 *
441 * @param groupId This entry's new group id.
442 */
443 public void setGroupId(int groupId) {
444 this.groupId = groupId;
Torsten Curdtca165392008-07-10 10:17:44 +0000445 }
446
447 /**
448 * Get this entry's user name.
449 *
450 * @return This entry's user name.
451 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000452 public String getUserName() {
453 return userName.toString();
Torsten Curdtca165392008-07-10 10:17:44 +0000454 }
455
456 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000457 * Set this entry's user name.
Torsten Curdtca165392008-07-10 10:17:44 +0000458 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000459 * @param userName This entry's new user name.
Torsten Curdtca165392008-07-10 10:17:44 +0000460 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000461 public void setUserName(String userName) {
Sebastian Bazley1d556702009-04-02 18:45:02 +0000462 this.userName = userName;
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000463 }
464
465 /**
466 * Get this entry's group name.
467 *
468 * @return This entry's group name.
469 */
470 public String getGroupName() {
471 return groupName.toString();
472 }
473
474 /**
475 * Set this entry's group name.
476 *
477 * @param groupName This entry's new group name.
478 */
479 public void setGroupName(String groupName) {
Sebastian Bazley1d556702009-04-02 18:45:02 +0000480 this.groupName = groupName;
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000481 }
482
483 /**
484 * Convenience method to set this entry's group and user ids.
485 *
486 * @param userId This entry's new user id.
487 * @param groupId This entry's new group id.
488 */
489 public void setIds(int userId, int groupId) {
490 setUserId(userId);
491 setGroupId(groupId);
492 }
493
494 /**
495 * Convenience method to set this entry's group and user names.
496 *
497 * @param userName This entry's new user name.
498 * @param groupName This entry's new group name.
499 */
500 public void setNames(String userName, String groupName) {
501 setUserName(userName);
502 setGroupName(groupName);
503 }
504
505 /**
506 * Set this entry's modification time. The parameter passed
507 * to this method is in "Java time".
508 *
509 * @param time This entry's new modification time.
510 */
511 public void setModTime(long time) {
512 modTime = time / MILLIS_PER_SECOND;
513 }
514
515 /**
516 * Set this entry's modification time.
517 *
518 * @param time This entry's new modification time.
519 */
520 public void setModTime(Date time) {
521 modTime = time.getTime() / MILLIS_PER_SECOND;
522 }
523
524 /**
525 * Set this entry's modification time.
526 *
527 * @return time This entry's new modification time.
528 */
529 public Date getModTime() {
530 return new Date(modTime * MILLIS_PER_SECOND);
531 }
532
Sebastian Bazley7b115db2010-03-13 13:44:50 +0000533 /** {@inheritDoc} */
Stefan Bodewig17ffd7f2009-08-01 14:52:15 +0000534 public Date getLastModifiedDate() {
535 return getModTime();
536 }
537
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000538 /**
539 * Get this entry's file.
540 *
541 * @return This entry's file.
542 */
543 public File getFile() {
544 return file;
545 }
546
547 /**
548 * Get this entry's mode.
549 *
550 * @return This entry's mode.
551 */
552 public int getMode() {
553 return mode;
554 }
555
556 /**
557 * Get this entry's file size.
558 *
559 * @return This entry's file size.
560 */
561 public long getSize() {
562 return size;
563 }
564
565 /**
566 * Set this entry's file size.
567 *
568 * @param size This entry's new file size.
Stefan Bodewiga6fbdae2011-12-08 14:03:57 +0000569 * @throws IllegalArgumentException if the size is &lt; 0.
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000570 */
571 public void setSize(long size) {
Stefan Bodewig5dad1122011-12-07 11:34:34 +0000572 if (size < 0){
Stefan Bodewiga6fbdae2011-12-08 14:03:57 +0000573 throw new IllegalArgumentException("Size is out of range: "+size);
Stefan Bodewig5dad1122011-12-07 11:34:34 +0000574 }
575 this.size = size;
576 }
577
578 /**
Stefan Bodewigaa9d0bc2011-07-23 05:03:52 +0000579 * Indicates in case of a sparse file if an extension sparse header
580 * follows.
581 *
582 * @return true if an extension sparse header follows.
583 */
584 public boolean isExtended() {
585 return isExtended;
586 }
587
588 /**
589 * Get this entry's real file size in case of a sparse file.
590 *
591 * @return This entry's real file size.
592 */
593 public long getRealSize() {
594 return realSize;
595 }
596
597 /**
598 * Indicate if this entry is a GNU sparse block
599 *
600 * @return true if this is a sparse extension provided by GNU tar
601 */
602 public boolean isGNUSparse() {
603 return linkFlag == LF_GNUTYPE_SPARSE;
604 }
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000605
606 /**
607 * Indicate if this entry is a GNU long name block
608 *
609 * @return true if this is a long name extension provided by GNU tar
610 */
611 public boolean isGNULongNameEntry() {
612 return linkFlag == LF_GNUTYPE_LONGNAME
Stefan Bodewig75f92f62009-03-17 12:14:17 +0000613 && name.toString().equals(GNU_LONGLINK);
Torsten Curdtca165392008-07-10 10:17:44 +0000614 }
615
616 /**
Sebastian Bazley33cd35e2010-05-09 20:38:20 +0000617 * Check if this is a Pax header.
618 *
619 * @return <code>true</code> if this is a Pax header.
Sebastian Bazley6209f812010-05-10 17:36:40 +0000620 *
621 * @since Apache Commons Compress 1.1
Sebastian Bazley33cd35e2010-05-09 20:38:20 +0000622 */
623 public boolean isPaxHeader(){
Sebastian Bazley05b6eb12010-05-10 01:43:08 +0000624 return linkFlag == LF_PAX_EXTENDED_HEADER_LC
625 || linkFlag == LF_PAX_EXTENDED_HEADER_UC;
626 }
627
628 /**
629 * Check if this is a Pax header.
630 *
631 * @return <code>true</code> if this is a Pax header.
Sebastian Bazley6209f812010-05-10 17:36:40 +0000632 *
633 * @since Apache Commons Compress 1.1
Sebastian Bazley05b6eb12010-05-10 01:43:08 +0000634 */
635 public boolean isGlobalPaxHeader(){
636 return linkFlag == LF_PAX_GLOBAL_EXTENDED_HEADER;
Sebastian Bazley33cd35e2010-05-09 20:38:20 +0000637 }
638
639 /**
Torsten Curdtca165392008-07-10 10:17:44 +0000640 * Return whether or not this entry represents a directory.
641 *
642 * @return True if this entry is a directory.
643 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000644 public boolean isDirectory() {
645 if (file != null) {
646 return file.isDirectory();
Torsten Curdtca165392008-07-10 10:17:44 +0000647 }
648
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000649 if (linkFlag == LF_DIR) {
Torsten Curdtca165392008-07-10 10:17:44 +0000650 return true;
651 }
652
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000653 if (getName().endsWith("/")) {
Torsten Curdtca165392008-07-10 10:17:44 +0000654 return true;
655 }
656
657 return false;
658 }
659
660 /**
Stefan Bodewig86aaf842010-10-29 15:03:49 +0000661 * Check if this is a "normal file"
662 *
663 * @since Apache Commons Compress 1.2
664 */
665 public boolean isFile() {
666 if (file != null) {
667 return file.isFile();
668 }
669 if (linkFlag == LF_OLDNORM || linkFlag == LF_NORMAL) {
670 return true;
671 }
672 return !getName().endsWith("/");
673 }
674
675 /**
676 * Check if this is a symbolic link entry.
677 *
678 * @since Apache Commons Compress 1.2
679 */
680 public boolean isSymbolicLink() {
681 return linkFlag == LF_SYMLINK;
682 }
683
684 /**
685 * Check if this is a link entry.
686 *
687 * @since Apache Commons Compress 1.2
688 */
689 public boolean isLink() {
690 return linkFlag == LF_LINK;
691 }
692
693 /**
694 * Check if this is a character device entry.
695 *
696 * @since Apache Commons Compress 1.2
697 */
698 public boolean isCharacterDevice() {
699 return linkFlag == LF_CHR;
700 }
701
702 /**
703 * Check if this is a block device entry.
704 *
705 * @since Apache Commons Compress 1.2
706 */
707 public boolean isBlockDevice() {
708 return linkFlag == LF_BLK;
709 }
710
711 /**
712 * Check if this is a FIFO (pipe) entry.
713 *
714 * @since Apache Commons Compress 1.2
715 */
716 public boolean isFIFO() {
717 return linkFlag == LF_FIFO;
718 }
719
720 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000721 * If this entry represents a file, and the file is a directory, return
722 * an array of TarEntries for this entry's children.
Torsten Curdtca165392008-07-10 10:17:44 +0000723 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000724 * @return An array of TarEntry's for this entry's children.
Torsten Curdtca165392008-07-10 10:17:44 +0000725 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000726 public TarArchiveEntry[] getDirectoryEntries() {
727 if (file == null || !file.isDirectory()) {
728 return new TarArchiveEntry[0];
729 }
730
731 String[] list = file.list();
732 TarArchiveEntry[] result = new TarArchiveEntry[list.length];
733
734 for (int i = 0; i < list.length; ++i) {
735 result[i] = new TarArchiveEntry(new File(file, list[i]));
736 }
737
738 return result;
Torsten Curdtca165392008-07-10 10:17:44 +0000739 }
740
741 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000742 * Write an entry's header information to a header buffer.
Torsten Curdtca165392008-07-10 10:17:44 +0000743 *
Stefan Bodewiga6fbdae2011-12-08 14:03:57 +0000744 * <p>This method does not use the star/GNU tar/BSD tar extensions.</p>
745 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000746 * @param outbuf The tar entry header buffer to fill in.
Torsten Curdtca165392008-07-10 10:17:44 +0000747 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000748 public void writeEntryHeader(byte[] outbuf) {
Stefan Bodewiga6fbdae2011-12-08 14:03:57 +0000749 writeEntryHeader(outbuf, false);
750 }
751
752 /**
753 * Write an entry's header information to a header buffer.
754 *
755 * @param outbuf The tar entry header buffer to fill in.
756 * @param starMode whether to use the star/GNU tar/BSD tar
757 * extension for the size field if the size is bigger than 8GiB
758 * @since Apache Commons Compress 1.4
759 */
760 public void writeEntryHeader(byte[] outbuf, boolean starMode) {
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000761 int offset = 0;
762
Sebastian Bazley1d556702009-04-02 18:45:02 +0000763 offset = TarUtils.formatNameBytes(name, outbuf, offset, NAMELEN);
764 offset = TarUtils.formatOctalBytes(mode, outbuf, offset, MODELEN);
765 offset = TarUtils.formatOctalBytes(userId, outbuf, offset, UIDLEN);
766 offset = TarUtils.formatOctalBytes(groupId, outbuf, offset, GIDLEN);
Stefan Bodewiga6fbdae2011-12-08 14:03:57 +0000767 if (size > TarConstants.MAXSIZE && !starMode) {
768 // size is in PAX header
769 offset = TarUtils.formatLongOctalBytes(0, outbuf, offset, SIZELEN);
770 } else {
771 offset = TarUtils.formatLongOctalOrBinaryBytes(size, outbuf, offset, SIZELEN);
772 }
Sebastian Bazley1d556702009-04-02 18:45:02 +0000773 offset = TarUtils.formatLongOctalBytes(modTime, outbuf, offset, MODTIMELEN);
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000774
775 int csOffset = offset;
776
777 for (int c = 0; c < CHKSUMLEN; ++c) {
778 outbuf[offset++] = (byte) ' ';
779 }
780
781 outbuf[offset++] = linkFlag;
Sebastian Bazley1d556702009-04-02 18:45:02 +0000782 offset = TarUtils.formatNameBytes(linkName, outbuf, offset, NAMELEN);
783 offset = TarUtils.formatNameBytes(magic, outbuf, offset, MAGICLEN);
Sebastian Bazley8118f822009-04-02 23:34:48 +0000784 offset = TarUtils.formatNameBytes(version, outbuf, offset, VERSIONLEN);
Sebastian Bazley1d556702009-04-02 18:45:02 +0000785 offset = TarUtils.formatNameBytes(userName, outbuf, offset, UNAMELEN);
786 offset = TarUtils.formatNameBytes(groupName, outbuf, offset, GNAMELEN);
787 offset = TarUtils.formatOctalBytes(devMajor, outbuf, offset, DEVLEN);
788 offset = TarUtils.formatOctalBytes(devMinor, outbuf, offset, DEVLEN);
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000789
790 while (offset < outbuf.length) {
791 outbuf[offset++] = 0;
792 }
793
794 long chk = TarUtils.computeCheckSum(outbuf);
795
Sebastian Bazley1d556702009-04-02 18:45:02 +0000796 TarUtils.formatCheckSumOctalBytes(chk, outbuf, csOffset, CHKSUMLEN);
Torsten Curdtca165392008-07-10 10:17:44 +0000797 }
798
799 /**
800 * Parse an entry's header information from a header buffer.
801 *
802 * @param header The tar entry header buffer to get information from.
803 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000804 public void parseTarHeader(byte[] header) {
Torsten Curdtca165392008-07-10 10:17:44 +0000805 int offset = 0;
806
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000807 name = TarUtils.parseName(header, offset, NAMELEN);
Torsten Curdtca165392008-07-10 10:17:44 +0000808 offset += NAMELEN;
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000809 mode = (int) TarUtils.parseOctal(header, offset, MODELEN);
810 offset += MODELEN;
Sebastian Bazley7ee6b672012-02-22 23:42:49 +0000811 userId = (int) TarUtils.parseOctalOrBinary(header, offset, UIDLEN);
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000812 offset += UIDLEN;
Sebastian Bazley7ee6b672012-02-22 23:42:49 +0000813 groupId = (int) TarUtils.parseOctalOrBinary(header, offset, GIDLEN);
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000814 offset += GIDLEN;
Stefan Bodewig68465642011-12-05 09:58:04 +0000815 size = TarUtils.parseOctalOrBinary(header, offset, SIZELEN);
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000816 offset += SIZELEN;
817 modTime = TarUtils.parseOctal(header, offset, MODTIMELEN);
818 offset += MODTIMELEN;
819 offset += CHKSUMLEN;
820 linkFlag = header[offset++];
821 linkName = TarUtils.parseName(header, offset, NAMELEN);
Torsten Curdtca165392008-07-10 10:17:44 +0000822 offset += NAMELEN;
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000823 magic = TarUtils.parseName(header, offset, MAGICLEN);
824 offset += MAGICLEN;
Sebastian Bazley8118f822009-04-02 23:34:48 +0000825 version = TarUtils.parseName(header, offset, VERSIONLEN);
826 offset += VERSIONLEN;
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000827 userName = TarUtils.parseName(header, offset, UNAMELEN);
828 offset += UNAMELEN;
829 groupName = TarUtils.parseName(header, offset, GNAMELEN);
830 offset += GNAMELEN;
831 devMajor = (int) TarUtils.parseOctal(header, offset, DEVLEN);
832 offset += DEVLEN;
833 devMinor = (int) TarUtils.parseOctal(header, offset, DEVLEN);
Sebastian Bazley33cd35e2010-05-09 20:38:20 +0000834 offset += DEVLEN;
Stefan Bodewigaa9d0bc2011-07-23 05:03:52 +0000835
836 int type = evaluateType(header);
837 switch (type) {
838 case FORMAT_OLDGNU: {
839 offset += ATIMELEN_GNU;
840 offset += CTIMELEN_GNU;
841 offset += OFFSETLEN_GNU;
842 offset += LONGNAMESLEN_GNU;
843 offset += PAD2LEN_GNU;
844 offset += SPARSELEN_GNU;
845 isExtended = TarUtils.parseBoolean(header, offset);
846 offset += ISEXTENDEDLEN_GNU;
847 realSize = TarUtils.parseOctal(header, offset, REALSIZELEN_GNU);
848 offset += REALSIZELEN_GNU;
849 break;
Sebastian Bazley5b25f6c2010-05-10 09:36:15 +0000850 }
Stefan Bodewigaa9d0bc2011-07-23 05:03:52 +0000851 case FORMAT_POSIX:
852 default: {
853 String prefix = TarUtils.parseName(header, offset, PREFIXLEN);
854 // SunOS tar -E does not add / to directory names, so fix
855 // up to be consistent
856 if (isDirectory() && !name.endsWith("/")){
857 name = name + "/";
858 }
859 if (prefix.length() > 0){
860 name = prefix + "/" + name;
861 }
862 }
Sebastian Bazley33cd35e2010-05-09 20:38:20 +0000863 }
Torsten Curdtca165392008-07-10 10:17:44 +0000864 }
Stefan Bodewig32eea1e2009-03-17 12:53:22 +0000865
866 /**
867 * Strips Windows' drive letter as well as any leading slashes,
868 * turns path separators into forward slahes.
869 */
Stefan Bodewig3e2ddad2009-08-25 08:11:44 +0000870 private static String normalizeFileName(String fileName,
871 boolean preserveLeadingSlashes) {
Stefan Bodewigbed564b2010-02-18 12:34:02 +0000872 String osname = System.getProperty("os.name").toLowerCase(Locale.ENGLISH);
Stefan Bodewig32eea1e2009-03-17 12:53:22 +0000873
874 if (osname != null) {
875
876 // Strip off drive letters!
877 // REVIEW Would a better check be "(File.separator == '\')"?
878
879 if (osname.startsWith("windows")) {
880 if (fileName.length() > 2) {
881 char ch1 = fileName.charAt(0);
882 char ch2 = fileName.charAt(1);
883
884 if (ch2 == ':'
885 && ((ch1 >= 'a' && ch1 <= 'z')
886 || (ch1 >= 'A' && ch1 <= 'Z'))) {
887 fileName = fileName.substring(2);
888 }
889 }
890 } else if (osname.indexOf("netware") > -1) {
891 int colon = fileName.indexOf(':');
892 if (colon != -1) {
893 fileName = fileName.substring(colon + 1);
894 }
895 }
896 }
897
898 fileName = fileName.replace(File.separatorChar, '/');
899
900 // No absolute pathnames
901 // Windows (and Posix?) paths can start with "\\NetworkDrive\",
902 // so we loop on starting /'s.
Stefan Bodewig3e2ddad2009-08-25 08:11:44 +0000903 while (!preserveLeadingSlashes && fileName.startsWith("/")) {
Stefan Bodewig32eea1e2009-03-17 12:53:22 +0000904 fileName = fileName.substring(1);
905 }
906 return fileName;
907 }
Stefan Bodewigaa9d0bc2011-07-23 05:03:52 +0000908
909 /**
910 * Evaluate an entry's header format from a header buffer.
911 *
912 * @param header The tar entry header buffer to evaluate the format for.
913 * @return format type
914 */
915 private int evaluateType(byte[] header) {
916 final ByteBuffer magic = ByteBuffer.wrap(header, MAGIC_OFFSET, MAGICLEN);
917 if (magic.compareTo(ByteBuffer.wrap(MAGIC_GNU.getBytes())) == 0)
918 return FORMAT_OLDGNU;
919 if (magic.compareTo(ByteBuffer.wrap(MAGIC_POSIX.getBytes())) == 0)
920 return FORMAT_POSIX;
921 return 0;
922 }
Torsten Curdtca165392008-07-10 10:17:44 +0000923}
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000924