Skip to content

Commit

Permalink
Added license header + small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
hierynomus committed Apr 15, 2024
1 parent c3f92a8 commit 4e02c2f
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright (C)2009 - SSHJ 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.sshj.sftp;

import com.hierynomus.sshj.sftp.RemoteResourceSelector.Result;
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/com/hierynomus/sshj/sftp/RemoteResourceSelector.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,29 @@
/*
* Copyright (C)2009 - SSHJ 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.sshj.sftp;

import net.schmizz.sshj.sftp.RemoteResourceInfo;

public interface RemoteResourceSelector {
public static RemoteResourceSelector ALL = new RemoteResourceSelector() {
@Override
public Result select(RemoteResourceInfo resource) {
return Result.ACCEPT;
}
};

enum Result {
/**
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/net/schmizz/sshj/sftp/RemoteDirectory.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,15 @@ public RemoteDirectory(SFTPEngine requester, String path, byte[] handle) {

public List<RemoteResourceInfo> scan(RemoteResourceFilter filter)
throws IOException {
return scan(filter == null ? null : selectorFrom(filter));
return scan(filter == null ? RemoteResourceSelector.ALL : selectorFrom(filter));
}

public List<RemoteResourceInfo> scan(RemoteResourceSelector selector)
throws IOException {
if (selector == null) {
selector = RemoteResourceSelector.ALL;
}

List<RemoteResourceInfo> remoteResourceInfos = new LinkedList<>();

while (true) {
Expand All @@ -59,8 +63,7 @@ public List<RemoteResourceInfo> scan(RemoteResourceSelector selector)
continue;
}

final RemoteResourceSelector.Result selectionResult = selector == null ?
RemoteResourceSelector.Result.ACCEPT : selector.select(inf);
final RemoteResourceSelector.Result selectionResult = selector.select(inf);
switch (selectionResult) {
case ACCEPT:
remoteResourceInfos.add(inf);
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/net/schmizz/sshj/sftp/SFTPClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,18 @@ public SFTPFileTransfer getFileTransfer() {

public List<RemoteResourceInfo> ls(String path)
throws IOException {
return ls(path, (RemoteResourceSelector) null);
return ls(path, RemoteResourceSelector.ALL);
}

public List<RemoteResourceInfo> ls(String path, RemoteResourceFilter filter)
throws IOException {
return ls(path, filter == null ? null : selectorFrom(filter));
return ls(path, filter == null ? RemoteResourceSelector.ALL : selectorFrom(filter));
}

public List<RemoteResourceInfo> ls(String path, RemoteResourceSelector selector)
throws IOException {
try (RemoteDirectory dir = engine.openDir(path)) {
return dir.scan(selector);
return dir.scan(selector == null ? RemoteResourceSelector.ALL : selector);
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/net/schmizz/sshj/sftp/StatefulSFTPClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public synchronized void cd(String dirname)

public synchronized List<RemoteResourceInfo> ls()
throws IOException {
return ls(cwd, (RemoteResourceSelector) null);
return ls(cwd, RemoteResourceSelector.ALL);
}

public synchronized List<RemoteResourceInfo> ls(RemoteResourceFilter filter)
Expand All @@ -75,19 +75,19 @@ public synchronized String pwd()

public List<RemoteResourceInfo> ls(String path)
throws IOException {
return ls(path, (RemoteResourceSelector) null);
return ls(path, RemoteResourceSelector.ALL);
}

public List<RemoteResourceInfo> ls(String path, RemoteResourceFilter filter)
throws IOException {
return ls(path, filter == null ? null : selectorFrom(filter));
return ls(path, filter == null ? RemoteResourceSelector.ALL : selectorFrom(filter));
}

@Override
public List<RemoteResourceInfo> ls(String path, RemoteResourceSelector selector)
throws IOException {
try (RemoteDirectory dir = getSFTPEngine().openDir(cwdify(path))) {
return dir.scan(selector);
return dir.scan(selector == null ? RemoteResourceSelector.ALL : selector);
}
}

Expand Down

0 comments on commit 4e02c2f

Please sign in to comment.