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

Replace dCache's 'DirectoryStream' with Java's 'DirectoryStream' #7262

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
import diskCacheV111.util.NamespaceHandlerAware;
import diskCacheV111.util.PnfsHandler;
import java.lang.Thread.UncaughtExceptionHandler;
import java.nio.file.DirectoryStream;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
Expand Down Expand Up @@ -103,7 +104,6 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
import org.dcache.util.BoundedExecutor;
import org.dcache.util.SignalAware;
import org.dcache.util.list.DirectoryEntry;
import org.dcache.util.list.DirectoryStream;
import org.dcache.util.list.ListDirectoryHandler;
import org.dcache.vehicles.FileAttributes;
import org.dcache.vehicles.PnfsResolveSymlinksMessage;
Expand Down Expand Up @@ -406,7 +406,7 @@ protected void checkForRequestCancellation() throws InterruptedException {
}
}

protected DirectoryStream getDirectoryListing(FsPath path)
protected DirectoryStream<DirectoryEntry> getDirectoryListing(FsPath path)
throws CacheException, InterruptedException {
LOGGER.trace("getDirectoryListing {}, path {}, calling list ...", ruid, path);
return listHandler.list(subject, restriction, path, null,
Expand All @@ -416,14 +416,16 @@ protected DirectoryStream getDirectoryListing(FsPath path)
protected void expandDepthFirst(Long id, PID pid, FsPath path, FileAttributes dirAttributes)
throws BulkServiceException, CacheException, InterruptedException {
checkForRequestCancellation();


DirListTask task = new DirListTask() {
@Override
public void doList() throws Throwable {
try {
DirectoryStream stream = getDirectoryListing(path);
DirectoryStream<DirectoryEntry> stream = getDirectoryListing(path);
for (DirectoryEntry entry : stream) {
LOGGER.trace("expandDepthFirst {}, directory {}, entry {}", ruid, path,
entry.getName());
entry.getName());
FsPath childPath = path.child(entry.getName());
FileAttributes childAttributes = entry.getFileAttributes();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import io.swagger.annotations.Authorization;
import java.nio.file.DirectoryStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -60,7 +61,6 @@
import org.dcache.restful.util.RequestUser;
import org.dcache.restful.util.namespace.NamespaceUtils;
import org.dcache.util.list.DirectoryEntry;
import org.dcache.util.list.DirectoryStream;
import org.dcache.util.list.ListDirectoryHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -165,7 +165,7 @@ public JsonFileAttributes getFileAttributes(@ApiParam("Path of file or directory
try {
List<JsonFileAttributes> children = new ArrayList<>();

DirectoryStream stream = listDirectoryHandler.listVirtualDirectory(
DirectoryStream<DirectoryEntry> stream = listDirectoryHandler.listVirtualDirectory(
HttpServletRequests.roleAwareSubject(request),
HttpServletRequests.roleAwareRestriction(request),
path,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.DirectoryStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -76,7 +77,6 @@
import org.dcache.restful.util.RequestUser;
import org.dcache.restful.util.namespace.NamespaceUtils;
import org.dcache.util.list.DirectoryEntry;
import org.dcache.util.list.DirectoryStream;
import org.dcache.util.list.ListDirectoryHandler;
import org.dcache.vehicles.FileAttributes;
import org.json.JSONArray;
Expand Down Expand Up @@ -212,7 +212,7 @@ public JsonFileAttributes getFileAttributes(@ApiParam("Path of file or directory

List<JsonFileAttributes> children = new ArrayList<>();

DirectoryStream stream = listDirectoryHandler.list(
DirectoryStream<DirectoryEntry> stream = listDirectoryHandler.list(
HttpServletRequests.roleAwareSubject(request),
HttpServletRequests.roleAwareRestriction(request),
path,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
import static org.dcache.util.NetworkUtils.isInetAddress;

import com.google.common.base.CharMatcher;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Predicate;
import com.google.common.base.Throwables;
Expand Down Expand Up @@ -141,13 +140,15 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
import dmg.cells.services.login.LoginBrokerInfo;
import dmg.cells.services.login.LoginBrokerSource;
import eu.emi.security.authn.x509.X509Credential;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ProtocolFamily;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.nio.file.DirectoryStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand All @@ -161,7 +162,6 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
import java.util.Optional;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
Expand Down Expand Up @@ -201,7 +201,6 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
import org.dcache.srm.CopyCallbacks;
import org.dcache.srm.FileMetaData;
import org.dcache.srm.RemoveFileCallback;
import org.dcache.srm.SRMAbortedException;
import org.dcache.srm.SRMAuthorizationException;
import org.dcache.srm.SRMDuplicationException;
import org.dcache.srm.SRMExceedAllocationException;
Expand Down Expand Up @@ -229,7 +228,6 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
import org.dcache.util.list.DirectoryEntry;
import org.dcache.util.list.DirectoryListPrinter;
import org.dcache.util.list.DirectoryListSource;
import org.dcache.util.list.DirectoryStream;
import org.dcache.util.list.NullListPrinter;
import org.dcache.vehicles.FileAttributes;
import org.dcache.vehicles.pool.CacheEntryInfoMessage;
Expand Down Expand Up @@ -1432,7 +1430,7 @@ private void listSubdirectoriesRecursivelyForDelete(Subject subject,
List<FsPath> result)
throws SRMException {
List<DirectoryEntry> children = new ArrayList<>();
try (DirectoryStream list = _listSource.list(subject, restriction, dir, null,
try (DirectoryStream<DirectoryEntry> list = _listSource.list(subject, restriction, dir, null,
Range.<Integer>all(), attributesRequiredForRmdir)) {
for (DirectoryEntry child : list) {
FileAttributes childAttributes = child.getFileAttributes();
Expand Down Expand Up @@ -1460,6 +1458,9 @@ private void listSubdirectoriesRecursivelyForDelete(Subject subject,
throw new SRMInternalErrorException("Name space timeout", e);
} catch (CacheException e) {
throw new SRMException(dir + " (" + e.getMessage() + ")");
} catch (IOException e) {
// Should not be thrown, since Stream does not throw an IOException on close().
throw new RuntimeException("Unexpected Exception thrown.", e);
}

// Result list uses post-order so directories will be deleted bottom-up.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@
import dmg.util.command.CommandLine;
import dmg.util.command.HelpFormat;
import java.io.CharArrayWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Serializable;
import java.nio.file.DirectoryStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -90,7 +92,6 @@
import dmg.util.PagedCommandResult;
import org.dcache.util.Version;
import org.dcache.util.list.DirectoryEntry;
import org.dcache.util.list.DirectoryStream;
import org.dcache.util.list.ListDirectoryHandler;
import org.dcache.vehicles.FileAttributes;
import org.dcache.vehicles.PnfsGetFileAttributes;
Expand Down Expand Up @@ -967,7 +968,7 @@ private int completePath(String buffer, int cursor, List<CharSequence> candidate
String dir = buffer.length() == 1 ? "/" : buffer.substring(0, endIndex);
String file = buffer.substring(endIndex + 1);

try (DirectoryStream stream = list(dir, file + "*")) {
try (DirectoryStream<DirectoryEntry> stream = list(dir, file + "*")) {
for (DirectoryEntry entry : stream) {
if (entry.getFileAttributes().getFileType() == FileType.DIR) {
candidates.add(entry.getName() + "/");
Expand All @@ -980,6 +981,9 @@ private int completePath(String buffer, int cursor, List<CharSequence> candidate
} catch (CacheException e) {
LOGGER.info("Completion failed: {}", e.toString());
return -1;
} catch (IOException e) {
// Should not be thrown, since Stream does not throw an IOException on close().
throw new RuntimeException("Unexpected Exception thrown.", e);
}
return endIndex + 1;
}
Expand Down Expand Up @@ -1017,7 +1021,7 @@ private int completeSendLocationsCommand(String buffer, int cursor,
* Utility method to initiate a directory listing returning entries matching the given glob
* string.
*/
private DirectoryStream list(String dir, String pattern)
private DirectoryStream<DirectoryEntry> list(String dir, String pattern)
throws InterruptedException, CacheException {
return _list.list(Subjects.ROOT, Restrictions.none(), FsPath.create(dir),
new Glob(pattern), Range.all(), EnumSet.of(FileAttribute.TYPE));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import diskCacheV111.vehicles.PnfsRemoveExtendedAttributesMessage;
import diskCacheV111.vehicles.PnfsRemoveLabelsMessage;
import diskCacheV111.vehicles.PnfsWriteExtendedAttributesMessage;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.util.Collection;
import java.util.List;
import java.util.Set;
Expand All @@ -36,7 +38,6 @@
import org.dcache.util.ChecksumType;
import org.dcache.util.Glob;
import org.dcache.util.list.DirectoryEntry;
import org.dcache.util.list.DirectoryStream;
import org.dcache.util.list.ListDirectoryHandler;
import org.dcache.vehicles.FileAttributes;

Expand Down Expand Up @@ -190,13 +191,16 @@ public FileAttributes setFileAttributes(Subject subject, PnfsId id,
public void list(Subject subject, String path, Glob glob,
Range<Integer> range, Set<FileAttribute> attrs, ListHandler handler)
throws CacheException {
try (DirectoryStream stream = _handler.list(subject, Restrictions.none(),
try (DirectoryStream<DirectoryEntry> stream = _handler.list(subject, Restrictions.none(),
FsPath.create(path), glob, range, attrs)) {
for (DirectoryEntry entry : stream) {
handler.addEntry(entry.getName(), entry.getFileAttributes());
}
} catch (InterruptedException e) {
throw new TimeoutCacheException(e.getMessage());
} catch (IOException e) {
// Should not be thrown, since Stream does not throw an IOException on close().
throw new RuntimeException("Unexpected Exception thrown.", e);
}
}

Expand All @@ -205,12 +209,15 @@ public void listVirtualDirectory(Subject subject, String path,
Range<Integer> range, Set<FileAttribute> attrs, ListHandler handler)
throws CacheException
{
try (DirectoryStream stream = _handler.listVirtualDirectory(subject, Restrictions.none(), FsPath.create(path), range, attrs)) {
try (DirectoryStream<DirectoryEntry> stream = _handler.listVirtualDirectory(subject, Restrictions.none(), FsPath.create(path), range, attrs)) {
for (DirectoryEntry entry : stream) {
handler.addEntry(entry.getName(), entry.getFileAttributes());
}
} catch (InterruptedException e) {
throw new TimeoutCacheException(e.getMessage());
} catch (IOException e) {
// Should not be thrown, since Stream does not throw an IOException on close().
throw new RuntimeException("Unexpected Exception thrown.", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.common.collect.Range;
import diskCacheV111.util.CacheException;
import diskCacheV111.util.FsPath;
import java.nio.file.DirectoryStream;
import java.util.Set;
import javax.security.auth.Subject;
import org.dcache.auth.attributes.Restriction;
Expand Down Expand Up @@ -32,7 +33,7 @@ public interface DirectoryListSource {
* @param range The range of entries to return; may be null
* @return A DirectoryStream of the entries in the directory
*/
DirectoryStream list(Subject subject, Restriction restriction, FsPath path,
DirectoryStream<DirectoryEntry> list(Subject subject, Restriction restriction, FsPath path,
Glob pattern, Range<Integer> range)
throws InterruptedException, CacheException;

Expand All @@ -57,7 +58,7 @@ DirectoryStream list(Subject subject, Restriction restriction, FsPath path,
* @param attrs The file attributes to query for each entry
* @return A DirectoryStream of the entries in the directory
*/
DirectoryStream list(Subject subject, Restriction restriction, FsPath path,
DirectoryStream<DirectoryEntry> list(Subject subject, Restriction restriction, FsPath path,
Glob pattern, Range<Integer> range,
Set<FileAttribute> attrs)
throws InterruptedException, CacheException;
Expand All @@ -74,7 +75,7 @@ DirectoryStream list(Subject subject, Restriction restriction, FsPath path,
* @return A DirectoryStream of the entries in the directory

*/
DirectoryStream listVirtualDirectory(Subject subject, Restriction restriction, FsPath path,
DirectoryStream<DirectoryEntry> listVirtualDirectory(Subject subject, Restriction restriction, FsPath path,
Range<Integer> range,
Set<FileAttribute> attrs)
throws InterruptedException, CacheException;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import diskCacheV111.util.FsPath;
import diskCacheV111.util.PnfsHandler;
import dmg.cells.nucleus.CellMessageReceiver;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.util.EnumSet;
import java.util.Iterator;
import java.util.Map;
Expand Down Expand Up @@ -62,7 +64,7 @@ public ListDirectoryHandler(PnfsHandler pnfs) {
* underlying PnfsHandler instance.
*/
@Override
public DirectoryStream
public DirectoryStream<DirectoryEntry>
list(Subject subject, Restriction restriction, FsPath path, Glob pattern, Range<Integer> range)
throws InterruptedException, CacheException {
return list(subject, restriction, path, pattern, range,
Expand All @@ -82,7 +84,7 @@ public ListDirectoryHandler(PnfsHandler pnfs) {
* underlying PnfsHandler instance.
*/
@Override
public DirectoryStream
public DirectoryStream<DirectoryEntry>
list(Subject subject, Restriction restriction, FsPath path, Glob pattern,
Range<Integer> range, Set<FileAttribute> attributes)
throws InterruptedException, CacheException {
Expand Down Expand Up @@ -124,7 +126,7 @@ public ListDirectoryHandler(PnfsHandler pnfs) {
*/

@Override
public DirectoryStream
public DirectoryStream<DirectoryEntry>
listVirtualDirectory(Subject subject, Restriction restriction, FsPath path,
Range<Integer> range, Set<FileAttribute> attributes)
throws InterruptedException, CacheException
Expand Down Expand Up @@ -177,14 +179,17 @@ public int printDirectory(Subject subject, Restriction restriction,
printer.getRequiredAttributes();
FileAttributes dirAttr =
_pnfs.getFileAttributes(path.toString(), required);
try (DirectoryStream stream = list(subject, restriction, path, glob, range, required)) {
try (DirectoryStream<DirectoryEntry> stream = list(subject, restriction, path, glob, range, required)) {
int total = 0;
for (DirectoryEntry entry : stream) {
printer.print(path, dirAttr, entry);
total++;
}
printer.close();
return total;
} catch (IOException e) {
// Should not be thrown, since Stream does not throw an IOException on close().
throw new RuntimeException("Unexpected Exception thrown.", e);
}
}

Expand All @@ -210,13 +215,12 @@ public void messageArrived(PnfsListDirectoryMessage reply) {
}

/**
* Implementation of DirectoryStream, translating PnfsListDirectoryMessage replies to a stream
* of DirectoryEntries.
* Implementation of {@link java.nio.file.DirectoryStream}, translating PnfsListDirectoryMessage
* replies to a stream of DirectoryEntries.
* <p>
* The stream acts as its own iterator, and multiple iterators are not supported.
*/
public class Stream
implements DirectoryStream, Iterator<DirectoryEntry> {
private class Stream implements DirectoryStream<DirectoryEntry>, Iterator<DirectoryEntry> {

private final BlockingQueue<PnfsListDirectoryMessage> _queue =
new LinkedBlockingQueue<>();
Expand Down