All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jcifs.smb.SmbComOpenAndX Maven / Gradle / Ivy

/* jcifs smb client library in Java
 * Copyright (C) 2000  "Michael B. Allen" 
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package jcifs.smb;


import java.util.Date;

import jcifs.Configuration;
import jcifs.SmbConstants;
import jcifs.util.Hexdump;


class SmbComOpenAndX extends AndXServerMessageBlock {

    // flags (not the same as flags constructor argument)
    static final int FLAGS_RETURN_ADDITIONAL_INFO = 0x01;
    static final int FLAGS_REQUEST_OPLOCK = 0x02;
    static final int FLAGS_REQUEST_BATCH_OPLOCK = 0x04;

    // Access Mode Encoding for desiredAccess
    static final int SHARING_COMPATIBILITY = 0x00;
    static final int SHARING_DENY_READ_WRITE_EXECUTE = 0x10;
    static final int SHARING_DENY_WRITE = 0x20;
    static final int SHARING_DENY_READ_EXECUTE = 0x30;
    static final int SHARING_DENY_NONE = 0x40;

    static final int DO_NOT_CACHE = 0x1000; // bit 12
    static final int WRITE_THROUGH = 0x4000; // bit 14

    static final int OPEN_FN_CREATE = 0x10;
    static final int OPEN_FN_FAIL_IF_EXISTS = 0x00;
    static final int OPEN_FN_OPEN = 0x01;
    static final int OPEN_FN_TRUNC = 0x02;

    int tflags, desiredAccess, searchAttributes, fileAttributes, creationTime, openFunction, allocationSize;


    // flags is NOT the same as flags member

    SmbComOpenAndX ( Configuration config, String fileName, int access, int flags, ServerMessageBlock andx ) {
        super(config, andx);
        this.path = fileName;
        this.command = SMB_COM_OPEN_ANDX;

        this.desiredAccess = access & 0x3;
        if ( this.desiredAccess == 0x3 ) {
            this.desiredAccess = 0x2; /* Mmm, I thought 0x03 was RDWR */
        }
        this.desiredAccess |= SHARING_DENY_NONE;
        this.desiredAccess &= ~0x1; // Win98 doesn't like GENERIC_READ ?! -- get Access Denied.

        // searchAttributes
        this.searchAttributes = SmbConstants.ATTR_DIRECTORY | SmbConstants.ATTR_HIDDEN | SmbConstants.ATTR_SYSTEM;

        // fileAttributes
        this.fileAttributes = 0;

        // openFunction
        if ( ( flags & SmbFile.O_TRUNC ) == SmbFile.O_TRUNC ) {
            // truncate the file
            if ( ( flags & SmbFile.O_CREAT ) == SmbFile.O_CREAT ) {
                // create it if necessary
                this.openFunction = OPEN_FN_TRUNC | OPEN_FN_CREATE;
            }
            else {
                this.openFunction = OPEN_FN_TRUNC;
            }
        }
        else {
            // don't truncate the file
            if ( ( flags & SmbFile.O_CREAT ) == SmbFile.O_CREAT ) {
                // create it if necessary
                if ( ( flags & SmbFile.O_EXCL ) == SmbFile.O_EXCL ) {
                    // fail if already exists
                    this.openFunction = OPEN_FN_CREATE | OPEN_FN_FAIL_IF_EXISTS;
                }
                else {
                    this.openFunction = OPEN_FN_CREATE | OPEN_FN_OPEN;
                }
            }
            else {
                this.openFunction = OPEN_FN_OPEN;
            }
        }
    }


    @Override
    int getBatchLimit ( Configuration cfg, byte cmd ) {
        return cmd == SMB_COM_READ_ANDX ? cfg.getBatchLimit("OpenAndX.ReadAndX") : 0;
    }


    @Override
    int writeParameterWordsWireFormat ( byte[] dst, int dstIndex ) {
        int start = dstIndex;

        SMBUtil.writeInt2(this.tflags, dst, dstIndex);
        dstIndex += 2;
        SMBUtil.writeInt2(this.desiredAccess, dst, dstIndex);
        dstIndex += 2;
        SMBUtil.writeInt2(this.searchAttributes, dst, dstIndex);
        dstIndex += 2;
        SMBUtil.writeInt2(this.fileAttributes, dst, dstIndex);
        dstIndex += 2;
        this.creationTime = 0;
        SMBUtil.writeInt4(this.creationTime, dst, dstIndex);
        dstIndex += 4;
        SMBUtil.writeInt2(this.openFunction, dst, dstIndex);
        dstIndex += 2;
        SMBUtil.writeInt4(this.allocationSize, dst, dstIndex);
        dstIndex += 4;
        for ( int i = 0; i < 8; i++ ) {
            dst[ dstIndex++ ] = 0x00;
        }

        return dstIndex - start;
    }


    @Override
    int writeBytesWireFormat ( byte[] dst, int dstIndex ) {
        int start = dstIndex;

        if ( this.useUnicode ) {
            dst[ dstIndex++ ] = (byte) '\0';
        }
        dstIndex += writeString(this.path, dst, dstIndex);

        return dstIndex - start;
    }


    @Override
    int readParameterWordsWireFormat ( byte[] buffer, int bufferIndex ) {
        return 0;
    }


    @Override
    int readBytesWireFormat ( byte[] buffer, int bufferIndex ) {
        return 0;
    }


    @Override
    public String toString () {
        return new String(
            "SmbComOpenAndX[" + super.toString() + ",flags=0x" + Hexdump.toHexString(this.tflags, 2) + ",desiredAccess=0x"
                    + Hexdump.toHexString(this.desiredAccess, 4) + ",searchAttributes=0x" + Hexdump.toHexString(this.searchAttributes, 4)
                    + ",fileAttributes=0x" + Hexdump.toHexString(this.fileAttributes, 4) + ",creationTime=" + new Date(this.creationTime)
                    + ",openFunction=0x" + Hexdump.toHexString(this.openFunction, 2) + ",allocationSize=" + this.allocationSize + ",fileName="
                    + this.path + "]");
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy