diff --git a/.gitignore b/.gitignore index 0f67992a1..8a9e052e0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.DS_Store *.class # Mobile Tools for Java (J2ME) diff --git a/src/ru/fizteh/fivt/students/Gudkov Konstantin 394/shell/ShellMain.java b/src/ru/fizteh/fivt/students/Gudkov Konstantin 394/shell/ShellMain.java deleted file mode 100644 index c7e6ea78f..000000000 --- a/src/ru/fizteh/fivt/students/Gudkov Konstantin 394/shell/ShellMain.java +++ /dev/null @@ -1,12 +0,0 @@ -package ru.fizteh.fivt.students.test.shell; - -/** - * @author test - */ -public class ShellMain { - - public static void main(String[] args) { - System.out.println("not working"); - System.exit(1); - } -} diff --git a/src/ru/fizteh/fivt/students/Oktosha/Command/Command.java b/src/ru/fizteh/fivt/students/Oktosha/Command/Command.java new file mode 100644 index 000000000..0a2b9882f --- /dev/null +++ b/src/ru/fizteh/fivt/students/Oktosha/Command/Command.java @@ -0,0 +1,16 @@ +package ru.fizteh.fivt.students.Oktosha.Command; + +/** + * A simple class which holds name of command and it's arguments. + */ +public class Command { + public Command(String commandString) { + commandString = commandString.trim(); + String[] split = commandString.split("\\s+"); + name = split[0]; + args = new String[split.length - 1]; + System.arraycopy(split, 1, args, 0, split.length - 1); + } + public String name; + public String[] args; +} diff --git a/src/ru/fizteh/fivt/students/Oktosha/ConsoleUtility/CommandArgumentSyntaxException.java b/src/ru/fizteh/fivt/students/Oktosha/ConsoleUtility/CommandArgumentSyntaxException.java new file mode 100644 index 000000000..810100cf1 --- /dev/null +++ b/src/ru/fizteh/fivt/students/Oktosha/ConsoleUtility/CommandArgumentSyntaxException.java @@ -0,0 +1,11 @@ +package ru.fizteh.fivt.students.Oktosha.ConsoleUtility; + +/** + * The exception which is thrown when there is a syntax error in arguments + * given to command of ConsoleUtility. + */ +public class CommandArgumentSyntaxException extends ConsoleUtilitySyntaxException { + public CommandArgumentSyntaxException(String s) { + super(s); + } +} diff --git a/src/ru/fizteh/fivt/students/Oktosha/ConsoleUtility/CommandIsNotSupportedException.java b/src/ru/fizteh/fivt/students/Oktosha/ConsoleUtility/CommandIsNotSupportedException.java new file mode 100644 index 000000000..a69f807bf --- /dev/null +++ b/src/ru/fizteh/fivt/students/Oktosha/ConsoleUtility/CommandIsNotSupportedException.java @@ -0,0 +1,10 @@ +package ru.fizteh.fivt.students.Oktosha.ConsoleUtility; + +/** + * Created by DKolodzey on 30.09.14. + */ +public class CommandIsNotSupportedException extends ConsoleUtilitySyntaxException { + public CommandIsNotSupportedException(String s) { + super(s); + } +} diff --git a/src/ru/fizteh/fivt/students/Oktosha/ConsoleUtility/ConsoleUtility.java b/src/ru/fizteh/fivt/students/Oktosha/ConsoleUtility/ConsoleUtility.java new file mode 100644 index 000000000..ccdb38ef5 --- /dev/null +++ b/src/ru/fizteh/fivt/students/Oktosha/ConsoleUtility/ConsoleUtility.java @@ -0,0 +1,13 @@ +package ru.fizteh.fivt.students.Oktosha.ConsoleUtility; + +import ru.fizteh.fivt.students.Oktosha.Command.Command; + +/** + * ConsoleUtility is an interface which should be implemented by Shell, + * DbMain or whatever else ConsoleUtility run by Executor. + */ +public interface ConsoleUtility { + void run(Command cmd) throws CommandIsNotSupportedException, + CommandArgumentSyntaxException, + ConsoleUtilityRuntimeException; +} diff --git a/src/ru/fizteh/fivt/students/Oktosha/ConsoleUtility/ConsoleUtilityException.java b/src/ru/fizteh/fivt/students/Oktosha/ConsoleUtility/ConsoleUtilityException.java new file mode 100644 index 000000000..7d77db79e --- /dev/null +++ b/src/ru/fizteh/fivt/students/Oktosha/ConsoleUtility/ConsoleUtilityException.java @@ -0,0 +1,13 @@ +package ru.fizteh.fivt.students.Oktosha.ConsoleUtility; + +/** + * Parent class for all exceptions thrown by ConsoleUtility + */ +public class ConsoleUtilityException extends Exception { + public ConsoleUtilityException(String s) { + super(s); + } + public ConsoleUtilityException(String s, Exception e) { + super(s, e); + } +} diff --git a/src/ru/fizteh/fivt/students/Oktosha/ConsoleUtility/ConsoleUtilityRuntimeException.java b/src/ru/fizteh/fivt/students/Oktosha/ConsoleUtility/ConsoleUtilityRuntimeException.java new file mode 100644 index 000000000..bf01e0c54 --- /dev/null +++ b/src/ru/fizteh/fivt/students/Oktosha/ConsoleUtility/ConsoleUtilityRuntimeException.java @@ -0,0 +1,13 @@ +package ru.fizteh.fivt.students.Oktosha.ConsoleUtility; + +/** + * This exception is thrown when an error during running command occurs. + */ +public class ConsoleUtilityRuntimeException extends ConsoleUtilityException { + public ConsoleUtilityRuntimeException(String s) { + super(s); + } + public ConsoleUtilityRuntimeException(String s, Exception e) { + super(s, e); + } +} diff --git a/src/ru/fizteh/fivt/students/Oktosha/ConsoleUtility/ConsoleUtilitySyntaxException.java b/src/ru/fizteh/fivt/students/Oktosha/ConsoleUtility/ConsoleUtilitySyntaxException.java new file mode 100644 index 000000000..aa9d52384 --- /dev/null +++ b/src/ru/fizteh/fivt/students/Oktosha/ConsoleUtility/ConsoleUtilitySyntaxException.java @@ -0,0 +1,12 @@ +package ru.fizteh.fivt.students.Oktosha.ConsoleUtility; + +/** + * Child class of ConsoleUtilityException. + * It is thrown when command has invalid syntax. + */ + +public class ConsoleUtilitySyntaxException extends ConsoleUtilityException { + ConsoleUtilitySyntaxException(String s) { + super(s); + } +} diff --git a/src/ru/fizteh/fivt/students/Oktosha/Executor/Executor.java b/src/ru/fizteh/fivt/students/Oktosha/Executor/Executor.java new file mode 100644 index 000000000..41f2c6d7d --- /dev/null +++ b/src/ru/fizteh/fivt/students/Oktosha/Executor/Executor.java @@ -0,0 +1,21 @@ +package ru.fizteh.fivt.students.Oktosha.Executor; + +import ru.fizteh.fivt.students.Oktosha.Command.Command; + +/** + * A class which executes an instance of ConsoleUtility + */ +public abstract class Executor { + + static final int SYNTAX_ERROR = 1; + static final int COMMAND_RUNTIME_ERROR = 2; + + protected static Command[] parse(String s) { + String[] commandStrings = s.split(";"); + Command[] res = new Command[commandStrings.length]; + for (int i = 0; i < res.length; ++i) { + res[i] = new Command(commandStrings[i]); + } + return res; + } +} diff --git a/src/ru/fizteh/fivt/students/Oktosha/Executor/ExecutorException.java b/src/ru/fizteh/fivt/students/Oktosha/Executor/ExecutorException.java new file mode 100644 index 000000000..7e391e281 --- /dev/null +++ b/src/ru/fizteh/fivt/students/Oktosha/Executor/ExecutorException.java @@ -0,0 +1,7 @@ +package ru.fizteh.fivt.students.Oktosha.Executor; + +/** + * Parent class for all exceptions thrown by executor. + */ +public class ExecutorException extends Exception { +} diff --git a/src/ru/fizteh/fivt/students/Oktosha/Executor/ExecutorParseException.java b/src/ru/fizteh/fivt/students/Oktosha/Executor/ExecutorParseException.java new file mode 100644 index 000000000..7e16917ae --- /dev/null +++ b/src/ru/fizteh/fivt/students/Oktosha/Executor/ExecutorParseException.java @@ -0,0 +1,7 @@ +package ru.fizteh.fivt.students.Oktosha.Executor; + +/** + * This exception is thrown when parsed command is empty + */ +public class ExecutorParseException extends ExecutorException { +} diff --git a/src/ru/fizteh/fivt/students/Oktosha/Executor/InteractiveExecutor.java b/src/ru/fizteh/fivt/students/Oktosha/Executor/InteractiveExecutor.java new file mode 100644 index 000000000..961f05edf --- /dev/null +++ b/src/ru/fizteh/fivt/students/Oktosha/Executor/InteractiveExecutor.java @@ -0,0 +1,36 @@ +package ru.fizteh.fivt.students.Oktosha.Executor; + +import ru.fizteh.fivt.students.Oktosha.Command.Command; +import ru.fizteh.fivt.students.Oktosha.ConsoleUtility.ConsoleUtility; +import ru.fizteh.fivt.students.Oktosha.ConsoleUtility.ConsoleUtilityException; + +import java.util.Scanner; + +/** + * InteractiveExecutor is a child of Executor + * which runs ConsoleUtility in interactive mode + */ +public class InteractiveExecutor extends Executor { + public static void execute(ConsoleUtility utility) { + Scanner sc = new Scanner(System.in); + System.out.print("$ "); + System.out.flush(); + while (sc.hasNextLine()) { + String commandsString = sc.nextLine(); + try { + Command[] commands = parse(commandsString); + for (Command cmd : commands) { + utility.run(cmd); + } + } catch (ConsoleUtilityException e) { + System.err.println(e.getMessage()); + System.err.flush(); + System.out.flush(); + } finally { + System.out.print("$ "); + System.out.flush(); + System.err.flush(); + } + } + } +} diff --git a/src/ru/fizteh/fivt/students/Oktosha/Executor/PackageExecutor.java b/src/ru/fizteh/fivt/students/Oktosha/Executor/PackageExecutor.java new file mode 100644 index 000000000..af2072710 --- /dev/null +++ b/src/ru/fizteh/fivt/students/Oktosha/Executor/PackageExecutor.java @@ -0,0 +1,32 @@ +package ru.fizteh.fivt.students.Oktosha.Executor; + +import ru.fizteh.fivt.students.Oktosha.Command.Command; +import ru.fizteh.fivt.students.Oktosha.ConsoleUtility.ConsoleUtility; +import ru.fizteh.fivt.students.Oktosha.ConsoleUtility.ConsoleUtilityRuntimeException; +import ru.fizteh.fivt.students.Oktosha.ConsoleUtility.ConsoleUtilitySyntaxException; + +/** + * Class which runs console utility in package mode + */ +public class PackageExecutor extends Executor { + public static void execute(ConsoleUtility utility, String[] args) { + StringBuilder builder = new StringBuilder(); + for (String str : args) { + builder.append(" "); + builder.append(str); + } + String commandsString = builder.toString(); + try { + Command[] commands = parse(commandsString); + for (Command cmd : commands) { + utility.run(cmd); + } + } catch (ConsoleUtilitySyntaxException e) { + System.err.println(e.getMessage()); + System.exit(Executor.SYNTAX_ERROR); + } catch (ConsoleUtilityRuntimeException e) { + System.err.println(e.getMessage()); + System.exit(Executor.COMMAND_RUNTIME_ERROR); + } + } +} diff --git a/src/ru/fizteh/fivt/students/Oktosha/Shell/CpFileVisitor.java b/src/ru/fizteh/fivt/students/Oktosha/Shell/CpFileVisitor.java new file mode 100644 index 000000000..699e04f1a --- /dev/null +++ b/src/ru/fizteh/fivt/students/Oktosha/Shell/CpFileVisitor.java @@ -0,0 +1,32 @@ +package ru.fizteh.fivt.students.Oktosha.Shell; + +import java.io.IOException; +import java.nio.file.*; +import java.nio.file.attribute.BasicFileAttributes; + +import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; + +/** + * Class which copies a directory recursively + */ + +public class CpFileVisitor extends SimpleFileVisitor { + private Path source; + private Path target; + CpFileVisitor(Path source, Path target) { + this.source = source; + this.target = target; + } + + @Override + public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { + Files.createDirectory(target.resolve(source.relativize(dir))); + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + Files.copy(file, target.resolve(source.relativize(file)), REPLACE_EXISTING); + return FileVisitResult.CONTINUE; + } +} diff --git a/src/ru/fizteh/fivt/students/Oktosha/Shell/MvFileVisitor.java b/src/ru/fizteh/fivt/students/Oktosha/Shell/MvFileVisitor.java new file mode 100644 index 000000000..e0790150f --- /dev/null +++ b/src/ru/fizteh/fivt/students/Oktosha/Shell/MvFileVisitor.java @@ -0,0 +1,40 @@ +package ru.fizteh.fivt.students.Oktosha.Shell; + +import java.io.IOException; +import java.nio.file.*; +import java.nio.file.attribute.BasicFileAttributes; + +/** + * Class which moves a directory recursively + */ + +public class MvFileVisitor extends SimpleFileVisitor { + private Path source; + private Path target; + MvFileVisitor(Path source, Path target) { + this.source = source; + this.target = target; + } + + @Override + public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { + Files.createDirectory(target.resolve(source.relativize(dir))); + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + Files.move(file, target.resolve(source.relativize(file))); + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { + if (exc != null) { + throw exc; + } + Files.delete(dir); + return FileVisitResult.CONTINUE; + } +} + diff --git a/src/ru/fizteh/fivt/students/Oktosha/Shell/RmFileVisitor.java b/src/ru/fizteh/fivt/students/Oktosha/Shell/RmFileVisitor.java new file mode 100644 index 000000000..013034cf5 --- /dev/null +++ b/src/ru/fizteh/fivt/students/Oktosha/Shell/RmFileVisitor.java @@ -0,0 +1,27 @@ +package ru.fizteh.fivt.students.Oktosha.Shell; + +import java.io.IOException; +import java.nio.file.*; +import java.nio.file.attribute.BasicFileAttributes; + +/** + * Class which deletes a directory recursively + */ + +public class RmFileVisitor extends SimpleFileVisitor { + + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + Files.delete(file); + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { + if (exc != null) { + throw exc; + } + Files.delete(dir); + return FileVisitResult.CONTINUE; + } +} diff --git a/src/ru/fizteh/fivt/students/Oktosha/Shell/Shell.java b/src/ru/fizteh/fivt/students/Oktosha/Shell/Shell.java new file mode 100644 index 000000000..38e8a5962 --- /dev/null +++ b/src/ru/fizteh/fivt/students/Oktosha/Shell/Shell.java @@ -0,0 +1,277 @@ +package ru.fizteh.fivt.students.Oktosha.Shell; + +import static java.nio.file.StandardCopyOption.*; +import java.io.BufferedReader; +import java.io.IOException; +import java.nio.file.*; + +import ru.fizteh.fivt.students.Oktosha.Command.Command; +import ru.fizteh.fivt.students.Oktosha.ConsoleUtility.CommandArgumentSyntaxException; +import ru.fizteh.fivt.students.Oktosha.ConsoleUtility.CommandIsNotSupportedException; +import ru.fizteh.fivt.students.Oktosha.ConsoleUtility.ConsoleUtility; +import ru.fizteh.fivt.students.Oktosha.ConsoleUtility.ConsoleUtilityRuntimeException; +import ru.fizteh.fivt.students.Oktosha.Executor.InteractiveExecutor; +import ru.fizteh.fivt.students.Oktosha.Executor.PackageExecutor; + +public class Shell implements ConsoleUtility { + + private Path workingDirectory; + + Shell() { + workingDirectory = Paths.get(System.getProperty("user.dir")); + } + + public static void main(String[] args) { + if (args.length == 0) { + InteractiveExecutor.execute(new Shell()); + } else { + PackageExecutor.execute(new Shell(), args); + } + } + + public void run(Command cmd) throws CommandIsNotSupportedException, + CommandArgumentSyntaxException, + ConsoleUtilityRuntimeException { + switch (cmd.name) { + case "cat": + cat(cmd.args); + break; + case "cd": + cd(cmd.args); + break; + case "cp": + cp(cmd.args); + break; + case "exit": + exit(cmd.args); + break; + case "ls": + ls(cmd.args); + break; + case "mkdir": + mkdir(cmd.args); + break; + case "mv": + mv(cmd.args); + break; + case "pwd": + pwd(cmd.args); + break; + case "rm": + rm(cmd.args); + break; + case "": + break; + default: + throw new CommandIsNotSupportedException(cmd.name + ": command isn't supported"); + } + } + + private void cat(String[] args) throws CommandArgumentSyntaxException, + ConsoleUtilityRuntimeException { + if (args.length != 1) { + throw new CommandArgumentSyntaxException("cat: expected 1 argument, found " + args.length); + } + try { + Path filePath = workingDirectory.resolve(Paths.get(args[0])).toRealPath(); + try (BufferedReader reader = Files.newBufferedReader(filePath)) { + String line; + while ((line = reader.readLine()) != null) { + System.out.println(line); + } + } catch (AccessDeniedException e) { + throw new ConsoleUtilityRuntimeException("cat: '" + args[0] + "': Permission denied", e); + } catch (IOException e) { + throw new ConsoleUtilityRuntimeException("cat: something really bad happened " + e.toString(), e); + } + } catch (IOException e) { + throw new ConsoleUtilityRuntimeException("cat: " + args[0] + "': No such file or directory", e); + } + } + + private void cd(String[] args) throws CommandArgumentSyntaxException, + ConsoleUtilityRuntimeException { + if (args.length != 1) { + throw new CommandArgumentSyntaxException("cd: expected 1 argument, found " + args.length); + } + try { + Path destinationPath = Paths.get(args[0]); + Path newWorkingDirectory = workingDirectory.resolve(destinationPath); + newWorkingDirectory = newWorkingDirectory.toRealPath(); + if (!Files.isDirectory(newWorkingDirectory)) { + throw new ConsoleUtilityRuntimeException("cd: '" + args[0] + "': Not a directory"); + } + workingDirectory = newWorkingDirectory; + } catch (AccessDeniedException e) { + throw new ConsoleUtilityRuntimeException("cd: '" + args[0] + "': Permission denied", e); + } catch (NoSuchFileException | InvalidPathException e) { + throw new ConsoleUtilityRuntimeException("cd: '" + args[0] + "': No such file or directory", e); + } catch (IOException e) { + throw new ConsoleUtilityRuntimeException("cd: something really bad happened " + e.toString(), e); + } + } + + private void cp(String[] args) throws CommandArgumentSyntaxException, ConsoleUtilityRuntimeException { + if ((args.length < 2) || (args.length > 3) || ((args.length == 3) && !(args[0].equals("-r")))) { + throw new CommandArgumentSyntaxException("cp: Invalid syntax, usage: cp [-r] "); + } + boolean recursively = (args.length == 3); + try { + Path source = workingDirectory.resolve(Paths.get((args.length == 3) ? args[1] : args[0])).toRealPath(); + Path target = workingDirectory.resolve(Paths.get((args.length == 3) ? args[2] : args[1])); + + if (Files.exists(target) && source.equals(target.toRealPath())) { + throw new ConsoleUtilityRuntimeException("cp:" + source + + " and " + target + + " are identical (not copied)"); + } + + if (Files.isDirectory(source)) { + if (recursively) { + if (Files.exists(target)) { + if (Files.isDirectory(target)) { + Files.walkFileTree(source, new CpFileVisitor(source, target.resolve(source.getFileName()))); + } else { + throw new ConsoleUtilityRuntimeException("cp:" + args[2] + ": Not a directory"); + } + } else { + Files.walkFileTree(source, new CpFileVisitor(source, target)); + } + } else { + throw new ConsoleUtilityRuntimeException("cp:" + args[0] + ": Is a directory"); + } + } else { + if (Files.exists(target) && Files.isDirectory(target)) { + Files.copy(source, target.resolve(source.getFileName()), REPLACE_EXISTING); + } else { + Files.copy(source, target, REPLACE_EXISTING); + } + } + } catch (NoSuchFileException e) { + throw new ConsoleUtilityRuntimeException("cp: " + e.getMessage() + ": No such file or directory", e); + } catch (AccessDeniedException e) { + throw new ConsoleUtilityRuntimeException("cp: " + e.getMessage() + ": Permission denied", e); + } catch (IOException | SecurityException e) { + throw new ConsoleUtilityRuntimeException("cp: something really bad happened " + e.toString(), e); + } + + } + + private void exit(String[] args) throws CommandArgumentSyntaxException { + if (args.length != 0) { + throw new CommandArgumentSyntaxException("exit: too many arguments"); + } + System.exit(0); + } + + private void ls(String[] args) throws CommandArgumentSyntaxException, + ConsoleUtilityRuntimeException { + if (args.length != 0) { + throw new CommandArgumentSyntaxException("ls: too many arguments"); + } + try (DirectoryStream stream = Files.newDirectoryStream(workingDirectory)) { + for (Path file : stream) { + System.out.println(file.getFileName()); + } + } catch (NoSuchFileException e) { + throw new ConsoleUtilityRuntimeException("ls: Working directory doesn't exist", e); + } catch (AccessDeniedException e) { + throw new ConsoleUtilityRuntimeException("ls: Permission denied", e); + } catch (IOException | DirectoryIteratorException e) { + throw new ConsoleUtilityRuntimeException("ls: something really bad happened " + e.toString(), e); + } + } + + private void mkdir(String[] args) throws CommandArgumentSyntaxException, + ConsoleUtilityRuntimeException { + if (args.length != 1) { + throw new CommandArgumentSyntaxException("mkdir: expected 1 argument, found " + args.length); + } + Path newDirectoryPath = workingDirectory.resolve(Paths.get(args[0])); + try { + Files.createDirectory(newDirectoryPath); + } catch (FileAlreadyExistsException e) { + throw new ConsoleUtilityRuntimeException("mkdir: " + args[0] + ": File exists", e); + } catch (AccessDeniedException e) { + throw new ConsoleUtilityRuntimeException("mkdir: " + args[0] + ": Permission denied", e); + } catch (NoSuchFileException e) { + throw new ConsoleUtilityRuntimeException("mkdir: " + + Paths.get(args[0]).getParent() + + ": No such file or directory", e); + } catch (IOException | SecurityException e) { + throw new ConsoleUtilityRuntimeException("mkdir: something really bad happened " + e.toString(), e); + } + } + + private void mv(String[] args) throws CommandArgumentSyntaxException, ConsoleUtilityRuntimeException { + if (args.length != 2) { + throw new CommandArgumentSyntaxException("mv: expected 2 arguments, found " + args.length); + } + try { + Path source = workingDirectory.resolve(Paths.get(args[0])).toRealPath(); + Path target = workingDirectory.resolve(Paths.get(args[1])); + + if (Files.exists(target) && source.equals(target.toRealPath())) { + return; + } + + if (Files.isDirectory(source)) { + if (Files.exists(target)) { + if (Files.isDirectory(target)) { + Files.walkFileTree(source, new MvFileVisitor(source, target.resolve(source.getFileName()))); + } else { + throw new ConsoleUtilityRuntimeException("mv: rename '" + + args[0] + "' to '" + args[1] + + ": Not a directory"); + } + } else { + Files.walkFileTree(source, new MvFileVisitor(source, target)); + } + } else { + if (Files.exists(target) && Files.isDirectory(target)) { + Files.move(source, target.resolve(source.getFileName()), REPLACE_EXISTING); + } else { + Files.move(source, target, REPLACE_EXISTING); + } + } + } catch (NoSuchFileException e) { + throw new ConsoleUtilityRuntimeException("mv: " + e.getMessage() + ": No such file or directory", e); + } catch (AccessDeniedException e) { + throw new ConsoleUtilityRuntimeException("mv: " + e.getMessage() + ": Permission denied", e); + } catch (IOException | SecurityException e) { + throw new ConsoleUtilityRuntimeException("mv: something really bad happened " + e.toString(), e); + } + } + + private void pwd(String[] args) throws CommandArgumentSyntaxException { + if (args.length != 0) { + throw new CommandArgumentSyntaxException("pwd: too many arguments"); + } + System.out.println(workingDirectory); + } + + private void rm(String[] args) throws CommandArgumentSyntaxException, ConsoleUtilityRuntimeException { + if ((args.length == 0) || (args.length > 2) || ((args.length == 2) && !(args[0].equals("-r")))) { + throw new CommandArgumentSyntaxException("rm: Invalid syntax, usage: rm [-r] "); + } + boolean recursively = (args.length == 2); + try { + Path target = workingDirectory.resolve(Paths.get((args.length == 2) ? args[1] : args[0])).toRealPath(); + if (!Files.isDirectory(target)) { + Files.delete(target); + } else { + if (recursively) { + Files.walkFileTree(target, new RmFileVisitor()); + } else { + throw new ConsoleUtilityRuntimeException("rm: " + args[0] + ": Is a directory"); + } + } + } catch (NoSuchFileException e) { + throw new ConsoleUtilityRuntimeException("rm: " + e.getMessage() + ": No such file or directory", e); + } catch (AccessDeniedException e) { + throw new ConsoleUtilityRuntimeException("rm: " + e.getMessage() + ": Permission denied", e); + } catch (IOException | SecurityException e) { + throw new ConsoleUtilityRuntimeException("rm: something really bad happened " + e.toString(), e); + } + } +} diff --git a/src/ru/fizteh/fivt/students/Oktosha/Shell/package-info.java b/src/ru/fizteh/fivt/students/Oktosha/Shell/package-info.java new file mode 100644 index 000000000..47dc1c153 --- /dev/null +++ b/src/ru/fizteh/fivt/students/Oktosha/Shell/package-info.java @@ -0,0 +1,5 @@ +/** + * Created by Daria Kolodzey from group #394. + * Shell package is my solution for the first task. + */ +package ru.fizteh.fivt.students.Oktosha.Shell;