Skip to content

Commit

Permalink
Added cd command functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
pmihsan committed Jun 10, 2023
1 parent b323709 commit df3425a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
out
out
.idea
47 changes: 41 additions & 6 deletions src/CMD.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@
import java.util.Objects;

public class CMD {
public static boolean start = true;
public static void handleSocket(String[] cmd, DataInputStream in, DataOutputStream out, File dir) throws IOException{
if(cmd[0].equalsIgnoreCase("list")){
listDirectory(out, dir);
listDirectory(out, returnDirectory(dir));
}
else if(cmd[0].equalsIgnoreCase("get")){
for(int i=1;i<cmd.length;i++)
FTP.send(cmd[i], out, dir.getCanonicalPath());
FTP.send(cmd[i], out, returnDirectory(dir));
}
else if(cmd[0].equalsIgnoreCase("put")){
for(int i=1;i<cmd.length;i++)
FTP.receive(cmd[i], in, dir.getCanonicalPath());
FTP.receive(cmd[i], in, returnDirectory(dir));
}
else if(cmd[0].equalsIgnoreCase("cd")){
changeDirectory(cmd, out, dir);
}
else if(cmd[0].equalsIgnoreCase("pwd")){
out.writeUTF(dir.getCanonicalPath());
out.writeUTF(returnDirectory(dir));
}
else if(cmd[0].equalsIgnoreCase("whoami")){
out.writeUTF(System.getProperty("user.name"));
Expand All @@ -31,12 +35,43 @@ else if(cmd[0].equalsIgnoreCase("exit")){
}
}

public static void listDirectory(DataOutputStream out, File d) throws IOException {
String[] dirs = d.list();
public static void listDirectory(DataOutputStream out, String d) throws IOException {
File dir = new File(d);
String[] dirs = dir.list();
StringBuilder res = new StringBuilder();
for(int i = 0; i< Objects.requireNonNull(dirs).length; i++){
res.append(dirs[i]).append("\t");
}
out.writeUTF(res.toString());
}

public static void changeDirectory(String[] cmd, DataOutputStream out, File dir) throws IOException{
if(cmd.length > 1 && cmd[1] != null) {
if (cmd[1].equals("..")) {
String cwd = returnDirectory(dir);
String path = cwd.substring(0, cwd.lastIndexOf(HELPER.path_sep));
if(path.contains("jftp" + HELPER.path_sep + "share")) {
start = false;
System.setProperty("user.dir", path);
out.writeUTF("Current directory " + path);
}
else out.writeUTF("Unable to change directory");
return;
}
String path = dir.getAbsolutePath() + HELPER.path_sep + cmd[1];
File fp = new File(path);
if (fp.exists() && fp.isDirectory()) {
start = false;
System.setProperty("user.dir", fp.getAbsolutePath());
out.writeUTF("Directory changed to " + cmd[1]);
}
else out.writeUTF("Directory Invalid");
}
else out.writeUTF("Unable to change directory");
}

public static String returnDirectory(File dir){
if(start)return dir.getAbsolutePath();
else return System.getProperty("user.dir");
}
}

0 comments on commit df3425a

Please sign in to comment.