Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 19 | package org.apache.commons.compress.archivers.ar; |
| 20 | |
Sebastian Bazley | fec51a1 | 2009-03-31 00:35:56 +0000 | [diff] [blame] | 21 | import java.io.File; |
Stefan Bodewig | 17ffd7f | 2009-08-01 14:52:15 +0000 | [diff] [blame^] | 22 | import java.util.Date; |
Sebastian Bazley | fec51a1 | 2009-03-31 00:35:56 +0000 | [diff] [blame] | 23 | |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 24 | import org.apache.commons.compress.archivers.ArchiveEntry; |
| 25 | |
Sebastian Bazley | 1d3d6ea | 2009-03-14 02:24:54 +0000 | [diff] [blame] | 26 | /** |
| 27 | * Represents an archive entry in the "ar" format. |
Sebastian Bazley | a91e7c7 | 2009-03-27 21:13:51 +0000 | [diff] [blame] | 28 | * |
Christian Grobmeier | 272aafd | 2009-04-07 16:56:00 +0000 | [diff] [blame] | 29 | * Each AR archive starts with "!<arch>" followed by a LF. After these 8 bytes |
| 30 | * the archive entries are listed. The format of an entry header is as it follows: |
| 31 | * |
Sebastian Bazley | b7bcdc2 | 2009-04-14 15:49:33 +0000 | [diff] [blame] | 32 | * <pre> |
Sebastian Bazley | 5dd3a97 | 2009-04-14 15:37:46 +0000 | [diff] [blame] | 33 | * START BYTE END BYTE NAME FORMAT LENGTH |
Christian Grobmeier | 272aafd | 2009-04-07 16:56:00 +0000 | [diff] [blame] | 34 | * 0 15 File name ASCII 16 |
| 35 | * 16 27 Modification timestamp Decimal 12 |
| 36 | * 28 33 Owner ID Decimal 6 |
| 37 | * 34 39 Group ID Decimal 6 |
| 38 | * 40 47 File mode Octal 8 |
| 39 | * 48 57 File size (bytes) Decimal 10 |
| 40 | * 58 59 File magic \140\012 2 |
Sebastian Bazley | b7bcdc2 | 2009-04-14 15:49:33 +0000 | [diff] [blame] | 41 | * </pre> |
Christian Grobmeier | 272aafd | 2009-04-07 16:56:00 +0000 | [diff] [blame] | 42 | * |
| 43 | * This specifies that an ar archive entry header contains 60 bytes. |
| 44 | * |
| 45 | * Due to the limitation of the file name length to 16 bytes GNU and BSD has |
| 46 | * their own variants of this format. This formats are currently not supported |
| 47 | * and file names with a bigger size than 16 bytes are not possible at the |
| 48 | * moment. |
| 49 | * |
Sebastian Bazley | a91e7c7 | 2009-03-27 21:13:51 +0000 | [diff] [blame] | 50 | * @Immutable |
Sebastian Bazley | 1d3d6ea | 2009-03-14 02:24:54 +0000 | [diff] [blame] | 51 | */ |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 52 | public class ArArchiveEntry implements ArchiveEntry { |
| 53 | |
Sebastian Bazley | 6140bf0 | 2009-03-28 15:48:15 +0000 | [diff] [blame] | 54 | /** The header for each entry */ |
| 55 | public static final String HEADER = "!<arch>\n"; |
| 56 | |
| 57 | /** The trailer for each entry */ |
| 58 | public static final String TRAILER = "`\012"; |
| 59 | |
Sebastian Bazley | 7ef9107 | 2009-04-01 10:09:01 +0000 | [diff] [blame] | 60 | /** |
| 61 | * SVR4/GNU adds a trailing / to names; BSD does not. |
| 62 | * They also vary in how names longer than 16 characters are represented. |
| 63 | * (Not yet supported by this implementation) |
| 64 | */ |
Sebastian Bazley | fd648e2 | 2009-03-28 15:05:30 +0000 | [diff] [blame] | 65 | private final String name; |
Sebastian Bazley | 5402e0d | 2009-02-27 01:17:12 +0000 | [diff] [blame] | 66 | private final int userId; |
| 67 | private final int groupId; |
| 68 | private final int mode; |
Sebastian Bazley | fd648e2 | 2009-03-28 15:05:30 +0000 | [diff] [blame] | 69 | private static final int DEFAULT_MODE = 33188; // = (octal) 0100644 |
Sebastian Bazley | 5402e0d | 2009-02-27 01:17:12 +0000 | [diff] [blame] | 70 | private final long lastModified; |
| 71 | private final long length; |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 72 | |
| 73 | public ArArchiveEntry(String name, long length) { |
Sebastian Bazley | fd648e2 | 2009-03-28 15:05:30 +0000 | [diff] [blame] | 74 | this(name, length, 0, 0, DEFAULT_MODE, System.currentTimeMillis()); |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | public ArArchiveEntry(String name, long length, int userId, int groupId, int mode, long lastModified) { |
| 78 | this.name = name; |
| 79 | this.length = length; |
| 80 | this.userId = userId; |
| 81 | this.groupId = groupId; |
| 82 | this.mode = mode; |
| 83 | this.lastModified = lastModified; |
| 84 | } |
| 85 | |
Sebastian Bazley | fec51a1 | 2009-03-31 00:35:56 +0000 | [diff] [blame] | 86 | public ArArchiveEntry(File inputFile, String entryName) { |
| 87 | // TODO sort out mode |
Sebastian Bazley | 2de1b50 | 2009-03-31 13:01:25 +0000 | [diff] [blame] | 88 | this(entryName, inputFile.isFile() ? inputFile.length() : 0, 0, 0, 0, inputFile.lastModified()); |
Sebastian Bazley | fec51a1 | 2009-03-31 00:35:56 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | public long getSize() { |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 92 | return this.getLength(); |
| 93 | } |
| 94 | |
| 95 | public String getName() { |
| 96 | return name; |
| 97 | } |
| 98 | |
| 99 | public int getUserId() { |
| 100 | return userId; |
| 101 | } |
| 102 | |
| 103 | public int getGroupId() { |
| 104 | return groupId; |
| 105 | } |
| 106 | |
| 107 | public int getMode() { |
| 108 | return mode; |
| 109 | } |
| 110 | |
| 111 | public long getLastModified() { |
| 112 | return lastModified; |
| 113 | } |
| 114 | |
Stefan Bodewig | 17ffd7f | 2009-08-01 14:52:15 +0000 | [diff] [blame^] | 115 | /** {@inheritDocs} */ |
| 116 | public Date getLastModifiedDate() { |
| 117 | return new Date(1000 * getLastModified()); |
| 118 | } |
| 119 | |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 120 | public long getLength() { |
| 121 | return length; |
| 122 | } |
Torsten Curdt | d6612b6 | 2009-01-07 13:48:42 +0000 | [diff] [blame] | 123 | |
| 124 | public boolean isDirectory() { |
| 125 | return false; |
| 126 | } |
Christian Grobmeier | 28c23fb | 2009-04-17 05:14:49 +0000 | [diff] [blame] | 127 | |
| 128 | /* (non-Javadoc) |
| 129 | * @see java.lang.Object#hashCode() |
| 130 | */ |
| 131 | public int hashCode() { |
| 132 | final int prime = 31; |
| 133 | int result = 1; |
| 134 | result = prime * result + ((name == null) ? 0 : name.hashCode()); |
| 135 | return result; |
| 136 | } |
| 137 | |
| 138 | /* (non-Javadoc) |
| 139 | * @see java.lang.Object#equals(java.lang.Object) |
| 140 | */ |
| 141 | public boolean equals(Object obj) { |
| 142 | if (this == obj) { |
| 143 | return true; |
| 144 | } |
| 145 | if (obj == null || getClass() != obj.getClass()) { |
| 146 | return false; |
| 147 | } |
| 148 | ArArchiveEntry other = (ArArchiveEntry) obj; |
| 149 | if (name == null) { |
| 150 | if (other.name != null) { |
| 151 | return false; |
| 152 | } |
| 153 | } else if (!name.equals(other.name)) { |
| 154 | return false; |
| 155 | } |
| 156 | return true; |
| 157 | } |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 158 | } |