< prev index next >

src/java.base/unix/classes/sun/nio/fs/UnixCopyFile.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


 356                 }
 357             }
 358             if (flags.copyBasicAttributes) {
 359                 try {
 360                     utimes(target,
 361                            attrs.lastAccessTime().to(TimeUnit.MICROSECONDS),
 362                            attrs.lastModifiedTime().to(TimeUnit.MICROSECONDS));
 363                 } catch (UnixException x) {
 364                     if (flags.failIfUnableToCopyBasic)
 365                         x.rethrowAsIOException(target);
 366                 }
 367             }
 368             done = true;
 369         } finally {
 370             if (!done) {
 371                 try { unlink(target); } catch (UnixException ignore) { }
 372             }
 373         }
 374     }
 375 
















 376     // move file from source to target
 377     static void move(UnixPath source, UnixPath target, CopyOption... options)
 378         throws IOException
 379     {
 380         // permission check
 381         SecurityManager sm = System.getSecurityManager();
 382         if (sm != null) {
 383             source.checkWrite();
 384             target.checkWrite();
 385         }
 386 
 387         // translate options into flags
 388         Flags flags = Flags.fromMoveOptions(options);
 389 
 390         // handle atomic rename case
 391         if (flags.atomicMove) {
 392             try {
 393                 rename(source, target);
 394             } catch (UnixException x) {
 395                 if (x.errno() == EXDEV) {


 448                 {
 449                     throw new DirectoryNotEmptyException(
 450                         target.getPathForExceptionMessage());
 451                 }
 452                 x.rethrowAsIOException(target);
 453             }
 454         }
 455 
 456         // first try rename
 457         try {
 458             rename(source, target);
 459             return;
 460         } catch (UnixException x) {
 461             if (x.errno() != EXDEV && x.errno() != EISDIR) {
 462                 x.rethrowAsIOException(source, target);
 463             }
 464         }
 465 
 466         // copy source to target
 467         if (sourceAttrs.isDirectory()) {

 468             copyDirectory(source, sourceAttrs, target, flags);
 469         } else {
 470             if (sourceAttrs.isSymbolicLink()) {
 471                 copyLink(source, sourceAttrs, target, flags);
 472             } else {
 473                 if (sourceAttrs.isDevice()) {
 474                     copySpecial(source, sourceAttrs, target, flags);
 475                 } else {
 476                     copyFile(source, sourceAttrs, target, flags, 0L);
 477                 }
 478             }
 479         }
 480 
 481         // delete source
 482         try {
 483             if (sourceAttrs.isDirectory()) {
 484                 rmdir(source);
 485             } else {
 486                 unlink(source);
 487             }


   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


 356                 }
 357             }
 358             if (flags.copyBasicAttributes) {
 359                 try {
 360                     utimes(target,
 361                            attrs.lastAccessTime().to(TimeUnit.MICROSECONDS),
 362                            attrs.lastModifiedTime().to(TimeUnit.MICROSECONDS));
 363                 } catch (UnixException x) {
 364                     if (flags.failIfUnableToCopyBasic)
 365                         x.rethrowAsIOException(target);
 366                 }
 367             }
 368             done = true;
 369         } finally {
 370             if (!done) {
 371                 try { unlink(target); } catch (UnixException ignore) { }
 372             }
 373         }
 374     }
 375 
 376     // throw a DirectoryNotEmpty exception if appropriate
 377     static void ensureEmptyDir(UnixPath dir) throws IOException {
 378         try {
 379             long ptr = opendir(dir);
 380             try (UnixDirectoryStream stream =
 381                 new UnixDirectoryStream(dir, ptr, e -> true)) {
 382                 if (stream.iterator().hasNext()) {
 383                     throw new DirectoryNotEmptyException(
 384                         dir.getPathForExceptionMessage());
 385                 }
 386             }
 387         } catch (UnixException e) {
 388             e.rethrowAsIOException(dir);
 389         }
 390     }
 391 
 392     // move file from source to target
 393     static void move(UnixPath source, UnixPath target, CopyOption... options)
 394         throws IOException
 395     {
 396         // permission check
 397         SecurityManager sm = System.getSecurityManager();
 398         if (sm != null) {
 399             source.checkWrite();
 400             target.checkWrite();
 401         }
 402 
 403         // translate options into flags
 404         Flags flags = Flags.fromMoveOptions(options);
 405 
 406         // handle atomic rename case
 407         if (flags.atomicMove) {
 408             try {
 409                 rename(source, target);
 410             } catch (UnixException x) {
 411                 if (x.errno() == EXDEV) {


 464                 {
 465                     throw new DirectoryNotEmptyException(
 466                         target.getPathForExceptionMessage());
 467                 }
 468                 x.rethrowAsIOException(target);
 469             }
 470         }
 471 
 472         // first try rename
 473         try {
 474             rename(source, target);
 475             return;
 476         } catch (UnixException x) {
 477             if (x.errno() != EXDEV && x.errno() != EISDIR) {
 478                 x.rethrowAsIOException(source, target);
 479             }
 480         }
 481 
 482         // copy source to target
 483         if (sourceAttrs.isDirectory()) {
 484             ensureEmptyDir(source);
 485             copyDirectory(source, sourceAttrs, target, flags);
 486         } else {
 487             if (sourceAttrs.isSymbolicLink()) {
 488                 copyLink(source, sourceAttrs, target, flags);
 489             } else {
 490                 if (sourceAttrs.isDevice()) {
 491                     copySpecial(source, sourceAttrs, target, flags);
 492                 } else {
 493                     copyFile(source, sourceAttrs, target, flags, 0L);
 494                 }
 495             }
 496         }
 497 
 498         // delete source
 499         try {
 500             if (sourceAttrs.isDirectory()) {
 501                 rmdir(source);
 502             } else {
 503                 unlink(source);
 504             }


< prev index next >