예제 #1
0
  /** Does a 'get' command. */
  private void get(final String[] cmd) throws Exception {
    int fd = -1;
    if (cmd.length < 2) {
      throw new Exception("USAGE: get <path> [local-path]");
    }
    String filename = cmd[1];
    if (filename.charAt(0) != '/' && filename.charAt(0) != '\\') {
      filename = cwd + filename;
    }
    String localFilename = null;
    if (cmd.length > 2) {
      localFilename = cmd[2];
    } else {
      localFilename = (new java.io.File(filename)).getName();
    }

    byte[] pathBytes = filename.getBytes(charset);

    System.arraycopy(serverClient.returnInt(pathBytes.length), 0, serverClient.cmdOpen, 0, 4);
    out.write(serverClient.cmdOpen, 0, 9);
    out.write(pathBytes);
    out.flush();

    String response = serverClient.readCommandString();
    if (response.equals(serverClient.OPEN)) {
      fd = serverClient.getCode();
      if (fd < 0) System.out.println("Open returned: " + fd);
    } else {
      throw new Exception("Open returned: " + response + "(" + serverClient.getCode() + ")");
    }

    if (fd >= 0) {
      download(fd, localFilename);
    }
  }
예제 #2
0
  private void close(int fd) throws Exception {
    int pos = -1;
    System.arraycopy(serverClient.returnInt(fd), 0, serverClient.cmdClose, 0, 4);
    out.write(serverClient.cmdClose, 0, 9);
    out.flush();

    String response = serverClient.readCommandString();
    if (response.equals(serverClient.CLOSE)) {
      pos = serverClient.getCode();
      if (pos < 0) System.out.println("Close returned: " + pos);
    } else {
      throw new Exception("Close returned: " + response + "(" + serverClient.getCode() + ")");
    }
  }
예제 #3
0
  private int seek(int fd, int len, int whence) throws Exception {
    int pos = -1;
    System.arraycopy(serverClient.returnInt(fd), 0, serverClient.cmdSeek, 0, 4);
    out.write(serverClient.cmdSeek, 0, 9);
    byte[] data = new byte[4 * 2];
    System.arraycopy(serverClient.returnInt(len), 0, data, 0, 4);
    System.arraycopy(serverClient.returnInt(whence), 0, data, 4, 4);
    out.write(data);
    out.flush();

    String response = serverClient.readCommandString();
    if (response.equals(serverClient.SEEK)) {
      pos = serverClient.getCode();
      if (pos < 0) System.out.println("Seek returned: " + pos);
    } else {
      throw new Exception("Seek returned: " + response + "(" + serverClient.getCode() + ")");
    }
    return pos;
  }
예제 #4
0
  private int read(int fd, byte[] buffer, int offset, int len) throws Exception {
    int resultLen = 0;
    System.arraycopy(serverClient.returnInt(fd), 0, serverClient.cmdRead, 0, 4);
    out.write(serverClient.cmdRead, 0, 9);
    byte[] data = new byte[4];
    System.arraycopy(serverClient.returnInt(len), 0, data, 0, 4);
    out.write(data);
    out.flush();

    String response = serverClient.readCommandString();
    if (response.equals(serverClient.READ)) {
      resultLen = serverClient.getCode();
      if (resultLen < 0) System.out.println("Read returned: " + resultLen);
    } else {
      throw new Exception("Read returned: " + response + "(" + serverClient.getCode() + ")");
    }
    if (resultLen > 0) in.readFully(buffer, offset, resultLen);
    /*
    if(readLen != resultLen)
      System.out.println("Read returned " + resultLen + ", but only " + readLen +
    " was actually returned");
    */
    return resultLen;
  }
