Java Code Examples for jcifs.smb.SmbFile#exists()

The following examples show how to use jcifs.smb.SmbFile#exists() . 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: SMBOperationHandler.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 6 votes vote down vote up
private boolean delete(SmbFile file, DeleteParameters params) throws IOException {
	if (Thread.currentThread().isInterrupted()) {
		throw new IOException(FileOperationMessages.getString("IOperationHandler.interrupted")); //$NON-NLS-1$
	}
	if (!file.exists()) {
		throw new FileNotFoundException(MessageFormat.format(FileOperationMessages.getString("IOperationHandler.file_not_found"), file.toString())); //$NON-NLS-1$
	}
	if (file.isDirectory()) {
		if (!params.isRecursive()) {
			throw new IOException(MessageFormat.format(FileOperationMessages.getString("IOperationHandler.cannot_remove_directory"), file)); //$NON-NLS-1$
		}
		file = ensureTrailingSlash(file);
	}
	file.delete();
	return true;
}
 
Example 2
Source File: SMBOperationHandler.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 6 votes vote down vote up
private List<Info> list(SmbFile parent, ListParameters params) throws IOException {
	if (Thread.currentThread().isInterrupted()) {
		throw new IOException(FileOperationMessages.getString("IOperationHandler.interrupted")); //$NON-NLS-1$
	}
	if (!parent.exists()) {
		throw new FileNotFoundException(MessageFormat.format(FileOperationMessages.getString("IOperationHandler.file_not_found"), parent.toString())); //$NON-NLS-1$
	}
	if (!parent.isDirectory()) {
		return Arrays.asList(info(parent));
	}
	SmbFile[] children = ensureTrailingSlash(parent).listFiles();
	if (children != null) {
		List<Info> result = new ArrayList<Info>();
		for (SmbFile child: children) {
			result.add(info(child));
			if (params.isRecursive() && child.isDirectory()) {
				result.addAll(list(child, params));
			}
		}
		return result;
	}
	return new ArrayList<Info>(0);
}
 
Example 3
Source File: Samba1FileSystem.java    From iaf with Apache License 2.0 6 votes vote down vote up
@Override
public void deleteFile(SmbFile f) throws FileSystemException {
	try {
		if (!f.exists()) {
			throw new FileSystemException("File ["+f.getName()+"] not found");
		}
		if (f.isFile()) {
			f.delete();
		} else {
			throw new FileSystemException(
					"Trying to remove [" + f.getName() + "] which is a directory instead of a file");
		}
	} catch (SmbException e) {
		throw new FileSystemException(e);
	}
}
 
Example 4
Source File: SmbFileUtils.java    From onetwo with Apache License 2.0 5 votes vote down vote up
public static void mkdirs(SmbFile smbf){
	try {
		SmbFile parent = new SmbFile(smbf.getParent());
		if(!parent.exists())
			parent.mkdirs();
	} catch (Exception e) {
		throw new BaseException("make smb direcotry error: " + smbf.getParent());
	}
}
 
Example 5
Source File: SMBOperationHandler.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static List<SmbFile> resolve(String wildcardURL) throws IOException {
	if (Thread.currentThread().isInterrupted()) {
		throw new IOException(FileOperationMessages.getString("IOperationHandler.interrupted")); //$NON-NLS-1$
	}
	if (!hasWildcards(wildcardURL)) {
		return Arrays.asList(new SmbFile(wildcardURL));
	}

	List<String> parts = getParts(wildcardURL);
	SmbFile base = new SmbFile(parts.get(0));
	if (!base.exists()) {
		return Collections.emptyList();
	}
	List<SmbFile> bases = Arrays.asList(base);
	for (Iterator<String> it = parts.listIterator(1); it.hasNext(); ) {
		String part = it.next();
		boolean hasPathSeparator = part.endsWith(URIUtils.PATH_SEPARATOR);
		if (hasPathSeparator) {
			part = part.substring(0, part.length()-1);
		}
		List<SmbFile> nextBases = new ArrayList<SmbFile>(bases.size());
		for (SmbFile f: bases) {
			nextBases.addAll(expand(f, part, it.hasNext() || hasPathSeparator));
		}
		bases = nextBases;
	}
	return bases;
}
 
Example 6
Source File: SMBOperationHandler.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
private boolean create(SmbFile file, CreateParameters params) throws IOException {
	boolean success = true;
	Boolean isDirectory = params.isDirectory();
	boolean createParents = Boolean.TRUE.equals(params.isMakeParents()); 
	Date lastModified = params.getLastModified();
	if (!file.exists()) {
		boolean createDirectory = Boolean.TRUE.equals(isDirectory);
		if (createDirectory && createParents) {
			file.mkdirs(); // directory and parents
		} else if (createDirectory) {
			file.mkdir(); // directory
		} else { // file
			if (createParents) {
				mkParentDirs(file);
			}
			file.createNewFile();
		}
		if (lastModified != null) {
			file.setLastModified(lastModified.getTime());
		}
	} else {
		if ((isDirectory != null) && (!isDirectory.equals(file.isDirectory()))) {
			throw new IOException(MessageFormat.format(isDirectory ? FileOperationMessages.getString("IOperationHandler.exists_not_directory") : FileOperationMessages.getString("IOperationHandler.exists_not_file"), file)); //$NON-NLS-1$ //$NON-NLS-2$
		}
		if (lastModified != null) {
			file.setLastModified(lastModified.getTime());
		} else {
			file.setLastModified(System.currentTimeMillis());
		}
	}
	return success; 
}
 
