0N/A/*
2027N/A * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A */
0N/A
0N/A/* @test
1879N/A * @bug 4429043 4493595 6332756 6709457
1879N/A * @summary The FileChannel file locking
1879N/A */
1879N/A
1879N/Aimport java.io.*;
1879N/Aimport java.nio.channels.*;
1879N/Aimport static java.nio.file.StandardOpenOption.*;
1879N/A
1879N/A/**
1879N/A * Testing FileChannel's lock method.
1879N/A */
1879N/A
1879N/Apublic class Lock {
1879N/A
2073N/A public static void main(String[] args) throws Exception {
2073N/A if (args.length > 0) {
2073N/A if(args[0].equals("1")) {
2073N/A MadWriter mw = new MadWriter(args[1], false);
2073N/A } else {
2073N/A MadWriter mw = new MadWriter(args[1], true);
1879N/A }
0N/A return;
0N/A }
0N/A File blah = File.createTempFile("blah", null);
0N/A blah.deleteOnExit();
0N/A RandomAccessFile raf = new RandomAccessFile(blah, "rw");
0N/A raf.write(1);
0N/A raf.close();
0N/A test1(blah, "1");
0N/A test1(blah, "2");
0N/A test2(blah, true);
0N/A test2(blah, false);
0N/A test3(blah);
0N/A test4(blah);
0N/A blah.delete();
0N/A }
0N/A
0N/A private static void test2(File blah, boolean b) throws Exception {
0N/A RandomAccessFile raf = new RandomAccessFile(blah, "rw");
0N/A FileChannel channel = raf.getChannel();
0N/A FileLock lock;
0N/A if (b)
0N/A lock = channel.lock();
0N/A else
0N/A lock = channel.tryLock();
0N/A lock.release();
0N/A channel.close();
0N/A }
0N/A
0N/A static void test1(File blah, String str) throws Exception {
0N/A
0N/A // Grab the lock
0N/A RandomAccessFile fis = new RandomAccessFile(blah, "rw");
0N/A FileChannel fc = fis.getChannel();
0N/A FileLock lock = null;
0N/A
0N/A if (str.equals("1")) {
0N/A lock = fc.lock(0, 10, false);
0N/A if (lock == null)
0N/A throw new RuntimeException("Lock should not return null");
0N/A try {
0N/A FileLock lock2 = fc.lock(5, 10, false);
0N/A throw new RuntimeException("Overlapping locks allowed");
0N/A } catch (OverlappingFileLockException e) {
0N/A // Correct result
0N/A }
0N/A }
0N/A
0N/A // Exec the tamperer
0N/A String command = System.getProperty("java.home") +
0N/A File.separator + "bin" + File.separator + "java";
0N/A String testClasses = System.getProperty("test.classes");
0N/A if (testClasses != null)
0N/A command += " -cp " + testClasses;
0N/A command += " Lock " + str + " " + blah;
0N/A Process p = Runtime.getRuntime().exec(command);
0N/A
0N/A BufferedReader in = new BufferedReader
0N/A (new InputStreamReader(p.getInputStream()));
0N/A
0N/A String s;
0N/A int count = 0;
0N/A while ((s = in.readLine()) != null) {
0N/A if (!s.equals("good")) {
0N/A if (File.separatorChar == '/') {
0N/A // Fails on windows over NFS...
0N/A throw new RuntimeException("Failed: "+s);
0N/A }
0N/A }
0N/A count++;
2031N/A }
0N/A
0N/A if (count == 0) {
0N/A in = new BufferedReader(new InputStreamReader(p.getErrorStream()));
0N/A while ((s = in.readLine()) != null) {
0N/A System.err.println("Error output: " + s);
0N/A }
0N/A throw new RuntimeException("Failed, no output");
0N/A }
0N/A
0N/A // Clean up
0N/A if (lock != null) {
0N/A /* Check multiple releases */
0N/A lock.release();
1797N/A lock.release();
0N/A }
0N/A fc.close();
0N/A fis.close();
0N/A }
0N/A
0N/A // The overlap check for file locks should be JVM-wide
0N/A private static void test3(File blah) throws Exception {
0N/A FileChannel fc1 = new RandomAccessFile(blah, "rw").getChannel();
0N/A FileChannel fc2 = new RandomAccessFile(blah, "rw").getChannel();
0N/A
0N/A // lock via one channel, and then attempt to lock the same file
1879N/A // using a second channel
1879N/A FileLock fl1 = fc1.lock();
try {
fc2.tryLock();
throw new RuntimeException("Overlapping locks allowed");
} catch (OverlappingFileLockException x) {
}
try {
fc2.lock();
throw new RuntimeException("Overlapping locks allowed");
} catch (OverlappingFileLockException x) {
}
// release lock and the attempt to lock with the second channel
// should succeed.
fl1.release();
FileLock fl2 = fc2.lock();
try {
fc1.lock();
throw new RuntimeException("Overlapping locks allowed");
} catch (OverlappingFileLockException x) {
}
fc1.close();
fc2.close();
}
/**
* Test file locking when file is opened for append
*/
static void test4(File blah) throws Exception {
try (FileChannel fc = new FileOutputStream(blah, true).getChannel()) {
fc.tryLock().release();
fc.tryLock(0L, 1L, false).release();
fc.lock().release();
fc.lock(0L, 1L, false).release();
}
try (FileChannel fc = FileChannel.open(blah.toPath(), APPEND)) {
fc.tryLock().release();
fc.tryLock(0L, 1L, false).release();
fc.lock().release();
fc.lock(0L, 1L, false).release();
}
}
}
class MadWriter {
public MadWriter(String s, boolean b) throws Exception {
File f = new File(s);
RandomAccessFile fos = new RandomAccessFile(f, "rw");
FileChannel fc = fos.getChannel();
if (fc.tryLock(10, 10, false) == null) {
System.out.println("bad: Failed to grab adjacent lock");
}
FileLock lock = fc.tryLock(0, 10, false);
if (lock == null) {
if (b)
System.out.println("bad");
else
System.out.println("good");
} else {
if (b)
System.out.println("good");
else
System.out.println("bad");
}
fc.close();
fos.close();
}
}