[jcifs] Acquiring exclusive lock on a file seems to not be working anymore
bechler at agno3.eu
Tue Aug 9 10:03:47 UTC 2016
> 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:
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the jCIFS