-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcopy.lua
101 lines (83 loc) · 2.3 KB
/
copy.lua
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
--[[
documentation: copy nested directories and their content alphabeticaly to other location
usage: lua copy.lua <from> <to> <mode>
dependencies: https://keplerproject.github.io/luafilesystem/
]]
local lfs = require "lfs"
local function load_file(fileName, environment)
if setfenv and loadfile then
local f = assert(loadfile(fileName))
setfenv(f,environment)
return f
else
return assert(loadfile(fileName, "t", environment))
end
end
local donotcopy = load_file("donotcopy.lua", {mode = arg[3]})()
local counter = 1
local copyFilePath
copyFilePath = function (from, to)
for i=1,#donotcopy do if from:find(donotcopy[i], 0, true) then return end end
local attrFrom, errFrom = lfs.attributes(from)
if not attrFrom then return end
if attrFrom.mode == "directory" then
local attrTo, errTo = lfs.attributes(to)
if not attrTo then
print("copy.lua: " .. to)
lfs.mkdir(to)
end
local paths = {}
for entry in lfs.dir(from) do
if entry ~= "." and entry ~= ".." then
local pathFrom, pathTo
if from ~= "/" then
if from:sub(#from,#from) == "/" then
pathFrom = from .. entry
else
pathFrom = from .. "/" .. entry
end
if to:sub(#to,#to) == "/" then
pathTo = to .. entry
else
pathTo = to .. "/" .. entry
end
else
pathFrom = "/" .. entry
pathTo = "/" .. entry
end
paths[#paths+1] = {from = pathFrom, to = pathTo}
end
end
if #paths > 0 then
table.sort(paths, function(x,y) return x.from < y.from end)
for i=1,#paths do copyFilePath(paths[i].from, paths[i].to) end
end
elseif attrFrom.mode == "file" then
print("copy.lua: [" .. counter .. "] " .. to)
counter = counter + 1
local fileFrom = assert(io.open(from, "r"))
local content = fileFrom:read("*all")
fileFrom:close()
local fileTo = assert(io.open(to, "w"))
fileTo:write(content)
fileTo:close()
end
end
local function delete_destination ()
local f = assert(io.popen("rm -rf " .. arg[2] .. "/*", "r"))
f:close()
end
if #arg < 2 then
print("usage: lua copy.lua <from> <to> [<mode>]")
return
end
if arg[3] == "delete" then
delete_destination()
elseif arg[3] == "append" then
-- do not delete destination
elseif arg[3] == "home" then
delete_destination()
elseif arg[3] == "car" then
delete_destination()
end
copyFilePath(arg[1], arg[2])