blob: 79967c90bbdc45f397b76970d7dfb5db7ca2f43e [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;
22import java.util.Date;
23import java.util.Locale;
24
25import org.apache.commons.compress.archivers.ArchiveEntry;
26
27/**
Torsten Curdt46ad24d2009-01-08 11:09:25 +000028 * This class represents an entry in a Tar archive. It consists
29 * of the entry's header, as well as the entry's File. Entries
30 * can be instantiated in one of three ways, depending on how
31 * they are to be used.
32 * <p>
33 * TarEntries that are created from the header bytes read from
34 * an archive are instantiated with the TarEntry( byte[] )
35 * constructor. These entries will be used when extracting from
36 * or listing the contents of an archive. These entries have their
37 * header filled in using the header bytes. They also set the File
38 * to null, since they reference an archive entry not a file.
39 * <p>
40 * TarEntries that are created from Files that are to be written
41 * into an archive are instantiated with the TarEntry( File )
42 * constructor. These entries have their header filled in using
43 * the File's information. They also keep a reference to the File
44 * for convenience when writing entries.
45 * <p>
46 * Finally, TarEntries can be constructed from nothing but a name.
47 * This allows the programmer to construct the entry by hand, for
48 * instance when only an InputStream is available for writing to
49 * the archive, and the header information is constructed from
50 * other information. In this case the header fields are set to
51 * defaults and the File is set to null.
Torsten Curdtca165392008-07-10 10:17:44 +000052 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +000053 * <p>
54 * The C structure for a Tar Entry's header is:
55 * <pre>
Torsten Curdtca165392008-07-10 10:17:44 +000056 * struct header {
Sebastian Bazley8118f822009-04-02 23:34:48 +000057 * char name[100]; // TarConstants.NAMELEN - offset 0
58 * char mode[8]; // TarConstants.MODELEN - offset 100
59 * char uid[8]; // TarConstants.UIDLEN - offset 108
60 * char gid[8]; // TarConstants.GIDLEN - offset 116
61 * char size[12]; // TarConstants.SIZELEN - offset 124
62 * char mtime[12]; // TarConstants.MODTIMELEN - offset 136
63 * char chksum[8]; // TarConstants.CHKSUMLEN - offset 148
64 * char linkflag[1]; // - offset 156
65 * char linkname[100]; // TarConstants.NAMELEN - offset 157
Sebastian Bazley24f9c9b2009-04-02 15:19:17 +000066 * The following fields are only present in new-style POSIX tar archives:
Sebastian Bazley8118f822009-04-02 23:34:48 +000067 * char magic[6]; // TarConstants.MAGICLEN - offset 257
68 * char version[2]; // TarConstants.VERSIONLEN - offset 263
69 * char uname[32]; // TarConstants.UNAMELEN - offset 265
70 * char gname[32]; // TarConstants.GNAMELEN - offset 297
71 * char devmajor[8]; // TarConstants.DEVLEN - offset 329
72 * char devminor[8]; // TarConstants.DEVLEN - offset 337
73 * char prefix[155]; // TarConstants.PREFIXLEN - offset 345
74 * // Used if "name" field is not long enough to hold the path
75 * char pad[12]; // NULs - offset 500
Torsten Curdtca165392008-07-10 10:17:44 +000076 * } header;
Sebastian Bazley24f9c9b2009-04-02 15:19:17 +000077 * All unused bytes are set to null.
78 * New-style GNU tar files are slightly different from the above.
Torsten Curdtca165392008-07-10 10:17:44 +000079 * </pre>
Sebastian Bazley99870ef2009-03-28 00:04:36 +000080 *
81 * @NotThreadSafe
Torsten Curdtca165392008-07-10 10:17:44 +000082 */
Torsten Curdtca165392008-07-10 10:17:44 +000083
Torsten Curdt46ad24d2009-01-08 11:09:25 +000084public class TarArchiveEntry implements TarConstants, ArchiveEntry {
85 /** The entry's name. */
Sebastian Bazley1d556702009-04-02 18:45:02 +000086 private String name;
Torsten Curdtca165392008-07-10 10:17:44 +000087
Torsten Curdt46ad24d2009-01-08 11:09:25 +000088 /** The entry's permission mode. */
89 private int mode;
Torsten Curdtca165392008-07-10 10:17:44 +000090
Torsten Curdt46ad24d2009-01-08 11:09:25 +000091 /** The entry's user id. */
92 private int userId;
Torsten Curdtca165392008-07-10 10:17:44 +000093
Torsten Curdt46ad24d2009-01-08 11:09:25 +000094 /** The entry's group id. */
95 private int groupId;
Torsten Curdtca165392008-07-10 10:17:44 +000096
Torsten Curdt46ad24d2009-01-08 11:09:25 +000097 /** The entry's size. */
98 private long size;
Torsten Curdtca165392008-07-10 10:17:44 +000099
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000100 /** The entry's modification time. */
101 private long modTime;
Torsten Curdtca165392008-07-10 10:17:44 +0000102
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000103 /** The entry's link flag. */
104 private byte linkFlag;
Torsten Curdtca165392008-07-10 10:17:44 +0000105
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000106 /** The entry's link name. */
Sebastian Bazley1d556702009-04-02 18:45:02 +0000107 private String linkName;
Torsten Curdtca165392008-07-10 10:17:44 +0000108
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000109 /** The entry's magic tag. */
Sebastian Bazley1d556702009-04-02 18:45:02 +0000110 private String magic;
Sebastian Bazley8118f822009-04-02 23:34:48 +0000111 /** The version of the format */
112 private String version;
Torsten Curdtca165392008-07-10 10:17:44 +0000113
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000114 /** The entry's user name. */
Sebastian Bazley1d556702009-04-02 18:45:02 +0000115 private String userName;
Torsten Curdtca165392008-07-10 10:17:44 +0000116
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000117 /** The entry's group name. */
Sebastian Bazley1d556702009-04-02 18:45:02 +0000118 private String groupName;
Torsten Curdtca165392008-07-10 10:17:44 +0000119
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000120 /** The entry's major device number. */
121 private int devMajor;
Torsten Curdtca165392008-07-10 10:17:44 +0000122
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000123 /** The entry's minor device number. */
124 private int devMinor;
Torsten Curdtca165392008-07-10 10:17:44 +0000125
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000126 /** The entry's file reference */
127 private File file;
Torsten Curdtca165392008-07-10 10:17:44 +0000128
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000129 /** Maximum length of a user's name in the tar file */
130 public static final int MAX_NAMELEN = 31;
Torsten Curdtca165392008-07-10 10:17:44 +0000131
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000132 /** Default permissions bits for directories */
133 public static final int DEFAULT_DIR_MODE = 040755;
Torsten Curdtca165392008-07-10 10:17:44 +0000134
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000135 /** Default permissions bits for files */
136 public static final int DEFAULT_FILE_MODE = 0100644;
Torsten Curdtca165392008-07-10 10:17:44 +0000137
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000138 /** Convert millis to seconds */
139 public static final int MILLIS_PER_SECOND = 1000;
Torsten Curdtca165392008-07-10 10:17:44 +0000140
141 /**
142 * Construct an empty entry and prepares the header values.
143 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000144 private TarArchiveEntry () {
Sebastian Bazley1d556702009-04-02 18:45:02 +0000145 this.magic = MAGIC_POSIX;
Sebastian Bazley8118f822009-04-02 23:34:48 +0000146 this.version = VERSION_POSIX;
Sebastian Bazley1d556702009-04-02 18:45:02 +0000147 this.name = "";
148 this.linkName = "";
Torsten Curdtca165392008-07-10 10:17:44 +0000149
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000150 String user = System.getProperty("user.name", "");
151
152 if (user.length() > MAX_NAMELEN) {
153 user = user.substring(0, MAX_NAMELEN);
Torsten Curdtca165392008-07-10 10:17:44 +0000154 }
155
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000156 this.userId = 0;
157 this.groupId = 0;
Sebastian Bazley1d556702009-04-02 18:45:02 +0000158 this.userName = user;
159 this.groupName = "";
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000160 this.file = null;
Torsten Curdtca165392008-07-10 10:17:44 +0000161 }
162
163 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000164 * Construct an entry with only a name. This allows the programmer
165 * to construct the entry's header "by hand". File is set to null.
Torsten Curdtca165392008-07-10 10:17:44 +0000166 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000167 * @param name the entry name
Torsten Curdtca165392008-07-10 10:17:44 +0000168 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000169 public TarArchiveEntry(String name) {
Stefan Bodewig3e2ddad2009-08-25 08:11:44 +0000170 this(name, false);
171 }
172
173 /**
174 * Construct an entry with only a name. This allows the programmer
175 * to construct the entry's header "by hand". File is set to null.
176 *
177 * @param name the entry name
178 * @param preserveLeadingSlashes whether to allow leading slashes
179 * in the name.
180 */
181 public TarArchiveEntry(String name, boolean preserveLeadingSlashes) {
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000182 this();
183
Stefan Bodewig3e2ddad2009-08-25 08:11:44 +0000184 name = normalizeFileName(name, preserveLeadingSlashes);
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000185 boolean isDir = name.endsWith("/");
186
187 this.devMajor = 0;
188 this.devMinor = 0;
Sebastian Bazley1d556702009-04-02 18:45:02 +0000189 this.name = name;
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000190 this.mode = isDir ? DEFAULT_DIR_MODE : DEFAULT_FILE_MODE;
191 this.linkFlag = isDir ? LF_DIR : LF_NORMAL;
192 this.userId = 0;
193 this.groupId = 0;
194 this.size = 0;
195 this.modTime = (new Date()).getTime() / MILLIS_PER_SECOND;
Sebastian Bazley1d556702009-04-02 18:45:02 +0000196 this.linkName = "";
197 this.userName = "";
198 this.groupName = "";
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000199 this.devMajor = 0;
200 this.devMinor = 0;
201
Torsten Curdtca165392008-07-10 10:17:44 +0000202 }
203
204 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000205 * Construct an entry with a name and a link flag.
Torsten Curdtca165392008-07-10 10:17:44 +0000206 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000207 * @param name the entry name
208 * @param linkFlag the entry link flag.
Torsten Curdtca165392008-07-10 10:17:44 +0000209 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000210 public TarArchiveEntry(String name, byte linkFlag) {
211 this(name);
212 this.linkFlag = linkFlag;
Stefan Bodewig7bb5d882009-10-08 12:07:19 +0000213 if (linkFlag == LF_GNUTYPE_LONGNAME) {
214 magic = MAGIC_GNU;
215 version = VERSION_GNU_SPACE;
216 }
Torsten Curdtca165392008-07-10 10:17:44 +0000217 }
218
219 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000220 * Construct an entry for a file. File is set to file, and the
221 * header is constructed from information from the file.
Sebastian Bazleyfec51a12009-03-31 00:35:56 +0000222 * The name is set from the normalized file path.
Torsten Curdtca165392008-07-10 10:17:44 +0000223 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000224 * @param file The file that the entry represents.
Torsten Curdtca165392008-07-10 10:17:44 +0000225 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000226 public TarArchiveEntry(File file) {
Stefan Bodewig3e2ddad2009-08-25 08:11:44 +0000227 this(file, normalizeFileName(file.getPath(), false));
Sebastian Bazleyfec51a12009-03-31 00:35:56 +0000228 }
229
230 /**
231 * Construct an entry for a file. File is set to file, and the
232 * header is constructed from information from the file.
233 *
234 * @param file The file that the entry represents.
235 * @param fileName the name to be used for the entry.
236 */
237 public TarArchiveEntry(File file, String fileName) {
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000238 this();
Torsten Curdtca165392008-07-10 10:17:44 +0000239
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000240 this.file = file;
Torsten Curdtca165392008-07-10 10:17:44 +0000241
Sebastian Bazley1d556702009-04-02 18:45:02 +0000242 this.linkName = "";
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000243
244 if (file.isDirectory()) {
245 this.mode = DEFAULT_DIR_MODE;
246 this.linkFlag = LF_DIR;
247
Sebastian Bazley1d556702009-04-02 18:45:02 +0000248 int nameLength = fileName.length();
Christian Grobmeier3bb92822009-05-04 09:15:01 +0000249 if (nameLength == 0 || fileName.charAt(nameLength - 1) != '/') {
Sebastian Bazley1d556702009-04-02 18:45:02 +0000250 this.name = fileName + "/";
251 } else {
252 this.name = fileName;
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000253 }
Stefan Bodewigc013e282009-03-18 04:38:47 +0000254 this.size = 0;
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000255 } else {
256 this.mode = DEFAULT_FILE_MODE;
257 this.linkFlag = LF_NORMAL;
Stefan Bodewigc013e282009-03-18 04:38:47 +0000258 this.size = file.length();
Sebastian Bazley1d556702009-04-02 18:45:02 +0000259 this.name = fileName;
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000260 }
261
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000262 this.modTime = file.lastModified() / MILLIS_PER_SECOND;
263 this.devMajor = 0;
264 this.devMinor = 0;
Torsten Curdtca165392008-07-10 10:17:44 +0000265 }
266
267 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000268 * Construct an entry from an archive's header bytes. File is set
269 * to null.
Torsten Curdtca165392008-07-10 10:17:44 +0000270 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000271 * @param headerBuf The header bytes from a tar archive entry.
Torsten Curdtca165392008-07-10 10:17:44 +0000272 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000273 public TarArchiveEntry(byte[] headerBuf) {
274 this();
275 parseTarHeader(headerBuf);
Torsten Curdtca165392008-07-10 10:17:44 +0000276 }
277
278 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000279 * Determine if the two entries are equal. Equality is determined
280 * by the header names being equal.
Torsten Curdtca165392008-07-10 10:17:44 +0000281 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000282 * @param it Entry to be checked for equality.
283 * @return True if the entries are equal.
Torsten Curdtca165392008-07-10 10:17:44 +0000284 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000285 public boolean equals(TarArchiveEntry it) {
286 return getName().equals(it.getName());
Torsten Curdtca165392008-07-10 10:17:44 +0000287 }
288
289 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000290 * Determine if the two entries are equal. Equality is determined
291 * by the header names being equal.
Torsten Curdtca165392008-07-10 10:17:44 +0000292 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000293 * @param it Entry to be checked for equality.
294 * @return True if the entries are equal.
Torsten Curdtca165392008-07-10 10:17:44 +0000295 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000296 public boolean equals(Object it) {
297 if (it == null || getClass() != it.getClass()) {
298 return false;
299 }
300 return equals((TarArchiveEntry) it);
Torsten Curdtca165392008-07-10 10:17:44 +0000301 }
302
303 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000304 * Hashcodes are based on entry names.
Torsten Curdtca165392008-07-10 10:17:44 +0000305 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000306 * @return the entry hashcode
Torsten Curdtca165392008-07-10 10:17:44 +0000307 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000308 public int hashCode() {
309 return getName().hashCode();
Torsten Curdtca165392008-07-10 10:17:44 +0000310 }
311
312 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000313 * Determine if the given entry is a descendant of this entry.
314 * Descendancy is determined by the name of the descendant
315 * starting with this entry's name.
Torsten Curdtca165392008-07-10 10:17:44 +0000316 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000317 * @param desc Entry to be checked as a descendent of this.
318 * @return True if entry is a descendant of this.
Torsten Curdtca165392008-07-10 10:17:44 +0000319 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000320 public boolean isDescendent(TarArchiveEntry desc) {
321 return desc.getName().startsWith(getName());
Torsten Curdtca165392008-07-10 10:17:44 +0000322 }
323
324 /**
325 * Get this entry's name.
326 *
327 * @return This entry's name.
328 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000329 public String getName() {
330 return name.toString();
Torsten Curdtca165392008-07-10 10:17:44 +0000331 }
332
333 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000334 * Set this entry's name.
Torsten Curdtca165392008-07-10 10:17:44 +0000335 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000336 * @param name This entry's new name.
Torsten Curdtca165392008-07-10 10:17:44 +0000337 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000338 public void setName(String name) {
Stefan Bodewig3e2ddad2009-08-25 08:11:44 +0000339 this.name = normalizeFileName(name, false);
Torsten Curdtca165392008-07-10 10:17:44 +0000340 }
341
342 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000343 * Set the mode for this entry
Torsten Curdtca165392008-07-10 10:17:44 +0000344 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000345 * @param mode the mode for this entry
Torsten Curdtca165392008-07-10 10:17:44 +0000346 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000347 public void setMode(int mode) {
348 this.mode = mode;
Torsten Curdtca165392008-07-10 10:17:44 +0000349 }
350
351 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000352 * Get this entry's link name.
Torsten Curdtca165392008-07-10 10:17:44 +0000353 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000354 * @return This entry's link name.
Torsten Curdtca165392008-07-10 10:17:44 +0000355 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000356 public String getLinkName() {
357 return linkName.toString();
Torsten Curdtca165392008-07-10 10:17:44 +0000358 }
359
360 /**
361 * Get this entry's user id.
362 *
363 * @return This entry's user id.
364 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000365 public int getUserId() {
366 return userId;
367 }
368
369 /**
370 * Set this entry's user id.
371 *
372 * @param userId This entry's new user id.
373 */
374 public void setUserId(int userId) {
375 this.userId = userId;
376 }
377
378 /**
379 * Get this entry's group id.
380 *
381 * @return This entry's group id.
382 */
383 public int getGroupId() {
384 return groupId;
385 }
386
387 /**
388 * Set this entry's group id.
389 *
390 * @param groupId This entry's new group id.
391 */
392 public void setGroupId(int groupId) {
393 this.groupId = groupId;
Torsten Curdtca165392008-07-10 10:17:44 +0000394 }
395
396 /**
397 * Get this entry's user name.
398 *
399 * @return This entry's user name.
400 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000401 public String getUserName() {
402 return userName.toString();
Torsten Curdtca165392008-07-10 10:17:44 +0000403 }
404
405 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000406 * Set this entry's user name.
Torsten Curdtca165392008-07-10 10:17:44 +0000407 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000408 * @param userName This entry's new user name.
Torsten Curdtca165392008-07-10 10:17:44 +0000409 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000410 public void setUserName(String userName) {
Sebastian Bazley1d556702009-04-02 18:45:02 +0000411 this.userName = userName;
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000412 }
413
414 /**
415 * Get this entry's group name.
416 *
417 * @return This entry's group name.
418 */
419 public String getGroupName() {
420 return groupName.toString();
421 }
422
423 /**
424 * Set this entry's group name.
425 *
426 * @param groupName This entry's new group name.
427 */
428 public void setGroupName(String groupName) {
Sebastian Bazley1d556702009-04-02 18:45:02 +0000429 this.groupName = groupName;
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000430 }
431
432 /**
433 * Convenience method to set this entry's group and user ids.
434 *
435 * @param userId This entry's new user id.
436 * @param groupId This entry's new group id.
437 */
438 public void setIds(int userId, int groupId) {
439 setUserId(userId);
440 setGroupId(groupId);
441 }
442
443 /**
444 * Convenience method to set this entry's group and user names.
445 *
446 * @param userName This entry's new user name.
447 * @param groupName This entry's new group name.
448 */
449 public void setNames(String userName, String groupName) {
450 setUserName(userName);
451 setGroupName(groupName);
452 }
453
454 /**
455 * Set this entry's modification time. The parameter passed
456 * to this method is in "Java time".
457 *
458 * @param time This entry's new modification time.
459 */
460 public void setModTime(long time) {
461 modTime = time / MILLIS_PER_SECOND;
462 }
463
464 /**
465 * Set this entry's modification time.
466 *
467 * @param time This entry's new modification time.
468 */
469 public void setModTime(Date time) {
470 modTime = time.getTime() / MILLIS_PER_SECOND;
471 }
472
473 /**
474 * Set this entry's modification time.
475 *
476 * @return time This entry's new modification time.
477 */
478 public Date getModTime() {
479 return new Date(modTime * MILLIS_PER_SECOND);
480 }
481
Stefan Bodewig17ffd7f2009-08-01 14:52:15 +0000482 /** {@inheritDocs} */
483 public Date getLastModifiedDate() {
484 return getModTime();
485 }
486
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000487 /**
488 * Get this entry's file.
489 *
490 * @return This entry's file.
491 */
492 public File getFile() {
493 return file;
494 }
495
496 /**
497 * Get this entry's mode.
498 *
499 * @return This entry's mode.
500 */
501 public int getMode() {
502 return mode;
503 }
504
505 /**
506 * Get this entry's file size.
507 *
508 * @return This entry's file size.
509 */
510 public long getSize() {
511 return size;
512 }
513
514 /**
515 * Set this entry's file size.
516 *
517 * @param size This entry's new file size.
Sebastian Bazley26d12c52009-03-31 18:45:14 +0000518 * @throws IllegalArgumentException if the size is < 0
519 * or > {@link TarConstants#MAXSIZE} (077777777777L).
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000520 */
521 public void setSize(long size) {
Sebastian Bazley26d12c52009-03-31 18:45:14 +0000522 if (size > MAXSIZE || size < 0){
523 throw new IllegalArgumentException("Size is out of range: "+size);
524 }
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000525 this.size = size;
526 }
527
528
529 /**
530 * Indicate if this entry is a GNU long name block
531 *
532 * @return true if this is a long name extension provided by GNU tar
533 */
534 public boolean isGNULongNameEntry() {
535 return linkFlag == LF_GNUTYPE_LONGNAME
Stefan Bodewig75f92f62009-03-17 12:14:17 +0000536 && name.toString().equals(GNU_LONGLINK);
Torsten Curdtca165392008-07-10 10:17:44 +0000537 }
538
539 /**
540 * Return whether or not this entry represents a directory.
541 *
542 * @return True if this entry is a directory.
543 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000544 public boolean isDirectory() {
545 if (file != null) {
546 return file.isDirectory();
Torsten Curdtca165392008-07-10 10:17:44 +0000547 }
548
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000549 if (linkFlag == LF_DIR) {
Torsten Curdtca165392008-07-10 10:17:44 +0000550 return true;
551 }
552
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000553 if (getName().endsWith("/")) {
Torsten Curdtca165392008-07-10 10:17:44 +0000554 return true;
555 }
556
557 return false;
558 }
559
560 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000561 * If this entry represents a file, and the file is a directory, return
562 * an array of TarEntries for this entry's children.
Torsten Curdtca165392008-07-10 10:17:44 +0000563 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000564 * @return An array of TarEntry's for this entry's children.
Torsten Curdtca165392008-07-10 10:17:44 +0000565 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000566 public TarArchiveEntry[] getDirectoryEntries() {
567 if (file == null || !file.isDirectory()) {
568 return new TarArchiveEntry[0];
569 }
570
571 String[] list = file.list();
572 TarArchiveEntry[] result = new TarArchiveEntry[list.length];
573
574 for (int i = 0; i < list.length; ++i) {
575 result[i] = new TarArchiveEntry(new File(file, list[i]));
576 }
577
578 return result;
Torsten Curdtca165392008-07-10 10:17:44 +0000579 }
580
581 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000582 * Write an entry's header information to a header buffer.
Torsten Curdtca165392008-07-10 10:17:44 +0000583 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000584 * @param outbuf The tar entry header buffer to fill in.
Torsten Curdtca165392008-07-10 10:17:44 +0000585 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000586 public void writeEntryHeader(byte[] outbuf) {
587 int offset = 0;
588
Sebastian Bazley1d556702009-04-02 18:45:02 +0000589 offset = TarUtils.formatNameBytes(name, outbuf, offset, NAMELEN);
590 offset = TarUtils.formatOctalBytes(mode, outbuf, offset, MODELEN);
591 offset = TarUtils.formatOctalBytes(userId, outbuf, offset, UIDLEN);
592 offset = TarUtils.formatOctalBytes(groupId, outbuf, offset, GIDLEN);
593 offset = TarUtils.formatLongOctalBytes(size, outbuf, offset, SIZELEN);
594 offset = TarUtils.formatLongOctalBytes(modTime, outbuf, offset, MODTIMELEN);
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000595
596 int csOffset = offset;
597
598 for (int c = 0; c < CHKSUMLEN; ++c) {
599 outbuf[offset++] = (byte) ' ';
600 }
601
602 outbuf[offset++] = linkFlag;
Sebastian Bazley1d556702009-04-02 18:45:02 +0000603 offset = TarUtils.formatNameBytes(linkName, outbuf, offset, NAMELEN);
604 offset = TarUtils.formatNameBytes(magic, outbuf, offset, MAGICLEN);
Sebastian Bazley8118f822009-04-02 23:34:48 +0000605 offset = TarUtils.formatNameBytes(version, outbuf, offset, VERSIONLEN);
Sebastian Bazley1d556702009-04-02 18:45:02 +0000606 offset = TarUtils.formatNameBytes(userName, outbuf, offset, UNAMELEN);
607 offset = TarUtils.formatNameBytes(groupName, outbuf, offset, GNAMELEN);
608 offset = TarUtils.formatOctalBytes(devMajor, outbuf, offset, DEVLEN);
609 offset = TarUtils.formatOctalBytes(devMinor, outbuf, offset, DEVLEN);
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000610
611 while (offset < outbuf.length) {
612 outbuf[offset++] = 0;
613 }
614
615 long chk = TarUtils.computeCheckSum(outbuf);
616
Sebastian Bazley1d556702009-04-02 18:45:02 +0000617 TarUtils.formatCheckSumOctalBytes(chk, outbuf, csOffset, CHKSUMLEN);
Torsten Curdtca165392008-07-10 10:17:44 +0000618 }
619
620 /**
621 * Parse an entry's header information from a header buffer.
622 *
623 * @param header The tar entry header buffer to get information from.
624 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000625 public void parseTarHeader(byte[] header) {
Torsten Curdtca165392008-07-10 10:17:44 +0000626 int offset = 0;
627
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000628 name = TarUtils.parseName(header, offset, NAMELEN);
Torsten Curdtca165392008-07-10 10:17:44 +0000629 offset += NAMELEN;
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000630 mode = (int) TarUtils.parseOctal(header, offset, MODELEN);
631 offset += MODELEN;
632 userId = (int) TarUtils.parseOctal(header, offset, UIDLEN);
633 offset += UIDLEN;
634 groupId = (int) TarUtils.parseOctal(header, offset, GIDLEN);
635 offset += GIDLEN;
636 size = TarUtils.parseOctal(header, offset, SIZELEN);
637 offset += SIZELEN;
638 modTime = TarUtils.parseOctal(header, offset, MODTIMELEN);
639 offset += MODTIMELEN;
640 offset += CHKSUMLEN;
641 linkFlag = header[offset++];
642 linkName = TarUtils.parseName(header, offset, NAMELEN);
Torsten Curdtca165392008-07-10 10:17:44 +0000643 offset += NAMELEN;
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000644 magic = TarUtils.parseName(header, offset, MAGICLEN);
645 offset += MAGICLEN;
Sebastian Bazley8118f822009-04-02 23:34:48 +0000646 version = TarUtils.parseName(header, offset, VERSIONLEN);
647 offset += VERSIONLEN;
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000648 userName = TarUtils.parseName(header, offset, UNAMELEN);
649 offset += UNAMELEN;
650 groupName = TarUtils.parseName(header, offset, GNAMELEN);
651 offset += GNAMELEN;
652 devMajor = (int) TarUtils.parseOctal(header, offset, DEVLEN);
653 offset += DEVLEN;
654 devMinor = (int) TarUtils.parseOctal(header, offset, DEVLEN);
Torsten Curdtca165392008-07-10 10:17:44 +0000655 }
Stefan Bodewig32eea1e2009-03-17 12:53:22 +0000656
657 /**
658 * Strips Windows' drive letter as well as any leading slashes,
659 * turns path separators into forward slahes.
660 */
Stefan Bodewig3e2ddad2009-08-25 08:11:44 +0000661 private static String normalizeFileName(String fileName,
662 boolean preserveLeadingSlashes) {
Stefan Bodewig32eea1e2009-03-17 12:53:22 +0000663 String osname = System.getProperty("os.name").toLowerCase(Locale.US);
664
665 if (osname != null) {
666
667 // Strip off drive letters!
668 // REVIEW Would a better check be "(File.separator == '\')"?
669
670 if (osname.startsWith("windows")) {
671 if (fileName.length() > 2) {
672 char ch1 = fileName.charAt(0);
673 char ch2 = fileName.charAt(1);
674
675 if (ch2 == ':'
676 && ((ch1 >= 'a' && ch1 <= 'z')
677 || (ch1 >= 'A' && ch1 <= 'Z'))) {
678 fileName = fileName.substring(2);
679 }
680 }
681 } else if (osname.indexOf("netware") > -1) {
682 int colon = fileName.indexOf(':');
683 if (colon != -1) {
684 fileName = fileName.substring(colon + 1);
685 }
686 }
687 }
688
689 fileName = fileName.replace(File.separatorChar, '/');
690
691 // No absolute pathnames
692 // Windows (and Posix?) paths can start with "\\NetworkDrive\",
693 // so we loop on starting /'s.
Stefan Bodewig3e2ddad2009-08-25 08:11:44 +0000694 while (!preserveLeadingSlashes && fileName.startsWith("/")) {
Stefan Bodewig32eea1e2009-03-17 12:53:22 +0000695 fileName = fileName.substring(1);
696 }
697 return fileName;
698 }
Torsten Curdtca165392008-07-10 10:17:44 +0000699}
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000700