8265175: (fs) Files.copy(Path,Path,CopyOption...) should use sendfile on Linux
Reviewed-by: alanb
This commit is contained in:
parent
cee4f1d9c4
commit
1c3fd46d7d
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
@ -30,6 +30,10 @@
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
|
||||
#if defined(__linux__)
|
||||
#include <sys/sendfile.h>
|
||||
#endif
|
||||
|
||||
#include "sun_nio_fs_UnixCopyFile.h"
|
||||
|
||||
#define RESTARTABLE(_cmd, _result) do { \
|
||||
@ -47,15 +51,35 @@ static void throwUnixException(JNIEnv* env, int errnum) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Transfer all bytes from src to dst via user-space buffers
|
||||
* Transfer all bytes from src to dst within the kernel if possible (Linux),
|
||||
* otherwise via user-space buffers
|
||||
*/
|
||||
JNIEXPORT void JNICALL
|
||||
Java_sun_nio_fs_UnixCopyFile_transfer
|
||||
(JNIEnv* env, jclass this, jint dst, jint src, jlong cancelAddress)
|
||||
{
|
||||
char buf[8192];
|
||||
volatile jint* cancel = (jint*)jlong_to_ptr(cancelAddress);
|
||||
|
||||
#if defined(__linux__)
|
||||
// Transfer within the kernel
|
||||
const size_t count = 1048576; // 1 MB to give cancellation a chance
|
||||
ssize_t bytes_sent;
|
||||
do {
|
||||
// sendfile() can transfer at most 0x7ffff000 bytes
|
||||
RESTARTABLE(sendfile64(dst, src, NULL, count), bytes_sent);
|
||||
if (bytes_sent == -1) {
|
||||
throwUnixException(env, errno);
|
||||
return;
|
||||
}
|
||||
if (cancel != NULL && *cancel != 0) {
|
||||
throwUnixException(env, ECANCELED);
|
||||
return;
|
||||
}
|
||||
} while (bytes_sent > 0);
|
||||
#else
|
||||
// Transfer via user-space buffers
|
||||
char buf[8192];
|
||||
|
||||
for (;;) {
|
||||
ssize_t n, pos, len;
|
||||
RESTARTABLE(read((int)src, &buf, sizeof(buf)), n);
|
||||
@ -82,4 +106,5 @@ Java_sun_nio_fs_UnixCopyFile_transfer
|
||||
len -= n;
|
||||
} while (len > 0);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user