001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.commons.compress.archivers.ar; 020 021import java.io.File; 022import java.io.IOException; 023import java.nio.file.Files; 024import java.nio.file.LinkOption; 025import java.nio.file.Path; 026import java.util.Date; 027import java.util.Objects; 028 029import org.apache.commons.compress.archivers.ArchiveEntry; 030 031/** 032 * Represents an archive entry in the "ar" format. 033 * 034 * Each AR archive starts with "!<arch>" followed by a LF. After these 8 bytes 035 * the archive entries are listed. The format of an entry header is as it follows: 036 * 037 * <pre> 038 * START BYTE END BYTE NAME FORMAT LENGTH 039 * 0 15 File name ASCII 16 040 * 16 27 Modification timestamp Decimal 12 041 * 28 33 Owner ID Decimal 6 042 * 34 39 Group ID Decimal 6 043 * 40 47 File mode Octal 8 044 * 48 57 File size (bytes) Decimal 10 045 * 58 59 File magic \140\012 2 046 * </pre> 047 * 048 * This specifies that an ar archive entry header contains 60 bytes. 049 * 050 * Due to the limitation of the file name length to 16 bytes GNU and 051 * BSD has their own variants of this format. Currently Commons 052 * Compress can read but not write the GNU variant. It fully supports 053 * the BSD variant. 054 * 055 * @see <a href="https://www.freebsd.org/cgi/man.cgi?query=ar&sektion=5">ar man page</a> 056 * 057 * @Immutable 058 */ 059public class ArArchiveEntry implements ArchiveEntry { 060 061 /** The header for each entry */ 062 public static final String HEADER = "!<arch>\n"; 063 064 /** The trailer for each entry */ 065 public static final String TRAILER = "`\012"; 066 067 /** 068 * SVR4/GNU adds a trailing / to names; BSD does not. 069 * They also vary in how names longer than 16 characters are represented. 070 * (Not yet fully supported by this implementation) 071 */ 072 private final String name; 073 private final int userId; 074 private final int groupId; 075 private final int mode; 076 private static final int DEFAULT_MODE = 33188; // = (octal) 0100644 077 private final long lastModified; 078 private final long length; 079 080 /** 081 * Create a new instance using a couple of default values. 082 * 083 * <p>Sets userId and groupId to 0, the octal file mode to 644 and 084 * the last modified time to the current time.</p> 085 * 086 * @param name name of the entry 087 * @param length length of the entry in bytes 088 */ 089 public ArArchiveEntry(final String name, final long length) { 090 this(name, length, 0, 0, DEFAULT_MODE, 091 System.currentTimeMillis() / 1000); 092 } 093 094 /** 095 * Create a new instance. 096 * 097 * @param name name of the entry 098 * @param length length of the entry in bytes 099 * @param userId numeric user id 100 * @param groupId numeric group id 101 * @param mode file mode 102 * @param lastModified last modified time in seconds since the epoch 103 */ 104 public ArArchiveEntry(final String name, final long length, final int userId, final int groupId, 105 final int mode, final long lastModified) { 106 this.name = name; 107 if (length < 0) { 108 throw new IllegalArgumentException("length must not be negative"); 109 } 110 this.length = length; 111 this.userId = userId; 112 this.groupId = groupId; 113 this.mode = mode; 114 this.lastModified = lastModified; 115 } 116 117 /** 118 * Creates a new instance using the attributes of the given file 119 * @param inputFile the file to create an entry from 120 * @param entryName the name of the entry 121 */ 122 public ArArchiveEntry(final File inputFile, final String entryName) { 123 // TODO sort out mode 124 this(entryName, inputFile.isFile() ? inputFile.length() : 0, 125 0, 0, DEFAULT_MODE, inputFile.lastModified() / 1000); 126 } 127 128 /** 129 * Creates a new instance using the attributes of the given file 130 * @param inputPath the file to create an entry from 131 * @param entryName the name of the entry 132 * @param options options indicating how symbolic links are handled. 133 * @throws IOException if an I/O error occurs. 134 * @since 1.21 135 */ 136 public ArArchiveEntry(final Path inputPath, final String entryName, final LinkOption... options) throws IOException { 137 this(entryName, Files.isRegularFile(inputPath, options) ? Files.size(inputPath) : 0, 0, 0, DEFAULT_MODE, 138 Files.getLastModifiedTime(inputPath, options).toMillis() / 1000); 139 } 140 141 @Override 142 public long getSize() { 143 return this.getLength(); 144 } 145 146 @Override 147 public String getName() { 148 return name; 149 } 150 151 public int getUserId() { 152 return userId; 153 } 154 155 public int getGroupId() { 156 return groupId; 157 } 158 159 public int getMode() { 160 return mode; 161 } 162 163 /** 164 * Last modified time in seconds since the epoch. 165 * @return the last modified date 166 */ 167 public long getLastModified() { 168 return lastModified; 169 } 170 171 @Override 172 public Date getLastModifiedDate() { 173 return new Date(1000 * getLastModified()); 174 } 175 176 public long getLength() { 177 return length; 178 } 179 180 @Override 181 public boolean isDirectory() { 182 return false; 183 } 184 185 @Override 186 public int hashCode() { 187 return Objects.hash(name); 188 } 189 190 @Override 191 public boolean equals(final Object obj) { 192 if (this == obj) { 193 return true; 194 } 195 if (obj == null || getClass() != obj.getClass()) { 196 return false; 197 } 198 final ArArchiveEntry other = (ArArchiveEntry) obj; 199 if (name == null) { 200 return other.name == null; 201 } 202 return name.equals(other.name); 203 } 204}