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 | * |
Stefan Bodewig | a19e5f3 | 2011-07-23 03:04:09 +0000 | [diff] [blame] | 45 | * Due to the limitation of the file name length to 16 bytes GNU and |
| 46 | * BSD has their own variants of this format. Currently Commons |
Stefan Bodewig | 93e861d | 2013-01-20 20:16:13 +0000 | [diff] [blame] | 47 | * Compress can read but not write the GNU variant. It fully supports |
Stefan Bodewig | 46212e5 | 2013-01-20 17:07:40 +0000 | [diff] [blame] | 48 | * the BSD variant. |
Christian Grobmeier | 272aafd | 2009-04-07 16:56:00 +0000 | [diff] [blame] | 49 | * |
Gary D. Gregory | 6824421 | 2011-07-29 14:04:17 +0000 | [diff] [blame] | 50 | * @see <a href="http://www.freebsd.org/cgi/man.cgi?query=ar&sektion=5">ar man page</a> |
Stefan Bodewig | 541898f | 2011-07-20 14:23:20 +0000 | [diff] [blame] | 51 | * |
Sebastian Bazley | a91e7c7 | 2009-03-27 21:13:51 +0000 | [diff] [blame] | 52 | * @Immutable |
Sebastian Bazley | 1d3d6ea | 2009-03-14 02:24:54 +0000 | [diff] [blame] | 53 | */ |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 54 | public class ArArchiveEntry implements ArchiveEntry { |
| 55 | |
Sebastian Bazley | 6140bf0 | 2009-03-28 15:48:15 +0000 | [diff] [blame] | 56 | /** The header for each entry */ |
| 57 | public static final String HEADER = "!<arch>\n"; |
| 58 | |
| 59 | /** The trailer for each entry */ |
| 60 | public static final String TRAILER = "`\012"; |
Stefan Bodewig | 06fbac9 | 2009-08-01 14:53:32 +0000 | [diff] [blame] | 61 | |
Sebastian Bazley | 7ef9107 | 2009-04-01 10:09:01 +0000 | [diff] [blame] | 62 | /** |
| 63 | * SVR4/GNU adds a trailing / to names; BSD does not. |
| 64 | * They also vary in how names longer than 16 characters are represented. |
Stefan Bodewig | a19e5f3 | 2011-07-23 03:04:09 +0000 | [diff] [blame] | 65 | * (Not yet fully supported by this implementation) |
Sebastian Bazley | 7ef9107 | 2009-04-01 10:09:01 +0000 | [diff] [blame] | 66 | */ |
Sebastian Bazley | fd648e2 | 2009-03-28 15:05:30 +0000 | [diff] [blame] | 67 | private final String name; |
Stefan Bodewig | 06fbac9 | 2009-08-01 14:53:32 +0000 | [diff] [blame] | 68 | private final int userId; |
| 69 | private final int groupId; |
| 70 | private final int mode; |
Sebastian Bazley | fd648e2 | 2009-03-28 15:05:30 +0000 | [diff] [blame] | 71 | private static final int DEFAULT_MODE = 33188; // = (octal) 0100644 |
Stefan Bodewig | 06fbac9 | 2009-08-01 14:53:32 +0000 | [diff] [blame] | 72 | private final long lastModified; |
| 73 | private final long length; |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 74 | |
Stefan Bodewig | cc3d418 | 2009-08-01 15:04:22 +0000 | [diff] [blame] | 75 | /** |
| 76 | * Create a new instance using a couple of default values. |
| 77 | * |
| 78 | * <p>Sets userId and groupId to 0, the octal file mode to 644 and |
| 79 | * the last modified time to the current time.</p> |
| 80 | * |
| 81 | * @param name name of the entry |
| 82 | * @param length length of the entry in bytes |
| 83 | */ |
Stefan Bodewig | 06fbac9 | 2009-08-01 14:53:32 +0000 | [diff] [blame] | 84 | public ArArchiveEntry(String name, long length) { |
Stefan Bodewig | cc3d418 | 2009-08-01 15:04:22 +0000 | [diff] [blame] | 85 | this(name, length, 0, 0, DEFAULT_MODE, |
| 86 | System.currentTimeMillis() / 1000); |
Stefan Bodewig | 06fbac9 | 2009-08-01 14:53:32 +0000 | [diff] [blame] | 87 | } |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 88 | |
Stefan Bodewig | cc3d418 | 2009-08-01 15:04:22 +0000 | [diff] [blame] | 89 | /** |
| 90 | * Create a new instance. |
| 91 | * |
| 92 | * @param name name of the entry |
| 93 | * @param length length of the entry in bytes |
| 94 | * @param userId numeric user id |
| 95 | * @param groupId numeric group id |
| 96 | * @param mode file mode |
| 97 | * @param lastModified last modified time in seconds since the epoch |
| 98 | */ |
| 99 | public ArArchiveEntry(String name, long length, int userId, int groupId, |
| 100 | int mode, long lastModified) { |
Stefan Bodewig | 06fbac9 | 2009-08-01 14:53:32 +0000 | [diff] [blame] | 101 | this.name = name; |
| 102 | this.length = length; |
| 103 | this.userId = userId; |
| 104 | this.groupId = groupId; |
| 105 | this.mode = mode; |
| 106 | this.lastModified = lastModified; |
| 107 | } |
| 108 | |
Stefan Bodewig | cc3d418 | 2009-08-01 15:04:22 +0000 | [diff] [blame] | 109 | /** |
| 110 | * Create a new instance using the attributes of the given file |
| 111 | */ |
Stefan Bodewig | 06fbac9 | 2009-08-01 14:53:32 +0000 | [diff] [blame] | 112 | public ArArchiveEntry(File inputFile, String entryName) { |
| 113 | // TODO sort out mode |
Stefan Bodewig | cc3d418 | 2009-08-01 15:04:22 +0000 | [diff] [blame] | 114 | this(entryName, inputFile.isFile() ? inputFile.length() : 0, |
Stefan Bodewig | 4efda70 | 2009-08-21 07:05:07 +0000 | [diff] [blame] | 115 | 0, 0, DEFAULT_MODE, inputFile.lastModified() / 1000); |
Sebastian Bazley | fec51a1 | 2009-03-31 00:35:56 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | public long getSize() { |
Stefan Bodewig | 06fbac9 | 2009-08-01 14:53:32 +0000 | [diff] [blame] | 119 | return this.getLength(); |
| 120 | } |
| 121 | |
| 122 | public String getName() { |
| 123 | return name; |
| 124 | } |
| 125 | |
| 126 | public int getUserId() { |
| 127 | return userId; |
| 128 | } |
| 129 | |
| 130 | public int getGroupId() { |
| 131 | return groupId; |
| 132 | } |
| 133 | |
| 134 | public int getMode() { |
| 135 | return mode; |
| 136 | } |
| 137 | |
Stefan Bodewig | cc3d418 | 2009-08-01 15:04:22 +0000 | [diff] [blame] | 138 | /** |
| 139 | * Last modified time in seconds since the epoch. |
| 140 | */ |
Stefan Bodewig | 06fbac9 | 2009-08-01 14:53:32 +0000 | [diff] [blame] | 141 | public long getLastModified() { |
| 142 | return lastModified; |
| 143 | } |
| 144 | |
Stefan Bodewig | 17ffd7f | 2009-08-01 14:52:15 +0000 | [diff] [blame] | 145 | public Date getLastModifiedDate() { |
| 146 | return new Date(1000 * getLastModified()); |
| 147 | } |
| 148 | |
Stefan Bodewig | 06fbac9 | 2009-08-01 14:53:32 +0000 | [diff] [blame] | 149 | public long getLength() { |
| 150 | return length; |
| 151 | } |
Torsten Curdt | d6612b6 | 2009-01-07 13:48:42 +0000 | [diff] [blame] | 152 | |
Stefan Bodewig | 06fbac9 | 2009-08-01 14:53:32 +0000 | [diff] [blame] | 153 | public boolean isDirectory() { |
| 154 | return false; |
| 155 | } |
Christian Grobmeier | 28c23fb | 2009-04-17 05:14:49 +0000 | [diff] [blame] | 156 | |
Stefan Bodewig | 46628ef | 2011-08-06 16:30:40 +0000 | [diff] [blame] | 157 | @Override |
Christian Grobmeier | 28c23fb | 2009-04-17 05:14:49 +0000 | [diff] [blame] | 158 | public int hashCode() { |
| 159 | final int prime = 31; |
| 160 | int result = 1; |
| 161 | result = prime * result + ((name == null) ? 0 : name.hashCode()); |
| 162 | return result; |
| 163 | } |
| 164 | |
Stefan Bodewig | 46628ef | 2011-08-06 16:30:40 +0000 | [diff] [blame] | 165 | @Override |
Christian Grobmeier | 28c23fb | 2009-04-17 05:14:49 +0000 | [diff] [blame] | 166 | public boolean equals(Object obj) { |
| 167 | if (this == obj) { |
| 168 | return true; |
| 169 | } |
| 170 | if (obj == null || getClass() != obj.getClass()) { |
| 171 | return false; |
| 172 | } |
| 173 | ArArchiveEntry other = (ArArchiveEntry) obj; |
| 174 | if (name == null) { |
| 175 | if (other.name != null) { |
| 176 | return false; |
| 177 | } |
| 178 | } else if (!name.equals(other.name)) { |
| 179 | return false; |
| 180 | } |
| 181 | return true; |
| 182 | } |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 183 | } |