Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added List files #754

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public abstract class FileDirectoryQueryableInformation implements FileInformati
private final String fileName;
private long nextOffset;
private long fileIndex;

private String fullPath=null;

FileDirectoryQueryableInformation(long nextOffset, long fileIndex, String fileName) {
this.nextOffset = nextOffset;
this.fileIndex = fileIndex;
Expand All @@ -36,5 +37,12 @@ public long getFileIndex() {

public String getFileName() {
return fileName;
}
}
public void setFullPath(String fullPath) {
this.fullPath = fullPath;
}

public String getFullPath() {
return fullPath;
}
}
50 changes: 50 additions & 0 deletions src/main/java/com/hierynomus/smbj/share/DiskShare.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@
import com.hierynomus.smbj.paths.PathResolveException;
import com.hierynomus.smbj.paths.PathResolver;
import com.hierynomus.smbj.session.Session;
import com.hierynomus.smbj.utils.SmbFileAttributeUtils;

import java.util.EnumSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.hierynomus.msdtyp.AccessMask.*;
import static com.hierynomus.mserref.NtStatus.*;
Expand All @@ -52,6 +55,8 @@
import static java.util.EnumSet.noneOf;
import static java.util.EnumSet.of;

import java.util.ArrayList;

public class DiskShare extends Share {
private final PathResolver resolver;

Expand Down Expand Up @@ -239,7 +244,52 @@ public List<FileIdBothDirectoryInformation> list(String path) throws SMBApiExcep
public List<FileIdBothDirectoryInformation> list(String path, String searchPattern) throws SMBApiException {
return list(path, FileIdBothDirectoryInformation.class, searchPattern, null);
}

public List<FileIdBothDirectoryInformation> listAllFiles(String path) throws SMBApiException {
return listAllFiles(path, null);
}

public List<FileIdBothDirectoryInformation> listAllFiles(String path, String searchPattern) throws SMBApiException {
List<FileIdBothDirectoryInformation> filesInformation=new ArrayList<FileIdBothDirectoryInformation>();
return listAllFiles(path, FileIdBothDirectoryInformation.class, searchPattern, filesInformation);
}

public <I extends FileDirectoryQueryableInformation> List<FileIdBothDirectoryInformation> listAllFiles(String path, Class<FileIdBothDirectoryInformation> informationClass, String searchPattern, List<FileIdBothDirectoryInformation> filesInformation) {
EnumSet<AccessMask> accessMask=of(FILE_LIST_DIRECTORY, FILE_READ_ATTRIBUTES, FILE_READ_EA);
Directory directory = openDirectory(path, accessMask, null, ALL, FILE_OPEN, null);
try {
Iterator<FileIdBothDirectoryInformation> directoryIterator = directory.iterator(informationClass, searchPattern);
while (directoryIterator.hasNext()) {
FileIdBothDirectoryInformation directoryInformation = directoryIterator.next();
if (".".equalsIgnoreCase(directoryInformation.getFileName())
|| "..".equalsIgnoreCase(directoryInformation.getFileName())) {
continue;
}
if (SmbFileAttributeUtils.isDirectory(directoryInformation.getFileAttributes())) {
listAllFiles(path + "/" + directoryInformation.getFileName(), informationClass, searchPattern,
filesInformation);
} else {
filesInformation.addAll(directory.list(informationClass, searchPattern).stream()
// Exclude Linux . and .. directories
.filter(entry -> !entry.getFileName().equals("."))
.filter(entry -> !entry.getFileName().equals(".."))
.filter(entry -> !SmbFileAttributeUtils.isDirectory(entry.getFileAttributes()))
.map(info -> mapFullFilePath(path, info))
.collect(Collectors.toList()));
}
}
} finally {
if (directory != null) {
directory.closeSilently();
}
}
return filesInformation;
}

public FileIdBothDirectoryInformation mapFullFilePath(String path, FileIdBothDirectoryInformation info) {
info.setFullPath(path + "/" + info.getFileName());
return info;
}
/**
* Equivalent to calling {@link #list(String, Class, String, EnumSet<AccessMask>) list(path, informationClass, null, null)}.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (C)2016 - SMBJ Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hierynomus.smbj.utils;

/**
* This utility class provides utilities for file attributes.
*
*/
public class SmbFileAttributeUtils {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you maybe submit this as a separate PR to SmbFiles this is really useful and I missing such a good way to check if its a file or directory!


private static final long FILE_ATTRIBUTE_DIRECTORY = 0x10L;
private static final long FILE_ATTRIBUTE_READONLY = 0x1L;
private static final long FILE_ATTRIBUTE_HIDDEN = 0x2L;
private static final long FILE_ATTRIBUTE_ARCHIVE = 0x20L;
private static final long FILE_ATTRIBUTE_SYSTEM = 0x4L;

public static boolean isDirectory(final long attributes) {
return (attributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY;
}

public static boolean isArchive(final long attributes) {
return (attributes & FILE_ATTRIBUTE_ARCHIVE) == FILE_ATTRIBUTE_ARCHIVE;
}

public static boolean isHidden(final long attributes) {
return (attributes & FILE_ATTRIBUTE_HIDDEN) == FILE_ATTRIBUTE_HIDDEN;
}

public static boolean isReadOnly(final long attributes) {
return (attributes & FILE_ATTRIBUTE_READONLY) == FILE_ATTRIBUTE_READONLY;
}

public static boolean isSystem(final long attributes) {
return (attributes & FILE_ATTRIBUTE_SYSTEM) == FILE_ATTRIBUTE_SYSTEM;
}
}