1 18 19 23 24 package org.apache.tools.tar; 25 26 import java.io.File ; 27 import java.util.Date ; 28 import java.util.Locale ; 29 30 78 79 public class TarEntry implements TarConstants { 80 81 private StringBuffer name; 82 83 84 private int mode; 85 86 87 private int userId; 88 89 90 private int groupId; 91 92 93 private long size; 94 95 96 private long modTime; 97 98 99 private byte linkFlag; 100 101 102 private StringBuffer linkName; 103 104 105 private StringBuffer magic; 106 107 108 private StringBuffer userName; 109 110 111 private StringBuffer groupName; 112 113 114 private int devMajor; 115 116 117 private int devMinor; 118 119 120 private File file; 121 122 123 public static final int MAX_NAMELEN = 31; 124 125 126 public static final int DEFAULT_DIR_MODE = 040755; 127 128 129 public static final int DEFAULT_FILE_MODE = 0100644; 130 131 132 public static final int MILLIS_PER_SECOND = 1000; 133 134 137 private TarEntry () { 138 this.magic = new StringBuffer (TMAGIC); 139 this.name = new StringBuffer (); 140 this.linkName = new StringBuffer (); 141 142 String user = System.getProperty("user.name", ""); 143 144 if (user.length() > MAX_NAMELEN) { 145 user = user.substring(0, MAX_NAMELEN); 146 } 147 148 this.userId = 0; 149 this.groupId = 0; 150 this.userName = new StringBuffer (user); 151 this.groupName = new StringBuffer (""); 152 this.file = null; 153 } 154 155 161 public TarEntry(String name) { 162 this(); 163 164 boolean isDir = name.endsWith("/"); 165 166 this.devMajor = 0; 167 this.devMinor = 0; 168 this.name = new StringBuffer (name); 169 this.mode = isDir ? DEFAULT_DIR_MODE : DEFAULT_FILE_MODE; 170 this.linkFlag = isDir ? LF_DIR : LF_NORMAL; 171 this.userId = 0; 172 this.groupId = 0; 173 this.size = 0; 174 this.modTime = (new Date ()).getTime() / MILLIS_PER_SECOND; 175 this.linkName = new StringBuffer (""); 176 this.userName = new StringBuffer (""); 177 this.groupName = new StringBuffer (""); 178 this.devMajor = 0; 179 this.devMinor = 0; 180 181 } 182 183 189 public TarEntry(String name, byte linkFlag) { 190 this(name); 191 this.linkFlag = linkFlag; 192 } 193 194 200 public TarEntry(File file) { 201 this(); 202 203 this.file = file; 204 205 String fileName = file.getPath(); 206 String osname = System.getProperty("os.name").toLowerCase(Locale.US); 207 208 if (osname != null) { 209 210 213 if (osname.startsWith("windows")) { 214 if (fileName.length() > 2) { 215 char ch1 = fileName.charAt(0); 216 char ch2 = fileName.charAt(1); 217 218 if (ch2 == ':' 219 && ((ch1 >= 'a' && ch1 <= 'z') 220 || (ch1 >= 'A' && ch1 <= 'Z'))) { 221 fileName = fileName.substring(2); 222 } 223 } 224 } else if (osname.indexOf("netware") > -1) { 225 int colon = fileName.indexOf(':'); 226 if (colon != -1) { 227 fileName = fileName.substring(colon + 1); 228 } 229 } 230 } 231 232 fileName = fileName.replace(File.separatorChar, '/'); 233 234 while (fileName.startsWith("/")) { 238 fileName = fileName.substring(1); 239 } 240 241 this.linkName = new StringBuffer (""); 242 this.name = new StringBuffer (fileName); 243 244 if (file.isDirectory()) { 245 this.mode = DEFAULT_DIR_MODE; 246 this.linkFlag = LF_DIR; 247 248 if (this.name.charAt(this.name.length() - 1) != '/') { 249 this.name.append("/"); 250 } 251 } else { 252 this.mode = DEFAULT_FILE_MODE; 253 this.linkFlag = LF_NORMAL; 254 } 255 256 this.size = file.length(); 257 this.modTime = file.lastModified() / MILLIS_PER_SECOND; 258 this.devMajor = 0; 259 this.devMinor = 0; 260 } 261 262 268 public TarEntry(byte[] headerBuf) { 269 this(); 270 this.parseTarHeader(headerBuf); 271 } 272 273 280 public boolean equals(TarEntry it) { 281 return this.getName().equals(it.getName()); 282 } 283 284 291 public boolean equals(Object it) { 292 if (it == null || getClass() != it.getClass()) { 293 return false; 294 } 295 return equals((TarEntry) it); 296 } 297 298 303 public int hashCode() { 304 return getName().hashCode(); 305 } 306 307 315 public boolean isDescendent(TarEntry desc) { 316 return desc.getName().startsWith(this.getName()); 317 } 318 319 324 public String getName() { 325 return this.name.toString(); 326 } 327 328 333 public void setName(String name) { 334 this.name = new StringBuffer (name); 335 } 336 337 342 public void setMode(int mode) { 343 this.mode = mode; 344 } 345 346 351 public String getLinkName() { 352 return this.linkName.toString(); 353 } 354 355 360 public int getUserId() { 361 return this.userId; 362 } 363 364 369 public void setUserId(int userId) { 370 this.userId = userId; 371 } 372 373 378 public int getGroupId() { 379 return this.groupId; 380 } 381 382 387 public void setGroupId(int groupId) { 388 this.groupId = groupId; 389 } 390 391 396 public String getUserName() { 397 return this.userName.toString(); 398 } 399 400 405 public void setUserName(String userName) { 406 this.userName = new StringBuffer (userName); 407 } 408 409 414 public String getGroupName() { 415 return this.groupName.toString(); 416 } 417 418 423 public void setGroupName(String groupName) { 424 this.groupName = new StringBuffer (groupName); 425 } 426 427 433 public void setIds(int userId, int groupId) { 434 this.setUserId(userId); 435 this.setGroupId(groupId); 436 } 437 438 444 public void setNames(String userName, String groupName) { 445 this.setUserName(userName); 446 this.setGroupName(groupName); 447 } 448 449 455 public void setModTime(long time) { 456 this.modTime = time / MILLIS_PER_SECOND; 457 } 458 459 464 public void setModTime(Date time) { 465 this.modTime = time.getTime() / MILLIS_PER_SECOND; 466 } 467 468 473 public Date getModTime() { 474 return new Date (this.modTime * MILLIS_PER_SECOND); 475 } 476 477 482 public File getFile() { 483 return this.file; 484 } 485 486 491 public int getMode() { 492 return this.mode; 493 } 494 495 500 public long getSize() { 501 return this.size; 502 } 503 504 509 public void setSize(long size) { 510 this.size = size; 511 } 512 513 514 519 public boolean isGNULongNameEntry() { 520 return linkFlag == LF_GNUTYPE_LONGNAME 521 && name.toString().equals(GNU_LONGLINK); 522 } 523 524 529 public boolean isDirectory() { 530 if (this.file != null) { 531 return this.file.isDirectory(); 532 } 533 534 if (this.linkFlag == LF_DIR) { 535 return true; 536 } 537 538 if (this.getName().endsWith("/")) { 539 return true; 540 } 541 542 return false; 543 } 544 545 551 public TarEntry[] getDirectoryEntries() { 552 if (this.file == null || !this.file.isDirectory()) { 553 return new TarEntry[0]; 554 } 555 556 String [] list = this.file.list(); 557 TarEntry[] result = new TarEntry[list.length]; 558 559 for (int i = 0; i < list.length; ++i) { 560 result[i] = new TarEntry(new File (this.file, list[i])); 561 } 562 563 return result; 564 } 565 566 571 public void writeEntryHeader(byte[] outbuf) { 572 int offset = 0; 573 574 offset = TarUtils.getNameBytes(this.name, outbuf, offset, NAMELEN); 575 offset = TarUtils.getOctalBytes(this.mode, outbuf, offset, MODELEN); 576 offset = TarUtils.getOctalBytes(this.userId, outbuf, offset, UIDLEN); 577 offset = TarUtils.getOctalBytes(this.groupId, outbuf, offset, GIDLEN); 578 offset = TarUtils.getLongOctalBytes(this.size, outbuf, offset, SIZELEN); 579 offset = TarUtils.getLongOctalBytes(this.modTime, outbuf, offset, MODTIMELEN); 580 581 int csOffset = offset; 582 583 for (int c = 0; c < CHKSUMLEN; ++c) { 584 outbuf[offset++] = (byte) ' '; 585 } 586 587 outbuf[offset++] = this.linkFlag; 588 offset = TarUtils.getNameBytes(this.linkName, outbuf, offset, NAMELEN); 589 offset = TarUtils.getNameBytes(this.magic, outbuf, offset, MAGICLEN); 590 offset = TarUtils.getNameBytes(this.userName, outbuf, offset, UNAMELEN); 591 offset = TarUtils.getNameBytes(this.groupName, outbuf, offset, GNAMELEN); 592 offset = TarUtils.getOctalBytes(this.devMajor, outbuf, offset, DEVLEN); 593 offset = TarUtils.getOctalBytes(this.devMinor, outbuf, offset, DEVLEN); 594 595 while (offset < outbuf.length) { 596 outbuf[offset++] = 0; 597 } 598 599 long chk = TarUtils.computeCheckSum(outbuf); 600 601 TarUtils.getCheckSumOctalBytes(chk, outbuf, csOffset, CHKSUMLEN); 602 } 603 604 609 public void parseTarHeader(byte[] header) { 610 int offset = 0; 611 612 this.name = TarUtils.parseName(header, offset, NAMELEN); 613 offset += NAMELEN; 614 this.mode = (int) TarUtils.parseOctal(header, offset, MODELEN); 615 offset += MODELEN; 616 this.userId = (int) TarUtils.parseOctal(header, offset, UIDLEN); 617 offset += UIDLEN; 618 this.groupId = (int) TarUtils.parseOctal(header, offset, GIDLEN); 619 offset += GIDLEN; 620 this.size = TarUtils.parseOctal(header, offset, SIZELEN); 621 offset += SIZELEN; 622 this.modTime = TarUtils.parseOctal(header, offset, MODTIMELEN); 623 offset += MODTIMELEN; 624 offset += CHKSUMLEN; 625 this.linkFlag = header[offset++]; 626 this.linkName = TarUtils.parseName(header, offset, NAMELEN); 627 offset += NAMELEN; 628 this.magic = TarUtils.parseName(header, offset, MAGICLEN); 629 offset += MAGICLEN; 630 this.userName = TarUtils.parseName(header, offset, UNAMELEN); 631 offset += UNAMELEN; 632 this.groupName = TarUtils.parseName(header, offset, GNAMELEN); 633 offset += GNAMELEN; 634 this.devMajor = (int) TarUtils.parseOctal(header, offset, DEVLEN); 635 offset += DEVLEN; 636 this.devMinor = (int) TarUtils.parseOctal(header, offset, DEVLEN); 637 } 638 } 639 | Popular Tags |