2009-08-04 10:36:53 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2009, Swedish Institute of Computer Science
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* @author Nicolas Tsiftes
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-11-19 11:17:37 +00:00
|
|
|
package org.contikios.coffee;
|
2009-08-04 10:36:53 +00:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.regex.Pattern;
|
2013-11-19 11:17:37 +00:00
|
|
|
import org.contikios.coffee.CoffeeFS.CoffeeException;
|
|
|
|
import org.contikios.coffee.CoffeeFS.CoffeeFileException;
|
2009-08-04 10:36:53 +00:00
|
|
|
|
|
|
|
public class CoffeeManager {
|
2009-08-04 15:19:07 +00:00
|
|
|
public enum Command { INSERT, EXTRACT, REMOVE, LIST, STATS };
|
2009-08-04 10:36:53 +00:00
|
|
|
|
|
|
|
public static void main(String args[]) {
|
|
|
|
String platform = "sky";
|
2009-08-04 15:19:07 +00:00
|
|
|
Command command = Command.STATS;
|
|
|
|
String filename = "";
|
2009-08-04 10:36:53 +00:00
|
|
|
String fsImage = "";
|
2012-11-19 13:47:35 +00:00
|
|
|
String usage = "Usage: java -jar coffee.jar " +
|
|
|
|
"[-p <hardware platform>] " +
|
|
|
|
"[-i|e|r <file>] " +
|
|
|
|
"[-l|s] " +
|
|
|
|
"<file system image>";
|
2009-08-04 10:36:53 +00:00
|
|
|
|
2009-08-13 12:11:20 +00:00
|
|
|
if (args.length < 2) {
|
2009-08-04 10:36:53 +00:00
|
|
|
System.err.println(usage);
|
|
|
|
System.exit(1);
|
|
|
|
}
|
|
|
|
|
2009-08-13 12:11:20 +00:00
|
|
|
Pattern optionArg = Pattern.compile("-(p|i|e|r)");
|
2009-08-04 10:36:53 +00:00
|
|
|
|
2009-08-13 12:11:20 +00:00
|
|
|
for(int i = 0; i < args.length - 1; i++) {
|
|
|
|
if (optionArg.matcher(args[i]).matches()) {
|
|
|
|
if(i >= args.length - 2) {
|
2009-08-04 10:36:53 +00:00
|
|
|
System.err.println(usage);
|
|
|
|
System.exit(1);
|
|
|
|
}
|
2009-08-13 12:11:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(args[i].equals("-p")) {
|
|
|
|
platform = args[i + 1];
|
|
|
|
i++;
|
|
|
|
} else if (args[i].equals("-i")) {
|
|
|
|
command = Command.INSERT;
|
|
|
|
filename = args[i + 1];
|
|
|
|
i++;
|
|
|
|
} else if (args[i].equals("-r")) {
|
|
|
|
command = Command.REMOVE;
|
|
|
|
filename = args[i + 1];
|
|
|
|
i++;
|
2009-08-13 12:15:35 +00:00
|
|
|
} else if (args[i].equals("-e")) {
|
2009-08-13 12:11:20 +00:00
|
|
|
command = Command.EXTRACT;
|
|
|
|
filename = args[i + 1];
|
|
|
|
i++;
|
|
|
|
} else if (args[i].equals("-l")) {
|
|
|
|
command = Command.LIST;
|
|
|
|
} else if (args[i].equals("-s")) {
|
|
|
|
command = Command.STATS;
|
2009-08-04 10:36:53 +00:00
|
|
|
} else {
|
2009-08-13 12:11:20 +00:00
|
|
|
System.err.println(usage);
|
|
|
|
System.exit(1);
|
2009-08-04 10:36:53 +00:00
|
|
|
}
|
|
|
|
}
|
2009-08-13 12:11:20 +00:00
|
|
|
fsImage = args[args.length - 1];
|
2009-08-04 10:36:53 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
CoffeeConfiguration conf = new CoffeeConfiguration(platform + ".properties");
|
2012-11-19 13:47:35 +00:00
|
|
|
CoffeeFS coffeeFS = new CoffeeFS(new CoffeeImageFile(fsImage, conf));
|
2009-08-04 10:36:53 +00:00
|
|
|
switch (command) {
|
2009-08-04 15:19:07 +00:00
|
|
|
case INSERT:
|
2009-08-10 12:51:52 +00:00
|
|
|
if (coffeeFS.getFiles().get(filename) != null) {
|
2009-08-04 15:19:07 +00:00
|
|
|
System.err.println("error: file \"" +
|
|
|
|
filename + "\" already exists");
|
2009-08-04 10:36:53 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-08-10 12:51:52 +00:00
|
|
|
if (coffeeFS.insertFile(filename) != null) {
|
2009-08-04 15:19:07 +00:00
|
|
|
System.out.println("Inserted the local file \"" +
|
|
|
|
filename +
|
|
|
|
"\" into the file system image");
|
2009-08-04 10:36:53 +00:00
|
|
|
}
|
|
|
|
break;
|
2009-08-04 15:19:07 +00:00
|
|
|
case EXTRACT:
|
2009-08-10 12:51:52 +00:00
|
|
|
if (coffeeFS.extractFile(filename) == false) {
|
2009-08-04 15:19:07 +00:00
|
|
|
System.err.println("Inexistent file: " +
|
|
|
|
filename);
|
2009-08-04 10:36:53 +00:00
|
|
|
System.exit(1);
|
|
|
|
}
|
2009-08-04 15:19:07 +00:00
|
|
|
System.out.println("Saved the file \"" +
|
|
|
|
filename + "\"");
|
2009-08-04 10:36:53 +00:00
|
|
|
break;
|
2009-08-04 15:19:07 +00:00
|
|
|
case REMOVE:
|
|
|
|
coffeeFS.removeFile(filename);
|
|
|
|
System.out.println("Removed the file \"" +
|
|
|
|
filename +
|
|
|
|
"\" from the Coffee file system image");
|
2009-08-04 10:36:53 +00:00
|
|
|
break;
|
2009-08-04 15:19:07 +00:00
|
|
|
case LIST:
|
2009-08-04 10:36:53 +00:00
|
|
|
printFiles(coffeeFS.getFiles());
|
|
|
|
break;
|
2009-08-04 15:19:07 +00:00
|
|
|
case STATS:
|
2009-08-04 10:36:53 +00:00
|
|
|
printStatistics(coffeeFS);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
System.err.println("Unknown command!");
|
|
|
|
System.exit(1);
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
System.err.println(e.getMessage());
|
2009-08-13 12:11:20 +00:00
|
|
|
} catch (CoffeeException e) {
|
|
|
|
System.err.println(e.getMessage());
|
|
|
|
} catch (CoffeeFileException e) {
|
|
|
|
System.err.println(e.getMessage());
|
2009-08-04 10:36:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-11 17:03:59 +00:00
|
|
|
public static void printStatistics(CoffeeFS coffeeFS) {
|
2009-08-04 10:36:53 +00:00
|
|
|
int bytesWritten = 0;
|
|
|
|
int bytesReserved = 0;
|
|
|
|
int fileCount = 0;
|
|
|
|
CoffeeConfiguration conf = coffeeFS.getConfiguration();
|
|
|
|
|
|
|
|
try {
|
|
|
|
Iterator<Map.Entry<String, CoffeeFile>> iterator =
|
|
|
|
coffeeFS.getFiles().entrySet().iterator();
|
|
|
|
while (iterator.hasNext()) {
|
2012-01-28 02:34:16 +00:00
|
|
|
Map.Entry<String, CoffeeFile> pair = iterator.next();
|
2009-08-04 10:36:53 +00:00
|
|
|
CoffeeFile file = pair.getValue();
|
|
|
|
bytesWritten += file.getLength();
|
|
|
|
bytesReserved += file.getHeader().getReservedSize();
|
|
|
|
fileCount++;
|
|
|
|
}
|
|
|
|
bytesReserved *= conf.pageSize;
|
|
|
|
System.out.println("File system size: " +
|
|
|
|
conf.fsSize / 1024 + "kb");
|
|
|
|
System.out.println("Allocated files: " + fileCount);
|
|
|
|
System.out.println("Reserved bytes: " + bytesReserved + " (" +
|
|
|
|
(100 * ((float) bytesReserved / conf.fsSize)) +
|
|
|
|
"%)");
|
|
|
|
System.out.println("Written bytes: " + bytesWritten +
|
|
|
|
" (" +
|
|
|
|
(100 * ((float) bytesWritten / conf.fsSize)) +
|
|
|
|
"%)");
|
|
|
|
} catch (IOException e) {
|
|
|
|
System.err.println("failed to determine the file length");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-11 17:03:59 +00:00
|
|
|
public static void printFiles(Map<String, CoffeeFile> files) {
|
2009-08-04 10:36:53 +00:00
|
|
|
try {
|
|
|
|
Iterator<Map.Entry<String, CoffeeFile>> iterator = files.entrySet().iterator();
|
|
|
|
while (iterator.hasNext()) {
|
2012-01-28 02:34:16 +00:00
|
|
|
Map.Entry<String, CoffeeFile> pair = iterator.next();
|
2009-08-04 10:36:53 +00:00
|
|
|
CoffeeFile file = pair.getValue();
|
|
|
|
System.out.println(file.getName() + " " + file.getLength());
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
System.err.println("failed to determine the file length");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|