From 537b70c7f0e115ddd9182a2fa06a93f1a5fbc7b2 Mon Sep 17 00:00:00 2001 From: Alberto Scotto Date: Thu, 15 Apr 2021 11:55:26 +0200 Subject: [PATCH] Add DiskShare#mkdirs --- .../com/hierynomus/smbj/common/SmbPath.java | 2 +- .../com/hierynomus/smbj/share/DiskShare.java | 32 +++++++++++++++++-- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/hierynomus/smbj/common/SmbPath.java b/src/main/java/com/hierynomus/smbj/common/SmbPath.java index 45c76f0d..1e52dcec 100644 --- a/src/main/java/com/hierynomus/smbj/common/SmbPath.java +++ b/src/main/java/com/hierynomus/smbj/common/SmbPath.java @@ -37,7 +37,7 @@ public SmbPath(String hostname, String shareName, String path) { this.path = rewritePath(path); } - private static String rewritePath(String path) { + public static String rewritePath(String path) { if (Strings.isNotBlank(path)) { String replaced = path.replace('/', '\\'); if (replaced.charAt(0) == '\\') { diff --git a/src/main/java/com/hierynomus/smbj/share/DiskShare.java b/src/main/java/com/hierynomus/smbj/share/DiskShare.java index 01648982..53b7dd2b 100644 --- a/src/main/java/com/hierynomus/smbj/share/DiskShare.java +++ b/src/main/java/com/hierynomus/smbj/share/DiskShare.java @@ -35,6 +35,7 @@ import com.hierynomus.smbj.paths.PathResolver; import com.hierynomus.smbj.session.Session; +import java.nio.file.Path; import java.util.EnumSet; import java.util.List; import java.util.Set; @@ -45,6 +46,7 @@ import static com.hierynomus.msfscc.FileAttributes.FILE_ATTRIBUTE_NORMAL; import static com.hierynomus.mssmb2.SMB2CreateDisposition.FILE_CREATE; import static com.hierynomus.mssmb2.SMB2CreateDisposition.FILE_OPEN; +import static com.hierynomus.mssmb2.SMB2CreateDisposition.FILE_OPEN_IF; import static com.hierynomus.mssmb2.SMB2CreateOptions.FILE_DIRECTORY_FILE; import static com.hierynomus.mssmb2.SMB2CreateOptions.FILE_NON_DIRECTORY_FILE; import static com.hierynomus.mssmb2.SMB2ShareAccess.*; @@ -268,15 +270,41 @@ public List list(String path, C } /** - * Create a directory in the given path. + * Creates a directory in the given path, failing if it already exists. */ public void mkdir(String path) throws SMBApiException { + mkdir0(path, FILE_CREATE); + } + + /** + * Creates a directory by creating all nonexistent parent directories first. + * If the directory already exists, then this is a no-op. + */ + public void mkdirs(String path) throws SMBApiException { + String[] split = SmbPath.rewritePath(path).split("\\\\"); + if (split.length == 0) { + return; + } + String parent = split[0]; + mkdirQuietly(parent); + for (int i = 1; i < split.length; i++) { + String current = parent + "\\" + split[i]; + mkdirQuietly(current); + parent = current; + } + } + + private void mkdirQuietly(String path) { + mkdir0(path, FILE_OPEN_IF); + } + + private void mkdir0(String path, SMB2CreateDisposition createDisposition) { Directory fileHandle = openDirectory( path, of(FILE_LIST_DIRECTORY, FILE_ADD_SUBDIRECTORY), of(FILE_ATTRIBUTE_DIRECTORY), ALL, - FILE_CREATE, + createDisposition, of(FILE_DIRECTORY_FILE)); fileHandle.close(); }