com.hierynomus.mssmb2.SMB2CreateDisposition Java Examples

The following examples show how to use com.hierynomus.mssmb2.SMB2CreateDisposition. 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 8 votes vote down vote up
@Override
public OutputStream createFile(String f) throws FileSystemException, IOException {
	Set<AccessMask> accessMask = new HashSet<AccessMask>(EnumSet.of(AccessMask.FILE_ADD_FILE));
	Set<SMB2CreateOptions> createOptions = new HashSet<SMB2CreateOptions>(
			EnumSet.of(SMB2CreateOptions.FILE_NON_DIRECTORY_FILE, SMB2CreateOptions.FILE_WRITE_THROUGH));
	
	final File file = diskShare.openFile(f, accessMask, null, SMB2ShareAccess.ALL,
			SMB2CreateDisposition.FILE_OVERWRITE_IF, createOptions);
	OutputStream out = file.getOutputStream();
	FilterOutputStream fos = new FilterOutputStream(out) {

		boolean isOpen = true;
		@Override
		public void close() throws IOException {
			if(isOpen) {
				super.close();
				isOpen=false;
			}
			file.close();
		}
	};
	return fos;
}
 
Example #2
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 #3
Source File: Samba2FileSystem.java    From iaf with Apache License 2.0 6 votes vote down vote up
@Override
public OutputStream appendFile(String f) throws FileSystemException, IOException {
	final File file = getFile(f, AccessMask.FILE_APPEND_DATA, SMB2CreateDisposition.FILE_OPEN_IF);
	OutputStream out = file.getOutputStream();
	FilterOutputStream fos = new FilterOutputStream(out) {

		boolean isOpen = true;
		@Override
		public void close() throws IOException {
			if(isOpen) {
				super.close();
				isOpen=false;
			}
			file.close();
		}
	};
	return fos;
}
 
Example #4
Source File: Samba2FileSystem.java    From iaf with Apache License 2.0 6 votes vote down vote up
@Override
public InputStream readFile(String filename) throws FileSystemException, IOException {
	final File file = getFile(filename, AccessMask.GENERIC_READ, SMB2CreateDisposition.FILE_OPEN);
	InputStream is = file.getInputStream();
	FilterInputStream fis = new FilterInputStream(is) {

		boolean isOpen = true;
		@Override
		public void close() throws IOException {
			if(isOpen) {
				super.close();
				isOpen=false;
			}
			file.close();
		}
	};
	return fis;
}
 
Example #5
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 #6
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 #7
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 #8
Source File: PrimitiveSMB2OperationHandler.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public boolean createFile(URI target) throws IOException {
   	try (PooledSMB2Connection connection = getConnection(target)) {
   		try (Closeable file = SMB2Utils.openFile(connection.getShare(), getPath(target), EnumSet.of(AccessMask.FILE_ADD_FILE), SMB2CreateDisposition.FILE_OPEN_IF)) {
   		}
   		return true;
   	} catch (Exception ex) {
   		throw ExceptionUtils.getIOException(ex);
   	}
}
 
Example #9
Source File: PrimitiveSMB2OperationHandler.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * @see FileAttributes
 */
@Override
public boolean setLastModified(URI target, Date date) throws IOException {
   	try (PooledSMB2Connection connection = getConnection(target)) {
   		try (DiskEntry file = SMB2Utils.open(connection.getShare(), getPath(target), EnumSet.of(AccessMask.FILE_WRITE_ATTRIBUTES), SMB2CreateDisposition.FILE_OPEN)) {
   			FileTime changeTime = FileTime.fromDate(date);
			FileBasicInformation newMetadata = new FileBasicInformation(DONT_SET, DONT_SET, changeTime, DONT_SET, 0); // 0x00000000 means no change for file attributes 
			file.setFileInformation(newMetadata);
   		}
   		return true;
   	} catch (Exception ex) {
   		throw ExceptionUtils.getIOException(ex);
   	}
}
 
