smbfile.java
来自「java ftp 操作代码,程序可以直接运行」· Java 代码 · 共 1,707 行 · 第 1/5 页
JAVA
1,707 行
unc = "\\";
} else if( i == o ) {
share = canon.substring( 1, i );
unc = "\\";
} else {
share = canon.substring( 1, i );
unc = canon.substring( i, out[o] == '/' ? o : o + 1 );
unc = unc.replace( '/', '\\' );
}
} else {
share = null;
unc = "\\";
}
}
return unc;
}
/**
* Retuns the Windows UNC style path with backslashs intead of forward slashes.
*
* @return The UNC path.
*/
public String getUncPath() {
getUncPath0();
if( share == null ) {
return "\\\\" + url.getHost();
}
return "\\\\" + url.getHost() + canon.replace( '/', '\\' );
}
/**
* Returns the full URL of this SMB resource with '.' and '..' components
* factored out. An <code>SmbFile</code> constructed with the result of
* this method will result in an <code>SmbFile</code> that is equal to
* the original.
*
* @return The canonicalized URL of this SMB resource.
*/
public String getCanonicalPath() {
String str = url.getAuthority();
getUncPath0();
if( str.length() > 0 ) {
return "smb://" + url.getAuthority() + canon;
}
return "smb://";
}
/**
* Retrieves the share associated with this SMB resource. In
* the case of <code>smb://</code>, <code>smb://workgroup/</code>,
* and <code>smb://server/</code> URLs which do not specify a share,
* <code>null</code> will be returned.
*
* @return The share component or <code>null</code> if there is no share
*/
public String getShare() {
return share;
}
/**
* Retrieve the hostname of the server for this SMB resource. If this
* <code>SmbFile</code> references a workgroup, the name of the workgroup
* is returned. If this <code>SmbFile</code> refers to the root of this
* SMB network hierarchy, <code>null</code> is returned.
*
* @return The server or workgroup name or <code>null</code> if this
* <code>SmbFile</code> refers to the root <code>smb://</code> resource.
*/
public String getServer() {
String str = url.getHost();
if( str.length() == 0 ) {
return null;
}
return str;
}
/**
* Returns type of of object this <tt>SmbFile</tt> represents.
* @return <tt>TYPE_FILESYSTEM, TYPE_WORKGROUP, TYPE_SERVER, TYPE_SHARE,
* TYPE_PRINTER, TYPE_NAMED_PIPE</tt>, or <tt>TYPE_COMM</tt>.
*/
public int getType() throws SmbException {
if( type == 0 ) {
if( getUncPath0().length() > 1 ) {
type = TYPE_FILESYSTEM;
} else if( share != null ) {
// treeConnect good enough to test service type
connect0();
if( share.equals( "IPC$" )) {
type = TYPE_NAMED_PIPE;
} else if( tree.service.equals( "LPT1:" )) {
type = TYPE_PRINTER;
} else if( tree.service.equals( "COMM" )) {
type = TYPE_COMM;
} else {
type = TYPE_SHARE;
}
} else if( url.getAuthority().length() == 0 ) {
type = TYPE_WORKGROUP;
} else {
UniAddress addr;
try {
addr = getAddress();
} catch( UnknownHostException uhe ) {
throw new SmbException( url.toString(), uhe );
}
if( addr.getAddress() instanceof NbtAddress ) {
int code = ((NbtAddress)addr.getAddress()).getNameType();
if( code == 0x1d || code == 0x1b ) {
type = TYPE_WORKGROUP;
return type;
}
}
type = TYPE_SERVER;
}
}
return type;
}
boolean isWorkgroup0() throws UnknownHostException {
if( type == TYPE_WORKGROUP || url.getHost().length() == 0 ) {
type = TYPE_WORKGROUP;
return true;
} else {
getUncPath0();
if( share == null ) {
UniAddress addr = getAddress();
if( addr.getAddress() instanceof NbtAddress ) {
int code = ((NbtAddress)addr.getAddress()).getNameType();
if( code == 0x1d || code == 0x1b ) {
type = TYPE_WORKGROUP;
return true;
}
}
type = TYPE_SERVER;
}
}
return false;
}
Info queryPath( String path, int infoLevel ) throws SmbException {
connect0();
if( log.level > 2 )
log.println( "queryPath: " + path );
/* normally we'd check the negotiatedCapabilities for CAP_NT_SMBS
* however I can't seem to get a good last modified time from
* SMB_COM_QUERY_INFORMATION so if NT_SMBs are requested
* by the server than in this case that's what it will get
* regardless of what jcifs.smb.client.useNTSmbs is set
* to(overrides negotiatedCapabilities).
*/
/* We really should do the referral before this in case
* the redirected target has different capabilities. But
* the way we have been doing that is to call exists() which
* calls this method so another technique will be necessary
* to support DFS referral _to_ Win95/98/ME.
*/
if( tree.session.transport.hasCapability( ServerMessageBlock.CAP_NT_SMBS )) {
/*
* Trans2 Query Path Information Request / Response
*/
Trans2QueryPathInformationResponse response =
new Trans2QueryPathInformationResponse( infoLevel );
send( new Trans2QueryPathInformation( path, infoLevel ), response );
return response.info;
} else {
/*
* Query Information Request / Response
*/
SmbComQueryInformationResponse response =
new SmbComQueryInformationResponse(
tree.session.transport.server.serverTimeZone * 1000 * 60L );
send( new SmbComQueryInformation( path ), response );
return response;
}
}
/**
* Tests to see if the SMB resource exists. If the resource refers
* only to a server, this method determines if the server exists on the
* network and is advertising SMB services. If this resource refers to
* a workgroup, this method determines if the workgroup name is valid on
* the local SMB network. If this <code>SmbFile</code> refers to the root
* <code>smb://</code> resource <code>true</code> is always returned. If
* this <code>SmbFile</code> is a traditional file or directory, it will
* be queried for on the specified server as expected.
*
* @return <code>true</code> if the resource exists or is alive or
* <code>false</code> otherwise
*/
public boolean exists() throws SmbException {
if( attrExpiration > System.currentTimeMillis() ) {
return isExists;
}
attributes = ATTR_READONLY | ATTR_DIRECTORY;
createTime = 0L;
lastModified = 0L;
isExists = false;
try {
if( url.getHost().length() == 0 ) {
} else if( share == null ) {
if( getType() == TYPE_WORKGROUP ) {
UniAddress.getByName( url.getHost(), true );
} else {
UniAddress.getByName( url.getHost() ).getHostName();
}
} else if( getUncPath0().length() == 1 ||
share.equalsIgnoreCase( "IPC$" )) {
connect0(); // treeConnect is good enough
} else {
Info info = queryPath( getUncPath0(),
Trans2QueryPathInformationResponse.SMB_QUERY_FILE_BASIC_INFO );
attributes = info.getAttributes();
createTime = info.getCreateTime();
lastModified = info.getLastWriteTime();
}
/* If any of the above fail, isExists will not be set true
*/
isExists = true;
} catch( UnknownHostException uhe ) {
} catch( SmbException se ) {
switch (se.getNtStatus()) {
case NtStatus.NT_STATUS_NO_SUCH_FILE:
case NtStatus.NT_STATUS_OBJECT_NAME_INVALID:
case NtStatus.NT_STATUS_OBJECT_NAME_NOT_FOUND:
case NtStatus.NT_STATUS_OBJECT_PATH_NOT_FOUND:
break;
default:
throw se;
}
}
attrExpiration = System.currentTimeMillis() + attrExpirationPeriod;
return isExists;
}
/**
* Tests to see if the file this <code>SmbFile</code> represents can be
* read. Because any file, directory, or other resource can be read if it
* exists, this method simply calls the <code>exists</code> method.
*
* @return <code>true</code> if the file is read-only
*/
public boolean canRead() throws SmbException {
if( getType() == TYPE_NAMED_PIPE ) { // try opening the pipe for reading?
return true;
}
return exists(); // try opening and catch sharing violation?
}
/**
* Tests to see if the file this <code>SmbFile</code> represents
* exists and is not marked read-only. By default, resources are
* considered to be read-only and therefore for <code>smb://</code>,
* <code>smb://workgroup/</code>, and <code>smb://server/</code> resources
* will be read-only.
*
* @return <code>true</code> if the resource exists is not marked
* read-only
*/
public boolean canWrite() throws SmbException {
if( getType() == TYPE_NAMED_PIPE ) { // try opening the pipe for writing?
return true;
}
return exists() && ( attributes & ATTR_READONLY ) == 0;
}
/**
* Tests to see if the file this <code>SmbFile</code> represents is a directory.
*
* @return <code>true</code> if this <code>SmbFile</code> is a directory
*/
public boolean isDirectory() throws SmbException {
if( getUncPath0().length() == 1 ) {
return true;
}
if (!exists()) return false;
return ( attributes & ATTR_DIRECTORY ) == ATTR_DIRECTORY;
}
/**
* Tests to see if the file this <code>SmbFile</code> represents is not a directory.
*
* @return <code>true</code> if this <code>SmbFile</code> is not a directory
*/
public boolean isFile() throws SmbException {
if( getUncPath0().length() == 1 ) {
return false;
}
exists();
return ( attributes & ATTR_DIRECTORY ) == 0;
}
/**
* Tests to see if the file this SmbFile represents is marked as
* hidden. This method will also return true for shares with names that
* end with '$' such as <code>IPC$</code> or <code>C$</code>.
*
* @return <code>true</code> if the <code>SmbFile</code> is marked as being hidden
*/
public boolean isHidden() throws SmbException {
if( share == null ) {
return false;
} else if( getUncPath0().length() == 1 ) {
if( share.endsWith( "$" )) {
return true;
}
return false;
}
exists();
return ( attributes & ATTR_HIDDEN ) == ATTR_HIDDEN;
}
/**
* If the path of this <code>SmbFile</code> falls within a DFS volume,
* this method will return the referral path to which it maps. Otherwise
* <code>null</code> is returned.
*/
public String getDfsPath() throws SmbException {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?