Advertisement
columna1

Tar Program CC

Apr 16th, 2014
805
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.02 KB | None | 0 0
  1. --[[ MADE BY COLUMNA1 PLEASE DONT DISTRIBUTE WITHOUT PERMISSION ]]--
  2.  
  3. --update code
  4. local err = true
  5.  
  6. if http then
  7.   local version = "1.1"
  8.   local data = version
  9.   local up = http.get("http://pastebin.com/raw.php?i=PCjGdaM5")
  10.   if up then
  11.     data = up.readAll()
  12.     up.close()
  13.   else
  14.     print("Error grabbing update data.")
  15.     data = nil
  16.   end
  17.   if data and version ~= data then
  18.     print("Updating...")
  19.     local pro = shell.getRunningProgram()
  20.     local prog = http.get("http://pastebin.com/raw.php?i=bcgmNT2Q")
  21.     if prog then
  22.       local data2 = prog.readAll()
  23.       prog.close()
  24.       fs.delete(pro)
  25.       file = fs.open(pro,"w")
  26.       file.write(data2)
  27.       file.close()
  28.       err = false
  29.       print("Finished updating please run again.")
  30.     else
  31.       print("Problem grabbing update.")
  32.     end
  33.   end
  34. end
  35.  
  36.  
  37. local arg = {...}
  38.  
  39. local function printUsage()
  40.   term.setTextColor(colors.red)
  41.   print("Usage: \"tar c directory file.tar\" or \"tar u file\"")
  42.   print("THIS PROGRAM USES ABSOLUTE PATHS!!")
  43.   term.setTextColor(colors.white)
  44.   err = false
  45. end
  46.  
  47.  
  48. if #arg < 1 and err then
  49.   printUsage()
  50. end
  51.  
  52. if err then
  53.   if string.lower(arg[1]) == "u" or string.lower(arg[1]) == "c" then
  54.     if string.lower(arg[1]) == "c" and arg[2] then
  55.       if arg[2] == "/" then
  56.         arg[2] = ""
  57.       else
  58.         if not fs.isDir(arg[2]) then
  59.           err = false
  60.           printUsage()
  61.           error("Argument isnt a directory")
  62.         end
  63.         if fs.exists(arg[3]) or not arg[3] then
  64.           err = false
  65.           printUsage()
  66.           error("No destination provided/destination exists")
  67.         end
  68.       end
  69.     elseif string.lower(arg[1]) == "u" and arg[2] then
  70.       if not fs.exists(arg[2]) then
  71.         err = false
  72.         print("no file found")
  73.         printUsage()
  74.       end
  75.     else
  76.       err = false
  77.       printUsage()
  78.     end
  79.   else
  80.       err = false
  81.       printUsage()
  82.   end
  83. end
  84.  
  85. local function unPack()
  86.   local blank = 0
  87.   local block = 0
  88.   local curbyte = 0
  89.   local file = fs.open(arg[2],"rb")
  90.  
  91.   local function readByte() curbyte = curbyte + 1 if curbyte == 512 then curbyte = 0 ; block = block + 1 end return file:read(1) end
  92.   local function readString(num) local str = "" ; for i = 1,num do local byt = readByte() ; if byt ~= 0 then str = str..string.char(byt) end end; return str end
  93.   local function readBytes(num) local tab = {} ; for i = 1,num do tab[#tab+1] = readByte() end return tab end
  94.   --function octal_bytes_to_int(str,size) size = size + 1 ; local out = 0 ; local pos = size - 1 ; while pos > 0 do if str[size-pos] ~= 32 and str[size-pos] ~= 0 then out = out * 8 + tonumber(string.char(str[size-pos])) end pos = pos - 1 end return out end
  95.   local function octal_to_int(str,size) return tonumber(str:sub(0,size),8) or "" end
  96.   local function afterFileSkip() if curbyte > 0 then readString(512-curbyte) ; curbyte = 0 ; block = block + 1 end end
  97.   local function dumpFile(nam,size) local fi = fs.open("/"..nam,"wb") if not fi then print("ERROR NO FILE") end for i = 1,size do fi.write(readByte()) end fi.close() ; afterFileSkip() end
  98.  
  99.   --read headder
  100.   local dump = ""
  101.   local function readFileHeader()
  102.     local name = readString(100)
  103.     local mode = readBytes(8)
  104.     local uid = readBytes(8)
  105.     local gid = readBytes(8)
  106.     local size = readString(12)
  107.     local mtime = readBytes(12)
  108.     local chksum = readBytes(8)
  109.     local linkflag = readByte()
  110.     local linkname = readString(100)
  111.     local magic = readBytes(6)
  112.     local version = readBytes(2)
  113.     local uname = readString(32)
  114.     local gname = readString(32)
  115.     local devmajor = readBytes(8)
  116.     local devminor = readBytes(8)
  117.     afterFileSkip()
  118.     if name ~= "" then print("extracting: "..name) end
  119.     if tonumber(string.char(linkflag)) == 5 and not fs.exists("/"..name) then
  120.       fs.makeDir("/"..name)
  121.     elseif tonumber(string.char(linkflag)) == 0 then
  122.       dumpFile(name,octal_to_int(size,11))
  123.     end
  124.     local gotit = false
  125.     if name == "" and uname == "" and gname == "" then
  126.       gotit = true
  127.       if blank == 0 then
  128.         blank = 1
  129.       elseif blank == 1 then
  130.         blank = 2
  131.       end
  132.     end
  133.     if gotit == false then
  134.       blank = 0
  135.     end
  136.     name = ""
  137.   end
  138.   blank = 0
  139.   while blank ~=2 do
  140.     readFileHeader()
  141.     --os.pullEvent()
  142.     sleep(0.05)
  143.   end
  144.   file.close()
  145. end
  146.  
  147. local function pack()
  148.   local function getFileSize(file)
  149.     --try fs.getSize and if it is > 512 then return else read manually
  150.     local size = fs.getSize(file)
  151.     if size < 513 then
  152.       --read file manually
  153.       size = 0
  154.       local curf = fs.open(file,"rb")
  155.       local n = curf.read(1)
  156.       while n do
  157.         size = size + 1
  158.         n = curf.read(1)
  159.       end
  160.       curf.close()
  161.       size = size + #fs.getName(file)
  162.     end
  163.     return size
  164.   end
  165.   --get time
  166.   local time
  167.   if not http then
  168.     Print("Please enabe the http api")
  169.     Print("Using an old time as a fall back")
  170.     time = 1397584826
  171.   else
  172.     print("grabbing time...")
  173.     local res = http.get("http://www.timeapi.org/utc/now?format=%25s")
  174.     time = tonumber(res.readAll())
  175.     res.close()
  176.   end
  177.   --local time = "1397584826"
  178.   print("done")
  179.   --you have to go through each folder first and make those
  180.   local file = fs.open(arg[3],"wb")
  181.   local folders = {}
  182.   local entries = {}
  183.   local function getFolders(path,folder)
  184.     local files = fs.list(path..folder)
  185.     folders[#folders+1] = path..folder
  186.     local curfolders = {}
  187.     for i = 1,#files do
  188.       if fs.isDir(path..folder.."/"..files[i]) then
  189.         curfolders[#curfolders+1] = files[i]
  190.       else
  191.         entries[#entries+1] = path..folder.."/"..files[i]
  192.       end
  193.     end
  194.     for i = 1,#curfolders do
  195.       getFolders(path..folder.."/",curfolders[i])
  196.     end
  197.   end
  198.   --file writing helper functions
  199.   local fileBuffer = {}
  200.   local function flushFile() for i = 1,#fileBuffer do file.write(fileBuffer[i]) end if #fileBuffer%512 ~= 0 then for i = 1, 512-(#fileBuffer%512) do file.write(0) end end fileBuffer = {} end
  201.   local function writeByte(byte) fileBuffer[#fileBuffer+1] = byte end
  202.   local function writeString(str,size) for i = 1,#str do writeByte(string.byte(str:sub(i,i))) end for i = 1,size-#str do writeByte(0) end end
  203.   local function writeCharBytes(tab) for i = 1,#tab do writeByte(string.byte(tab[i])) end end
  204.   local function writeBytes(tab) for i = 1,#tab do writeByte(tab[i]) end end
  205.   local function basen(n,b) n = math.floor(n) if not b or b == 10 then return tostring(n) end local digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" ; local t = {} ; local sign = "" if n < 0 then sign = "-" ; n = -n end repeat local d = (n % b) + 1 ; n = math.floor(n / b) ; table.insert(t, 1, digits:sub(d,d)) until n == 0 return sign .. table.concat(t,"") end
  206.   local function writeOctal(num,size) local str = basen(num,8) ; str = string.rep(" ",(size-1)-#str)..str.." " ; writeString(tostring(str),12) end
  207.  
  208.   local function addFile(isdir,file)
  209.     --make a standard file header
  210.     if isdir then
  211.       writeString(file.."/",100)--name
  212.       writeCharBytes({" "," "," ","7","5","5"," ",string.char(0)})
  213.       writeCharBytes({" "," "," "," "," ","0"," ",string.char(0)," "," "," "," "," ","0"," ",string.char(0)})
  214.       writeOctal(0,12)--size
  215.       writeOctal(time,12)--mtime
  216.       writeString("       ",8)--chksum
  217.       writeByte(string.byte("5"))
  218.     else
  219.       writeString(file,100)--name
  220.       writeCharBytes({" "," "," ","6","4","4"," ",string.char(0)})
  221.       writeCharBytes({" "," "," "," "," ","0"," ",string.char(0)," "," "," "," "," ","0"," ",string.char(0)})
  222.       writeOctal(getFileSize(file)-#fs.getName(file),12)--size
  223.       writeOctal(time,12)--mtime
  224.       writeString("       ",8)--chksum
  225.       writeByte(string.byte("0"))
  226.     end
  227.     writeString("",108)--linkname, magic, and version
  228.     writeString("ComputerCraft",32)
  229.     writeString("CC",32)
  230.   end
  231.   local str = ""
  232.  
  233.   getFolders("",arg[2])
  234.   for i = 1,#folders do
  235.     addFile(true,folders[i])
  236.     --calculate and add checksum
  237.     local sum = 0
  238.     for i = 1,#fileBuffer do
  239.       sum = sum + fileBuffer[i]
  240.     end
  241.     str = basen(sum+32,8)
  242.     str = string.rep(" ",(7-1)-#str)..str.." "
  243.     for i = 1,7 do
  244.       fileBuffer[i+148] = string.byte(str:sub(i,i))
  245.     end
  246.     flushFile()
  247.   end
  248.  
  249.   for i = 1,#entries do
  250.     addFile(false,entries[i])
  251.     --calculate and add checksum
  252.     local sum = 0
  253.     for j = 1,#fileBuffer do
  254.       sum = sum + fileBuffer[j]
  255.     end
  256.     str = basen(sum+32,8)
  257.     str = string.rep(" ",(7-1)-#str)..str.." "
  258.     for j = 1,7 do
  259.       fileBuffer[j+148] = string.byte(str:sub(j,j))
  260.     end
  261.     flushFile()
  262.     --add the file contents
  263.     local curfile = fs.open(entries[i],"rb")
  264.     print(entries[i])
  265.     for j = 1,getFileSize(entries[i]) do
  266.       writeByte(curfile.read(1))
  267.     end
  268.     curfile.close()
  269.     flushFile()
  270.     sleep(0.05)
  271.   end
  272.   writeByte(0)
  273.   flushFile()
  274.   writeByte(0)
  275.   flushFile()
  276.   file.close()
  277. end
  278.  
  279. if err then
  280.   --actually do stuffs
  281.   if string.lower(arg[1]) == "u" then
  282.     unPack()
  283.   elseif string.lower(arg[1]) == "c" then
  284.     pack()
  285.   end
  286. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement