Path: blob/master/src/java.base/share/classes/jdk/internal/jrtfs/JrtFileAttributeView.java
41159 views
/*1* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package jdk.internal.jrtfs;2526import java.nio.file.LinkOption;27import java.nio.file.attribute.*;28import java.io.IOException;29import java.util.LinkedHashMap;30import java.util.Map;31import java.util.Objects;3233/**34* File attribute view for jrt file system.35*36* @implNote This class needs to maintain JDK 8 source compatibility.37*38* It is used internally in the JDK to implement jimage/jrtfs access,39* but also compiled and delivered as part of the jrtfs.jar to support access40* to the jimage file provided by the shipped JDK by tools running on JDK 8.41*/42final class JrtFileAttributeView implements BasicFileAttributeView {4344private static enum AttrID {45size,46creationTime,47lastAccessTime,48lastModifiedTime,49isDirectory,50isRegularFile,51isSymbolicLink,52isOther,53fileKey,54compressedSize,55extension56};5758private final JrtPath path;59private final boolean isJrtView;60private final LinkOption[] options;6162private JrtFileAttributeView(JrtPath path, boolean isJrtView, LinkOption... options) {63this.path = path;64this.isJrtView = isJrtView;65this.options = options;66}6768@SuppressWarnings("unchecked") // Cast to V69static <V extends FileAttributeView> V get(JrtPath path, Class<V> type, LinkOption... options) {70Objects.requireNonNull(type);71if (type == BasicFileAttributeView.class) {72return (V) new JrtFileAttributeView(path, false, options);73}74if (type == JrtFileAttributeView.class) {75return (V) new JrtFileAttributeView(path, true, options);76}77return null;78}7980static JrtFileAttributeView get(JrtPath path, String type, LinkOption... options) {81Objects.requireNonNull(type);82if (type.equals("basic")) {83return new JrtFileAttributeView(path, false, options);84}85if (type.equals("jrt")) {86return new JrtFileAttributeView(path, true, options);87}88return null;89}9091@Override92public String name() {93return isJrtView ? "jrt" : "basic";94}9596@Override97public JrtFileAttributes readAttributes() throws IOException {98return path.getAttributes(options);99}100101@Override102public void setTimes(FileTime lastModifiedTime,103FileTime lastAccessTime,104FileTime createTime) throws IOException {105path.setTimes(lastModifiedTime, lastAccessTime, createTime);106}107108static void setAttribute(JrtPath path, String attribute, Object value)109throws IOException {110int colonPos = attribute.indexOf(':');111if (colonPos != -1) { // type = "basic", if no ":"112String type = attribute.substring(0, colonPos++);113if (!type.equals("basic") && !type.equals("jrt")) {114throw new UnsupportedOperationException(115"view <" + type + "> is not supported");116}117attribute = attribute.substring(colonPos);118}119try {120AttrID id = AttrID.valueOf(attribute);121if (id == AttrID.lastModifiedTime) {122path.setTimes((FileTime) value, null, null);123} else if (id == AttrID.lastAccessTime) {124path.setTimes(null, (FileTime) value, null);125} else if (id == AttrID.creationTime) {126path.setTimes(null, null, (FileTime) value);127}128return;129} catch (IllegalArgumentException x) {}130throw new UnsupportedOperationException("'" + attribute131+ "' is unknown or read-only attribute");132}133134static Map<String, Object> readAttributes(JrtPath path, String attributes,135LinkOption... options)136throws IOException {137int colonPos = attributes.indexOf(':');138boolean isJrtView = false;139if (colonPos != -1) { // type = "basic", if no ":"140String type = attributes.substring(0, colonPos++);141if (!type.equals("basic") && !type.equals("jrt")) {142throw new UnsupportedOperationException("view <" + type +143"> is not supported");144}145isJrtView = true;146attributes = attributes.substring(colonPos);147}148JrtFileAttributes jrtfas = path.getAttributes();149LinkedHashMap<String, Object> map = new LinkedHashMap<>();150if ("*".equals(attributes)) {151for (AttrID id : AttrID.values()) {152map.put(id.name(), attribute(id, jrtfas, isJrtView));153}154} else {155String[] as = attributes.split(",");156for (String a : as) {157//throw IllegalArgumentException158map.put(a, attribute(AttrID.valueOf(a), jrtfas, isJrtView));159}160}161return map;162}163164static Object attribute(AttrID id, JrtFileAttributes jrtfas, boolean isJrtView) {165switch (id) {166case size:167return jrtfas.size();168case creationTime:169return jrtfas.creationTime();170case lastAccessTime:171return jrtfas.lastAccessTime();172case lastModifiedTime:173return jrtfas.lastModifiedTime();174case isDirectory:175return jrtfas.isDirectory();176case isRegularFile:177return jrtfas.isRegularFile();178case isSymbolicLink:179return jrtfas.isSymbolicLink();180case isOther:181return jrtfas.isOther();182case fileKey:183return jrtfas.fileKey();184case compressedSize:185if (isJrtView) {186return jrtfas.compressedSize();187}188break;189case extension:190if (isJrtView) {191return jrtfas.extension();192}193break;194}195return null;196}197}198199200