< prev index next >

src/java.base/windows/classes/sun/nio/fs/WindowsFileCopy.java

Print this page


   1 /*
   2  * Copyright (c) 2008, 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


 232             WindowsFileAttributeViews.Dos view =
 233                 WindowsFileAttributeViews.createDosView(target, false);
 234             try {
 235                 view.setAttributes(sourceAttrs);
 236             } catch (IOException x) {
 237                 if (sourceAttrs.isDirectory()) {
 238                     try {
 239                         RemoveDirectory(targetPath);
 240                     } catch (WindowsException ignore) { }
 241                 }
 242             }
 243 
 244             // copy security attributes. If this fail it doesn't cause the move
 245             // to fail.
 246             try {
 247                 copySecurityAttributes(source, target, followLinks);
 248             } catch (IOException ignore) { }
 249         }
 250     }
 251 











 252     /**
 253      * Move file from source to target
 254      */
 255     static void move(WindowsPath source, WindowsPath target, CopyOption... options)
 256         throws IOException
 257     {
 258         // map options
 259         boolean atomicMove = false;
 260         boolean replaceExisting = false;
 261         for (CopyOption option: options) {
 262             if (option == StandardCopyOption.ATOMIC_MOVE) {
 263                 atomicMove = true;
 264                 continue;
 265             }
 266             if (option == StandardCopyOption.REPLACE_EXISTING) {
 267                 replaceExisting = true;
 268                 continue;
 269             }
 270             if (option == LinkOption.NOFOLLOW_LINKS) {
 271                 // ignore


 390                 MoveFileEx(sourcePath, targetPath, MOVEFILE_COPY_ALLOWED);
 391             } catch (WindowsException x) {
 392                 x.rethrowAsIOException(source, target);
 393             }
 394             // MoveFileEx does not copy security attributes when moving
 395             // across volumes.
 396             try {
 397                 copySecurityAttributes(source, target, false);
 398             } catch (IOException x) {
 399                 // ignore
 400             }
 401             return;
 402         }
 403 
 404         // moving directory or directory-link to another file system
 405         assert sourceAttrs.isDirectory() || sourceAttrs.isDirectoryLink();
 406 
 407         // create new directory or directory junction
 408         try {
 409             if (sourceAttrs.isDirectory()) {

 410                 CreateDirectory(targetPath, 0L);
 411             } else {
 412                 String linkTarget = WindowsLinkSupport.readLink(source);
 413                 CreateSymbolicLink(targetPath,
 414                                    WindowsPath.addPrefixIfNeeded(linkTarget),
 415                                    SYMBOLIC_LINK_FLAG_DIRECTORY);
 416             }
 417         } catch (WindowsException x) {
 418             x.rethrowAsIOException(target);
 419         }
 420 
 421         // copy timestamps/DOS attributes
 422         WindowsFileAttributeViews.Dos view =
 423                 WindowsFileAttributeViews.createDosView(target, false);
 424         try {
 425             view.setAttributes(sourceAttrs);
 426         } catch (IOException x) {
 427             // rollback
 428             try {
 429                 RemoveDirectory(targetPath);


   1 /*
   2  * Copyright (c) 2008, 2018, 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


 232             WindowsFileAttributeViews.Dos view =
 233                 WindowsFileAttributeViews.createDosView(target, false);
 234             try {
 235                 view.setAttributes(sourceAttrs);
 236             } catch (IOException x) {
 237                 if (sourceAttrs.isDirectory()) {
 238                     try {
 239                         RemoveDirectory(targetPath);
 240                     } catch (WindowsException ignore) { }
 241                 }
 242             }
 243 
 244             // copy security attributes. If this fail it doesn't cause the move
 245             // to fail.
 246             try {
 247                 copySecurityAttributes(source, target, followLinks);
 248             } catch (IOException ignore) { }
 249         }
 250     }
 251 
 252     // throw a DirectoryNotEmpty exception if not empty
 253     static void ensureEmptyDir(WindowsPath dir) throws IOException {
 254         try (WindowsDirectoryStream dirStream =
 255             new WindowsDirectoryStream(dir, (e) -> true)) {
 256             if (dirStream.iterator().hasNext()) {
 257                 throw new DirectoryNotEmptyException(
 258                     dir.getPathForExceptionMessage());
 259             }
 260         }
 261     }
 262 
 263     /**
 264      * Move file from source to target
 265      */
 266     static void move(WindowsPath source, WindowsPath target, CopyOption... options)
 267         throws IOException
 268     {
 269         // map options
 270         boolean atomicMove = false;
 271         boolean replaceExisting = false;
 272         for (CopyOption option: options) {
 273             if (option == StandardCopyOption.ATOMIC_MOVE) {
 274                 atomicMove = true;
 275                 continue;
 276             }
 277             if (option == StandardCopyOption.REPLACE_EXISTING) {
 278                 replaceExisting = true;
 279                 continue;
 280             }
 281             if (option == LinkOption.NOFOLLOW_LINKS) {
 282                 // ignore


 401                 MoveFileEx(sourcePath, targetPath, MOVEFILE_COPY_ALLOWED);
 402             } catch (WindowsException x) {
 403                 x.rethrowAsIOException(source, target);
 404             }
 405             // MoveFileEx does not copy security attributes when moving
 406             // across volumes.
 407             try {
 408                 copySecurityAttributes(source, target, false);
 409             } catch (IOException x) {
 410                 // ignore
 411             }
 412             return;
 413         }
 414 
 415         // moving directory or directory-link to another file system
 416         assert sourceAttrs.isDirectory() || sourceAttrs.isDirectoryLink();
 417 
 418         // create new directory or directory junction
 419         try {
 420             if (sourceAttrs.isDirectory()) {
 421                 ensureEmptyDir(source);
 422                 CreateDirectory(targetPath, 0L);
 423             } else {
 424                 String linkTarget = WindowsLinkSupport.readLink(source);
 425                 CreateSymbolicLink(targetPath,
 426                                    WindowsPath.addPrefixIfNeeded(linkTarget),
 427                                    SYMBOLIC_LINK_FLAG_DIRECTORY);
 428             }
 429         } catch (WindowsException x) {
 430             x.rethrowAsIOException(target);
 431         }
 432 
 433         // copy timestamps/DOS attributes
 434         WindowsFileAttributeViews.Dos view =
 435                 WindowsFileAttributeViews.createDosView(target, false);
 436         try {
 437             view.setAttributes(sourceAttrs);
 438         } catch (IOException x) {
 439             // rollback
 440             try {
 441                 RemoveDirectory(targetPath);


< prev index next >