[jcifs] File Locking Questions

Sai Pullabhotla sai.pullabhotla at jmethods.com
Mon Jan 10 10:55:46 MST 2011

I've been facing a weird issue with File Locking and hoping some one
could provide feedback/resolution. Below is the summary:

Our application reads from and writes to files on network shares on
various operating systems such as Windows, Linux, AIX and OS/400. When
we have to write a file, we need to ensure that we acquire an
exclusive lock on the file. When we are reading a file, we need to
ensure we acquire a shared read lock on the file. We have been doing
this using the following code snippets:

//Acquire Shared Lock

		SmbFile f = new SmbFile(context, name, SmbFile.FILE_SHARE_READ);
		InputStream in = f.getInputStream();

//Acquire Exclusive Lock

		SmbFile f = new SmbFile(context, name, SmbFile.FILE_NO_SHARE);
		OutputStream out = new SmbFileOutputStream(f, append);

Firstly, I would like to know if this is the correct way to do what we
want to do.

Now, this exclusive lock thing when writing seems to be working as
expected with Windows Shared Folders. When trying the same with Linux
Shared Folder, we are running into issues. Linux (Samba) is returning
us a valid handle on the File Open request even though another process
has already an exclusive lock. The other process is a Java test
program running on the target Linux, which acquires an exclusive lock
on the File/Channel. We are able to write the data using the handle
with no errors. However, the data is never saved to the target file.
No error is returned on File closing either.

However, if we explicitly call the flush() method on the OutputStream
(before closing the stream), we get an SmbException with an NT Status
Code of 0x00000C54, which according to some Microsoft's documentation
is a File Lock Conflict. In this case, the target file is losing any
existing data and becoming a zero byte file.

Any feedback is greatly appreciated.

Sai Pullabhotla

More information about the jCIFS mailing list