smbfile.java

来自「java ftp 操作代码,程序可以直接运行」· Java 代码 · 共 1,707 行 · 第 1/5 页

JAVA
1,707
字号

    // extended file attribute encoding(others same as above)
    static final int ATTR_COMPRESSED       = 0x800;
    static final int ATTR_NORMAL           = 0x080;
    static final int ATTR_TEMPORARY        = 0x100;

    static final int ATTR_GET_MASK = 0x7FFF; /* orig 0x7fff */
    static final int ATTR_SET_MASK = 0x30A7; /* orig 0x0027 */

    static final int DEFAULT_ATTR_EXPIRATION_PERIOD = 5000;

    static final int HASH_DOT     = ".".hashCode();
    static final int HASH_DOT_DOT = "..".hashCode();

    static LogStream log = LogStream.getInstance();
    static long attrExpirationPeriod;

    static {
        try {
            Class.forName( "jcifs.Config" );
        } catch( ClassNotFoundException cnfe ) {
            cnfe.printStackTrace();
        }
        attrExpirationPeriod = Config.getLong( "jcifs.smb.client.attrExpirationPeriod", DEFAULT_ATTR_EXPIRATION_PERIOD );
    }

    /**
     * Returned by {@link #getType()} if the resource this <tt>SmbFile</tt>
     * represents is a regular file or directory.
     */
    public static final int TYPE_FILESYSTEM = 0x01;
    /**
     * Returned by {@link #getType()} if the resource this <tt>SmbFile</tt>
     * represents is a workgroup.
     */
    public static final int TYPE_WORKGROUP = 0x02;
    /**
     * Returned by {@link #getType()} if the resource this <tt>SmbFile</tt>
     * represents is a server.
     */
    public static final int TYPE_SERVER = 0x04;
    /**
     * Returned by {@link #getType()} if the resource this <tt>SmbFile</tt>
     * represents is a share.
     */
    public static final int TYPE_SHARE = 0x08;
    /**
     * Returned by {@link #getType()} if the resource this <tt>SmbFile</tt>
     * represents is a named pipe.
     */
    public static final int TYPE_NAMED_PIPE = 0x10;
    /**
     * Returned by {@link #getType()} if the resource this <tt>SmbFile</tt>
     * represents is a printer.
     */
    public static final int TYPE_PRINTER = 0x20;
    /**
     * Returned by {@link #getType()} if the resource this <tt>SmbFile</tt>
     * represents is a communications device.
     */
    public static final int TYPE_COMM = 0x40;


    private String canon;            // Initially null; set by getUncPath; dir must end with '/'
    private String share;            // Can be null
    private long createTime;
    private long lastModified;
    private int attributes;
    private long attrExpiration;
    private long size;
    private long sizeExpiration;
    private NtlmPasswordAuthentication auth; // Cannot be null
    private boolean isExists;
    private int shareAccess = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
    private SmbComBlankResponse blank_resp = null;
    private DfsReferral dfsReferral = null;  // Only used by getDfsPath()

    SmbTree tree = null;             // Initially null; may be !tree.treeConnected
    String unc;                      // Initially null; set by getUncPath; never ends with '/'
    int fid;                         // Initially 0; set by open()
    int type;
    boolean opened;
    int tree_num;

/** 
 * Constructs an SmbFile representing a resource on an SMB network such as
 * a file or directory. See the description and examples of smb URLs above.
 *
 * @param   url A URL string
 * @throws  MalformedURLException
 *          If the <code>parent</code> and <code>child</code> parameters
 *          do not follow the prescribed syntax
 */

    public SmbFile( String url ) throws MalformedURLException {
        this( new URL( null, url, Handler.SMB_HANDLER ));
    }

/**
 * Constructs an SmbFile representing a resource on an SMB network such
 * as a file or directory. The second parameter is a relative path from
 * the <code>parent SmbFile</code>. See the description above for examples
 * of using the second <code>name</code> parameter.
 *
 * @param   context A base <code>SmbFile</code>
 * @param   name A path string relative to the <code>parent</code> paremeter
 * @throws  MalformedURLException
 *          If the <code>parent</code> and <code>child</code> parameters
 *          do not follow the prescribed syntax
 * @throws  UnknownHostException
 *          If the server or workgroup of the <tt>context</tt> file cannot be determined
 */

    public SmbFile( SmbFile context, String name )
                throws MalformedURLException, UnknownHostException {
        this( context.isWorkgroup0() ?
            new URL( null, "smb://" + name, Handler.SMB_HANDLER ) :
            new URL( context.url, name, Handler.SMB_HANDLER ), context.auth );
    }

