jdk/test/java/nio/channels/FileChannel/Lock.java
author alanb
Wed, 01 Dec 2010 13:49:02 +0000
changeset 7515 43202796198e
parent 5970 d4e98bbfb0be
child 7668 d4a77089c587
permissions -rw-r--r--
6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win] Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2001, 2006, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5970
diff changeset
    25
 * @bug 4429043 4493595 6332756 6709457
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary The FileChannel file locking
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.nio.channels.*;
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5970
diff changeset
    31
import static java.nio.file.StandardOpenOption.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Testing FileChannel's lock method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class Lock {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        if (args.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
            if(args[0].equals("1")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
                MadWriter mw = new MadWriter(args[1], false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
                MadWriter mw = new MadWriter(args[1], true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        File blah = File.createTempFile("blah", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        blah.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        RandomAccessFile raf = new RandomAccessFile(blah, "rw");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        raf.write(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        raf.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        test1(blah, "1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        test1(blah, "2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        test2(blah, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        test2(blah, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        test3(blah);
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5970
diff changeset
    58
        test4(blah);
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    59
        blah.delete();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private static void test2(File blah, boolean b) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        RandomAccessFile raf = new RandomAccessFile(blah, "rw");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        FileChannel channel = raf.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        FileLock lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        if (b)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            lock = channel.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            lock = channel.tryLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        lock.release();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        channel.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    static void test1(File blah, String str) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        // Grab the lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        RandomAccessFile fis = new RandomAccessFile(blah, "rw");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        FileChannel fc = fis.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        FileLock lock = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        if (str.equals("1")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            lock = fc.lock(0, 10, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            if (lock == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                throw new RuntimeException("Lock should not return null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                FileLock lock2 = fc.lock(5, 10, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                throw new RuntimeException("Overlapping locks allowed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            } catch (OverlappingFileLockException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                // Correct result
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        // Exec the tamperer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        String command = System.getProperty("java.home") +
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    95
            File.separator + "bin" + File.separator + "java";
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    96
        String testClasses = System.getProperty("test.classes");
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    97
        if (testClasses != null)
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    98
            command += " -cp " + testClasses;
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    99
        command += " Lock " + str + " " + blah;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        Process p = Runtime.getRuntime().exec(command);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        BufferedReader in = new BufferedReader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            (new InputStreamReader(p.getInputStream()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        String s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        while ((s = in.readLine()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            if (!s.equals("good")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                if (File.separatorChar == '/') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    // Fails on windows over NFS...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                    throw new RuntimeException("Failed: "+s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if (count == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            in = new BufferedReader(new InputStreamReader(p.getErrorStream()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            while ((s = in.readLine()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                System.err.println("Error output: " + s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            throw new RuntimeException("Failed, no output");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        // Clean up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (lock != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            /* Check multiple releases */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            lock.release();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            lock.release();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        fc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        fis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    // The overlap check for file locks should be JVM-wide
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    private static void test3(File blah) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        FileChannel fc1 = new RandomAccessFile(blah, "rw").getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        FileChannel fc2 = new RandomAccessFile(blah, "rw").getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        // lock via one channel, and then attempt to lock the same file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        // using a second channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        FileLock fl1 = fc1.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            fc2.tryLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            throw new RuntimeException("Overlapping locks allowed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        } catch (OverlappingFileLockException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            fc2.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            throw new RuntimeException("Overlapping locks allowed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        } catch (OverlappingFileLockException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        // release lock and the attempt to lock with the second channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        // should succeed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        fl1.release();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        FileLock fl2 = fc2.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            fc1.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            throw new RuntimeException("Overlapping locks allowed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        } catch (OverlappingFileLockException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        fc1.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        fc2.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5970
diff changeset
   167
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5970
diff changeset
   168
    /**
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5970
diff changeset
   169
     * Test file locking when file is opened for append
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5970
diff changeset
   170
     */
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5970
diff changeset
   171
    static void test4(File blah) throws Exception {
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5970
diff changeset
   172
        try (FileChannel fc = new FileOutputStream(blah, true).getChannel()) {
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5970
diff changeset
   173
            fc.tryLock().release();
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5970
diff changeset
   174
            fc.tryLock(0L, 1L, false).release();
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5970
diff changeset
   175
            fc.lock().release();
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5970
diff changeset
   176
            fc.lock(0L, 1L, false).release();
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5970
diff changeset
   177
        }
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5970
diff changeset
   178
        try (FileChannel fc = FileChannel.open(blah.toPath(), APPEND)) {
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5970
diff changeset
   179
            fc.tryLock().release();
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5970
diff changeset
   180
            fc.tryLock(0L, 1L, false).release();
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5970
diff changeset
   181
            fc.lock().release();
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5970
diff changeset
   182
            fc.lock(0L, 1L, false).release();
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5970
diff changeset
   183
        }
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5970
diff changeset
   184
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
class MadWriter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    public MadWriter(String s, boolean b) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        File f = new File(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        RandomAccessFile fos = new RandomAccessFile(f, "rw");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        FileChannel fc = fos.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        if (fc.tryLock(10, 10, false) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            System.out.println("bad: Failed to grab adjacent lock");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        FileLock lock = fc.tryLock(0, 10, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        if (lock == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            if (b)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                System.out.println("bad");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                System.out.println("good");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            if (b)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                System.out.println("good");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                System.out.println("bad");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        fc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        fos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
}