From d1e15435a7fb458c490a3e6b97b21a88ec84ae5c Mon Sep 17 00:00:00 2001 From: rokkakasu <34742810+rokkakasu@users.noreply.github.com> Date: Sun, 19 Mar 2023 19:16:47 -0700 Subject: [PATCH 1/2] Added List files Added List files method to list all the files under the directory recursively using single session. --- .../com/hierynomus/smbj/share/DiskShare.java | 46 ++++++++++++++++- .../smbj/utils/SmbFileAttributeUtils.java | 49 +++++++++++++++++++ 2 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/hierynomus/smbj/utils/SmbFileAttributeUtils.java diff --git a/src/main/java/com/hierynomus/smbj/share/DiskShare.java b/src/main/java/com/hierynomus/smbj/share/DiskShare.java index 01648982..2edf67b3 100644 --- a/src/main/java/com/hierynomus/smbj/share/DiskShare.java +++ b/src/main/java/com/hierynomus/smbj/share/DiskShare.java @@ -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.*; @@ -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; @@ -239,7 +244,46 @@ public List list(String path) throws SMBApiExcep public List list(String path, String searchPattern) throws SMBApiException { return list(path, FileIdBothDirectoryInformation.class, searchPattern, null); } - + + public List listAllFiles(String path) throws SMBApiException { + return listAllFiles(path, null); + } + + public List listAllFiles(String path, String searchPattern) throws SMBApiException { + List filesInformation=new ArrayList(); + return listAllFiles(path, FileIdBothDirectoryInformation.class, searchPattern, filesInformation); + } + + public List listAllFiles(String path, Class informationClass, String searchPattern, List filesInformation) { + EnumSet accessMask=of(FILE_LIST_DIRECTORY, FILE_READ_ATTRIBUTES, FILE_READ_EA); + Directory directory = openDirectory(path, accessMask, null, ALL, FILE_OPEN, null); + try { + Iterator 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())) + .collect(Collectors.toList())); + } + } + } finally { + if (directory != null) { + directory.closeSilently(); + } + } + return filesInformation; + } /** * Equivalent to calling {@link #list(String, Class, String, EnumSet) list(path, informationClass, null, null)}. * diff --git a/src/main/java/com/hierynomus/smbj/utils/SmbFileAttributeUtils.java b/src/main/java/com/hierynomus/smbj/utils/SmbFileAttributeUtils.java new file mode 100644 index 00000000..19a98370 --- /dev/null +++ b/src/main/java/com/hierynomus/smbj/utils/SmbFileAttributeUtils.java @@ -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 { + + 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; + } +} From ebdcdb04a5666c043432a9b9270e8f2b28efd0b9 Mon Sep 17 00:00:00 2001 From: rokkakasu <34742810+rokkakasu@users.noreply.github.com> Date: Sun, 19 Mar 2023 20:43:03 -0700 Subject: [PATCH 2/2] Added Full File Path parameter Added Param for full file path. as its required to identify the full path of the file in the Share --- .../FileDirectoryQueryableInformation.java | 12 ++++++++++-- .../java/com/hierynomus/smbj/share/DiskShare.java | 6 ++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/hierynomus/msfscc/fileinformation/FileDirectoryQueryableInformation.java b/src/main/java/com/hierynomus/msfscc/fileinformation/FileDirectoryQueryableInformation.java index 9dc055b5..934a40d1 100644 --- a/src/main/java/com/hierynomus/msfscc/fileinformation/FileDirectoryQueryableInformation.java +++ b/src/main/java/com/hierynomus/msfscc/fileinformation/FileDirectoryQueryableInformation.java @@ -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; @@ -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; + } } diff --git a/src/main/java/com/hierynomus/smbj/share/DiskShare.java b/src/main/java/com/hierynomus/smbj/share/DiskShare.java index 2edf67b3..831e2c28 100644 --- a/src/main/java/com/hierynomus/smbj/share/DiskShare.java +++ b/src/main/java/com/hierynomus/smbj/share/DiskShare.java @@ -274,6 +274,7 @@ public List !entry.getFileName().equals(".")) .filter(entry -> !entry.getFileName().equals("..")) .filter(entry -> !SmbFileAttributeUtils.isDirectory(entry.getFileAttributes())) + .map(info -> mapFullFilePath(path, info)) .collect(Collectors.toList())); } } @@ -284,6 +285,11 @@ public List) list(path, informationClass, null, null)}. *