             tmpDir.pop()
           tmpDir.push aPath
       retPath = tmpDir.join("/")
-    fs.readdirSync @baseDir+retPath
+    fs.statSync @baseDir+retPath
     retPath
   
   getNlst: (nowDir, reqPath, callback)->
-    retPath = @getNewPath nowDir, reqPath
-    fs.readdir @baseDir+retPath, callback
+    try
+      retPath = @getNewPath nowDir, reqPath
+      fs.readdir @baseDir+retPath, callback
+    catch err
+      callback(err)
   
   getList: (nowDir, reqPath, callback)->
-    retPath = @getNewPath nowDir, reqPath
-    exec "export LANG=en_US.UTF-8; ls -l #{@baseDir}#{retPath}", callback
+    try
+      retPath = @getNewPath nowDir, reqPath
+      exec "export LANG=en_US.UTF-8; ls -l #{@baseDir}#{retPath}", callback
+    catch err
+      callback(err)
+  
+  getFile: (nowDir, reqPath, callback)->
+    try
+      retPath = @getNewPath nowDir, reqPath
+      fs.readFile @baseDir+retPath, callback
+    catch err
+      callback(err)

      @end()
    
    "PASV": ()->
      socket = @
      socket.passive = true
      socket.dtpServer = net.createServer()
      dtpHost = socket.server.address().address
      socket.dtpServer.dataQueue = null
      
      socket.dtpServer.on 'listening', ()->
        dtpAddress = @address()
        console.log "Data Transfer Proccess Server listening on #{dtpAddress.address}:#{dtpAddress.port}"
        host  = dtpAddress.address.split(".").join(",")
        port1 = parseInt(dtpAddress.port / 256, 10)
        port2 = dtpAddress.port % 256
        socket.reply 227, "Entering Extended Passive Mode (#{host},#{port1},#{port2})"
      
      socket.dtpServer.on "connection", (dtpSocket)->
        console.log "DTP Connect from #{dtpSocket.remoteAddress}"
        dtpSocket.setTimeout(0)
        dtpSocket.setNoDelay()
        dtpSocket.dataEncoding = "binary"
        
        dtpSocket.on "end", ()->
            socket.dtpServer.close()
            socket.reply 226
        dtpSocket.on "close", ()->
          console.log "DTP Socket closed"
        dtpSocket.on "connect", ()->
          console.log "DTP Socket connect"
        dtpSocket.on 'data', (recData)->
          console.log recData.toString().trim()
        
        dtpSocket.sender = ()->
          console.log socket.dtpServer.dataQueue
          if socket.dtpServer.dataQueue isnt null
            console.log "Send"
            clearInterval(dtpSocket.intervalId)
            dtpSocket.end(socket.dtpServer.dataQueue)
        
        dtpSocket.intervalId = setInterval(dtpSocket.sender, 100)
      
      socket.dtpServer.on "close", ()->
        console.log "Data Transfer Proccess Server closed"
        socket.passive = false
        
      socket.dtpServer.listen(0, dtpHost)
    
    "NOOP": ()->
      @reply 200

        CWD     QUIT    PASV    NOOP    HELP
        Direct comments to root
        """
  
  dtpServer = (socket)->
    dtp = net.createServer()
    
    dtp.on 'listening', ()->
      dtpAddress = @address()
      console.log "Data Transfer Proccess Server listening on #{dtpAddress.address}:#{dtpAddress.port}"
      host  = dtpAddress.address.split(".").join(",")
      port1 = parseInt(dtpAddress.port / 256, 10)
      port2 = dtpAddress.port % 256
      socket.reply 227, "Entering Extended Passive Mode (#{host},#{port1},#{port2})"
    
    dtp.on "close", ()->
      console.log "Data Transfer Proccess Server closed"
      socket.passive = false
    
    dtp.on "connection", (dtpSocket)->
      @dtpSocket = dtpSocket
      console.log "DTP Connect from #{dtpSocket.remoteAddress}"
      dtpSocket.setTimeout(0)
      dtpSocket.setNoDelay()
      dtpSocket.dataEncoding = "binary"
      
      dtpSocket.on "end", ()->
          socket.dtpServer.close()
          socket.reply 226
      dtpSocket.on "close", ()->
        console.log "DTP Socket closed"
      dtpSocket.on "connect", ()->
        console.log "DTP Socket connect"
      dtpSocket.on 'data', (recData)->
        console.log recData.toString().trim()
      
      dtpSocket.sender = (dataQueue)->
        socket.reply 150
        console.log "DTP Send"
        dtpSocket.end(dataQueue)
    dtp


class BabyFtpd.FileSystem