예제 #5
0
  /** Does a 'ls' or a 'cd' command. */
  private void lsAndCd(final String[] cmd) throws Exception {
    if (cmd.length < 1) {
      throw new Exception("USAGE: <cmd>");
    }
    boolean cd = false;
    if (cmd[0].equalsIgnoreCase("cd")) {
      cd = true;
      if (cmd.length < 2) {
        throw new Exception("USAGE: cd path");
      }
    }

    String path = cwd;
    byte[] pathBytes = null;
    int numEntries = 0;

    if (cmd.length >= 2) {
      path = cmd[1];
      if (path.equals("..")) {
        // Looking for parent
        int parentSep = cwd.lastIndexOf('/');
        // System.err.println("cwd:" + cwd + " " + parentSep);
        if (parentSep > 0) parentSep = cwd.lastIndexOf('/', parentSep - 1);

        if (parentSep > 0) path = cwd.substring(0, parentSep);
        else path = "/";
        // System.err.println("cwd:" + cwd + " parent: " + path);
      }
      if (path.charAt(0) != '/') path = cwd + path;
    }

    if (path.equals("/")) {
      pathBytes = null;
    } else {
      String sendPath = path;
      if (path.charAt(0) == '/') {
        sendPath = path.substring(1);
      }
      // System.err.println("dir of path: " + sendPath);
      pathBytes = sendPath.getBytes(charset);
    }

    int numPathBytes = 0;
    if (pathBytes != null) {
      numPathBytes = pathBytes.length;
    }
    System.arraycopy(serverClient.returnInt(numPathBytes), 0, serverClient.cmdPwd, 0, 4);
    out.write(serverClient.cmdPwd, 0, 9);
    if (numPathBytes != 0) out.write(pathBytes);
    out.flush();

    String response = serverClient.readCommandString();
    if (response.equals(serverClient.PWD)) {
      numEntries = serverClient.getCode();
      if (numEntries < 0) System.out.println("PWD returned: " + numEntries);
      if (cd) {
        if (numEntries >= 0) {
          cwd = path;
          if (!cwd.endsWith("/")) {
            cwd = cwd + "/";
          }
        }
      }
    } else {
      throw new Exception("PWD returned: " + response + "(" + serverClient.getCode() + ")");
    }

    // Read entry data;
    if (numEntries >= 0) {
      byte[] entries =
          new byte
              [ps2vfs.server.Ps2VfsClient.maxDirEntrySize
                  * ps2vfs.server.Ps2VfsClient.maxDirEntries];

      // Trying short mode first, i.e. read just what we need.
      int entriesToRead = numEntries + 1;
      if (entriesToRead > ps2vfs.server.Ps2VfsClient.maxDirEntries) {
        entriesToRead = ps2vfs.server.Ps2VfsClient.maxDirEntries;
      }
      in.readFully(entries, 0, entriesToRead * ps2vfs.server.Ps2VfsClient.maxDirEntrySize);

      if (!cd) {
        // Print content.
        for (int n = 0; n < numEntries; n++) {
          int entryOffset = n * ps2vfs.server.Ps2VfsClient.maxDirEntrySize;
          if (entries[entryOffset] == 0xff) {
            // This is the end marker, so we just end the printing here.
            break;
          }
          boolean isDir = (entries[entryOffset] & 0x02) != 0;
          int len = ps2vfs.server.Ps2VfsClient.maxDirEntryNameLength;
          while (entries[entryOffset + len] == 0) len--;

          String entryName = new String(entries, entryOffset + 1, len);
          if (isDir) entryName = entryName + "/";
          System.out.println(entryName);
        }
      }
      if (in.available() > 0 || !ps2vfs.server.Ps2VfsClient.shortDirMode) {
        int remainingBytes =
            (ps2vfs.server.Ps2VfsClient.maxDirEntries - entriesToRead)
                * ps2vfs.server.Ps2VfsClient.maxDirEntrySize;

        if (ps2vfs.server.Ps2VfsClient.shortDirMode) {
          System.err.println("More data available, assuming long mode.... ");
        }
        /*
        System.err.println("Reading remaining overhead " + remainingBytes + "(" +
        		   (entriesToRead*ps2vfs.server.Ps2VfsClient.maxDirEntrySize) +
        		   "/" +
        		   (remainingBytes +
        		    entriesToRead*ps2vfs.server.Ps2VfsClient.maxDirEntrySize) +
        		   "/" + (ps2vfs.server.Ps2VfsClient.maxDirEntrySize*
        			  ps2vfs.server.Ps2VfsClient.maxDirEntries) + ")" +
        		   " bytes.");
        */
        if (remainingBytes > 0) in.readFully(entries, 0, remainingBytes);

        if (ps2vfs.server.Ps2VfsClient.shortDirMode) {
          System.err.println("Full PWD mode ended.");
        }
      }
    }
  }