package eu.stratuslab.storage.disk.plugins;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import org.restlet.data.Status;
import org.restlet.resource.ResourceException;

import eu.stratuslab.storage.disk.main.RootApplication;
import eu.stratuslab.storage.disk.utils.FileUtils;
import eu.stratuslab.storage.disk.utils.ProcessUtils;

public final class PosixStorage implements DiskStorage {

	public PosixStorage() {

	}

	public void create(String uuid, int size) {
		File diskFile = new File(
				RootApplication.CONFIGURATION.STORAGE_LOCATION, uuid);

		if (diskFile.exists()) {
			throw new ResourceException(Status.SERVER_ERROR_INTERNAL,
					"A disk with the same name already exists.");
		}

		FileUtils.createZeroFile(diskFile, size);
	}

	public void delete(String uuid) {
		File diskFile = new File(
				RootApplication.CONFIGURATION.STORAGE_LOCATION, uuid);

		if (!diskFile.delete()) {
			throw new ResourceException(Status.SERVER_ERROR_INTERNAL,
					"An error occcured while removing disk content " + uuid);
		}
	}

	public void createCopyOnWrite(String baseUuid, String cowUuid, int size) {
		// TODO Auto-generated method stub
	}

	public String rebase(String uuid, String rebaseUuid) {
		// TODO Auto-generated method stub
		return null;
	}

	public String zip(String uuid) {
		String cachedDisk = RootApplication.CONFIGURATION.CACHE_LOCATION + "/"
				+ uuid;

		copyfile(
				RootApplication.CONFIGURATION.STORAGE_LOCATION.getAbsolutePath()
						+ "/" + uuid, cachedDisk);

		List<String> zipCmd = new ArrayList<String>();
		zipCmd.add("gzip");
		zipCmd.add("-f");
		zipCmd.add(cachedDisk);

		ProcessBuilder pb = new ProcessBuilder(zipCmd);
		ProcessUtils.execute(pb, "Unable to attach persistent disk");

		return cachedDisk + ".gz";
	}

	private static void copyfile(String srFile, String dtFile) {
		try {
			File src = new File(srFile);
			File dst = new File(dtFile);

			InputStream in = new FileInputStream(src);
			OutputStream out = new FileOutputStream(dst);

			byte[] buf = new byte[1024];
			int len;
			while ((len = in.read(buf)) > 0) {
				out.write(buf, 0, len);
			}

			in.close();
			out.close();
		} catch (FileNotFoundException ex) {
			throw new ResourceException(Status.SERVER_ERROR_INTERNAL,
					"Error while copying disk");
		} catch (IOException e) {
			throw new ResourceException(Status.SERVER_ERROR_INTERNAL,
					"Error while copying disk");
		}
	}
}

@@ -63,196 +63,8 @@
 le;%0A
-import java.io.FileInputStream;%0Aimport java.io.FileNotFoundException;%0Aimport java.io.FileOutputStream;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.io.OutputStream;%0A
 impo
@@ -1312,16 +1312,265 @@
 uuid) %7B%0A
+%09%09File disk = new File(%0A%09%09%09%09RootApplication.CONFIGURATION.STORAGE_LOCATION%0A%09%09%09%09%09%09.getAbsolutePath() + %22/%22 + uuid);%0A%0A%09%09if (!disk.isFile()) %7B%0A%09%09%09throw new ResourceException(Status.SERVER_ERROR_INTERNAL, %22Disk %22%0A%09%09%09%09%09+ uuid + %22 does not exists%22);%0A%09%09%7D%0A%0A
 %09%09String
@@ -1649,21 +1649,31 @@
 uid;%0A%0A%09%09
+FileUtils.
 copy
-f
+F
 ile(%0A%09%09%09
@@ -1719,16 +1719,23 @@
 LOCATION
+%0A%09%09%09%09%09%09
 .getAbso
@@ -1744,23 +1744,17 @@
 tePath()
-%0A%09%09%09%09%09%09
+ 
 + %22/%22 + 
@@ -2042,649 +2042,8 @@
 %0A%09%7D%0A
-%0A%09private static void copyfile(String srFile, String dtFile) %7B%0A%09%09try %7B%0A%09%09%09File src = new File(srFile);%0A%09%09%09File dst = new File(dtFile);%0A%0A%09%09%09InputStream in = new FileInputStream(src);%0A%09%09%09OutputStream out = new FileOutputStream(dst);%0A%0A%09%09%09byte%5B%5D buf = new byte%5B1024%5D;%0A%09%09%09int len;%0A%09%09%09while ((len = in.read(buf)) %3E 0) %7B%0A%09%09%09%09out.write(buf, 0, len);%0A%09%09%09%7D%0A%0A%09%09%09in.close();%0A%09%09%09out.close();%0A%09%09%7D catch (FileNotFoundException ex) %7B%0A%09%09%09throw new ResourceException(Status.SERVER_ERROR_INTERNAL,%0A%09%09%09%09%09%22Error while copying disk%22);%0A%09%09%7D catch (IOException e) %7B%0A%09%09%09throw new ResourceException(Status.SERVER_ERROR_INTERNAL,%0A%09%09%09%09%09%22Error while copying disk%22);%0A%09%09%7D%0A%09%7D
+%09
 %0A%7D%0A

Check that disk exist on ZIP request for Posix