Skip to content
Permalink
Browse files
8265175: (fs) Files.copy(Path,Path,CopyOption...) should use sendfile…
… on Linux

Reviewed-by: alanb
  • Loading branch information
Brian Burkhalter committed Apr 16, 2021
1 parent cee4f1d commit 1c3fd46d7da90d5a5b2a457f2a4ed77a12f8f3a5
Showing 1 changed file 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.
*
* 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
}

1 comment on commit 1c3fd46

@openjdk-notifier
Copy link

@openjdk-notifier openjdk-notifier bot commented on 1c3fd46 Apr 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.