⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 smbfile.java

📁 实现网上邻居需要的jar库;可以使用库中的接口实现文件共享的功能
💻 JAVA
📖 第 1 页 / 共 5 页
字号:

    // 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 );
        dfs = new Dfs();
    }

    /**
     * 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 boolean isExists;
    private int shareAccess = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
    private SmbComBlankResponse blank_resp = null;
    private DfsReferral dfsReferral = null;  // For getDfsPath() and getServerWithDfs()

    protected static Dfs dfs;

    NtlmPasswordAuthentication auth; // Cannot be null
    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;
            this.dfsReferral = context.dfsReferral;
        }
        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 resolveDfs(ServerMessageBlock request) throws SmbException {
        connect0();

        DfsReferral dr = dfs.resolve(
                    tree.session.transport.tconHostName,
                    tree.share,
                    unc,
                    auth);
        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 );

            if (log.level >= 3)
                log.println( dr );

            dfsReferral = dr;
            /* Nov 12, 2008: liuqiang from rst.ricoh.com claims these two

⌨️ 快捷键说明

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