/**
 * Constructs an SmbFile representing a resource on an SMB network such
 * as a file or directory. The second parameter is a relative path from
 * the <code>parent</code>. See the description above for examples of
 * using the second <code>chile</code> parameter.
 *
 * @param   context A URL string
 * @param   name A path string relative to the <code>context</code> paremeter
 * @throws  MalformedURLException
 *          If the <code>context</code> and <code>name</code> parameters
 *          do not follow the prescribed syntax
 */

    public SmbFile( String context, String name ) throws MalformedURLException {
        this( new URL( new URL( null, context, Handler.SMB_HANDLER ),
                name, Handler.SMB_HANDLER ));
    }

/**
 * Constructs an SmbFile representing a resource on an SMB network such
 * as a file or directory.
 *
 * @param   url A URL string
 * @param   auth The credentials the client should use for authentication
 * @throws  MalformedURLException
 *          If the <code>url</code> parameter does not follow the prescribed syntax
 */
    public SmbFile( String url, NtlmPasswordAuthentication auth )
                    throws MalformedURLException {
        this( new URL( null, url, Handler.SMB_HANDLER ), auth );
    }
/**
 * Constructs an SmbFile representing a file on an SMB network. The
 * <tt>shareAccess</tt> parameter controls what permissions other
 * clients have when trying to access the same file while this instance
 * is still open. This value is either <tt>FILE_NO_SHARE</tt> or any
 * combination of <tt>FILE_SHARE_READ</tt>, <tt>FILE_SHARE_WRITE</tt>,
 * and <tt>FILE_SHARE_DELETE</tt> logically OR'd together.
 *
 * @param   url A URL string
 * @param   auth The credentials the client should use for authentication
 * @param   shareAccess Specifies what access other clients have while this file is open.
 * @throws  MalformedURLException
 *          If the <code>url</code> parameter does not follow the prescribed syntax
 */
    public SmbFile( String url, NtlmPasswordAuthentication auth, int shareAccess )
                    throws MalformedURLException {
        this( new URL( null, url, Handler.SMB_HANDLER ), auth );
        if ((shareAccess & ~(FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE)) != 0) {
            throw new RuntimeException( "Illegal shareAccess parameter" );
        }
        this.shareAccess = shareAccess;
    }
/**
 * Constructs an SmbFile representing a resource on an SMB network such
 * as a file or directory. The second parameter is a relative path from
 * the <code>context</code>. See the description above for examples of
 * using the second <code>name</code> parameter.
 *
 * @param   context A URL string
 * @param   name A path string relative to the <code>context</code> paremeter
 * @param   auth The credentials the client should use for authentication
 * @throws  MalformedURLException
 *          If the <code>context</code> and <code>name</code> parameters
 *          do not follow the prescribed syntax
 */
    public SmbFile( String context, String name, NtlmPasswordAuthentication auth )
                    throws MalformedURLException {
        this( new URL( new URL( null, context, Handler.SMB_HANDLER ), name, Handler.SMB_HANDLER ), auth );
    }
/**
 * Constructs an SmbFile representing a resource on an SMB network such
 * as a file or directory. The second parameter is a relative path from
 * the <code>context</code>. See the description above for examples of
 * using the second <code>name</code> parameter. The <tt>shareAccess</tt>
 * parameter controls what permissions other clients have when trying
 * to access the same file while this instance is still open. This
 * value is either <tt>FILE_NO_SHARE</tt> or any combination
 * of <tt>FILE_SHARE_READ</tt>, <tt>FILE_SHARE_WRITE</tt>, and
 * <tt>FILE_SHARE_DELETE</tt> logically OR'd together.
 *
 * @param   context A URL string
 * @param   name A path string relative to the <code>context</code> paremeter
 * @param   auth The credentials the client should use for authentication
 * @param   shareAccess Specifies what access other clients have while this file is open.
 * @throws  MalformedURLException
 *          If the <code>context</code> and <code>name</code> parameters
 *          do not follow the prescribed syntax
 */
    public SmbFile( String context, String name, NtlmPasswordAuthentication auth, int shareAccess )
                    throws MalformedURLException {
        this( new URL( new URL( null, context, Handler.SMB_HANDLER ), name, Handler.SMB_HANDLER ), auth );
        if ((shareAccess & ~(FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE)) != 0) {
            throw new RuntimeException( "Illegal shareAccess parameter" );
        }
        this.shareAccess = shareAccess;
    }
