/*
 * Licensed to the University of California, Berkeley under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for additional information regarding
 * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance with the License. You may obtain a
 * copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package tachyon;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import tachyon.conf.TachyonConf;
import tachyon.util.CommonUtils;

/**
 * Format Tachyon File System.
 */
public class Format {
  private static final Logger LOG = LoggerFactory.getLogger(Constants.LOGGER_TYPE);
  private static final String USAGE = "java -cp target/tachyon-" + Version.VERSION
      + "-jar-with-dependencies.jar tachyon.Format <MASTER/WORKER>";

  private static boolean formatFolder(String name, String folder, TachyonConf tachyonConf)
      throws IOException {
    UnderFileSystem ufs = UnderFileSystem.get(folder, tachyonConf);
    LOG.info("Formatting {}:{}", name, folder);
    if (ufs.exists(folder) && !ufs.delete(folder, true)) {
      LOG.info("Failed to remove {}:{}", name, folder);
      return false;
    }
    if (!ufs.mkdirs(folder, true)) {
      LOG.info("Failed to create {}:{}", name, folder);
      return false;
    }
    return true;
  }

  public static void main(String[] args) throws IOException {
    if (args.length != 1) {
      LOG.info(USAGE);
      System.exit(-1);
    }

    TachyonConf tachyonConf = new TachyonConf();

    if (args[0].toUpperCase().equals("MASTER")) {

      String masterJournal = tachyonConf.get(Constants.MASTER_JOURNAL_FOLDER,
          Constants.DEFAULT_JOURNAL_FOLDER);
      if (!formatFolder("JOURNAL_FOLDER", masterJournal, tachyonConf)) {
        System.exit(-1);
      }

      String tachyonHome = tachyonConf.get(Constants.TACHYON_HOME, Constants.DEFAULT_HOME);
      String ufsAddress = tachyonConf.get(Constants.UNDERFS_ADDRESS, tachyonHome + "/underfs");
      String ufsDataFolder = tachyonConf.get(Constants.UNDERFS_DATA_FOLDER,
          ufsAddress + "/tachyon/data");
      String ufsWorkerFolder = tachyonConf.get(Constants.UNDERFS_WORKERS_FOLDER,
          ufsAddress + "/tachyon/workers");
      if (!formatFolder("UNDERFS_DATA_FOLDER", ufsDataFolder, tachyonConf)
          || !formatFolder("UNDERFS_WORKERS_FOLDER", ufsWorkerFolder, tachyonConf)) {
        System.exit(-1);
      }

      CommonUtils.touch(masterJournal + Constants.FORMAT_FILE_PREFIX + System.currentTimeMillis(),
          tachyonConf);
    } else if (args[0].toUpperCase().equals("WORKER")) {
      int maxStorageLevels = tachyonConf.getInt(Constants.WORKER_MAX_HIERARCHY_STORAGE_LEVEL, 1);
      String workerDataFolder =
          tachyonConf.get(Constants.WORKER_DATA_FOLDER, Constants.DEFAULT_DATA_FOLDER);
      for (int level = 0; level < maxStorageLevels; level ++) {
        String tierLevelDirPath = "tachyon.worker.hierarchystore.level" + level + ".dirs.path";
        String[] dirPaths = tachyonConf.get(tierLevelDirPath, "/mnt/ramdisk").split(",");
        for (int i = 0; i < dirPaths.length; i ++) {
          String dataPath = CommonUtils.concat(dirPaths[i].trim(), workerDataFolder);
          UnderFileSystem ufs = UnderFileSystem.get(dataPath, tachyonConf);
          LOG.info("Removing data under folder: {}", dataPath);
          if (ufs.exists(dataPath)) {
            String[] files = ufs.list(dataPath);
            for (String file : files) {
              ufs.delete(CommonUtils.concat(dataPath, file), true);
            }
          }
        }
      }

    } else {
      LOG.info(USAGE);
      System.exit(-1);
    }
  }
}

@@ -1522,31 +1522,107 @@
 der)
- && !ufs.delete
+) %7B%0A      for (String file : ufs.list(folder)) %7B%0A        if (!ufs.delete(CommonUtils.concat
 (folder,
  tru
@@ -1609,32 +1609,39 @@
 s.concat(folder,
+ file),
  true)) %7B%0A      
@@ -1632,32 +1632,36 @@
  true)) %7B%0A      
+  
+  
 LOG.info(%22Failed
@@ -1678,37 +1678,35 @@
  %7B%7D:%7B%7D%22, name, f
-older
+ile
 );%0A      return 
@@ -1690,32 +1690,36 @@
 e, file);%0A      
+  
+  
 return false;%0A  
@@ -1708,32 +1708,36 @@
   return false;%0A
+    
     %7D%0A    if (!u
@@ -1725,24 +1725,39 @@
        %7D%0A   
+ 
+  %7D%0A    %7D else
  if (!ufs.mk
@@ -3263,128 +3263,8 @@
 1);%0A
-      String workerDataFolder =%0A          tachyonConf.get(Constants.WORKER_DATA_FOLDER, Constants.DEFAULT_DATA_FOLDER);%0A
     
@@ -3319,24 +3319,24 @@
 level ++) %7B%0A
+
         Stri
@@ -3521,62 +3521,61 @@
     
-for (int i = 0; i %3C dirPaths.length; i ++) %7B%0A 
+String name = %22TIER_%22 + level + %22_DIR_PATH%22;%0A
         
  Str
@@ -3574,17 +3574,21 @@
     
- 
+for (
 String d
 ataP
@@ -3587,424 +3587,123 @@
 ng d
-ata
+ir
 Path 
-= CommonUtils.concat(dirPaths%5Bi%5D.trim(), workerDataFolder);%0A          UnderFileSystem ufs = UnderFileSystem.get(dataPath, tachyonConf);%0A          LOG.info(%22Removing data under folder: %7B%7D%22, dataPath);%0A          if (ufs.exists(dataPath)) %7B%0A            String%5B%5D files = ufs.list(dataPath);%0A            for (String file : files) %7B%0A              ufs.delete(CommonUtils.concat(dataPath, file), true);%0A            %7D
+: dirPaths) %7B%0A          if (!formatFolder(name, dirPath.trim(), tachyonConf)) %7B%0A            System.exit(-1);
 %0A   
@@ -3721,24 +3721,24 @@
       %7D%0A
+
       %7D%0A
 %0A    %7D e
@@ -3725,25 +3725,24 @@
   %7D%0A      %7D%0A
-%0A
     %7D else %7B

fix format issue for formatWorker