1/*
2 * Copyright (c) 2016, 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 */
25package jdk.internal.jrtfs;
26
27import java.nio.file.attribute.BasicFileAttributes;
28import java.nio.file.attribute.FileTime;
29import java.util.Formatter;
30import jdk.internal.jimage.ImageReader.Node;
31
32/**
33 * File attributes implementation for jrt image file system.
34 *
35 * @implNote This class needs to maintain JDK 8 source compatibility.
36 *
37 * It is used internally in the JDK to implement jimage/jrtfs access,
38 * but also compiled and delivered as part of the jrtfs.jar to support access
39 * to the jimage file provided by the shipped JDK by tools running on JDK 8.
40 */
41final class JrtFileAttributes  implements BasicFileAttributes {
42
43    private final Node node;
44
45    JrtFileAttributes(Node node) {
46        this.node = node;
47    }
48
49    ///////// basic attributes ///////////
50    @Override
51    public FileTime creationTime() {
52        return node.creationTime();
53    }
54
55    @Override
56    public boolean isDirectory() {
57        return node.isDirectory();
58    }
59
60    @Override
61    public boolean isOther() {
62        return false;
63    }
64
65    @Override
66    public boolean isRegularFile() {
67        return !isDirectory();
68    }
69
70    @Override
71    public FileTime lastAccessTime() {
72        return node.lastAccessTime();
73    }
74
75    @Override
76    public FileTime lastModifiedTime() {
77        return node.lastModifiedTime();
78    }
79
80    @Override
81    public long size() {
82        return node.size();
83    }
84
85    @Override
86    public boolean isSymbolicLink() {
87        return node.isLink();
88    }
89
90    @Override
91    public Object fileKey() {
92        return node.resolveLink(true);
93    }
94
95    ///////// jrtfs specific attributes ///////////
96    /**
97     * Compressed resource file. If not available or not applicable, 0L is
98     * returned.
99     *
100     * @return the compressed resource size for compressed resources.
101     */
102    public long compressedSize() {
103        return node.compressedSize();
104    }
105
106    /**
107     * "file" extension of a file resource.
108     *
109     * @return extension string for the file resource
110     */
111    public String extension() {
112        return node.extension();
113    }
114
115    @Override
116    public final String toString() {
117        StringBuilder sb = new StringBuilder(1024);
118        try (Formatter fm = new Formatter(sb)) {
119            if (creationTime() != null) {
120                fm.format("    creationTime    : %tc%n", creationTime().toMillis());
121            } else {
122                fm.format("    creationTime    : null%n");
123            }
124            if (lastAccessTime() != null) {
125                fm.format("    lastAccessTime  : %tc%n", lastAccessTime().toMillis());
126            } else {
127                fm.format("    lastAccessTime  : null%n");
128            }
129            fm.format("    lastModifiedTime: %tc%n", lastModifiedTime().toMillis());
130            fm.format("    isRegularFile   : %b%n", isRegularFile());
131            fm.format("    isDirectory     : %b%n", isDirectory());
132            fm.format("    isSymbolicLink  : %b%n", isSymbolicLink());
133            fm.format("    isOther         : %b%n", isOther());
134            fm.format("    fileKey         : %s%n", fileKey());
135            fm.format("    size            : %d%n", size());
136            fm.format("    compressedSize  : %d%n", compressedSize());
137            fm.format("    extension       : %s%n", extension());
138        }
139        return sb.toString();
140    }
141}
142