From: David ‘Bombe’ Roden Date: Wed, 16 Apr 2008 07:05:31 +0000 (+0000) Subject: implement direct disk access check X-Git-Tag: v0.1.1~150 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=7e564a4ec294adc5d42209834d609556c6011883;p=jFCPlib.git implement direct disk access check git-svn-id: http://trooper/svn/projects/jFCPlib/branch/high-level-client@837 c3eda9e8-030b-0410-8277-bc7414b0a119 --- diff --git a/src/net/pterodactylus/fcp/highlevel/DirectDiskAccessResult.java b/src/net/pterodactylus/fcp/highlevel/DirectDiskAccessResult.java new file mode 100644 index 0000000..fc841d2 --- /dev/null +++ b/src/net/pterodactylus/fcp/highlevel/DirectDiskAccessResult.java @@ -0,0 +1,84 @@ +/* + * jFCPlib-high-level-client - DDAResult.java - + * Copyright © 2008 David Roden + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +package net.pterodactylus.fcp.highlevel; + +/** + * The result of a direct disk access check. + * + * @author David ‘Bombe’ Roden <bombe@freenetproject.org> + * @version $Id$ + */ +public class DirectDiskAccessResult extends HighLevelResult { + + /** Whether reading is allowed. */ + private boolean readAllowed; + + /** Whether writing is allowed. */ + private boolean writeAllowed; + + /** + * Package-private constructor. + */ + DirectDiskAccessResult() { + } + + /** + * Returns whether reading the directory is allowed. + * + * @return true if the client is allowed to read from the + * directory, false otherwise + */ + public boolean isReadAllowed() { + return readAllowed; + } + + /** + * Sets whether reading the directory is allowed. + * + * @param readAllowed + * true if the client is allowed to read from the + * directory, false otherwise + */ + void setReadAllowed(boolean readAllowed) { + this.readAllowed = readAllowed; + } + + /** + * Returns whether writing to the directory is allowed. + * + * @return true if the client is allowed to write to the + * directory, false otherwise + */ + public boolean isWriteAllowed() { + return writeAllowed; + } + + /** + * Sets whether writing to the directory is allowed. + * + * @param writeAllowed + * true if the client is allowed to write to the + * directory, false otherwise + */ + void setWriteAllowed(boolean writeAllowed) { + this.writeAllowed = writeAllowed; + } + +} diff --git a/src/net/pterodactylus/fcp/highlevel/HighLevelClient.java b/src/net/pterodactylus/fcp/highlevel/HighLevelClient.java index 4671245..089c205 100644 --- a/src/net/pterodactylus/fcp/highlevel/HighLevelClient.java +++ b/src/net/pterodactylus/fcp/highlevel/HighLevelClient.java @@ -19,6 +19,10 @@ package net.pterodactylus.fcp.highlevel; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; import java.io.IOException; import java.net.InetAddress; import java.net.URL; @@ -27,6 +31,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; +import java.util.logging.Logger; import net.pterodactylus.fcp.AddPeer; import net.pterodactylus.fcp.AllData; @@ -41,6 +46,7 @@ import net.pterodactylus.fcp.FCPPluginReply; import net.pterodactylus.fcp.FcpConnection; import net.pterodactylus.fcp.FcpListener; import net.pterodactylus.fcp.FcpMessage; +import net.pterodactylus.fcp.FcpUtils; import net.pterodactylus.fcp.FinishedCompression; import net.pterodactylus.fcp.GenerateSSK; import net.pterodactylus.fcp.GetFailed; @@ -68,6 +74,8 @@ import net.pterodactylus.fcp.StartedCompression; import net.pterodactylus.fcp.SubscribedUSKUpdate; import net.pterodactylus.fcp.TestDDAComplete; import net.pterodactylus.fcp.TestDDAReply; +import net.pterodactylus.fcp.TestDDARequest; +import net.pterodactylus.fcp.TestDDAResponse; import net.pterodactylus.fcp.URIGenerated; import net.pterodactylus.fcp.UnknownNodeIdentifier; import net.pterodactylus.fcp.UnknownPeerNoteType; @@ -81,6 +89,9 @@ import net.pterodactylus.fcp.UnknownPeerNoteType; */ public class HighLevelClient { + /** Logger. */ + private static final Logger logger = Logger.getLogger(HighLevelClient.class.getName()); + /** Object for internal synchronization. */ private final Object syncObject = new Object(); @@ -111,6 +122,9 @@ public class HighLevelClient { /** Mapping from request identifier to peer callbacks. */ private Map> peerCallbacks = Collections.synchronizedMap(new HashMap>()); + /** Mapping from directories to DDA callbacks. */ + private Map> directDiskAccessCallbacks = Collections.synchronizedMap(new HashMap>()); + /** * Creates a new high-level client that connects to a node on * localhost. @@ -289,6 +303,28 @@ public class HighLevelClient { return peerCallback; } + /** + * Checks whether direct disk access for the given directory is possible. + * You have to perform this check before you can upload or download anything + * from or the disk directly! + * + * @param directory + * The directory to check + * @param wantRead + * Whether you want to read the given directory + * @param wantWrite + * Whether you want to write to the given directory + * @return A direct disk access callback + * @throws IOException + */ + public HighLevelCallback checkDirectDiskAccess(String directory, boolean wantRead, boolean wantWrite) throws IOException { + TestDDARequest testDDARequest = new TestDDARequest(directory, wantRead, wantWrite); + HighLevelCallback directDiskAccessCallback = new HighLevelCallback(new DirectDiskAccessResult()); + directDiskAccessCallbacks.put(directory, directDiskAccessCallback); + fcpConnection.sendMessage(testDDARequest); + return directDiskAccessCallback; + } + // // PRIVATE METHODS // @@ -312,6 +348,9 @@ public class HighLevelClient { */ private class HighLevelClientFcpListener implements FcpListener { + /** Mapping from directory to written file (for cleanup). */ + private final Map writtenFiles = new HashMap(); + /** * Creates a new FCP listener for {@link HighLevelClient}. */ @@ -358,6 +397,12 @@ public class HighLevelClient { peerEntry.getValue().setDone(); } peerCallbacks.clear(); + /* direct disk access callbacks. */ + for (Entry> directDiskAccessEntry: directDiskAccessCallbacks.entrySet()) { + directDiskAccessEntry.getValue().getIntermediaryResult().setFailed(true); + directDiskAccessEntry.getValue().setDone(); + } + directDiskAccessCallbacks.clear(); } else { HighLevelCallback keyGenerationCallback = keyGenerationCallbacks.remove(identifier); if (keyGenerationCallback != null) { @@ -377,6 +422,78 @@ public class HighLevelClient { peerCallback.setDone(); return; } + HighLevelCallback directDiskAccessCallback = directDiskAccessCallbacks.remove(identifier); + if (directDiskAccessCallback != null) { + directDiskAccessCallback.getIntermediaryResult().setFailed(true); + directDiskAccessCallback.setDone(); + return; + } + } + } + + /** + * Reads the given file and returns the first line of the file. + * + * @param readFilename + * The name of the file to read + * @return The content of the file + */ + private String readContent(String readFilename) { + FileReader fileReader = null; + BufferedReader bufferedFileReader = null; + try { + fileReader = new FileReader(readFilename); + bufferedFileReader = new BufferedReader(fileReader); + String content = bufferedFileReader.readLine(); + return content; + } catch (IOException ioe1) { + /* swallow. */ + } finally { + FcpUtils.close(bufferedFileReader); + FcpUtils.close(fileReader); + } + return null; + } + + /** + * Writes the given content to the given file. + * + * @param directDiskAccessResult + * The DDA result + * @param writeFilename + * The name of the file to write to + * @param writeContent + * The content to write to the file + */ + private void writeContent(DirectDiskAccessResult directDiskAccessResult, String writeFilename, String writeContent) { + if ((writeFilename == null) || (writeContent == null)) { + return; + } + writtenFiles.put(directDiskAccessResult, writeFilename); + FileWriter fileWriter = null; + try { + fileWriter = new FileWriter(writeFilename); + fileWriter.write(writeContent); + } catch (IOException ioe1) { + /* swallow. */ + } finally { + FcpUtils.close(fileWriter); + } + } + + /** + * Cleans up any files that written for the given result. + * + * @param directDiskAccessResult + * The direct disk access result + */ + @SuppressWarnings("synthetic-access") + private void cleanFiles(DirectDiskAccessResult directDiskAccessResult) { + String writeFilename = writtenFiles.remove(directDiskAccessResult); + if (writeFilename != null) { + if (!new File(writeFilename).delete()) { + logger.warning("could not delete " + writeFilename); + } } } @@ -667,14 +784,48 @@ public class HighLevelClient { * @see net.pterodactylus.fcp.FcpListener#receivedTestDDAComplete(net.pterodactylus.fcp.FcpConnection, * net.pterodactylus.fcp.TestDDAComplete) */ + @SuppressWarnings("synthetic-access") public void receivedTestDDAComplete(FcpConnection fcpConnection, TestDDAComplete testDDAComplete) { + if (fcpConnection != HighLevelClient.this.fcpConnection) { + return; + } + String directory = testDDAComplete.getDirectory(); + if (directory == null) { + return; + } + HighLevelCallback directDiskAccessCallback = directDiskAccessCallbacks.remove(directory); + DirectDiskAccessResult directDiskAccessResult = directDiskAccessCallback.getIntermediaryResult(); + cleanFiles(directDiskAccessResult); + directDiskAccessResult.setReadAllowed(testDDAComplete.isReadDirectoryAllowed()); + directDiskAccessResult.setWriteAllowed(testDDAComplete.isWriteDirectoryAllowed()); + directDiskAccessCallback.setDone(); } /** * @see net.pterodactylus.fcp.FcpListener#receivedTestDDAReply(net.pterodactylus.fcp.FcpConnection, * net.pterodactylus.fcp.TestDDAReply) */ + @SuppressWarnings("synthetic-access") public void receivedTestDDAReply(FcpConnection fcpConnection, TestDDAReply testDDAReply) { + if (fcpConnection != HighLevelClient.this.fcpConnection) { + return; + } + String directory = testDDAReply.getDirectory(); + if (directory == null) { + return; + } + DirectDiskAccessResult directDiskAccessResult = directDiskAccessCallbacks.get(directory).getIntermediaryResult(); + String readFilename = testDDAReply.getReadFilename(); + String readContent = readContent(readFilename); + String writeFilename = testDDAReply.getWriteFilename(); + String writeContent = testDDAReply.getContentToWrite(); + writeContent(directDiskAccessResult, writeFilename, writeContent); + TestDDAResponse testDDAResponse = new TestDDAResponse(directory, readContent); + try { + fcpConnection.sendMessage(testDDAResponse); + } catch (IOException e) { + /* swallow. I’m verry unhappy about this. */ + } } /**