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.FileTime;
  31 import java.util.LinkedHashMap;
  32 import java.util.Map;
  33 
  34 /**
  35  * @author Xueming Shen, Rajendra Gutupalli, Jaya Hangal
  36  */
  37 class ZipFileAttributeView implements BasicFileAttributeView {
  38     private enum AttrID {
  39         size,
  40         creationTime,
  41         lastAccessTime,
  42         lastModifiedTime,
  43         isDirectory,
  44         isRegularFile,
  45         isSymbolicLink,
  46         isOther,
  47         fileKey,
  48         compressedSize,
  49         crc,
  50         method
  51     }
  52 
  53     private final ZipPath path;
  54     private final boolean isZipView;
  55 
  56     ZipFileAttributeView(ZipPath path, boolean isZipView) {
  57         this.path = path;
  58         this.isZipView = isZipView;
  59     }
  60 
  61     @Override
  62     public String name() {
  63         return isZipView ? "zip" : "basic";
  64     }
  65 
  66     @Override
  67     public ZipFileAttributes readAttributes() throws IOException {
  68         return path.readAttributes();
  69     }
  70 
  71     @Override
  72     public void setTimes(FileTime lastModifiedTime,
  73                          FileTime lastAccessTime,
  74                          FileTime createTime)
  75         throws IOException
  76     {
  77         path.setTimes(lastModifiedTime, lastAccessTime, createTime);
  78     }
  79 
  80     void setAttribute(String attribute, Object value)
  81         throws IOException
  82     {
  83         try {
  84             if (AttrID.valueOf(attribute) == AttrID.lastModifiedTime)
  85                 setTimes((FileTime)value, null, null);
  86             if (AttrID.valueOf(attribute) == AttrID.lastAccessTime)
  87                 setTimes(null, (FileTime)value, null);
  88             if (AttrID.valueOf(attribute) == AttrID.creationTime)
  89                 setTimes(null, null, (FileTime)value);
  90         } catch (IllegalArgumentException x) {
  91             throw new UnsupportedOperationException("'" + attribute +
  92                 "' is unknown or read-only attribute");
  93         }
  94     }
  95 
  96     Map<String, Object> readAttributes(String attributes)
  97         throws IOException
  98     {
  99         ZipFileAttributes zfas = readAttributes();
 100         LinkedHashMap<String, Object> map = new LinkedHashMap<>();
 101         if ("*".equals(attributes)) {
 102             for (AttrID id : AttrID.values()) {
 103                 try {
 104                     map.put(id.name(), attribute(id, zfas));
 105                 } catch (IllegalArgumentException x) {}
 106             }
 107         } else {
 108             String[] as = attributes.split(",");
 109             for (String a : as) {
 110                 try {
 111                     map.put(a, attribute(AttrID.valueOf(a), zfas));
 112                 } catch (IllegalArgumentException x) {}
 113             }
 114         }
 115         return map;
 116     }
 117 
 118     private Object attribute(AttrID id, ZipFileAttributes zfas) {
 119         switch (id) {
 120         case size:
 121             return zfas.size();
 122         case creationTime:
 123             return zfas.creationTime();
 124         case lastAccessTime:
 125             return zfas.lastAccessTime();
 126         case lastModifiedTime:
 127             return zfas.lastModifiedTime();
 128         case isDirectory:
 129             return zfas.isDirectory();
 130         case isRegularFile:
 131             return zfas.isRegularFile();
 132         case isSymbolicLink:
 133             return zfas.isSymbolicLink();
 134         case isOther:
 135             return zfas.isOther();
 136         case fileKey:
 137             return zfas.fileKey();
 138         case compressedSize:
 139             if (isZipView)
 140                 return zfas.compressedSize();
 141             break;
 142         case crc:
 143             if (isZipView)
 144                 return zfas.crc();
 145             break;
 146         case method:
 147             if (isZipView)
 148                 return zfas.method();
 149             break;
 150         default:
 151             break;
 152         }
 153         return null;
 154     }
 155 }