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

Add DiskShare#mkdirs #627

Open
wants to merge 1 commit 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
2 changes: 1 addition & 1 deletion src/main/java/com/hierynomus/smbj/common/SmbPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) == '\\') {
Expand Down
32 changes: 30 additions & 2 deletions src/main/java/com/hierynomus/smbj/share/DiskShare.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.*;
Expand Down Expand Up @@ -268,15 +270,41 @@ public <I extends FileDirectoryQueryableInformation> List<I> 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();
}
Expand Down