com.hierynomus.smbj.share.DiskShare Java Examples

The following examples show how to use com.hierynomus.smbj.share.DiskShare. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Samba2FileSystem.java    From iaf with Apache License 2.0 9 votes vote down vote up
@Override
public void open() throws FileSystemException {
	try {
		AuthenticationContext auth = authenticate();
		client = new SMBClient();
		connection = client.connect(domain);
		if(connection.isConnected()) {
			log.debug("successfully created connection to ["+connection.getRemoteHostname()+"]");
		}
		session = connection.authenticate(auth);
		if(session == null) {
			throw new FileSystemException("Cannot create session for user ["+username+"] on domain ["+domain+"]");
		}
		diskShare = (DiskShare) session.connectShare(shareName);
		if(diskShare == null) {
			throw new FileSystemException("Cannot connect to the share ["+ shareName +"]");
		}
	} catch (IOException e) {
		throw new FileSystemException("Cannot connect to samba server", e);
	}
}
 
Example #2
Source File: SMBJBuildProcessAdapter.java    From teamcity-deployer-plugin with Apache License 2.0 7 votes vote down vote up
private void maybeCreate(@NotNull final DiskShare diskShare, @NotNull final String pathInShare) {
  String existingPrefix = FileUtil.normalizeRelativePath(pathInShare).replace('/', '\\');
  final Stack<String> toCreate = new Stack<>();

  while (existingPrefix.length() > 0 && !diskShare.folderExists(existingPrefix)) {
    final int endIndex = existingPrefix.lastIndexOf('\\');
    if (endIndex > -1) {
      toCreate.push(existingPrefix.substring(endIndex + 1));
      existingPrefix = existingPrefix.substring(0, endIndex);
    } else {
      toCreate.push(existingPrefix);
      existingPrefix = "";
    }
  }

  while (!toCreate.empty()) {
    existingPrefix = (existingPrefix.length() > 0 ? existingPrefix + "\\" : "") + toCreate.pop();
    diskShare.mkdir(existingPrefix);
  }
}
 
Example #3
Source File: SMBJController.java    From DanDanPlayForAndroid with MIT License 6 votes vote down vote up
/**
 * get smb file, just need reed permission
 *
 * @param share    share
 * @param filePath file path not share name
 * @return smb file
 */
private File openFile(DiskShare share, String filePath) {
    return share.openFile(filePath,
            EnumSet.of(AccessMask.FILE_READ_DATA),
            null,
            SMB2ShareAccess.ALL,
            FILE_OPEN,
            null);
}
 
Example #4
Source File: SMB2Utils.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 6 votes vote down vote up
private static OutputStream getOutputStream(final PooledSMB2Connection connection, String path, boolean append) throws IOException {
	try {
		DiskShare share = connection.getShare();
		
		Set<AccessMask> accessMask = append ? EnumSet.of(AccessMask.FILE_APPEND_DATA) : EnumSet.of(AccessMask.FILE_WRITE_DATA);
		SMB2CreateDisposition createDisposition = append ? SMB2CreateDisposition.FILE_OPEN_IF : SMB2CreateDisposition.FILE_OVERWRITE_IF;
		
		final com.hierynomus.smbj.share.File file = openFile(share, path, accessMask, createDisposition);
		OutputStream os = append ? new AppendOutputStream(file) : file.getOutputStream();
		if (append) { // add buffering, AppendOutputStream is slow
			os = new BufferedOutputStream(os, connection.getWriteBufferSize());
		}
		os = new CloseOnceOutputStream(os) {

			@Override
			protected void doClose() throws IOException {
				try {
					super.doClose();
				} finally {
					FileUtils.closeAll(file, connection);
				}
			}
			
		};
		return os;
	} catch (Throwable t) {
		connection.returnToPool();
		throw ExceptionUtils.getIOException(t);
	}
}
 
Example #5
Source File: SMBJ_RPCController.java    From DanDanPlayForAndroid with MIT License 6 votes vote down vote up
@Override
public List<SmbFileInfo> getChildList(String dirName) {
    List<SmbFileInfo> infoList = new ArrayList<>();
    try {
        Directory childDir;
        if (isRootDir()) {
            //in root directory the child is share
            diskShare = (DiskShare) session.connectShare(dirName);
            childDir = openDirectory(diskShare, "");
        } else {
            childDir = openDirectory(diskShare, getPathNotShare(dirName));
        }

        mPath += "\\" + dirName;
        List<FileIdBothDirectoryInformation> childDirInfoList = childDir.list();
        infoList.addAll(getFileInfoList(childDirInfoList, diskShare));

    } catch (Exception e) {
        e.printStackTrace();
    }
    return infoList;
}
 