/**
 * Constructs an SmbFile representing a resource on an SMB network such
 * as a file or directory. The second parameter is a relative path from
 * the <code>context</code>. See the description above for examples of
 * using the second <code>name</code> parameter. The <tt>shareAccess</tt>
 * parameter controls what permissions other clients have when trying
 * to access the same file while this instance is still open. This
 * value is either <tt>FILE_NO_SHARE</tt> or any combination
 * of <tt>FILE_SHARE_READ</tt>, <tt>FILE_SHARE_WRITE</tt>, and
 * <tt>FILE_SHARE_DELETE</tt> logically OR'd together.
 *
 * @param   context A base <code>SmbFile</code>
 * @param   name A path string relative to the <code>context</code> file path
 * @param   shareAccess Specifies what access other clients have while this file is open.
 * @throws  MalformedURLException
 *          If the <code>context</code> and <code>name</code> parameters
 *          do not follow the prescribed syntax
 */
    public SmbFile( SmbFile context, String name, int shareAccess )
                    throws MalformedURLException, UnknownHostException {
        this( context.isWorkgroup0() ?
            new URL( null, "smb://" + name, Handler.SMB_HANDLER ) :
            new URL( context.url, name, Handler.SMB_HANDLER ), context.auth );
        if ((shareAccess & ~(FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE)) != 0) {
            throw new RuntimeException( "Illegal shareAccess parameter" );
        }
        this.shareAccess = shareAccess;
    }
/**
 * Constructs an SmbFile representing a resource on an SMB network such
 * as a file or directory from a <tt>URL</tt> object.
 *
 * @param   url The URL of the target resource
 */
    public SmbFile( URL url ) {
        this( url, new NtlmPasswordAuthentication( url.getUserInfo() ));
    }
/**
 * Constructs an SmbFile representing a resource on an SMB network such
 * as a file or directory from a <tt>URL</tt> object and an
 * <tt>NtlmPasswordAuthentication</tt> object.
 *
 * @param   url The URL of the target resource
 * @param   auth The credentials the client should use for authentication
 */
    public SmbFile( URL url, NtlmPasswordAuthentication auth ) {
        super( url );
        this.auth = auth == null ? new NtlmPasswordAuthentication( url.getUserInfo() ) : auth;

        getUncPath0();
    }
    SmbFile( SmbFile context, String name, int type,
                int attributes, long createTime, long lastModified, long size )
                throws MalformedURLException, UnknownHostException {
        this( context.isWorkgroup0() ?
            new URL( null, "smb://" + name + "/", Handler.SMB_HANDLER ) :
            new URL( context.url, name + (( attributes & ATTR_DIRECTORY ) > 0 ? "/" : "" )));


        /* why was this removed before? DFS? copyTo? Am I going around in circles? */
        auth = context.auth;


        if( context.share != null ) {
            this.tree = context.tree;
        }
        int last = name.length() - 1;
        if( name.charAt( last ) == '/' ) {
            name = name.substring( 0, last );
        }
        if( context.share == null ) {
            this.unc = "\\";
        } else if( context.unc.equals( "\\" )) {
            this.unc = '\\' + name;
        } else {
            this.unc = context.unc + '\\' + name;
        }
    /* why? am I going around in circles?
     *  this.type = type == TYPE_WORKGROUP ? 0 : type;
     */
        this.type = type;
        this.attributes = attributes;
        this.createTime = createTime;
        this.lastModified = lastModified;
        this.size = size;
        isExists = true;

        attrExpiration = sizeExpiration =
                System.currentTimeMillis() + attrExpirationPeriod;
    }

    private SmbComBlankResponse blank_resp() {
        if( blank_resp == null ) {
            blank_resp = new SmbComBlankResponse();
        }
        return blank_resp;
    }
    void send( ServerMessageBlock request,
                    ServerMessageBlock response ) throws SmbException {
        for( ;; ) {
            connect0();
            if( tree.inDfs ) {
                DfsReferral dr = tree.session.transport.lookupReferral( unc );
                if( dr != null ) {
                    UniAddress addr;
                    SmbTransport trans;

                    try {
                        addr = UniAddress.getByName( dr.server );
                    } catch( UnknownHostException uhe ) {
                        throw new SmbException( dr.server, uhe );
                    }

                    trans = SmbTransport.getSmbTransport( addr, url.getPort() );
                    tree = trans.getSmbSession( auth ).getSmbTree( dr.share, null );
                    unc = dr.nodepath + unc.substring( dr.path.length() );
                    if( request.path.charAt( request.path.length() - 1 ) == '\\' ) {
                        request.path = unc + '\\'; /* preserve trailing slash */
                    } else {
                        request.path = unc;
                    }
                    dfsReferral = dr; /* for getDfsPath */
                }
                request.flags2 |= ServerMessageBlock.FLAGS2_RESOLVE_PATHS_IN_DFS;
            } else {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?