< prev index next >

src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipFileAttributeView.java

Print this page
rev 55003 : 8213031: (zipfs) Add support for POSIX file permissions


  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 }


  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 }
< prev index next >