Example #6
Source File: SMBJ_RPCController.java    From DanDanPlayForAndroid with MIT License 6 votes vote down vote up
/**
 * get smb file, just need reed permission
 *
 * @param share    share
 * @param filePath file path not share name
 * @return smb file
 */
private File openFile(DiskShare share, String filePath) {
    return share.openFile(filePath,
            EnumSet.of(AccessMask.FILE_READ_DATA),
            null,
            SMB2ShareAccess.ALL,
            FILE_OPEN,
            null);
}
 
Example #7
Source File: SMBJ_RPCController.java    From DanDanPlayForAndroid with MIT License 5 votes vote down vote up
/**
 * get smb disk entry, just need reed permission
 *
 * @param share    share
 * @param filePath file or directory path nor share name
 * @return dis entry
 */
private DiskEntry openDiskEntry(DiskShare share, String filePath) {
    return share.open(
            filePath,
            EnumSet.of(AccessMask.GENERIC_READ),
            null,
            SMB2ShareAccess.ALL,
            FILE_OPEN,
            null);
}
 
Example #8
Source File: SMB2Utils.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static InputStream getInputStream(final PooledSMB2Connection connection, String path) throws IOException {
	try {
		DiskShare share = connection.getShare();
		
		Set<AccessMask> accessMask = EnumSet.of(AccessMask.FILE_READ_DATA);
		SMB2CreateDisposition createDisposition = SMB2CreateDisposition.FILE_OPEN;
		
		final com.hierynomus.smbj.share.File file = openFile(share, path, accessMask, createDisposition);
		InputStream is = file.getInputStream();
		is = new CloseOnceInputStream(is) {

			@Override
			protected void doClose() throws IOException {
				try {
					super.doClose();
				} finally {
					FileUtils.closeAll(file, connection);
				}
			}
			
		};
		return is;
	} catch (Throwable t) {
		connection.returnToPool();
		throw ExceptionUtils.getIOException(t);
	}
}
 
Example #9
Source File: PooledSMB2Connection.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
private DiskShare connectShare() throws IOException {
	String shareName = getAuthority().getShare();
	if (StringUtils.isEmpty(shareName) || shareName.equals(URIUtils.CURRENT_DIR_NAME)) {
		throw new IOException("Share name is missing in the URL");
	}
	Share share = session.connectShare(shareName);
	if (share instanceof DiskShare) {
		return (DiskShare) share;
	} else {
		throw new IOException(shareName + " is not a disk share");
	}
}
 
Example #10
Source File: SMBJController.java    From DanDanPlayForAndroid with MIT License 5 votes vote down vote up
@Override
public boolean linkStart(SmbLinkInfo smbLinkInfo, SmbLinkException exception) {
    String rootFolder = smbLinkInfo.getRootFolder();
    if (SmbUtils.isTextEmpty(rootFolder)) {
        exception.addException(SmbType.SMBJ, "Root Folder Must Not Empty");
        return false;
    }

    SmbConfig smbConfig = SmbConfig.builder()
            .withTimeout(180, TimeUnit.SECONDS)
            .withSoTimeout(180, TimeUnit.SECONDS)
            .build();

    try {
        smbClient = new SMBClient(smbConfig);
        connection = smbClient.connect(smbLinkInfo.getIP());
        AuthenticationContext ac = new AuthenticationContext(
                smbLinkInfo.getAccount(), smbLinkInfo.getPassword().toCharArray(), smbLinkInfo.getDomain());
        session = connection.authenticate(ac);

        ROOT_FLAG += smbLinkInfo.getRootFolder();
        mPath = ROOT_FLAG;

        diskShare = (DiskShare) session.connectShare(smbLinkInfo.getRootFolder());
        rootFileList = getFileInfoList(diskShare.list(""), diskShare);

        return true;
    } catch (Exception e) {
        e.printStackTrace();
        exception.addException(SmbType.SMBJ, e.getMessage());
    }
    return false;
}
 
Example #11
Source File: SMBJ_RPCController.java    From DanDanPlayForAndroid with MIT License 5 votes vote down vote up
/**
 * get smb directory, just need read permission
 *
 * @param share    share
 * @param filePath directory path not share name
 * @return smb directory
 */
