Advertisement
ZNZNCOOP

ftp server

Dec 22nd, 2015
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.31 KB | None | 0 0
  1. modem  = require('component').modem
  2. event  = require('event')
  3. computer=require("computer")
  4. fs=require("component").proxy(computer.getBootAddress())
  5. ser=require("serialization").serialize
  6.  
  7. ftpname, ftppath= ...
  8. if not ftpname then
  9.   print("Использование: ftp_serv <server_name> [<shared_folder>]")
  10.   return
  11. end
  12. ftppath=ftppath or "/"
  13. if ftppath:sub(-1)~="/" then ftppath=ftppath.."/" end
  14.  
  15. if not fs.isDirectory(ftppath) then
  16.   print("Дирректория "..ftppath.." не найдена")
  17.   return
  18. end
  19.  
  20. local eventname, sendID, comm, p1,p2
  21.  
  22. if not modem then
  23.   print("Сетевая карта не найдена")
  24.   return
  25. end
  26. FTPport = 21
  27. modem.open(FTPport)
  28. files={}
  29. ftpfs={
  30.   isReadOnly=fs.isReadOnly,
  31.   spaceTotal= fs.spaceTotal,
  32.   spaceUsed= fs.spaceUsed,
  33.   getLabel=function() return ftpname end,
  34.  
  35.   makeDirectory=function(path)
  36.     return fs.makeDirectory(ftppath..path)
  37.   end,
  38.  
  39.   isDirectory=function(path)
  40.     return fs.isDirectory(ftppath..path)
  41.   end,
  42.  
  43.   exists=function(path)
  44.     return fs.exists(ftppath..path)
  45.   end,
  46.  
  47.   remove=function(path)
  48.     return fs.remove(ftppath..path)
  49.   end,
  50.  
  51.   lastModified=function(path)
  52.     return fs.lastModified(ftppath..path)
  53.   end,
  54.  
  55.   size=function(path)
  56.     return fs.size(ftppath..path)
  57.   end,
  58.  
  59. open=function(path, mode)
  60.   if files[sendID] then fs.close(files[sendID]) end
  61.   files[sendID]=fs.open(ftppath..path, mode)
  62.   return files[sendID]
  63. end,
  64.  
  65. rename=function(from, to)
  66.   return fs.rename(ftppath..from, ftppath..to)
  67. end,
  68.  
  69. list=function(path)
  70.   return ser(fs.list(ftppath..path))
  71. end,
  72.  
  73. write=function(handle, value)
  74.   return fs.write(handle, value)
  75. end,
  76.  
  77. read=function(handle, count)
  78.   if files[sendID]~=handle then return nil, "file is closed" end
  79.   if count>8000 then count=8000 end
  80.   return fs.read(handle, count)
  81. end,
  82.  
  83. seek=function(handle, whence, offset)
  84.   if files[sendID]~=handle then return nil, "file is closed" end
  85.   return fs.seek(handle, whence, offset)
  86. end,
  87.  
  88. close=function(handle)
  89.   files[sendID]=nil
  90.   return fs.close(handle)
  91. end,
  92. }
  93.  
  94. while true do
  95.   eventname, _,sendID, _, _, comm, p1,p2 = event.pull("modem_message")
  96.   if eventname=="modem_message" then
  97.     if ftpfs[comm] then
  98. --    print(sendID, comm, p1,p2)
  99.       modem.send(sendID,FTPport,comm,ftpfs[comm](p1,p2))
  100.     end
  101.   end
  102. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement