Advertisement
ZNZNCOOP

BackupSystem

Dec 20th, 2015
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.55 KB | None | 0 0
  1. local max = 2^32 -1
  2.  
  3. local CRC32 = {
  4.     0,79764919,159529838,222504665,319059676,
  5.     398814059,445009330,507990021,638119352,
  6.     583659535,797628118,726387553,890018660,
  7.     835552979,1015980042,944750013,1276238704,
  8.     1221641927,1167319070,1095957929,1595256236,
  9.     1540665371,1452775106,1381403509,1780037320,
  10.     1859660671,1671105958,1733955601,2031960084,
  11.     2111593891,1889500026,1952343757,2552477408,
  12.     2632100695,2443283854,2506133561,2334638140,
  13.     2414271883,2191915858,2254759653,3190512472,
  14.     3135915759,3081330742,3009969537,2905550212,
  15.     2850959411,2762807018,2691435357,3560074640,
  16.     3505614887,3719321342,3648080713,3342211916,
  17.     3287746299,3467911202,3396681109,4063920168,
  18.     4143685023,4223187782,4286162673,3779000052,
  19.     3858754371,3904687514,3967668269,881225847,
  20.     809987520,1023691545,969234094,662832811,
  21.     591600412,771767749,717299826,311336399,
  22.     374308984,453813921,533576470,25881363,
  23.     88864420,134795389,214552010,2023205639,
  24.     2086057648,1897238633,1976864222,1804852699,
  25.     1867694188,1645340341,1724971778,1587496639,
  26.     1516133128,1461550545,1406951526,1302016099,
  27.     1230646740,1142491917,1087903418,2896545431,
  28.     2825181984,2770861561,2716262478,3215044683,
  29.     3143675388,3055782693,3001194130,2326604591,
  30.     2389456536,2200899649,2280525302,2578013683,
  31.     2640855108,2418763421,2498394922,3769900519,
  32.     3832873040,3912640137,3992402750,4088425275,
  33.     4151408268,4197601365,4277358050,3334271071,
  34.     3263032808,3476998961,3422541446,3585640067,
  35.     3514407732,3694837229,3640369242,1762451694,
  36.     1842216281,1619975040,1682949687,2047383090,
  37.     2127137669,1938468188,2001449195,1325665622,
  38.     1271206113,1183200824,1111960463,1543535498,
  39.     1489069629,1434599652,1363369299,622672798,
  40.     568075817,748617968,677256519,907627842,
  41.     853037301,1067152940,995781531,51762726,
  42.     131386257,177728840,240578815,269590778,
  43.     349224269,429104020,491947555,4046411278,
  44.     4126034873,4172115296,4234965207,3794477266,
  45.     3874110821,3953728444,4016571915,3609705398,
  46.     3555108353,3735388376,3664026991,3290680682,
  47.     3236090077,3449943556,3378572211,3174993278,
  48.     3120533705,3032266256,2961025959,2923101090,
  49.     2868635157,2813903052,2742672763,2604032198,
  50.     2683796849,2461293480,2524268063,2284983834,
  51.     2364738477,2175806836,2238787779,1569362073,
  52.     1498123566,1409854455,1355396672,1317987909,
  53.     1246755826,1192025387,1137557660,2072149281,
  54.     2135122070,1912620623,1992383480,1753615357,
  55.     1816598090,1627664531,1707420964,295390185,
  56.     358241886,404320391,483945776,43990325,
  57.     106832002,186451547,266083308,932423249,
  58.     861060070,1041341759,986742920,613929101,
  59.     542559546,756411363,701822548,3316196985,
  60.     3244833742,3425377559,3370778784,3601682597,
  61.     3530312978,3744426955,3689838204,3819031489,
  62.     3881883254,3928223919,4007849240,4037393693,
  63.     4100235434,4180117107,4259748804,2310601993,
  64.     2373574846,2151335527,2231098320,2596047829,
  65.     2659030626,2470359227,2550115596,2947551409,
  66.     2876312838,2788305887,2733848168,3165939309,
  67.     3094707162,3040238851,2985771188,
  68. }
  69.  
  70. local function xor(a, b)
  71.     local calc = 0    
  72.  
  73.     for i = 32, 0, -1 do
  74.     local val = 2 ^ i
  75.     local aa = false
  76.     local bb = false
  77.  
  78.     if a == 0 then
  79.         calc = calc + b
  80.         break
  81.     end
  82.  
  83.     if b == 0 then
  84.         calc = calc + a
  85.         break
  86.     end
  87.  
  88.     if a >= val then
  89.         aa = true
  90.         a = a - val
  91.     end
  92.  
  93.     if b >= val then
  94.         bb = true
  95.         b = b - val
  96.     end
  97.  
  98.     if not (aa and bb) and (aa or bb) then
  99.         calc = calc + val
  100.     end
  101.     end
  102.  
  103.     return calc
  104. end
  105.  
  106. local function lshift(num, left)
  107.     local res = num * (2 ^ left)
  108.     return res % (2 ^ 32)
  109. end
  110.  
  111. local function rshift(num, right)
  112.     local res = num / (2 ^ right)
  113.     return math.floor(res)
  114. end
  115.  
  116. function hash(str)
  117.     local count = string.len(tostring(str))
  118.     local crc = max
  119.    
  120.     local i = 1
  121.     cc = 0
  122.     while count > 0 do
  123.     cc = cc + 1
  124.     if(cc == 500) then
  125.         os.sleep(0)
  126.         cc = 0
  127.     end
  128.     local byte = string.byte(str, i)
  129.  
  130.     crc = xor(lshift(crc, 8), CRC32[xor(rshift(crc, 24), byte) + 1])
  131.  
  132.     i = i + 1
  133.     count = count - 1
  134.     end
  135.  
  136.     return crc
  137. end
  138.  
  139. local fs = require("filesystem")
  140. local os = require("os")
  141. local term = require("term")
  142. local gpu = require("component").gpu
  143.  
  144. local function log(message, level)
  145.     if(gpu.maxDepth() > 1) then
  146.         if(level == 1) then
  147.             gpu.setForeground(0x00FF00)
  148.             print(message)
  149.         end
  150.         if(level == 2) then
  151.             gpu.setForeground(0xFFCC33)
  152.             print(message)
  153.         end
  154.         if(level == 3) then
  155.             gpu.setForeground(0xFF0000)
  156.             print(message)
  157.         end
  158.         gpu.setForeground(0xFFFFFF)
  159.     else
  160.         print(message)
  161.     end
  162. end
  163.  
  164.  
  165. local function getFileList(path, list)
  166.    count = 0
  167.    if path == "mnt/" then return end
  168.    print("Geting files in directory "..path)
  169.    for k in fs.list(path) do
  170.       count = count + 1
  171.       if count == 100 then
  172.          os.sleep(0)
  173.          count = 0
  174.       end
  175.       if(fs.isDirectory(path..k)) then
  176.          getFileList(path..k, list)
  177.       else
  178.          list[#list + 1] = path..k
  179.       end
  180.    end
  181.    return list
  182. end
  183.  
  184. local function getStrFile(file)
  185.     str = ""
  186.     count = 0
  187.     if(fs.exists(file)) then
  188.         for line in io.lines(file) do
  189.             count = count + 1
  190.             if(count == 10) then
  191.                 os.sleep(0)
  192.                 count = 0
  193.             end
  194.             str = str..line.."\n"
  195.         end
  196.     end
  197.     return str
  198. end
  199.  
  200. local function getCRCFiles(list)
  201.     inf = {}
  202.     print()
  203.     print("Geting checksums from files list")
  204.     x, y = term.getCursor()
  205.     print()
  206.     for i = 1, #list do
  207.         str = getStrFile(list[i])
  208.         checksum = hash(str)
  209.         inf[#inf + 1] = {}
  210.         inf[#inf].path = list[i]
  211.         inf[#inf].checksum = checksum
  212.         term.setCursor(x, y)
  213.             per = i/(#list/50)
  214.             for z = 1, per do
  215.                 io.write("#")
  216.             end
  217.             io.write(" "..math.floor(per*2).."%")
  218.             term.setCursor(x, y + 1)
  219.             term.clearLine()
  220.             io.write(list[i])
  221.     end
  222.     print()
  223.     print()
  224.     return inf
  225. end
  226.  
  227. local function saveInf()
  228.     print("Save inf in crcfiles.inf")
  229.     print()
  230.     list = getFileList("", {})
  231.     inf = getCRCFiles(list)
  232.     f = io.open("crcfiles.inf", "w")
  233.     count = 0
  234.     x, y = term.getCursor()
  235.     for i=1, #inf do
  236.         count = count + 1
  237.         if(count == 100) then
  238.             count = 0
  239.             os.sleep(0)
  240.         end
  241.         f:write(inf[i].path.." "..inf[i].checksum.." ")
  242.         per = i/(#inf/50)
  243.         term.setCursor(x, y)
  244.         for i = 1, per do
  245.             io.write("#")
  246.         end
  247.         io.write(" "..math.floor(per*2).."%")
  248.         term.setCursor(x, y + 1)
  249.         term.clearLine()
  250.         io.write(inf[i].path)
  251.     end
  252.     f:close()
  253.     print()
  254.     print("Save ended.")
  255.     print()
  256. end
  257.  
  258. local function loadinf()
  259.     print("Loading inf from crcfiles.inf")
  260.     local x, y = term.getCursor()
  261.     if(fs.exists("crcfiles.inf")) then
  262.         for line in io.lines("crcfiles.inf") do
  263.             local list = {}
  264.             inf = {}
  265.             for w in string.gmatch(line, "%S+") do
  266.                 list[#list + 1] = w
  267.             end
  268.             for i = 1, #list do
  269.                 if(i%2 ~= 0) then
  270.                     inf[#inf +1] = {}
  271.                     inf[#inf].path = list[i]
  272.                 else
  273.                     inf[#inf].checksum = list[i]+0
  274.                     term.setCursor(x, y)
  275.                     per = i/(#list/50)
  276.                     for i=1, per do
  277.                         io.write("#")
  278.                     end
  279.                     io.write(" "..math.floor(per*2).."%")
  280.                     term.setCursor(x, y + 1)
  281.                     term.clearLine()
  282.                     io.write(inf[#inf].path)
  283.                 end
  284.             end
  285.             print()
  286.             print("Loading ended.")
  287.             print()
  288.             return inf
  289.         end
  290.     else
  291.         error("crcfiles.inf not exists.")
  292.     end
  293. end
  294.  
  295. local function checkFiles()
  296.     print("Checking Files...")
  297.     list = getFileList("", {})
  298.     inf2 = getCRCFiles(list)
  299.     inff = loadinf()
  300.     f = io.open("bsys.log", "w")
  301.     for i=1, #inff do
  302.         path = inff[i].path
  303.         checksum = inff[i].checksum
  304.         find = true
  305.         if(not fs.exists(path)) then
  306.             str = path.." not found.".."\n"
  307.             log(str, 3)
  308.             f:write("\n")
  309.             f:write(str)
  310.             f:write("\n")
  311.             os.sleep(2)
  312.             find = false
  313.         end
  314.         if(find) then
  315.             for i=1, #inf2 do
  316.                 if(inf2[i].path == path) then
  317.                     if(inf2[i].checksum ~= checksum) then
  318.                         str = path.." editing.".."\n"
  319.                         log(str, 2)
  320.                         f:write("\n")
  321.                         f:write(str)
  322.                         f:write("\n")
  323.                         os.sleep(2)
  324.                     else
  325.                         log(path.." done.", 1)
  326.                         f:write(path.." done.".."\n")
  327.                     end
  328.                 end
  329.             end
  330.         end
  331.     end
  332.     f:close()
  333.     print("Log saved in bsys.log")
  334. end
  335.  
  336. local args = {...}
  337.  
  338. if(#args == 0) then
  339.     print("use check or save")
  340.     error("not argumens")
  341. end
  342.  
  343. if(#args == 1) then
  344.     if(args[1] == "check") then
  345.         checkFiles()
  346.     end
  347.     if(args[1] == "save") then
  348.         saveInf()
  349.     end
  350. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement