From 8547b51753198c8f09708f74889880c8712dd32c Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Thu, 30 Apr 2015 18:25:44 +0300 Subject: [PATCH] add mdtm command closes #5 Signed-off-by: Vasiliy Tolstov --- main.go | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/main.go b/main.go index 1c88ce3..38803b5 100644 --- a/main.go +++ b/main.go @@ -7,7 +7,9 @@ import ( "path/filepath" "strconv" "strings" + "time" + "github.com/jehiah/go-strftime" "github.com/ncw/swift" ) @@ -49,6 +51,8 @@ func handle(c net.Conn) { cmd := parts[0] params := strings.Join(parts[1:], "") switch cmd { + case "MDTM": + s.cmdServerMdtm(params) case "ABOR": s.cmdServerAbor(params) case "USER": @@ -626,3 +630,40 @@ func (s *Conn) cmdServerAbor(args string) { } s.ctrl.PrintfLine(`226 Closing data connection`) } + +func (s *Conn) cmdServerMdtm(args string) { + fmt.Printf("cmdServerMdtm: %s\n", args) + + if s.path == "/" && (len(args) == 0 || args == "/") { + s.ctrl.PrintfLine("213 %s", strftime.Format("%Y%m%d%H%M%S", time.Now())) + return + } + + p := "" + cnt := "" + + if args[0] == '/' { + cnt = strings.Split(args, "/")[1] + p = strings.Join(strings.Split(args, "/")[2:], "/") + } else { + if s.path != "/" { + cnt = strings.Split(s.path, "/")[1] + p = filepath.Clean(filepath.Join(strings.Join(strings.Split(s.path, "/")[2:], "/"), args)) + } else { + cnt = strings.Split(args, "/")[0] + p = filepath.Clean(strings.Join(strings.Split(args, "/")[1:], "/")) + } + } + fmt.Printf("cnt: %s p: %s\n", cnt, p) + if p != "" { + obj, _, err := s.sw.Object(cnt, p) + if err != nil { + fmt.Printf(err.Error()) + s.ctrl.PrintfLine("501 Error") + return + } + s.ctrl.PrintfLine("213 %s", strftime.Format("%Y%m%d%H%M%S", obj.LastModified)) + } else { + s.ctrl.PrintfLine("550 Could not get file mod time.") + } +}