mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-28 23:34:52 +02:00
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 changed files with 28 additions and 3 deletions
|
@ -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.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
@ -30,6 +30,10 @@
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
|
#if defined(__linux__)
|
||||||
|
#include <sys/sendfile.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#include "sun_nio_fs_UnixCopyFile.h"
|
#include "sun_nio_fs_UnixCopyFile.h"
|
||||||
|
|
||||||
#define RESTARTABLE(_cmd, _result) do { \
|
#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
|
JNIEXPORT void JNICALL
|
||||||
Java_sun_nio_fs_UnixCopyFile_transfer
|
Java_sun_nio_fs_UnixCopyFile_transfer
|
||||||
(JNIEnv* env, jclass this, jint dst, jint src, jlong cancelAddress)
|
(JNIEnv* env, jclass this, jint dst, jint src, jlong cancelAddress)
|
||||||
{
|
{
|
||||||
char buf[8192];
|
|
||||||
volatile jint* cancel = (jint*)jlong_to_ptr(cancelAddress);
|
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 (;;) {
|
for (;;) {
|
||||||
ssize_t n, pos, len;
|
ssize_t n, pos, len;
|
||||||
RESTARTABLE(read((int)src, &buf, sizeof(buf)), n);
|
RESTARTABLE(read((int)src, &buf, sizeof(buf)), n);
|
||||||
|
@ -82,4 +106,5 @@ Java_sun_nio_fs_UnixCopyFile_transfer
|
||||||
len -= n;
|
len -= n;
|
||||||
} while (len > 0);
|
} while (len > 0);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue