-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate.lua
More file actions
162 lines (149 loc) · 3.94 KB
/
update.lua
File metadata and controls
162 lines (149 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
-- Update --
if http == nil then
printError("HTTP is disabled.")
return
end
local tArgs = {...}
if #tArgs < 1 then
print("Usage: update server/pc")
return
end
tArgs[1] = string.lower(tArgs[1])
local tfg = term.getTextColor()
local URL = "http://pastebin.com/raw/"
local req = {
filename = "req",
url = URL.."4sFhyLC9",
handle = {},
dat = {},
}
req.handle.new = http.get(req.url)
req.handle.old = fs.open(req.filename,"r")
req.dat.new = {}
req.dat.old = {}
if req.handle.new then -- load new req
local l
repeat
l = req.handle.new.readLine()
if l and l ~= "" and l:sub(1,1) ~= "#" then
if l:sub(1,1) == "%" then
p = l:gsub("%%","",1)
elseif p == tArgs[1] or p == "all" then
table.insert(
req.dat.new,
{ -- info table
path = l, -- download path
ver = tonumber(req.handle.new.readLine()), -- version
code = req.handle.new.readLine(), -- pastebin code
}
)
end
end
until l == nil
else
printError("cannot connect to \""..URL.."\".")
return
end
if req.handle.old then -- load old req
local l
repeat
l = req.handle.old.readLine()
if l and l ~= "" and l:sub(1,1) ~= "#" then
if l:sub(1,1) == "%" then
p = l:gsub("%%","",1)
elseif p == tArgs[1] or p == "all" then
table.insert(
req.dat.old,
{ -- info table
path = l, -- download path
ver = tonumber(req.handle.old.readLine()), -- version
code = req.handle.old.readLine(), -- pastebin code
}
)
end
end
until l == nil
end
-- remove old unused files
for _,old in pairs(req.dat.old) do
local isReq = false
for _,new in pairs(req.dat.new) do
if new.path == old.path then
isReq = true
break
end
end
if not isReq then
fs.delete(old.path) -- delete old file
term.setTextColor(colors.red)
term.write("-Removed file ")
term.setTextColor(colors.lightBlue)
term.write(old.path)
term.setTextColor(colors.red)
print("")
end
end
-- update old required files
for _,new in pairs(req.dat.new) do
for _,old in pairs(req.dat.old) do
if old.path == new.path and old.ver < new.ver then -- filepaths match and newer version detected
local h,err = http.get(URL..new.code) -- get new code
if h then
if fs.exists(old.path) then -- delete old file
fs.delete(old.path)
end
local f = fs.open(new.path,"w") -- create new file
f.write(h.readAll()) -- write to file
f.close()
term.setTextColor(colors.orange)
term.write("*Updated file ")
term.setTextColor(colors.lightBlue)
term.write(old.path)
term.setTextColor(colors.orange)
print("")
else
printError("failed to update "..old.path..": "..err)
end
end
end
end
-- add new required files
for _,new in pairs(req.dat.new) do
local isNew = true
for _,old in pairs(req.dat.old) do
if new.path == old.path then
isNew = false
break
end
end
if isNew then
local h,err = http.get(URL..new.code) -- get new code
if h then
local f = fs.open(new.path,"w") -- create new file
f.write(h.readAll()) -- write new code
f.close()
term.setTextColor(colors.green)
term.write("+Added file ")
term.setTextColor(colors.lightBlue)
term.write(new.path)
term.setTextColor(colors.green)
print("")
else
printError("failed to download "..new.path..": "..err)
end
end
end
-- update old req file
if req.handle.new then
req.handle.new.close()
end
if req.handle.old then
req.handle.old.close()
end
req.handle.new = http.get(req.url)
req.handle.old = fs.open(req.filename,"w") -- flush old req
req.handle.old.write(req.handle.new.readAll()) -- write new req
term.setTextColor(tfg)
-- close handles
req.handle.old.close()
req.handle.new.close()