Skip to content
Permalink
Browse files
8269074: (fs) Files.copy fails to copy from /proc on some linux kerne…
…l versions

Reviewed-by: alanb, vtewari
  • Loading branch information
Brian Burkhalter committed Jun 25, 2021
1 parent d799563 commit 5ebed06e8ae27ca73433497b4e4aa559d06ed8ec
Showing 1 changed file with 40 additions and 30 deletions.
@@ -68,6 +68,39 @@ int fcopyfile_callback(int what, int stage, copyfile_state_t state,
}
#endif

// Transfer via user-space buffers
void transfer(JNIEnv* env, jint dst, jint src, volatile jint* cancel)
{
char buf[8192];

for (;;) {
ssize_t n, pos, len;
RESTARTABLE(read((int)src, &buf, sizeof(buf)), n);
if (n <= 0) {
if (n < 0)
throwUnixException(env, errno);
return;
}
if (cancel != NULL && *cancel != 0) {
throwUnixException(env, ECANCELED);
return;
}
pos = 0;
len = n;
do {
char* bufp = buf;
bufp += pos;
RESTARTABLE(write((int)dst, bufp, len), n);
if (n == -1) {
throwUnixException(env, errno);
return;
}
pos += n;
len -= n;
} while (len > 0);
}
}

/**
* Transfer all bytes from src to dst within the kernel if possible (Linux),
* otherwise via user-space buffers
@@ -87,7 +120,12 @@ Java_sun_nio_fs_UnixCopyFile_transfer
do {
RESTARTABLE(sendfile64(dst, src, NULL, count), bytes_sent);
if (bytes_sent == -1) {
throwUnixException(env, errno);
if (errno == EINVAL || errno == ENOSYS) {
// Fall back to copying via user-space buffers
transfer(env, dst, src, cancel);
} else {
throwUnixException(env, errno);
}
return;
}
if (cancel != NULL && *cancel != 0) {
@@ -114,34 +152,6 @@ Java_sun_nio_fs_UnixCopyFile_transfer
if (state != NULL)
copyfile_state_free(state);
#else
// Transfer via user-space buffers
char buf[8192];

for (;;) {
ssize_t n, pos, len;
RESTARTABLE(read((int)src, &buf, sizeof(buf)), n);
if (n <= 0) {
if (n < 0)
throwUnixException(env, errno);
return;
}
if (cancel != NULL && *cancel != 0) {
throwUnixException(env, ECANCELED);
return;
}
pos = 0;
len = n;
do {
char* bufp = buf;
bufp += pos;
RESTARTABLE(write((int)dst, bufp, len), n);
if (n == -1) {
throwUnixException(env, errno);
return;
}
pos += n;
len -= n;
} while (len > 0);
}
transfer(env, dst, src, cancel);
#endif
}

0 comments on commit 5ebed06

Please sign in to comment.