Skip to content

Address FileChannel.lock runtime exception #6721

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion api/src/org/labkey/api/util/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.OverlappingFileLockException;
import java.nio.channels.ReadableByteChannel;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
Expand Down Expand Up @@ -1078,6 +1079,17 @@ public static void copyFile(File src, File dst) throws IOException
}
}

private static FileLock acquireLock(FileChannel out) throws IOException
{
try
{
return out.lock();
}
catch (OverlappingFileLockException e)
{
throw new IOException(e);
}
}

// FileUtil.copyFile() does not use transferTo() or sync()
public static void copyFile(ReadableByteChannel in, long expected, File dst) throws IOException
Expand All @@ -1092,7 +1104,7 @@ public static void copyFile(ReadableByteChannel in, long expected, File dst) thr

try (FileOutputStream os = new FileOutputStream(dst);
FileChannel out = os.getChannel();
FileLock lockOut = out.lock())
FileLock lockOut = acquireLock(out))
{
do
{
Expand Down