package org.jboss.fresh.shell.commands;
import org.jboss.fresh.vfs.FileInfo;
import org.jboss.fresh.vfs.FileName;
import org.jboss.fresh.vfs.VFS;
import org.jboss.fresh.io.BufferWriter;
import org.jboss.fresh.io.PrintWriter2;
import org.jboss.fresh.shell.AbstractExecutable;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
// EX OK
public class TouchExe extends AbstractExecutable {
private static transient org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(TouchExe.class);
private static final String CREATE = "-c";
/**
Updates the modification dates of the files specified, if a file doesn't exist a new file is created.
*/
public void process(String exename, String[] params) throws Exception {
log.debug("entered");
if (helpRequested()) {
PrintWriter2 out = new PrintWriter2(new BufferWriter(getStdOut()));
out.print("Usage: touch [--help] [-c] [files]\n");
out.print(" files : files to be 'touched'\n");
out.print(" -c : don't create inexisting files\n");
out.print(" --help : this help\n\n");
out.close();
log.debug("done");
return;
}
PrintWriter2 out = new PrintWriter2(new BufferWriter(getStdOut()));
VFS vfs = shell.getVFS();
FileName pwd = new FileName(shell.getEnvProperty("PWD"));
boolean create = true;
List paths = new LinkedList();
for (int i = 0; i < params.length; i++) {
String param = params[i];
if (param.equals(CREATE))
create = false;
else {
FileName path = new FileName(param);
if (path.isRelative())
path = pwd.absolutize(path);
// path = vfs.resolve(shell.getUserCtx(), path, true);
paths.add(path);
}
}
// List result = new LinkedList();
// if no paths given, read from stdin
if (!paths.isEmpty()) {
Iterator it = paths.iterator();
while (it.hasNext()) {
FileName path = (FileName) it.next();
if (vfs.exists(shell.getUserCtx(), path, true)) {
FileInfo info = vfs.getFileInfo(shell.getUserCtx(), path, true);
info.setLastModified(new Date());
vfs.updateFile(shell.getUserCtx(), info);
out.println(path + ": Modified date set to now.");
} else if (create) {
FileInfo info = new FileInfo(path, FileInfo.TYPE_FILE);
info.setMime("undefined");
vfs.createFile(shell.getUserCtx(), info);
out.println(path + ": File created.");
} else {
out.println(path + ": File does not exist.");
}
}
} else {
out.println("Usage: touch [-c] [FILES]");
out.println(" -c = don't create inexisting files");
}
out.close();
log.debug("done");
}
}