< prev index next >

src/java.base/unix/classes/sun/nio/fs/UnixCopyFile.java

Print this page

 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 sun.nio.fs;
 27 
 28 import java.io.IOException;
 29 import java.nio.file.AtomicMoveNotSupportedException;
 30 import java.nio.file.CopyOption;
 31 import java.nio.file.DirectoryNotEmptyException;
 32 import java.nio.file.FileAlreadyExistsException;
 33 import java.nio.file.LinkOption;
 34 import java.nio.file.LinkPermission;
 35 import java.nio.file.StandardCopyOption;
 36 import java.util.concurrent.ExecutionException;
 37 import java.util.concurrent.TimeUnit;
 38 
 39 import static sun.nio.fs.UnixNativeDispatcher.*;
 40 import static sun.nio.fs.UnixConstants.*;
 41 
 42 
 43 /**
 44  * Unix implementation of Path#copyTo and Path#moveTo methods.
 45  */
 46 
 47 class UnixCopyFile {
 48     private UnixCopyFile() {  }
 49 
 50     // The flags that control how a file is copied or moved
 51     private static class Flags {
 52         boolean replaceExisting;
 53         boolean atomicMove;
 54         boolean followLinks;
 55         boolean interruptible;
 56 
 57         // the attributes to copy
 58         boolean copyBasicAttributes;
 59         boolean copyPosixAttributes;
 60         boolean copyNonPosixAttributes;
 61 
 62         // flags that indicate if we should fail if attributes cannot be copied

234         }
235 
236         try {
237             // open new file
238             int fo = -1;
239             try {
240                 fo = open(target,
241                            (O_WRONLY |
242                             O_CREAT |
243                             O_EXCL),
244                            attrs.mode());
245             } catch (UnixException x) {
246                 x.rethrowAsIOException(target);
247             }
248 
249             // set to true when file and attributes copied
250             boolean complete = false;
251             try {
252                 // transfer bytes to target file
253                 try {
254                     transfer(fo, fi, addressToPollForCancel);






255                 } catch (UnixException x) {
256                     x.rethrowAsIOException(source, target);
257                 }
258                 // copy owner/permissions
259                 if (flags.copyPosixAttributes) {
260                     try {
261                         fchown(fo, attrs.uid(), attrs.gid());
262                         fchmod(fo, attrs.mode());
263                     } catch (UnixException x) {
264                         if (flags.failIfUnableToCopyPosix)
265                             x.rethrowAsIOException(target);
266                     }
267                 }
268                 // copy non POSIX attributes (depends on file system)
269                 if (flags.copyNonPosixAttributes) {
270                     source.getFileSystem().copyNonPosixAttributes(fi, fo);
271                 }
272                 // copy time attributes
273                 if (flags.copyBasicAttributes) {
274                     try {

 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 sun.nio.fs;
 27 
 28 import java.io.IOException;
 29 import java.nio.file.AtomicMoveNotSupportedException;
 30 import java.nio.file.CopyOption;
 31 import java.nio.file.DirectoryNotEmptyException;
 32 import java.nio.file.FileAlreadyExistsException;
 33 import java.nio.file.LinkOption;
 34 import java.nio.file.LinkPermission;
 35 import java.nio.file.StandardCopyOption;
 36 import java.util.concurrent.ExecutionException;
 37 import java.util.concurrent.TimeUnit;
 38 import jdk.internal.misc.Blocker;
 39 import static sun.nio.fs.UnixNativeDispatcher.*;
 40 import static sun.nio.fs.UnixConstants.*;
 41 

 42 /**
 43  * Unix implementation of Path#copyTo and Path#moveTo methods.
 44  */
 45 
 46 class UnixCopyFile {
 47     private UnixCopyFile() {  }
 48 
 49     // The flags that control how a file is copied or moved
 50     private static class Flags {
 51         boolean replaceExisting;
 52         boolean atomicMove;
 53         boolean followLinks;
 54         boolean interruptible;
 55 
 56         // the attributes to copy
 57         boolean copyBasicAttributes;
 58         boolean copyPosixAttributes;
 59         boolean copyNonPosixAttributes;
 60 
 61         // flags that indicate if we should fail if attributes cannot be copied

233         }
234 
235         try {
236             // open new file
237             int fo = -1;
238             try {
239                 fo = open(target,
240                            (O_WRONLY |
241                             O_CREAT |
242                             O_EXCL),
243                            attrs.mode());
244             } catch (UnixException x) {
245                 x.rethrowAsIOException(target);
246             }
247 
248             // set to true when file and attributes copied
249             boolean complete = false;
250             try {
251                 // transfer bytes to target file
252                 try {
253                     int dst = fo;
254                     int src = fi;
255                     if (Thread.currentThread().isVirtual()) {
256                         Blocker.managedBlock(() -> transfer(dst, src, addressToPollForCancel));
257                     } else {
258                         transfer(dst, src, addressToPollForCancel);
259                     }
260                 } catch (UnixException x) {
261                     x.rethrowAsIOException(source, target);
262                 }
263                 // copy owner/permissions
264                 if (flags.copyPosixAttributes) {
265                     try {
266                         fchown(fo, attrs.uid(), attrs.gid());
267                         fchmod(fo, attrs.mode());
268                     } catch (UnixException x) {
269                         if (flags.failIfUnableToCopyPosix)
270                             x.rethrowAsIOException(target);
271                     }
272                 }
273                 // copy non POSIX attributes (depends on file system)
274                 if (flags.copyNonPosixAttributes) {
275                     source.getFileSystem().copyNonPosixAttributes(fi, fo);
276                 }
277                 // copy time attributes
278                 if (flags.copyBasicAttributes) {
279                     try {
< prev index next >