blob: 860d0eb5fa25c18f5c4ad727c8410961e4095429 [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) {
170 this();
171
Stefan Bodewig32eea1e2009-03-17 12:53:22 +0000172 name = normalizeFileName(name);
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000173 boolean isDir = name.endsWith("/");
174
175 this.devMajor = 0;
176 this.devMinor = 0;
Sebastian Bazley1d556702009-04-02 18:45:02 +0000177 this.name = name;
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000178 this.mode = isDir ? DEFAULT_DIR_MODE : DEFAULT_FILE_MODE;
179 this.linkFlag = isDir ? LF_DIR : LF_NORMAL;
180 this.userId = 0;
181 this.groupId = 0;
182 this.size = 0;
183 this.modTime = (new Date()).getTime() / MILLIS_PER_SECOND;
Sebastian Bazley1d556702009-04-02 18:45:02 +0000184 this.linkName = "";
185 this.userName = "";
186 this.groupName = "";
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000187 this.devMajor = 0;
188 this.devMinor = 0;
189
Torsten Curdtca165392008-07-10 10:17:44 +0000190 }
191
192 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000193 * Construct an entry with a name and a link flag.
Torsten Curdtca165392008-07-10 10:17:44 +0000194 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000195 * @param name the entry name
196 * @param linkFlag the entry link flag.
Torsten Curdtca165392008-07-10 10:17:44 +0000197 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000198 public TarArchiveEntry(String name, byte linkFlag) {
199 this(name);
200 this.linkFlag = linkFlag;
Torsten Curdtca165392008-07-10 10:17:44 +0000201 }
202
203 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000204 * Construct an entry for a file. File is set to file, and the
205 * header is constructed from information from the file.
Sebastian Bazleyfec51a12009-03-31 00:35:56 +0000206 * The name is set from the normalized file path.
Torsten Curdtca165392008-07-10 10:17:44 +0000207 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000208 * @param file The file that the entry represents.
Torsten Curdtca165392008-07-10 10:17:44 +0000209 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000210 public TarArchiveEntry(File file) {
Sebastian Bazleyfec51a12009-03-31 00:35:56 +0000211 this(file, normalizeFileName(file.getPath()));
212 }
213
214 /**
215 * Construct an entry for a file. File is set to file, and the
216 * header is constructed from information from the file.
217 *
218 * @param file The file that the entry represents.
219 * @param fileName the name to be used for the entry.
220 */
221 public TarArchiveEntry(File file, String fileName) {
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000222 this();
Torsten Curdtca165392008-07-10 10:17:44 +0000223
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000224 this.file = file;
Torsten Curdtca165392008-07-10 10:17:44 +0000225
Sebastian Bazley1d556702009-04-02 18:45:02 +0000226 this.linkName = "";
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000227
228 if (file.isDirectory()) {
229 this.mode = DEFAULT_DIR_MODE;
230 this.linkFlag = LF_DIR;
231
Sebastian Bazley1d556702009-04-02 18:45:02 +0000232 int nameLength = fileName.length();
Stefan Bodewig32eea1e2009-03-17 12:53:22 +0000233 if (nameLength == 0 || name.charAt(nameLength - 1) != '/') {
Sebastian Bazley1d556702009-04-02 18:45:02 +0000234 this.name = fileName + "/";
235 } else {
236 this.name = fileName;
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000237 }
Stefan Bodewigc013e282009-03-18 04:38:47 +0000238 this.size = 0;
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000239 } else {
240 this.mode = DEFAULT_FILE_MODE;
241 this.linkFlag = LF_NORMAL;
Stefan Bodewigc013e282009-03-18 04:38:47 +0000242 this.size = file.length();
Sebastian Bazley1d556702009-04-02 18:45:02 +0000243 this.name = fileName;
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000244 }
245
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000246 this.modTime = file.lastModified() / MILLIS_PER_SECOND;
247 this.devMajor = 0;
248 this.devMinor = 0;
Torsten Curdtca165392008-07-10 10:17:44 +0000249 }
250
251 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000252 * Construct an entry from an archive's header bytes. File is set
253 * to null.
Torsten Curdtca165392008-07-10 10:17:44 +0000254 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000255 * @param headerBuf The header bytes from a tar archive entry.
Torsten Curdtca165392008-07-10 10:17:44 +0000256 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000257 public TarArchiveEntry(byte[] headerBuf) {
258 this();
259 parseTarHeader(headerBuf);
Torsten Curdtca165392008-07-10 10:17:44 +0000260 }
261
262 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000263 * Determine if the two entries are equal. Equality is determined
264 * by the header names being equal.
Torsten Curdtca165392008-07-10 10:17:44 +0000265 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000266 * @param it Entry to be checked for equality.
267 * @return True if the entries are equal.
Torsten Curdtca165392008-07-10 10:17:44 +0000268 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000269 public boolean equals(TarArchiveEntry it) {
270 return getName().equals(it.getName());
Torsten Curdtca165392008-07-10 10:17:44 +0000271 }
272
273 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000274 * Determine if the two entries are equal. Equality is determined
275 * by the header names being equal.
Torsten Curdtca165392008-07-10 10:17:44 +0000276 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000277 * @param it Entry to be checked for equality.
278 * @return True if the entries are equal.
Torsten Curdtca165392008-07-10 10:17:44 +0000279 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000280 public boolean equals(Object it) {
281 if (it == null || getClass() != it.getClass()) {
282 return false;
283 }
284 return equals((TarArchiveEntry) it);
Torsten Curdtca165392008-07-10 10:17:44 +0000285 }
286
287 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000288 * Hashcodes are based on entry names.
Torsten Curdtca165392008-07-10 10:17:44 +0000289 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000290 * @return the entry hashcode
Torsten Curdtca165392008-07-10 10:17:44 +0000291 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000292 public int hashCode() {
293 return getName().hashCode();
Torsten Curdtca165392008-07-10 10:17:44 +0000294 }
295
296 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000297 * Determine if the given entry is a descendant of this entry.
298 * Descendancy is determined by the name of the descendant
299 * starting with this entry's name.
Torsten Curdtca165392008-07-10 10:17:44 +0000300 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000301 * @param desc Entry to be checked as a descendent of this.
302 * @return True if entry is a descendant of this.
Torsten Curdtca165392008-07-10 10:17:44 +0000303 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000304 public boolean isDescendent(TarArchiveEntry desc) {
305 return desc.getName().startsWith(getName());
Torsten Curdtca165392008-07-10 10:17:44 +0000306 }
307
308 /**
309 * Get this entry's name.
310 *
311 * @return This entry's name.
312 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000313 public String getName() {
314 return name.toString();
Torsten Curdtca165392008-07-10 10:17:44 +0000315 }
316
317 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000318 * Set this entry's name.
Torsten Curdtca165392008-07-10 10:17:44 +0000319 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000320 * @param name This entry's new name.
Torsten Curdtca165392008-07-10 10:17:44 +0000321 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000322 public void setName(String name) {
Sebastian Bazley1d556702009-04-02 18:45:02 +0000323 this.name = normalizeFileName(name);
Torsten Curdtca165392008-07-10 10:17:44 +0000324 }
325
326 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000327 * Set the mode for this entry
Torsten Curdtca165392008-07-10 10:17:44 +0000328 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000329 * @param mode the mode for this entry
Torsten Curdtca165392008-07-10 10:17:44 +0000330 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000331 public void setMode(int mode) {
332 this.mode = mode;
Torsten Curdtca165392008-07-10 10:17:44 +0000333 }
334
335 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000336 * Get this entry's link name.
Torsten Curdtca165392008-07-10 10:17:44 +0000337 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000338 * @return This entry's link name.
Torsten Curdtca165392008-07-10 10:17:44 +0000339 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000340 public String getLinkName() {
341 return linkName.toString();
Torsten Curdtca165392008-07-10 10:17:44 +0000342 }
343
344 /**
345 * Get this entry's user id.
346 *
347 * @return This entry's user id.
348 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000349 public int getUserId() {
350 return userId;
351 }
352
353 /**
354 * Set this entry's user id.
355 *
356 * @param userId This entry's new user id.
357 */
358 public void setUserId(int userId) {
359 this.userId = userId;
360 }
361
362 /**
363 * Get this entry's group id.
364 *
365 * @return This entry's group id.
366 */
367 public int getGroupId() {
368 return groupId;
369 }
370
371 /**
372 * Set this entry's group id.
373 *
374 * @param groupId This entry's new group id.
375 */
376 public void setGroupId(int groupId) {
377 this.groupId = groupId;
Torsten Curdtca165392008-07-10 10:17:44 +0000378 }
379
380 /**
381 * Get this entry's user name.
382 *
383 * @return This entry's user name.
384 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000385 public String getUserName() {
386 return userName.toString();
Torsten Curdtca165392008-07-10 10:17:44 +0000387 }
388
389 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000390 * Set this entry's user name.
Torsten Curdtca165392008-07-10 10:17:44 +0000391 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000392 * @param userName This entry's new user name.
Torsten Curdtca165392008-07-10 10:17:44 +0000393 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000394 public void setUserName(String userName) {
Sebastian Bazley1d556702009-04-02 18:45:02 +0000395 this.userName = userName;
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000396 }
397
398 /**
399 * Get this entry's group name.
400 *
401 * @return This entry's group name.
402 */
403 public String getGroupName() {
404 return groupName.toString();
405 }
406
407 /**
408 * Set this entry's group name.
409 *
410 * @param groupName This entry's new group name.
411 */
412 public void setGroupName(String groupName) {
Sebastian Bazley1d556702009-04-02 18:45:02 +0000413 this.groupName = groupName;
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000414 }
415
416 /**
417 * Convenience method to set this entry's group and user ids.
418 *
419 * @param userId This entry's new user id.
420 * @param groupId This entry's new group id.
421 */
422 public void setIds(int userId, int groupId) {
423 setUserId(userId);
424 setGroupId(groupId);
425 }
426
427 /**
428 * Convenience method to set this entry's group and user names.
429 *
430 * @param userName This entry's new user name.
431 * @param groupName This entry's new group name.
432 */
433 public void setNames(String userName, String groupName) {
434 setUserName(userName);
435 setGroupName(groupName);
436 }
437
438 /**
439 * Set this entry's modification time. The parameter passed
440 * to this method is in "Java time".
441 *
442 * @param time This entry's new modification time.
443 */
444 public void setModTime(long time) {
445 modTime = time / MILLIS_PER_SECOND;
446 }
447
448 /**
449 * Set this entry's modification time.
450 *
451 * @param time This entry's new modification time.
452 */
453 public void setModTime(Date time) {
454 modTime = time.getTime() / MILLIS_PER_SECOND;
455 }
456
457 /**
458 * Set this entry's modification time.
459 *
460 * @return time This entry's new modification time.
461 */
462 public Date getModTime() {
463 return new Date(modTime * MILLIS_PER_SECOND);
464 }
465
466 /**
467 * Get this entry's file.
468 *
469 * @return This entry's file.
470 */
471 public File getFile() {
472 return file;
473 }
474
475 /**
476 * Get this entry's mode.
477 *
478 * @return This entry's mode.
479 */
480 public int getMode() {
481 return mode;
482 }
483
484 /**
485 * Get this entry's file size.
486 *
487 * @return This entry's file size.
488 */
489 public long getSize() {
490 return size;
491 }
492
493 /**
494 * Set this entry's file size.
495 *
496 * @param size This entry's new file size.
Sebastian Bazley26d12c52009-03-31 18:45:14 +0000497 * @throws IllegalArgumentException if the size is < 0
498 * or > {@link TarConstants#MAXSIZE} (077777777777L).
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000499 */
500 public void setSize(long size) {
Sebastian Bazley26d12c52009-03-31 18:45:14 +0000501 if (size > MAXSIZE || size < 0){
502 throw new IllegalArgumentException("Size is out of range: "+size);
503 }
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000504 this.size = size;
505 }
506
507
508 /**
509 * Indicate if this entry is a GNU long name block
510 *
511 * @return true if this is a long name extension provided by GNU tar
512 */
513 public boolean isGNULongNameEntry() {
514 return linkFlag == LF_GNUTYPE_LONGNAME
Stefan Bodewig75f92f62009-03-17 12:14:17 +0000515 && name.toString().equals(GNU_LONGLINK);
Torsten Curdtca165392008-07-10 10:17:44 +0000516 }
517
518 /**
519 * Return whether or not this entry represents a directory.
520 *
521 * @return True if this entry is a directory.
522 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000523 public boolean isDirectory() {
524 if (file != null) {
525 return file.isDirectory();
Torsten Curdtca165392008-07-10 10:17:44 +0000526 }
527
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000528 if (linkFlag == LF_DIR) {
Torsten Curdtca165392008-07-10 10:17:44 +0000529 return true;
530 }
531
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000532 if (getName().endsWith("/")) {
Torsten Curdtca165392008-07-10 10:17:44 +0000533 return true;
534 }
535
536 return false;
537 }
538
539 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000540 * If this entry represents a file, and the file is a directory, return
541 * an array of TarEntries for this entry's children.
Torsten Curdtca165392008-07-10 10:17:44 +0000542 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000543 * @return An array of TarEntry's for this entry's children.
Torsten Curdtca165392008-07-10 10:17:44 +0000544 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000545 public TarArchiveEntry[] getDirectoryEntries() {
546 if (file == null || !file.isDirectory()) {
547 return new TarArchiveEntry[0];
548 }
549
550 String[] list = file.list();
551 TarArchiveEntry[] result = new TarArchiveEntry[list.length];
552
553 for (int i = 0; i < list.length; ++i) {
554 result[i] = new TarArchiveEntry(new File(file, list[i]));
555 }
556
557 return result;
Torsten Curdtca165392008-07-10 10:17:44 +0000558 }
559
560 /**
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000561 * Write an entry's header information to a header buffer.
Torsten Curdtca165392008-07-10 10:17:44 +0000562 *
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000563 * @param outbuf The tar entry header buffer to fill in.
Torsten Curdtca165392008-07-10 10:17:44 +0000564 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000565 public void writeEntryHeader(byte[] outbuf) {
566 int offset = 0;
567
Sebastian Bazley1d556702009-04-02 18:45:02 +0000568 offset = TarUtils.formatNameBytes(name, outbuf, offset, NAMELEN);
569 offset = TarUtils.formatOctalBytes(mode, outbuf, offset, MODELEN);
570 offset = TarUtils.formatOctalBytes(userId, outbuf, offset, UIDLEN);
571 offset = TarUtils.formatOctalBytes(groupId, outbuf, offset, GIDLEN);
572 offset = TarUtils.formatLongOctalBytes(size, outbuf, offset, SIZELEN);
573 offset = TarUtils.formatLongOctalBytes(modTime, outbuf, offset, MODTIMELEN);
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000574
575 int csOffset = offset;
576
577 for (int c = 0; c < CHKSUMLEN; ++c) {
578 outbuf[offset++] = (byte) ' ';
579 }
580
581 outbuf[offset++] = linkFlag;
Sebastian Bazley1d556702009-04-02 18:45:02 +0000582 offset = TarUtils.formatNameBytes(linkName, outbuf, offset, NAMELEN);
583 offset = TarUtils.formatNameBytes(magic, outbuf, offset, MAGICLEN);
Sebastian Bazley8118f822009-04-02 23:34:48 +0000584 offset = TarUtils.formatNameBytes(version, outbuf, offset, VERSIONLEN);
Sebastian Bazley1d556702009-04-02 18:45:02 +0000585 offset = TarUtils.formatNameBytes(userName, outbuf, offset, UNAMELEN);
586 offset = TarUtils.formatNameBytes(groupName, outbuf, offset, GNAMELEN);
587 offset = TarUtils.formatOctalBytes(devMajor, outbuf, offset, DEVLEN);
588 offset = TarUtils.formatOctalBytes(devMinor, outbuf, offset, DEVLEN);
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000589
590 while (offset < outbuf.length) {
591 outbuf[offset++] = 0;
592 }
593
594 long chk = TarUtils.computeCheckSum(outbuf);
595
Sebastian Bazley1d556702009-04-02 18:45:02 +0000596 TarUtils.formatCheckSumOctalBytes(chk, outbuf, csOffset, CHKSUMLEN);
Torsten Curdtca165392008-07-10 10:17:44 +0000597 }
598
599 /**
600 * Parse an entry's header information from a header buffer.
601 *
602 * @param header The tar entry header buffer to get information from.
603 */
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000604 public void parseTarHeader(byte[] header) {
Torsten Curdtca165392008-07-10 10:17:44 +0000605 int offset = 0;
606
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000607 name = TarUtils.parseName(header, offset, NAMELEN);
Torsten Curdtca165392008-07-10 10:17:44 +0000608 offset += NAMELEN;
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000609 mode = (int) TarUtils.parseOctal(header, offset, MODELEN);
610 offset += MODELEN;
611 userId = (int) TarUtils.parseOctal(header, offset, UIDLEN);
612 offset += UIDLEN;
613 groupId = (int) TarUtils.parseOctal(header, offset, GIDLEN);
614 offset += GIDLEN;
615 size = TarUtils.parseOctal(header, offset, SIZELEN);
616 offset += SIZELEN;
617 modTime = TarUtils.parseOctal(header, offset, MODTIMELEN);
618 offset += MODTIMELEN;
619 offset += CHKSUMLEN;
620 linkFlag = header[offset++];
621 linkName = TarUtils.parseName(header, offset, NAMELEN);
Torsten Curdtca165392008-07-10 10:17:44 +0000622 offset += NAMELEN;
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000623 magic = TarUtils.parseName(header, offset, MAGICLEN);
624 offset += MAGICLEN;
Sebastian Bazley8118f822009-04-02 23:34:48 +0000625 version = TarUtils.parseName(header, offset, VERSIONLEN);
626 offset += VERSIONLEN;
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000627 userName = TarUtils.parseName(header, offset, UNAMELEN);
628 offset += UNAMELEN;
629 groupName = TarUtils.parseName(header, offset, GNAMELEN);
630 offset += GNAMELEN;
631 devMajor = (int) TarUtils.parseOctal(header, offset, DEVLEN);
632 offset += DEVLEN;
633 devMinor = (int) TarUtils.parseOctal(header, offset, DEVLEN);
Torsten Curdtca165392008-07-10 10:17:44 +0000634 }
Stefan Bodewig32eea1e2009-03-17 12:53:22 +0000635
636 /**
637 * Strips Windows' drive letter as well as any leading slashes,
638 * turns path separators into forward slahes.
639 */
640 private static String normalizeFileName(String fileName) {
641 String osname = System.getProperty("os.name").toLowerCase(Locale.US);
642
643 if (osname != null) {
644
645 // Strip off drive letters!
646 // REVIEW Would a better check be "(File.separator == '\')"?
647
648 if (osname.startsWith("windows")) {
649 if (fileName.length() > 2) {
650 char ch1 = fileName.charAt(0);
651 char ch2 = fileName.charAt(1);
652
653 if (ch2 == ':'
654 && ((ch1 >= 'a' && ch1 <= 'z')
655 || (ch1 >= 'A' && ch1 <= 'Z'))) {
656 fileName = fileName.substring(2);
657 }
658 }
659 } else if (osname.indexOf("netware") > -1) {
660 int colon = fileName.indexOf(':');
661 if (colon != -1) {
662 fileName = fileName.substring(colon + 1);
663 }
664 }
665 }
666
667 fileName = fileName.replace(File.separatorChar, '/');
668
669 // No absolute pathnames
670 // Windows (and Posix?) paths can start with "\\NetworkDrive\",
671 // so we loop on starting /'s.
672 while (fileName.startsWith("/")) {
673 fileName = fileName.substring(1);
674 }
675 return fileName;
676 }
Torsten Curdtca165392008-07-10 10:17:44 +0000677}
Torsten Curdt46ad24d2009-01-08 11:09:25 +0000678