Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/classes/sun/nio/fs/WindowsFileAttributeViews.java
32288 views
/*1* Copyright (c) 2008, 2011, 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*/2425package sun.nio.fs;262728import java.nio.file.attribute.*;29import java.util.*;30import java.io.IOException;3132import static sun.nio.fs.WindowsNativeDispatcher.*;33import static sun.nio.fs.WindowsConstants.*;3435class WindowsFileAttributeViews {3637private static class Basic extends AbstractBasicFileAttributeView {38final WindowsPath file;39final boolean followLinks;4041Basic(WindowsPath file, boolean followLinks) {42this.file = file;43this.followLinks = followLinks;44}4546@Override47public WindowsFileAttributes readAttributes() throws IOException {48file.checkRead();49try {50return WindowsFileAttributes.get(file, followLinks);51} catch (WindowsException x) {52x.rethrowAsIOException(file);53return null; // keep compiler happy54}55}5657/**58* Adjusts a Windows time for the FAT epoch.59*/60private long adjustForFatEpoch(long time) {61// 1/1/1980 in Windows Time62final long FAT_EPOCH = 119600064000000000L;63if (time != -1L && time < FAT_EPOCH) {64return FAT_EPOCH;65} else {66return time;67}68}6970/**71* Parameter values in Windows times.72*/73void setFileTimes(long createTime,74long lastAccessTime,75long lastWriteTime)76throws IOException77{78long handle = -1L;79try {80int flags = FILE_FLAG_BACKUP_SEMANTICS;81if (!followLinks && file.getFileSystem().supportsLinks())82flags |= FILE_FLAG_OPEN_REPARSE_POINT;8384handle = CreateFile(file.getPathForWin32Calls(),85FILE_WRITE_ATTRIBUTES,86(FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE),87OPEN_EXISTING,88flags);89} catch (WindowsException x) {90x.rethrowAsIOException(file);91}9293// update times94try {95SetFileTime(handle,96createTime,97lastAccessTime,98lastWriteTime);99} catch (WindowsException x) {100// If ERROR_INVALID_PARAMATER is returned and the volume is101// FAT then adjust to the FAT epoch and retry.102if (followLinks && x.lastError() == ERROR_INVALID_PARAMATER) {103try {104if (WindowsFileStore.create(file).type().equals("FAT")) {105SetFileTime(handle,106adjustForFatEpoch(createTime),107adjustForFatEpoch(lastAccessTime),108adjustForFatEpoch(lastWriteTime));109// retry succeeded110x = null;111}112} catch (SecurityException ignore) {113} catch (WindowsException ignore) {114} catch (IOException ignore) {115// ignore exceptions to let original exception be thrown116}117}118if (x != null)119x.rethrowAsIOException(file);120} finally {121CloseHandle(handle);122}123}124125@Override126public void setTimes(FileTime lastModifiedTime,127FileTime lastAccessTime,128FileTime createTime) throws IOException129{130// if all null then do nothing131if (lastModifiedTime == null && lastAccessTime == null &&132createTime == null)133{134// no effect135return;136}137138// permission check139file.checkWrite();140141// update times142long t1 = (createTime == null) ? -1L :143WindowsFileAttributes.toWindowsTime(createTime);144long t2 = (lastAccessTime == null) ? -1L :145WindowsFileAttributes.toWindowsTime(lastAccessTime);146long t3 = (lastModifiedTime == null) ? -1L :147WindowsFileAttributes.toWindowsTime(lastModifiedTime);148setFileTimes(t1, t2, t3);149}150}151152static class Dos extends Basic implements DosFileAttributeView {153private static final String READONLY_NAME = "readonly";154private static final String ARCHIVE_NAME = "archive";155private static final String SYSTEM_NAME = "system";156private static final String HIDDEN_NAME = "hidden";157private static final String ATTRIBUTES_NAME = "attributes";158159// the names of the DOS attribtues (includes basic)160static final Set<String> dosAttributeNames =161Util.newSet(basicAttributeNames,162READONLY_NAME, ARCHIVE_NAME, SYSTEM_NAME, HIDDEN_NAME, ATTRIBUTES_NAME);163164Dos(WindowsPath file, boolean followLinks) {165super(file, followLinks);166}167168@Override169public String name() {170return "dos";171}172173@Override174public void setAttribute(String attribute, Object value)175throws IOException176{177if (attribute.equals(READONLY_NAME)) {178setReadOnly((Boolean)value);179return;180}181if (attribute.equals(ARCHIVE_NAME)) {182setArchive((Boolean)value);183return;184}185if (attribute.equals(SYSTEM_NAME)) {186setSystem((Boolean)value);187return;188}189if (attribute.equals(HIDDEN_NAME)) {190setHidden((Boolean)value);191return;192}193super.setAttribute(attribute, value);194}195196@Override197public Map<String,Object> readAttributes(String[] attributes)198throws IOException199{200AttributesBuilder builder =201AttributesBuilder.create(dosAttributeNames, attributes);202WindowsFileAttributes attrs = readAttributes();203addRequestedBasicAttributes(attrs, builder);204if (builder.match(READONLY_NAME))205builder.add(READONLY_NAME, attrs.isReadOnly());206if (builder.match(ARCHIVE_NAME))207builder.add(ARCHIVE_NAME, attrs.isArchive());208if (builder.match(SYSTEM_NAME))209builder.add(SYSTEM_NAME, attrs.isSystem());210if (builder.match(HIDDEN_NAME))211builder.add(HIDDEN_NAME, attrs.isHidden());212if (builder.match(ATTRIBUTES_NAME))213builder.add(ATTRIBUTES_NAME, attrs.attributes());214return builder.unmodifiableMap();215}216217/**218* Update DOS attributes219*/220private void updateAttributes(int flag, boolean enable)221throws IOException222{223file.checkWrite();224225// GetFileAttribtues & SetFileAttributes do not follow links so when226// following links we need the final target227String path = WindowsLinkSupport.getFinalPath(file, followLinks);228try {229int oldValue = GetFileAttributes(path);230int newValue = oldValue;231if (enable) {232newValue |= flag;233} else {234newValue &= ~flag;235}236if (newValue != oldValue) {237SetFileAttributes(path, newValue);238}239} catch (WindowsException x) {240// don't reveal target in exception241x.rethrowAsIOException(file);242}243}244245@Override246public void setReadOnly(boolean value) throws IOException {247updateAttributes(FILE_ATTRIBUTE_READONLY, value);248}249250@Override251public void setHidden(boolean value) throws IOException {252updateAttributes(FILE_ATTRIBUTE_HIDDEN, value);253}254255@Override256public void setArchive(boolean value) throws IOException {257updateAttributes(FILE_ATTRIBUTE_ARCHIVE, value);258}259260@Override261public void setSystem(boolean value) throws IOException {262updateAttributes(FILE_ATTRIBUTE_SYSTEM, value);263}264265// package-private266// Copy given attributes to the file.267void setAttributes(WindowsFileAttributes attrs)268throws IOException269{270// copy DOS attributes to target271int flags = 0;272if (attrs.isReadOnly()) flags |= FILE_ATTRIBUTE_READONLY;273if (attrs.isHidden()) flags |= FILE_ATTRIBUTE_HIDDEN;274if (attrs.isArchive()) flags |= FILE_ATTRIBUTE_ARCHIVE;275if (attrs.isSystem()) flags |= FILE_ATTRIBUTE_SYSTEM;276updateAttributes(flags, true);277278// copy file times to target - must be done after updating FAT attributes279// as otherwise the last modified time may be wrong.280setFileTimes(281WindowsFileAttributes.toWindowsTime(attrs.creationTime()),282WindowsFileAttributes.toWindowsTime(attrs.lastModifiedTime()),283WindowsFileAttributes.toWindowsTime(attrs.lastAccessTime()));284}285}286287static Basic createBasicView(WindowsPath file, boolean followLinks) {288return new Basic(file, followLinks);289}290291static Dos createDosView(WindowsPath file, boolean followLinks) {292return new Dos(file, followLinks);293}294}295296297