package org.cloudcoder.builder2.util;

import java.io.File;
import java.io.IOException;

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

public class FileUtil {
	public static final Logger logger = LoggerFactory.getLogger(FileUtil.class);

	/**
	 * Make a temporary directory as a subdirectory of the standard system
	 * temp directory.
	 *  
	 * @return the {@link File} naming the temp directory
	 */
	public static File makeTempDir() {
		final File sysTempDir = new File(System.getProperty("java.io.tmpdir"));
		return FileUtil.makeTempDir(sysTempDir.getAbsolutePath());
	}

	/**
	 * Make a temporary directory.
	 * 
	 * @param baseDir  the parent of the temporary directory to create (e.g., "/tmp")
	 * @return a File representing the newly-created temp directory
	 */
	public static File makeTempDir(String baseDir) {
		int attempts = 1;
		File tempDir = null;
	
		while (tempDir == null && attempts < 10) {
			try {
				// start by creating a temporary file
				File tempFile = File.createTempFile("cmp", "", new File(baseDir));
	
				// temporary file created successfully - delete it and make an identically-named
				// directory
				if (tempFile.delete() && tempFile.mkdir()) {
					// success!
					tempDir = tempFile;
				}
			} catch (IOException e) {
				logger.warn("Unable to delete temp file and create directory");
			}
			attempts++;
		}
		return tempDir;
	}

}

@@ -1,8 +1,921 @@
+// CloudCoder - a web-based pedagogical programming environment%0A// Copyright (C) 2011-2013, Jaime Spacco %3Cjspacco@knox.edu%3E%0A// Copyright (C) 2011-2013, David H. Hovemeyer %3Cdhovemey@ycp.edu%3E%0A// Copyright (C) 2013, York College of Pennsylvania%0A//%0A// This program is free software: you can redistribute it and/or modify%0A// it under the terms of the GNU Affero General Public License as published by%0A// the Free Software Foundation, either version 3 of the License, or%0A// (at your option) any later version.%0A//%0A// This program is distributed in the hope that it will be useful,%0A// but WITHOUT ANY WARRANTY; without even the implied warranty of%0A// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the%0A// GNU Affero General Public License for more details.%0A//%0A// You should have received a copy of the GNU Affero General Public License%0A// along with this program.  If not, see %3Chttp://www.gnu.org/licenses/%3E.%0A%0A
 package 
@@ -1053,16 +1053,81 @@
 ctory;%0A%0A
+/**%0A * File utility methods.%0A *  %0A * @author David Hovemeyer%0A */%0A
 public c

add copyright/license header