[jcifs] Problem locking using jcifs SmbFileOutputStream

Allen, Michael B (RSCH) Michael_B_Allen at ml.com
Tue Jan 14 11:38:25 EST 2003


CIFS provides for specifying a "share access". Internally the jCIFS library also provides for
utilizing it. However for the sake of simplicity we do not explose this attribute in the public API.
You would have to modify the jCIFS source to permit the share access flag to be specified
externally.

To do this you need to familiarize yourself with the SmbComNTCreateAndX constructor which
follows:

    SmbComNTCreateAndX( String name, int flags, ServerMessageBlock andx ) {
        super( andx );
        this.name = name;
        command = SMB_COM_NT_CREATE_ANDX;

        // desiredAccess
        desiredAccess = ( flags >>> 16 ) & 0xFFFF;
        desiredAccess |= FILE_READ_EA | FILE_READ_ATTRIBUTES;

        // extFileAttributes
        extFileAttributes = ATTR_NORMAL;

        // shareAccess
        shareAccess = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;

        // createDisposition
        if(( flags & SmbFile.O_TRUNC ) == SmbFile.O_TRUNC ) {
        ....

You will see there are quite a few options to the SmbComNTCreateAndX command many of
which are hardcoded. In particular the share access is hard coded to permit reading, writing, and
deleting the file even if we have it open. To "lock" the file one might specify a shareAccess of
FILE_NO_SHARE or perhaps FILE_SHARE_READ would be more polite. Note that this is not
a real "lock" but it will achive the effect you seek.

To do this you could modify this construtor to accept a share access flag or boolean perhaps
that toggles these flags. Then modify the SmbFile.open() function to use this modified
constructor. You might add an SmbFile constructor too so that the functionality is exposed in
the public API.

It might be wise to just hardcode shareAddress = FILE_SHARE_READ at first to see if it does
what you want.

Note that this will not work with Windows98 or below. Only Samba, NT, 2K, XP, etc.

If you do this, let us know how it goes. Perhaps I will add the feature to the main codebase. But
I think you're actually the fist person to ask for this functionality.

Mike 

> -----Original Message-----
> From:	Mario Rodriguez [SMTP:marior at caclearing.com.ar]
> Sent:	Monday, January 13, 2003 7:10 PM
> To:	jcifs at lists.samba.org
> Subject:	[jcifs] Problem locking using jcifs SmbFileOutputStream
> 
> Hi!
> 
> I'm trying to write a long file using SmbFileOutputStream and I need to 
> lock this file on the Windows NT resource while I'm writing to this file 
> to avoid the use of this file.
> 
> How can I do that?
> 
> This is an example code that I'm using.
> 
> SmbFile pipe=null;
> 
>        try {
>           pipe = new SmbFile ("smb://user:passwd@HOST/uploads/thefile.txt");
>        }
>        catch (MalformedURLException ex)
>        {}
>        catch (UnknownHostException ex)
>        {}
> 
>      
>       try {
>            byte buffer[] = new byte[150000];
>            OutputStream out = null; 
>            out = new SmbFileOutputStream(pipe,true);
>            for (int i = 0 ; i<20;i++){
>              out.write(buffer);
>              out.flush();
>            }
>       }
>       catch (IOException ex) {
>          ex.printStackTrace();
>       }
> 
>    ...
> 
> 
> best regards.
> 
> marior
> 




More information about the jcifs mailing list