|
33 | 33 | import java.util.Collections;
|
34 | 34 | import java.util.HashSet;
|
35 | 35 | import java.util.Set;
|
| 36 | +import sun.nio.ch.IOStatus; |
36 | 37 | import sun.security.action.GetPropertyAction;
|
37 | 38 |
|
| 39 | +import static sun.nio.fs.UnixConstants.*; |
| 40 | +import static sun.nio.fs.UnixNativeDispatcher.chown; |
| 41 | +import static sun.nio.fs.UnixNativeDispatcher.unlink; |
| 42 | + |
38 | 43 | /**
|
39 | 44 | * Bsd implementation of FileSystem
|
40 | 45 | */
|
@@ -71,13 +76,84 @@ public Set<String> supportedFileAttributeViews() {
|
71 | 76 | return SupportedFileFileAttributeViewsHolder.supportedFileAttributeViews;
|
72 | 77 | }
|
73 | 78 |
|
| 79 | + /** |
| 80 | + * Clones the file whose path name is {@code src} to that whose path |
| 81 | + * name is {@code dst} using the {@code clonefile} system call. |
| 82 | + * |
| 83 | + * @param src the path of the source file |
| 84 | + * @param dst the path of the destination file (clone) |
| 85 | + * @param followLinks whether to follow links |
| 86 | + * |
| 87 | + * @return 0 on success, or IOStatus.UNSUPPORTED_CASE if the call |
| 88 | + * does not work with the given parameters |
| 89 | + */ |
| 90 | + private int clone(UnixPath src, UnixPath dst, boolean followLinks) |
| 91 | + throws IOException |
| 92 | + { |
| 93 | + int flags = followLinks ? 0 : CLONE_NOFOLLOW; |
| 94 | + try { |
| 95 | + BsdNativeDispatcher.clonefile(src, dst, flags); |
| 96 | + } catch (UnixException x) { |
| 97 | + switch (x.errno()) { |
| 98 | + case ENOTSUP: // cloning not supported by filesystem |
| 99 | + case EXDEV: // src and dst on different filesystems |
| 100 | + case ENOTDIR: // problematic path parameter(s) |
| 101 | + return IOStatus.UNSUPPORTED_CASE; |
| 102 | + default: |
| 103 | + x.rethrowAsIOException(src, dst); |
| 104 | + return IOStatus.THROWN; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + return 0; |
| 109 | + } |
| 110 | + |
74 | 111 | @Override
|
75 | 112 | protected int directCopy(int dst, int src, long addressToPollForCancel)
|
76 | 113 | throws UnixException
|
77 | 114 | {
|
78 | 115 | return directCopy0(dst, src, addressToPollForCancel);
|
79 | 116 | }
|
80 | 117 |
|
| 118 | + @Override |
| 119 | + protected void copyFile(UnixPath source, |
| 120 | + UnixFileAttributes attrs, |
| 121 | + UnixPath target, |
| 122 | + Flags flags, |
| 123 | + long addressToPollForCancel) |
| 124 | + throws IOException |
| 125 | + { |
| 126 | + // Attempt to clone the source unless cancellation is not possible, |
| 127 | + // or attributes are not to be copied |
| 128 | + if (addressToPollForCancel == 0 && flags.copyPosixAttributes) { |
| 129 | + try { |
| 130 | + int res = clone(source, target, flags.followLinks); |
| 131 | + |
| 132 | + if (res == 0) { |
| 133 | + // copy owner (not done by clonefile) |
| 134 | + try { |
| 135 | + chown(target, attrs.uid(), attrs.gid()); |
| 136 | + } catch (UnixException x) { |
| 137 | + if (flags.failIfUnableToCopyPosix) |
| 138 | + x.rethrowAsIOException(target); |
| 139 | + } |
| 140 | + return; |
| 141 | + } |
| 142 | + } catch (IOException e) { |
| 143 | + // clone or chown failed so roll back |
| 144 | + try { |
| 145 | + unlink(target); |
| 146 | + } catch (UnixException ignore) { } |
| 147 | + |
| 148 | + throw e; |
| 149 | + } |
| 150 | + |
| 151 | + // fall through to superclass method |
| 152 | + } |
| 153 | + |
| 154 | + super.copyFile(source, attrs, target, flags, addressToPollForCancel); |
| 155 | + } |
| 156 | + |
81 | 157 | @Override
|
82 | 158 | void copyNonPosixAttributes(int ofd, int nfd) {
|
83 | 159 | UnixUserDefinedFileAttributeView.copyExtendedAttributes(ofd, nfd);
|
|
1 commit comments
openjdk-notifier[bot] commentedon Sep 14, 2022
Review
Issues