Example 7
Source File: SMBOperationHandlerTest.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void testResolve() throws Exception {
	super.testResolve();
	
	CloverURI uri;
	ResolveResult result;
	
	for (SmbFile file : Arrays.asList(new SmbFile("smb://"))) { // hmmm, creates a bit strange "smb:////" URL, but seems to work anyway...
		if (file.exists()) {
			uri = CloverURI.createURI(file.toString() + "*");
			result = manager.resolve(uri);
			System.out.println(uri);
			assertTrue(ExceptionUtils.stackTraceToString(result.getFirstError()), result.success());
			System.out.println(result.getResult());

			uri = CloverURI.createURI(file.toString());
			result = manager.resolve(uri);
			System.out.println(uri);
			assertTrue(result.success());
			assertEquals(1, result.totalCount());
			System.out.println(result.getResult());
		}
	}
	
	// CLO-4062:
	String input = "smb://honza:test*@virt-support/mysmbshare";
	uri = CloverURI.createURI(input);
	result = manager.resolve(uri);
	assertEquals(1, result.getResult().size());
	assertEquals(URI.create(input), result.getResult().get(0).toURI());
}
 
Example 8
Source File: Samba1FileSystem.java    From iaf with Apache License 2.0 5 votes vote down vote up
@Override
public boolean exists(SmbFile f) throws FileSystemException {
	try {
		return f.exists();
	} catch (SmbException e) {
		throw new FileSystemException(e);
	}
}
 
Example 9
Source File: SmbDistributor.java    From nextreports-server with Apache License 2.0 5 votes vote down vote up
public void test(Destination destination) throws DistributionException {
	SmbDestination smbDestination = (SmbDestination) destination;
	try {
		SmbFile smbFile = toSmbFile("", smbDestination);
		smbFile.exists();
	} catch (Exception e) {
		throw new DistributionException(e);
	}
}
 
Example 10
Source File: SMBOperationHandler.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 4 votes vote down vote up
private void copyInternal(SmbFile source, SmbFile target, CopyParameters params) throws IOException {
	if (Thread.currentThread().isInterrupted()) {
		throw new IOException(FileOperationMessages.getString("IOperationHandler.interrupted")); //$NON-NLS-1$
	}
	source = toCannonicalFile(source);
	target = toCannonicalFile(target);
	if (source.equals(target)) {
		throw new SameFileException(source, target);
	}
	boolean makeParents = Boolean.TRUE.equals(params.isMakeParents());
	if (source.isDirectory()) {
		if (!params.isRecursive()) {
			throw new IOException(MessageFormat.format(FileOperationMessages.getString("IOperationHandler.cannot_copy_directory"), source)); //$NON-NLS-1$
		}
		if (target.exists() && !target.isDirectory()) {
			throw new IOException(MessageFormat.format(FileOperationMessages.getString("IOperationHandler.cannot_overwrite_not_a_directory"), source, target)); //$NON-NLS-1$
		}
		source = ensureTrailingSlash(source);
		target = ensureTrailingSlash(target);
		if (!target.exists()) {
			try {
				if (makeParents) {
					target.mkdirs();
				} else {
					target.mkdir();
				}
			} catch (SmbException e) {
				throw new IOException(MessageFormat.format(FileOperationMessages.getString("IOperationHandler.create_failed"), target), e); //$NON-NLS-1$
			}
			source.copyTo(target);
		} else {
			for (SmbFile child: source.listFiles()) {
				copyInternal(child, new SmbFile(target, child.getName()), params);
			}
		}
	} else {
		if (target.exists()) {
			if (params.isNoOverwrite()) {
				return;
			}
			if (params.isUpdate() && (source.lastModified() <= target.lastModified())) {
				return;
			}
		} else if (makeParents) {
			mkParentDirs(target);
		}

		source.copyTo(target);
	}
}
 
Example 11
Source File: SMBOperationHandler.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 4 votes vote down vote up
private static void mkParentDirs(SmbFile target) throws MalformedURLException, SmbException {
	SmbFile parent = new SmbFile(target.getParent());
	if (!parent.exists()) {
		parent.mkdirs();
	}
}
 
Example 12
Source File: SMBOperationHandler.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 4 votes vote down vote up
private static List<SmbFile> expand(SmbFile base, String part, boolean directory) throws IOException {
	if (base == null) {
		throw new NullPointerException("base"); //$NON-NLS-1$
	}
	if (!base.isDirectory()) {
		throw new IllegalArgumentException(MessageFormat.format(FileOperationMessages.getString("IOperationHandler.not_a_directory"), base)); //$NON-NLS-1$
	}
	
	base = ensureTrailingSlash(base);
	part = URIUtils.urlDecode(part);
	if (hasWildcards(part)) {
		SmbFile[] children;
		try {
			// Let's use cool SmbFile.listFiles(String) method which resolves exactly our (DOS) wildcards...
			children = base.listFiles(part);
		} catch (SmbException e) {
			// ... but it uses not so cool way of letting know that there are no files matching the pattern 
			if (e.getNtStatus() == NtStatus.NT_STATUS_NO_SUCH_FILE) {
				return Collections.emptyList(); // maybe this could be done on any SmbException
			} else {
				throw e;
			}
		}
		if (!directory) {
			return Arrays.asList(children);
		} else {
			List<SmbFile> dirsOnly = new ArrayList<SmbFile>(children.length);
			for (SmbFile child : children) {
				if (child.isDirectory()) {
					dirsOnly.add(child);
				}
			}
			return dirsOnly;
		}
	} else {
		SmbFile file = new SmbFile(base, part);
		if (file.exists()) {
			return Arrays.asList(file);
		} else {
			return new ArrayList<SmbFile>(0);
		}
	}
}
 
Example 13
Source File: SMBOperationHandler.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 4 votes vote down vote up
private Info info(SmbFile file) throws SmbException {
	if (file.exists()) {
		return new SMBFileInfo(file);
	}
	return null;
}