[jcifs] Acquiring exclusive lock on a file seems to not be working anymore

Moritz Bechler bechler at agno3.eu
Tue Aug 9 10:03:47 UTC 2016


Hi,

>
> has anyone noticed that the SmbFile.FILE_NO_SHARE option which is
> supposed to grant an exclusive lock on a file, is not working anymore?
> I wrote this small example program, which I run under windows 7 machine.
> The program basically starts 45 threads which try to append the text
> “test\n” in the same file at the same time, incrementing a counter if
> a IOException “File used by another process” occurs.
>
>
> At the end, it is expected to have the file text.txt containing N rows
> with the word “test”, and the number 45 – N printed in the console
> (the number of threads that got “File used by another process” exception).
> However, the result is different on each execution and never correct –
> the number of rows containing “test” in the test.txt file plus the
> number printed in the console is always smaller than 45, which
> indicates that locking is not working at all.
>
Yep, there is a bug - but it's not the locking that is broken, it's
SmbFileOutputStream's append mode. It does use
Trans2QueryFSInformationResponse to retrieve the current file length as
it calls SmbFile.length before opening it (which does not follow the
sharing semantics and probably has no consistency guarantees
whatsoever). Therefor your file pointer will be set to a stale position
and you do not actually append but overwrite already present data. Nice.

( Patch, which may or may not apply cleanly:
https://github.com/AgNO3/jcifs-ng/commit/022842a1685293335e918a2a1d5416cf39bccdd9
)

regards

Moritz
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.samba.org/pipermail/jcifs/attachments/20160809/8186aa0f/attachment.html>


More information about the jCIFS mailing list