Browse Source

Added CurrentDir() and ChangeDirToParent()

Added more tests.
jlaffaye 14 years ago
parent
commit
6110501482
2 changed files with 48 additions and 2 deletions
  1. 27 2
      client_test.go
  2. 21 0
      ftp.go

+ 27 - 2
client_test.go

@@ -8,6 +8,7 @@ import (
 
 const (
 	testData = "Just some text"
+	testDir = "mydir"
 )
 
 func TestConn(t *testing.T) {
@@ -61,15 +62,39 @@ func TestConn(t *testing.T) {
 		t.Error(err)
 	}
 
-	err = c.MakeDir("mydir")
+	err = c.MakeDir(testDir)
 	if err != nil {
 		t.Error(err)
 	}
 
-	err = c.RemoveDir("mydir")
+	err = c.ChangeDir(testDir)
+	if err != nil {
+		t.Error(err)
+	}
+
+	dir, err := c.CurrentDir()
+	if err != nil {
+		t.Error(err)
+	} else {
+		if dir != "/" + testDir {
+			t.Error("Wrong dir: " + dir)
+		}
+	}
+
+	err = c.ChangeDirToParent()
+	if err != nil {
+		t.Error(err)
+	}
+
+	err = c.RemoveDir(testDir)
 	if err != nil {
 		t.Error(err)
 	}
 
 	c.Quit()
+
+	err = c.NoOp()
+	if err == nil {
+		t.Error("Expected error")
+	}
 }

+ 21 - 0
ftp.go

@@ -188,6 +188,27 @@ func (c *ServerConn) ChangeDir(path string) os.Error {
 	return err
 }
 
+func (c *ServerConn) ChangeDirToParent() os.Error {
+	_, _, err := c.cmd(StatusRequestedFileActionOK, "CDUP")
+	return err
+}
+
+func (c *ServerConn) CurrentDir() (string, os.Error) {
+	_, msg, err := c.cmd(StatusPathCreated, "PWD")
+	if err != nil {
+		return "", err
+	}
+
+	start := strings.Index(msg, "\"")
+	end := strings.LastIndex(msg, "\"")
+
+	if start == -1 || end == -1 {
+		return "", os.NewError("Unsuported PWD response format")
+	}
+
+	return msg[start+1:end], nil
+}
+
 // Retrieves a remote file
 func (c *ServerConn) Retr(path string) (io.ReadCloser, os.Error) {
 	conn, err := c.cmdDataConn("RETR %s", path)