示例#1
0
文件: Pre.java 项目: cmstest/drftpd
  public CommandResponse doSITE_PRE(CommandRequest request) throws ImproperUsageException {
    if (!request.hasArgument()) {
      throw new ImproperUsageException();
    }

    String[] args = request.getArgument().split(" ");

    if (args.length != 2) {
      return StandardCommandManager.genericResponse("RESPONSE_501_SYNTAX_ERROR");
    }

    SectionInterface section =
        GlobalContext.getGlobalContext().getSectionManager().getSection(args[1]);

    if (section.getName().equals("")) {
      return new CommandResponse(
          500, "Invalid section, see SITE SECTIONS for a list of available sections");
    }

    User user = request.getSession().getUserNull(request.getUser());

    DirectoryHandle preDir;

    String path = VirtualFileSystem.fixPath(args[0]);
    if (!(path.startsWith(VirtualFileSystem.separator))) {
      // Not a full path, let's make it one
      if (request.getCurrentDirectory().isRoot()) {
        path = VirtualFileSystem.separator + path;
      } else {
        path = request.getCurrentDirectory().getPath() + VirtualFileSystem.separator + path;
      }
    }

    try {
      preDir = request.getCurrentDirectory().getDirectory(path, user);
    } catch (FileNotFoundException e) {
      return StandardCommandManager.genericResponse("RESPONSE_550_REQUESTED_ACTION_NOT_TAKEN");
    } catch (ObjectNotValidException e) {
      return StandardCommandManager.genericResponse(
          "RESPONSE_553_REQUESTED_ACTION_NOT_TAKEN_FILE_EXISTS");
    }

    ConfigInterface config = GlobalContext.getConfig();
    if (!config.checkPathPermission("pre", user, preDir)) {
      return StandardCommandManager.genericResponse("RESPONSE_530_ACCESS_DENIED");
    }

    DirectoryHandle toInode =
        new DirectoryHandle(
            section.getCurrentDirectory().getPath()
                + VirtualFileSystem.separator
                + preDir.getName());

    if (toInode.exists()) {
      return new CommandResponse(500, "Directory already exist in target section");
    }

    CommandResponse response =
        new CommandResponse(250, request.getCommand().toUpperCase() + " command successful.");

    // AWARD CREDITS
    HashMap<User, Long> awards = new HashMap<User, Long>();
    preAwardCredits(preDir, awards);

    for (Map.Entry<User, Long> entry : awards.entrySet()) {
      User owner = entry.getKey();
      if (StatsManager.getStatsManager().getCreditCheckRatio(preDir, owner) == 0) {
        Long award = entry.getValue();
        owner.updateCredits(award);
        response.addComment("Awarded " + Bytes.formatBytes(award) + " to " + owner.getName());
      }
    }

    recursiveRemoveOwnership(preDir, System.currentTimeMillis());

    int files = getFiles(preDir);
    long bytes = 0;
    try {
      bytes = preDir.getSize();
    } catch (FileNotFoundException e) {
      logger.warn("FileNotFoundException ", e);
    }

    try {
      preDir.renameToUnchecked(toInode);
    } catch (FileNotFoundException e) {
      logger.warn("FileNotFoundException on renameTo()", e);
      return new CommandResponse(500, "FileNotFound - " + e.getMessage());
    } catch (IOException e) {
      logger.warn("IOException on renameTo()", e);
      return new CommandResponse(500, "IOException - " + e.getMessage());
    }

    preDir = toInode;

    GlobalContext.getEventService()
        .publishAsync(
            new PreEvent(preDir, section, Integer.toString(files), Bytes.formatBytes(bytes)));

    response.setObject(PREDIR, preDir);

    return response;
  }