Example #10
Source File: Samba2FileSystem.java    From iaf with Apache License 2.0 5 votes vote down vote up
@Override
public String renameFile(String f, String newName, boolean force) throws FileSystemException {
	try (File file = getFile(f, AccessMask.GENERIC_ALL, SMB2CreateDisposition.FILE_OPEN)) {
		if (exists(newName) && !force) {
			throw new FileSystemException("Cannot rename file. Destination file already exists.");
		}
		file.rename(newName, force);
	}
	return newName;
}
 
Example #11
Source File: Samba2FileSystem.java    From iaf with Apache License 2.0 5 votes vote down vote up
@Override
public String moveFile(String f, String destinationFolder, boolean createFolder) throws FileSystemException {
	try (File file = getFile(f, AccessMask.GENERIC_ALL, SMB2CreateDisposition.FILE_OPEN)) {
		String destination = getDestinationFile(f, destinationFolder, createFolder, "move");
		file.rename(destination, false);
		return destination;
	}
}
 
Example #12
Source File: Samba2FileSystem.java    From iaf with Apache License 2.0 5 votes vote down vote up
@Override
public String copyFile(String f, String destinationFolder, boolean createFolder) throws FileSystemException {
	try (File file = getFile(f, AccessMask.GENERIC_ALL, SMB2CreateDisposition.FILE_OPEN)) {
		String destination = getDestinationFile(f, destinationFolder, createFolder, "copy");
		try (File destinationFile = getFile(f, AccessMask.GENERIC_ALL, SMB2CreateDisposition.FILE_OVERWRITE)) {
			file.remoteCopyTo(destinationFile);
		} catch (TransportException | BufferException e) {
			throw new FileSystemException("cannot copy file ["+f+"] to ["+destinationFolder+"]",e);
		}
		return destination;
	}
}
 
Example #13
Source File: Samba2FileSystem.java    From iaf with Apache License 2.0 5 votes vote down vote up
private File getFile(String filename, AccessMask accessMask, SMB2CreateDisposition createDisposition) {
	Set<SMB2ShareAccess> shareAccess = new HashSet<SMB2ShareAccess>();
	shareAccess.addAll(SMB2ShareAccess.ALL);

	Set<SMB2CreateOptions> createOptions = new HashSet<SMB2CreateOptions>();
	createOptions.add(SMB2CreateOptions.FILE_WRITE_THROUGH);
	
	Set<AccessMask> accessMaskSet = new HashSet<AccessMask>();
	accessMaskSet.add(accessMask);
	File file;

	file = diskShare.openFile(filename, accessMaskSet, null, shareAccess, createDisposition, createOptions);
	return file;
}
 
Example #14
Source File: Samba2FileSystem.java    From iaf with Apache License 2.0 5 votes vote down vote up
private Directory getFolder(String filename, AccessMask accessMask, SMB2CreateDisposition createDisposition) {
	Set<SMB2ShareAccess> shareAccess = new HashSet<SMB2ShareAccess>();
	shareAccess.addAll(SMB2ShareAccess.ALL);

	Set<AccessMask> accessMaskSet = new HashSet<AccessMask>();
	accessMaskSet.add(accessMask);
	
	Directory file;
	file = diskShare.openDirectory(filename, accessMaskSet, null, shareAccess, createDisposition, null);
	return file;
}
 
Example #15
Source File: Samba2FileSystem.java    From iaf with Apache License 2.0 5 votes vote down vote up
@Override
public long getFileSize(String f) throws FileSystemException {
	long size;
	if (isFolder(f)) {
		try (Directory dir = getFolder(f, AccessMask.FILE_READ_ATTRIBUTES, SMB2CreateDisposition.FILE_OPEN)) {
			size = dir.getFileInformation().getStandardInformation().getAllocationSize();
			return size;
		}
	} else {
		try (File file = getFile(f, AccessMask.FILE_READ_ATTRIBUTES, SMB2CreateDisposition.FILE_OPEN)) {
			size = file.getFileInformation().getStandardInformation().getAllocationSize();
			return size;
		}
	}
}
 
Example #16
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);
}