private Directory openDirectory(DiskShare share, String filePath) {
    return share.openDirectory(
            filePath,
            EnumSet.of(AccessMask.GENERIC_READ),
            null,
            SMB2ShareAccess.ALL,
            SMB2CreateDisposition.FILE_OPEN,
            null);
}
 
Example #12
Source File: SMBJ_RPCController.java    From DanDanPlayForAndroid with MIT License 5 votes vote down vote up
/**
 * traversal directory info list filtering does not use folders and get file types
 *
 * @param dirInfoList directory list
 * @param diskShare   share
 * @return file info list
 */
private List<SmbFileInfo> getFileInfoList(List<FileIdBothDirectoryInformation> dirInfoList, DiskShare diskShare) {
    List<SmbFileInfo> fileInfoList = new ArrayList<>();
    for (FileIdBothDirectoryInformation dirInfo : dirInfoList) {
        //ignore directories beginning with '.', like '.', '..'
        if (dirInfo.getFileName().startsWith("."))
            continue;

        //get file standard info by disk entry because file type unknown
        DiskEntry diskEntry = openDiskEntry(diskShare, getPathNotShare(dirInfo.getFileName()));
        FileStandardInformation standardInformation = diskEntry.getFileInformation(FileStandardInformation.class);
        fileInfoList.add(new SmbFileInfo(dirInfo.getFileName(), standardInformation.isDirectory()));
    }
    return fileInfoList;
}
 
Example #13
Source File: SMBJController.java    From DanDanPlayForAndroid with MIT License 5 votes vote down vote up
/**
 * get smb disk entry, just need reed permission
 *
 * @param share    share
 * @param filePath file or directory path nor share name
 * @return dis entry
 */
private DiskEntry openDiskEntry(DiskShare share, String filePath) {
    return share.open(
            filePath,
            EnumSet.of(AccessMask.GENERIC_READ),
            null,
            SMB2ShareAccess.ALL,
            FILE_OPEN,
            null);
}
 
Example #14
Source File: SMBJController.java    From DanDanPlayForAndroid with MIT License 5 votes vote down vote up
/**
 * get smb directory, just need reed permission
 *
 * @param share    share
 * @param filePath directory path not share name
 * @return smb directory
 */
private Directory openDirectory(DiskShare share, String filePath) {
    return share.openDirectory(
            filePath,
            EnumSet.of(AccessMask.GENERIC_READ),
            null,
            SMB2ShareAccess.ALL,
            SMB2CreateDisposition.FILE_OPEN,
            null);
}
 
Example #15
Source File: SMBJController.java    From DanDanPlayForAndroid with MIT License 5 votes vote down vote up
/**
 * traversal directory info list filtering does not use folders and get file types
 *
 * @param dirInfoList directory list
 * @param diskShare   share
 * @return file info list
 */
private List<SmbFileInfo> getFileInfoList(List<FileIdBothDirectoryInformation> dirInfoList, DiskShare diskShare) {
    List<SmbFileInfo> fileInfoList = new ArrayList<>();
    for (FileIdBothDirectoryInformation dirInfo : dirInfoList) {
        //ignore directories beginning with '.', like '.', '..'
        if (dirInfo.getFileName().startsWith("."))
            continue;

        //get file standard info by disk entry because file type unknown
        DiskEntry diskEntry = openDiskEntry(diskShare, getPathNotShare(dirInfo.getFileName()));
        FileStandardInformation standardInformation = diskEntry.getFileInformation(FileStandardInformation.class);
        fileInfoList.add(new SmbFileInfo(dirInfo.getFileName(), standardInformation.isDirectory()));
    }
    return fileInfoList;
}
 
Example #16
Source File: PooledSMB2Connection.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 4 votes vote down vote up
public DiskShare getShare() {
	return share;
}
 
Example #17
Source File: PooledSMB2Connection.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Used in tests.
 * 
 * @return the domain passed to SMBJ connection
 */
String getDomain() {
	DiskShare share = getShare();
	AuthenticationContext authenticationContext = share.getTreeConnect().getSession().getAuthenticationContext();
	return authenticationContext.getDomain();
}
 
Example #18
Source File: SMB2Utils.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 4 votes vote down vote up
static DiskEntry open(DiskShare share, String path, Set<AccessMask> accessMask, SMB2CreateDisposition createDisposition) throws IOException {
	// use SMB2ShareAccess.ALL to prevent TimeoutException / buffer underflow on concurrent operations
	return share.open(path, accessMask, null, SMB2ShareAccess.ALL, createDisposition, null);
}