Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8193594

Unable to obtain file lock on a file shared on a network.(Mac OS)

XMLWordPrintable

    • x86_64
    • os_x

      FULL PRODUCT VERSION :
      ALIPLR2508:~ user$ java -version
      Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF16
      ??java version "1.8.0_151"
      Java(TM) SE Runtime Environment (build 1.8.0_151-b12)
      Java HotSpot(TM) 64-Bit Server VM (build 25.151-b12, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      macOS High Sierra
      version 10.13.1

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      MacBook Pro (2016 model)
      Processor 2GHz intel core i5
      Memory 8 GB 1867 MHz LPDDR3
      Graphics Intel Irish Graphics 540 1536 MB

      A DESCRIPTION OF THE PROBLEM :
      I am trying to access and obtain lock on a file on a shared location in my network.
      But the tryLock method on FileChannel fails to obtain lock giving error message "Operation not supported".

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile and run the below program in any MAC environment and try to access a shared file on network.
      Note: Please edit the "filePath" variable to a valid file path on shared network.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      We should see the following output:
      Lock object obtained
      Lock released successfully
      ACTUAL -
      java.io.IOException: Operation not supported
      at sun.nio.ch.FileDispatcherImpl.lock0(Native Method)
      at sun.nio.ch.FileDispatcherImpl.lock(FileDispatcherImpl.java:90)
      at sun.nio.ch.FileChannelImpl.tryLock(FileChannelImpl.java:1115)
      at com.file.lock.FileLock.main(FileLock.java:24)
      Operation not supported
      Failed to obtain lock

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------

      import java.io.File;
      import java.io.FileNotFoundException;
      import java.io.IOException;
      import java.io.RandomAccessFile;
      import java.nio.channels.FileChannel;
      import java.util.Scanner;

      public class FileLock {

      private static RandomAccessFile raf = null;
      private static java.nio.channels.FileLock lock = null;
      private static FileChannel channel = null;
      private static File file = null;

      public static void main(String[] args) {

      // path for mac which fails to lock
      String filePath1 = "/Volumes/accion/Wonderful Life Demo V5.msd";
      String filePath = "\\\\ALIPL0343\\Users\\AL1398\\MMData\\MM Scheduling\\Examples\\Wonderful Life Demo V5.msd";
      file = new File(filePath);
      try {
      raf = new RandomAccessFile(file, file.canWrite() ? "rw" : "r");
      channel = raf.getChannel();
      // This method fails on MAC and works fine on windows
      lock = channel.tryLock(0L, Long.MAX_VALUE, file.canWrite() ? false : true);
      } catch (FileNotFoundException e1) {
      e1.printStackTrace();
      System.out.println(e1.getMessage());
      } catch (IOException e) {
      e.printStackTrace();
      System.out.println(e.getMessage());
      }

      if (null == lock) {
      System.out.println("Failed to obtain lock");
      } else {
      try {
      Scanner scan = new Scanner(file);
      System.out.println("Lock object obtained");
      while (scan.hasNextLine()) {
      System.out.println(scan.nextLine());
      }
      scan.close();
      } catch (FileNotFoundException e) {
      e.printStackTrace();
      } finally {
      try {
      lock.release();
      channel.close();
      raf.close();
      System.out.println("Lock released successfully");
      } catch (IOException e) {
      e.printStackTrace();
      }
      }
      }
      }

      }
      ---------- END SOURCE ----------

            bpb Brian Burkhalter
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: