1 /* 2 * Copyright (c) 2009, 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package jdk.nio.zipfs; 27 28 import java.io.IOException; 29 import java.nio.file.attribute.BasicFileAttributeView; 30 import java.nio.file.attribute.BasicFileAttributes; 31 import java.nio.file.attribute.FileTime; 32 import java.nio.file.attribute.PosixFilePermission; 33 import java.util.LinkedHashMap; 34 import java.util.Map; 35 import java.util.Set; 36 37 /** 38 * @author Xueming Shen, Rajendra Gutupalli, Jaya Hangal 39 */ 40 class ZipFileAttributeView implements BasicFileAttributeView { 41 static enum AttrID { 42 size, 43 creationTime, 44 lastAccessTime, 45 lastModifiedTime, 46 isDirectory, 47 isRegularFile, 48 isSymbolicLink, 49 isOther, 50 fileKey, 51 compressedSize, 52 crc, 53 method, 54 owner, 55 group, 56 permissions 57 } 58 59 final ZipPath path; 60 private final boolean isZipView; 61 62 ZipFileAttributeView(ZipPath path, boolean isZipView) { 63 this.path = path; 64 this.isZipView = isZipView; 65 } 66 67 @Override 68 public String name() { 69 return isZipView ? "zip" : "basic"; 70 } 71 72 @Override 73 public BasicFileAttributes readAttributes() throws IOException { 74 return path.readAttributes(); 75 } 76 77 @Override 78 public void setTimes(FileTime lastModifiedTime, 79 FileTime lastAccessTime, 80 FileTime createTime) 81 throws IOException 82 { 83 path.setTimes(lastModifiedTime, lastAccessTime, createTime); 84 } 85 86 public void setPermissions(Set<PosixFilePermission> perms) throws IOException { 87 path.setPermissions(perms); 88 } 89 90 @SuppressWarnings("unchecked") 91 void setAttribute(String attribute, Object value) 92 throws IOException 93 { 94 try { 95 if (AttrID.valueOf(attribute) == AttrID.lastModifiedTime) 96 setTimes((FileTime)value, null, null); 97 if (AttrID.valueOf(attribute) == AttrID.lastAccessTime) 98 setTimes(null, (FileTime)value, null); 99 if (AttrID.valueOf(attribute) == AttrID.creationTime) 100 setTimes(null, null, (FileTime)value); 101 if (AttrID.valueOf(attribute) == AttrID.permissions) 102 setPermissions((Set<PosixFilePermission>)value); 103 } catch (IllegalArgumentException x) { 104 throw new UnsupportedOperationException("'" + attribute + 105 "' is unknown or read-only attribute"); 106 } 107 } 108 109 Map<String, Object> readAttributes(String attributes) 110 throws IOException 111 { 112 ZipFileAttributes zfas = (ZipFileAttributes)readAttributes(); 113 LinkedHashMap<String, Object> map = new LinkedHashMap<>(); 114 if ("*".equals(attributes)) { 115 for (AttrID id : AttrID.values()) { 116 try { 117 map.put(id.name(), attribute(id, zfas)); 118 } catch (IllegalArgumentException x) {} 119 } 120 } else { 121 String[] as = attributes.split(","); 122 for (String a : as) { 123 try { 124 map.put(a, attribute(AttrID.valueOf(a), zfas)); 125 } catch (IllegalArgumentException x) {} 126 } 127 } 128 return map; 129 } 130 131 Object attribute(AttrID id, ZipFileAttributes zfas) { 132 switch (id) { 133 case size: 134 return zfas.size(); 135 case creationTime: 136 return zfas.creationTime(); 137 case lastAccessTime: 138 return zfas.lastAccessTime(); 139 case lastModifiedTime: 140 return zfas.lastModifiedTime(); 141 case isDirectory: 142 return zfas.isDirectory(); 143 case isRegularFile: 144 return zfas.isRegularFile(); 145 case isSymbolicLink: 146 return zfas.isSymbolicLink(); 147 case isOther: 148 return zfas.isOther(); 149 case fileKey: 150 return zfas.fileKey(); 151 case compressedSize: 152 if (isZipView) 153 return zfas.compressedSize(); 154 break; 155 case crc: 156 if (isZipView) 157 return zfas.crc(); 158 break; 159 case method: 160 if (isZipView) 161 return zfas.method(); 162 break; 163 case permissions: 164 if (isZipView) { 165 return zfas.storedPermissions().orElse(null); 166 } 167 break; 168 default: 169 break; 170 } 171 return null; 172 } 173 }