Example #1
0
    /**
     * Will move: messages.log.1 -> messages.log.2 messages.log -> messages.log.1
     *
     * <p>Will delete (if exists): messages.log.2
     */
    private void moveAwayFile() {
      File oldLogFile =
          new File(file.getParentFile(), file.getName() + "." + NUMBER_OF_OLD_LOGS_TO_KEEP);
      if (fileSystem.fileExists(oldLogFile)) {
        fileSystem.deleteFile(oldLogFile);
      }

      for (int i = NUMBER_OF_OLD_LOGS_TO_KEEP - 1; i >= 0; i--) {
        oldLogFile = new File(file.getParentFile(), file.getName() + (i == 0 ? "" : ("." + i)));
        if (fileSystem.fileExists(oldLogFile)) {
          try {
            fileSystem.renameFile(
                oldLogFile, new File(file.getParentFile(), file.getName() + "." + (i + 1)));
          } catch (IOException e) {
            throw new RuntimeException(e);
          }
        }
      }
    }
Example #2
0
  /**
   * Moves a file from one directory to another, by a rename op.
   *
   * @param fs
   * @param fileName The base filename of the file to move, not the complete path
   * @param fromDirectory The directory currently containing filename
   * @param toDirectory The directory to host filename - must be in the same disk partition as
   *     filename
   * @param allowOverwriteTarget
   * @throws java.io.IOException
   */
  static void moveFile(
      FileSystemAbstraction fs,
      String fileName,
      File fromDirectory,
      File toDirectory,
      boolean allowSkipNonExistentFiles,
      boolean allowOverwriteTarget)
      throws IOException {
    File sourceFile = new File(fromDirectory, fileName);
    if (allowSkipNonExistentFiles
        && !fs.fileExists(
            sourceFile)) { // The source file doesn't exist and we allow skipping, so return
      return;
    }

    File toFile = new File(toDirectory, fileName);
    if (allowOverwriteTarget && fs.fileExists(toFile)) {
      fs.deleteFile(toFile);
    }

    fs.moveToDirectory(sourceFile, toDirectory);
  }