blob: 91283a8092da8871a7c0f15385eb95cb76158a7b [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 Bazley24f9c9b2009-04-02 15:19:17 +000057 * char name[100]; // TarConstants.NAMELEN
58 * char mode[8]; // TarConstants.MODELEN
59 * char uid[8]; // TarConstants.UIDLEN
60 * char gid[8]; // TarConstants.GIDLEN
61 * char size[12]; // TarConstants.SIZELEN
62 * char mtime[12]; // TarConstants.MODTIMELEN
63 * char chksum[8]; // TarConstants.CHKSUMLEN
64 * char linkflag[1];
65 * char linkname[100]; // TarConstants.NAMELEN
66 * The following fields are only present in new-style POSIX tar archives:
67 * char magic[8]; // TarConstants.MAGICLEN
68 * TODO: Posix/GNU split this into magic[6] and char version[2];
69 * char uname[32]; // TarConstants.UNAMELEN
70 * char gname[32]; // TarConstants.GNAMELEN
71 * char devmajor[8]; // TarConstants.DEVLEN
72 * char devminor[8]; // TarConstants.DEVLEN
73 * char prefix[155]; // Used if "name" field is not long enough to hold the path
74 * char pad[12]; // NULs
Torsten Curdtca165392008-07-10 10:17:44 +000075 * } header;
Sebastian Bazley24f9c9b2009-04-02 15:19:17 +000076 * All unused bytes are set to null.
77 * New-style GNU tar files are slightly different from the above.
Torsten Curdtca165392008-07-10 10:17:44 +000078 * </pre>
Sebastian Bazley99870ef2009-03-28 00:04:36 +000079 *
80 * @NotThreadSafe
Torsten Curdtca165392008-07-10 10:17:44 +000081 */
Torsten Curdtca165392008-07-10 10:17:44 +000082
Torsten Curdt46ad24d2009-01-08 11:09:25 +000083public class TarArchiveEntry implements TarConstants, ArchiveEntry {
84 /** The entry's name. */
85 private StringBuffer name;
Torsten Curdtca165392008-07-10 10:17:44 +000086
Torsten Curdt46ad24d2009-01-08 11:09:25 +000087 /** The entry's permission mode. */
88 private int mode;
Torsten Curdtca165392008-07-10 10:17:44 +000089
Torsten Curdt46ad24d2009-01-08 11:09:25 +000090 /** The entry's user id. */
91 private int userId;
Torsten Curdtca165392008-07-10 10:17:44 +000092
Torsten Curdt46ad24d2009-01-08 11:09:25 +000093 /** The entry's group id. */
94 private int groupId;
Torsten Curdtca165392008-07-10 10:17:44 +000095
Torsten Curdt46ad24d2009-01-08 11:09:25 +000096 /** The entry's size. */
97 private long size;
Torsten Curdtca165392008-07-10 10:17:44 +000098
Torsten Curdt46ad24d2009-01-08 11:09:25 +000099 /** The entry's modification time. */
100 private long modTime;
Torsten Curdtca165392008-07-10 10:17:44 +0000101
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000102 /** The entry's link flag. */
103 private byte linkFlag;
Torsten Curdtca165392008-07-10 10:17:44 +0000104
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000105 /** The entry's link name. */
106 private StringBuffer linkName;
Torsten Curdtca165392008-07-10 10:17:44 +0000107
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000108 /** The entry's magic tag. */
109 private StringBuffer magic;
Torsten Curdtca165392008-07-10 10:17:44 +0000110
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000111 /** The entry's user name. */
112 private StringBuffer userName;
Torsten Curdtca165392008-07-10 10:17:44 +0000113
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000114 /** The entry's group name. */
115 private StringBuffer groupName;
Torsten Curdtca165392008-07-10 10:17:44 +0000116
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000117 /** The entry's major device number. */
118 private int devMajor;
Torsten Curdtca165392008-07-10 10:17:44 +0000119
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000120 /** The entry's minor device number. */
121 private int devMinor;
Torsten Curdtca165392008-07-10 10:17:44 +0000122
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000123 /** The entry's file reference */
124 private File file;
Torsten Curdtca165392008-07-10 10:17:44 +0000125
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000126 /** Maximum length of a user's name in the tar file */
127 public static final int MAX_NAMELEN = 31;
Torsten Curdtca165392008-07-10 10:17:44 +0000128
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000129 /** Default permissions bits for directories */
130 public static final int DEFAULT_DIR_MODE = 040755;
Torsten Curdtca165392008-07-10 10:17:44 +0000131
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000132 /** Default permissions bits for files */
133 public static final int DEFAULT_FILE_MODE = 0100644;
Torsten Curdtca165392008-07-10 10:17:44 +0000134
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000135 /** Convert millis to seconds */
136 public static final int MILLIS_PER_SECOND = 1000;
Torsten Curdtca165392008-07-10 10:17:44 +0000137
138 /**
139 * Construct an empty entry and prepares the header values.
140 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000141 private TarArchiveEntry () {
Sebastian Bazley24f9c9b2009-04-02 15:19:17 +0000142 this.magic = new StringBuffer(MAGIC_POSIX);
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000143 this.name = new StringBuffer();
144 this.linkName = new StringBuffer();
Torsten Curdtca165392008-07-10 10:17:44 +0000145
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000146 String user = System.getProperty("user.name", "");
147
148 if (user.length() > MAX_NAMELEN) {
149 user = user.substring(0, MAX_NAMELEN);
Torsten Curdtca165392008-07-10 10:17:44 +0000150 }
151
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000152 this.userId = 0;
153 this.groupId = 0;
154 this.userName = new StringBuffer(user);
155 this.groupName = new StringBuffer("");
156 this.file = null;
Torsten Curdtca165392008-07-10 10:17:44 +0000157 }
158
159 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000160 * Construct an entry with only a name. This allows the programmer
161 * to construct the entry's header "by hand". File is set to null.
Torsten Curdtca165392008-07-10 10:17:44 +0000162 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000163 * @param name the entry name
Torsten Curdtca165392008-07-10 10:17:44 +0000164 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000165 public TarArchiveEntry(String name) {
166 this();
167
Stefan Bodewig32eea1e2009-03-17 12:53:22 +0000168 name = normalizeFileName(name);
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000169 boolean isDir = name.endsWith("/");
170
171 this.devMajor = 0;
172 this.devMinor = 0;
173 this.name = new StringBuffer(name);
174 this.mode = isDir ? DEFAULT_DIR_MODE : DEFAULT_FILE_MODE;
175 this.linkFlag = isDir ? LF_DIR : LF_NORMAL;
176 this.userId = 0;
177 this.groupId = 0;
178 this.size = 0;
179 this.modTime = (new Date()).getTime() / MILLIS_PER_SECOND;
180 this.linkName = new StringBuffer("");
181 this.userName = new StringBuffer("");
182 this.groupName = new StringBuffer("");
183 this.devMajor = 0;
184 this.devMinor = 0;
185
Torsten Curdtca165392008-07-10 10:17:44 +0000186 }
187
188 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000189 * Construct an entry with a name and a link flag.
Torsten Curdtca165392008-07-10 10:17:44 +0000190 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000191 * @param name the entry name
192 * @param linkFlag the entry link flag.
Torsten Curdtca165392008-07-10 10:17:44 +0000193 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000194 public TarArchiveEntry(String name, byte linkFlag) {
195 this(name);
196 this.linkFlag = linkFlag;
Torsten Curdtca165392008-07-10 10:17:44 +0000197 }
198
199 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000200 * Construct an entry for a file. File is set to file, and the
201 * header is constructed from information from the file.
Sebastian Bazleyfec51a12009-03-31 00:35:56 +0000202 * The name is set from the normalized file path.
Torsten Curdtca165392008-07-10 10:17:44 +0000203 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000204 * @param file The file that the entry represents.
Torsten Curdtca165392008-07-10 10:17:44 +0000205 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000206 public TarArchiveEntry(File file) {
Sebastian Bazleyfec51a12009-03-31 00:35:56 +0000207 this(file, normalizeFileName(file.getPath()));
208 }
209
210 /**
211 * Construct an entry for a file. File is set to file, and the
212 * header is constructed from information from the file.
213 *
214 * @param file The file that the entry represents.
215 * @param fileName the name to be used for the entry.
216 */
217 public TarArchiveEntry(File file, String fileName) {
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000218 this();
Torsten Curdtca165392008-07-10 10:17:44 +0000219
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000220 this.file = file;
Torsten Curdtca165392008-07-10 10:17:44 +0000221
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000222 this.linkName = new StringBuffer("");
223 this.name = new StringBuffer(fileName);
224
225 if (file.isDirectory()) {
226 this.mode = DEFAULT_DIR_MODE;
227 this.linkFlag = LF_DIR;
228
Stefan Bodewig32eea1e2009-03-17 12:53:22 +0000229 int nameLength = name.length();
230 if (nameLength == 0 || name.charAt(nameLength - 1) != '/') {
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000231 this.name.append("/");
232 }
Stefan Bodewigc013e282009-03-18 04:38:47 +0000233 this.size = 0;
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000234 } else {
235 this.mode = DEFAULT_FILE_MODE;
236 this.linkFlag = LF_NORMAL;
Stefan Bodewigc013e282009-03-18 04:38:47 +0000237 this.size = file.length();
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000238 }
239
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000240 this.modTime = file.lastModified() / MILLIS_PER_SECOND;
241 this.devMajor = 0;
242 this.devMinor = 0;
Torsten Curdtca165392008-07-10 10:17:44 +0000243 }
244
245 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000246 * Construct an entry from an archive's header bytes. File is set
247 * to null.
Torsten Curdtca165392008-07-10 10:17:44 +0000248 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000249 * @param headerBuf The header bytes from a tar archive entry.
Torsten Curdtca165392008-07-10 10:17:44 +0000250 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000251 public TarArchiveEntry(byte[] headerBuf) {
252 this();
253 parseTarHeader(headerBuf);
Torsten Curdtca165392008-07-10 10:17:44 +0000254 }
255
256 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000257 * Determine if the two entries are equal. Equality is determined
258 * by the header names being equal.
Torsten Curdtca165392008-07-10 10:17:44 +0000259 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000260 * @param it Entry to be checked for equality.
261 * @return True if the entries are equal.
Torsten Curdtca165392008-07-10 10:17:44 +0000262 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000263 public boolean equals(TarArchiveEntry it) {
264 return getName().equals(it.getName());
Torsten Curdtca165392008-07-10 10:17:44 +0000265 }
266
267 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000268 * Determine if the two entries are equal. Equality is determined
269 * by the header names being equal.
Torsten Curdtca165392008-07-10 10:17:44 +0000270 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000271 * @param it Entry to be checked for equality.
272 * @return True if the entries are equal.
Torsten Curdtca165392008-07-10 10:17:44 +0000273 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000274 public boolean equals(Object it) {
275 if (it == null || getClass() != it.getClass()) {
276 return false;
277 }
278 return equals((TarArchiveEntry) it);
Torsten Curdtca165392008-07-10 10:17:44 +0000279 }
280
281 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000282 * Hashcodes are based on entry names.
Torsten Curdtca165392008-07-10 10:17:44 +0000283 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000284 * @return the entry hashcode
Torsten Curdtca165392008-07-10 10:17:44 +0000285 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000286 public int hashCode() {
287 return getName().hashCode();
Torsten Curdtca165392008-07-10 10:17:44 +0000288 }
289
290 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000291 * Determine if the given entry is a descendant of this entry.
292 * Descendancy is determined by the name of the descendant
293 * starting with this entry's name.
Torsten Curdtca165392008-07-10 10:17:44 +0000294 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000295 * @param desc Entry to be checked as a descendent of this.
296 * @return True if entry is a descendant of this.
Torsten Curdtca165392008-07-10 10:17:44 +0000297 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000298 public boolean isDescendent(TarArchiveEntry desc) {
299 return desc.getName().startsWith(getName());
Torsten Curdtca165392008-07-10 10:17:44 +0000300 }
301
302 /**
303 * Get this entry's name.
304 *
305 * @return This entry's name.
306 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000307 public String getName() {
308 return name.toString();
Torsten Curdtca165392008-07-10 10:17:44 +0000309 }
310
311 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000312 * Set this entry's name.
Torsten Curdtca165392008-07-10 10:17:44 +0000313 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000314 * @param name This entry's new name.
Torsten Curdtca165392008-07-10 10:17:44 +0000315 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000316 public void setName(String name) {
Stefan Bodewig32eea1e2009-03-17 12:53:22 +0000317 this.name = new StringBuffer(normalizeFileName(name));
Torsten Curdtca165392008-07-10 10:17:44 +0000318 }
319
320 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000321 * Set the mode for this entry
Torsten Curdtca165392008-07-10 10:17:44 +0000322 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000323 * @param mode the mode for this entry
Torsten Curdtca165392008-07-10 10:17:44 +0000324 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000325 public void setMode(int mode) {
326 this.mode = mode;
Torsten Curdtca165392008-07-10 10:17:44 +0000327 }
328
329 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000330 * Get this entry's link name.
Torsten Curdtca165392008-07-10 10:17:44 +0000331 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000332 * @return This entry's link name.
Torsten Curdtca165392008-07-10 10:17:44 +0000333 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000334 public String getLinkName() {
335 return linkName.toString();
Torsten Curdtca165392008-07-10 10:17:44 +0000336 }
337
338 /**
339 * Get this entry's user id.
340 *
341 * @return This entry's user id.
342 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000343 public int getUserId() {
344 return userId;
345 }
346
347 /**
348 * Set this entry's user id.
349 *
350 * @param userId This entry's new user id.
351 */
352 public void setUserId(int userId) {
353 this.userId = userId;
354 }
355
356 /**
357 * Get this entry's group id.
358 *
359 * @return This entry's group id.
360 */
361 public int getGroupId() {
362 return groupId;
363 }
364
365 /**
366 * Set this entry's group id.
367 *
368 * @param groupId This entry's new group id.
369 */
370 public void setGroupId(int groupId) {
371 this.groupId = groupId;
Torsten Curdtca165392008-07-10 10:17:44 +0000372 }
373
374 /**
375 * Get this entry's user name.
376 *
377 * @return This entry's user name.
378 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000379 public String getUserName() {
380 return userName.toString();
Torsten Curdtca165392008-07-10 10:17:44 +0000381 }
382
383 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000384 * Set this entry's user name.
Torsten Curdtca165392008-07-10 10:17:44 +0000385 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000386 * @param userName This entry's new user name.
Torsten Curdtca165392008-07-10 10:17:44 +0000387 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000388 public void setUserName(String userName) {
389 this.userName = new StringBuffer(userName);
390 }
391
392 /**
393 * Get this entry's group name.
394 *
395 * @return This entry's group name.
396 */
397 public String getGroupName() {
398 return groupName.toString();
399 }
400
401 /**
402 * Set this entry's group name.
403 *
404 * @param groupName This entry's new group name.
405 */
406 public void setGroupName(String groupName) {
407 this.groupName = new StringBuffer(groupName);
408 }
409
410 /**
411 * Convenience method to set this entry's group and user ids.
412 *
413 * @param userId This entry's new user id.
414 * @param groupId This entry's new group id.
415 */
416 public void setIds(int userId, int groupId) {
417 setUserId(userId);
418 setGroupId(groupId);
419 }
420
421 /**
422 * Convenience method to set this entry's group and user names.
423 *
424 * @param userName This entry's new user name.
425 * @param groupName This entry's new group name.
426 */
427 public void setNames(String userName, String groupName) {
428 setUserName(userName);
429 setGroupName(groupName);
430 }
431
432 /**
433 * Set this entry's modification time. The parameter passed
434 * to this method is in "Java time".
435 *
436 * @param time This entry's new modification time.
437 */
438 public void setModTime(long time) {
439 modTime = time / MILLIS_PER_SECOND;
440 }
441
442 /**
443 * Set this entry's modification time.
444 *
445 * @param time This entry's new modification time.
446 */
447 public void setModTime(Date time) {
448 modTime = time.getTime() / MILLIS_PER_SECOND;
449 }
450
451 /**
452 * Set this entry's modification time.
453 *
454 * @return time This entry's new modification time.
455 */
456 public Date getModTime() {
457 return new Date(modTime * MILLIS_PER_SECOND);
458 }
459
460 /**
461 * Get this entry's file.
462 *
463 * @return This entry's file.
464 */
465 public File getFile() {
466 return file;
467 }
468
469 /**
470 * Get this entry's mode.
471 *
472 * @return This entry's mode.
473 */
474 public int getMode() {
475 return mode;
476 }
477
478 /**
479 * Get this entry's file size.
480 *
481 * @return This entry's file size.
482 */
483 public long getSize() {
484 return size;
485 }
486
487 /**
488 * Set this entry's file size.
489 *
490 * @param size This entry's new file size.
Sebastian Bazley26d12c52009-03-31 18:45:14 +0000491 * @throws IllegalArgumentException if the size is < 0
492 * or > {@link TarConstants#MAXSIZE} (077777777777L).
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000493 */
494 public void setSize(long size) {
Sebastian Bazley26d12c52009-03-31 18:45:14 +0000495 if (size > MAXSIZE || size < 0){
496 throw new IllegalArgumentException("Size is out of range: "+size);
497 }
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000498 this.size = size;
499 }
500
501
502 /**
503 * Indicate if this entry is a GNU long name block
504 *
505 * @return true if this is a long name extension provided by GNU tar
506 */
507 public boolean isGNULongNameEntry() {
508 return linkFlag == LF_GNUTYPE_LONGNAME
Stefan Bodewig75f92f62009-03-17 12:14:17 +0000509 && name.toString().equals(GNU_LONGLINK);
Torsten Curdtca165392008-07-10 10:17:44 +0000510 }
511
512 /**
513 * Return whether or not this entry represents a directory.
514 *
515 * @return True if this entry is a directory.
516 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000517 public boolean isDirectory() {
518 if (file != null) {
519 return file.isDirectory();
Torsten Curdtca165392008-07-10 10:17:44 +0000520 }
521
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000522 if (linkFlag == LF_DIR) {
Torsten Curdtca165392008-07-10 10:17:44 +0000523 return true;
524 }
525
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000526 if (getName().endsWith("/")) {
Torsten Curdtca165392008-07-10 10:17:44 +0000527 return true;
528 }
529
530 return false;
531 }
532
533 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000534 * If this entry represents a file, and the file is a directory, return
535 * an array of TarEntries for this entry's children.
Torsten Curdtca165392008-07-10 10:17:44 +0000536 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000537 * @return An array of TarEntry's for this entry's children.
Torsten Curdtca165392008-07-10 10:17:44 +0000538 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000539 public TarArchiveEntry[] getDirectoryEntries() {
540 if (file == null || !file.isDirectory()) {
541 return new TarArchiveEntry[0];
542 }
543
544 String[] list = file.list();
545 TarArchiveEntry[] result = new TarArchiveEntry[list.length];
546
547 for (int i = 0; i < list.length; ++i) {
548 result[i] = new TarArchiveEntry(new File(file, list[i]));
549 }
550
551 return result;
Torsten Curdtca165392008-07-10 10:17:44 +0000552 }
553
554 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000555 * Write an entry's header information to a header buffer.
Torsten Curdtca165392008-07-10 10:17:44 +0000556 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000557 * @param outbuf The tar entry header buffer to fill in.
Torsten Curdtca165392008-07-10 10:17:44 +0000558 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000559 public void writeEntryHeader(byte[] outbuf) {
560 int offset = 0;
561
562 offset = TarUtils.getNameBytes(name, outbuf, offset, NAMELEN);
563 offset = TarUtils.getOctalBytes(mode, outbuf, offset, MODELEN);
564 offset = TarUtils.getOctalBytes(userId, outbuf, offset, UIDLEN);
565 offset = TarUtils.getOctalBytes(groupId, outbuf, offset, GIDLEN);
566 offset = TarUtils.getLongOctalBytes(size, outbuf, offset, SIZELEN);
567 offset = TarUtils.getLongOctalBytes(modTime, outbuf, offset, MODTIMELEN);
568
569 int csOffset = offset;
570
571 for (int c = 0; c < CHKSUMLEN; ++c) {
572 outbuf[offset++] = (byte) ' ';
573 }
574
575 outbuf[offset++] = linkFlag;
576 offset = TarUtils.getNameBytes(linkName, outbuf, offset, NAMELEN);
577 offset = TarUtils.getNameBytes(magic, outbuf, offset, MAGICLEN);
578 offset = TarUtils.getNameBytes(userName, outbuf, offset, UNAMELEN);
579 offset = TarUtils.getNameBytes(groupName, outbuf, offset, GNAMELEN);
580 offset = TarUtils.getOctalBytes(devMajor, outbuf, offset, DEVLEN);
581 offset = TarUtils.getOctalBytes(devMinor, outbuf, offset, DEVLEN);
582
583 while (offset < outbuf.length) {
584 outbuf[offset++] = 0;
585 }
586
587 long chk = TarUtils.computeCheckSum(outbuf);
588
589 TarUtils.getCheckSumOctalBytes(chk, outbuf, csOffset, CHKSUMLEN);
Torsten Curdtca165392008-07-10 10:17:44 +0000590 }
591
592 /**
593 * Parse an entry's header information from a header buffer.
594 *
595 * @param header The tar entry header buffer to get information from.
596 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000597 public void parseTarHeader(byte[] header) {
Torsten Curdtca165392008-07-10 10:17:44 +0000598 int offset = 0;
599
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000600 name = TarUtils.parseName(header, offset, NAMELEN);
Torsten Curdtca165392008-07-10 10:17:44 +0000601 offset += NAMELEN;
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000602 mode = (int) TarUtils.parseOctal(header, offset, MODELEN);
603 offset += MODELEN;
604 userId = (int) TarUtils.parseOctal(header, offset, UIDLEN);
605 offset += UIDLEN;
606 groupId = (int) TarUtils.parseOctal(header, offset, GIDLEN);
607 offset += GIDLEN;
608 size = TarUtils.parseOctal(header, offset, SIZELEN);
609 offset += SIZELEN;
610 modTime = TarUtils.parseOctal(header, offset, MODTIMELEN);
611 offset += MODTIMELEN;
612 offset += CHKSUMLEN;
613 linkFlag = header[offset++];
614 linkName = TarUtils.parseName(header, offset, NAMELEN);
Torsten Curdtca165392008-07-10 10:17:44 +0000615 offset += NAMELEN;
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000616 magic = TarUtils.parseName(header, offset, MAGICLEN);
617 offset += MAGICLEN;
618 userName = TarUtils.parseName(header, offset, UNAMELEN);
619 offset += UNAMELEN;
620 groupName = TarUtils.parseName(header, offset, GNAMELEN);
621 offset += GNAMELEN;
622 devMajor = (int) TarUtils.parseOctal(header, offset, DEVLEN);
623 offset += DEVLEN;
624 devMinor = (int) TarUtils.parseOctal(header, offset, DEVLEN);
Torsten Curdtca165392008-07-10 10:17:44 +0000625 }
Stefan Bodewig32eea1e2009-03-17 12:53:22 +0000626
627 /**
628 * Strips Windows' drive letter as well as any leading slashes,
629 * turns path separators into forward slahes.
630 */
631 private static String normalizeFileName(String fileName) {
632 String osname = System.getProperty("os.name").toLowerCase(Locale.US);
633
634 if (osname != null) {
635
636 // Strip off drive letters!
637 // REVIEW Would a better check be "(File.separator == '\')"?
638
639 if (osname.startsWith("windows")) {
640 if (fileName.length() > 2) {
641 char ch1 = fileName.charAt(0);
642 char ch2 = fileName.charAt(1);
643
644 if (ch2 == ':'
645 && ((ch1 >= 'a' && ch1 <= 'z')
646 || (ch1 >= 'A' && ch1 <= 'Z'))) {
647 fileName = fileName.substring(2);
648 }
649 }
650 } else if (osname.indexOf("netware") > -1) {
651 int colon = fileName.indexOf(':');
652 if (colon != -1) {
653 fileName = fileName.substring(colon + 1);
654 }
655 }
656 }
657
658 fileName = fileName.replace(File.separatorChar, '/');
659
660 // No absolute pathnames
661 // Windows (and Posix?) paths can start with "\\NetworkDrive\",
662 // so we loop on starting /'s.
663 while (fileName.startsWith("/")) {
664 fileName = fileName.substring(1);
665 }
666 return fileName;
667 }
Torsten Curdtca165392008-07-10 10:17:44 +0000668}
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000669