aboutsummaryrefslogtreecommitdiff
path: root/shutil.lua
blob: e8ca26960e164feb3576c948c6759fc554998528 (plain)
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

shutil = { }

function shutil.copy_file(src, dst)
	local e, sf, df, cont, ok
	sf, e = io.open(src, "rb")
	if not sf then
		return nil, "Error opening input: "..(e or "")
	end
	df, e = io.open(dst, "wb")
	if not df then
		sf:close()
		return nil, "Error opening output: "..(e or "")
	end
	cont, e = sf:read("*a")
	if not cont then
		sf:close()
		df:close()
		return nil, "Error reading input: "..(e or "")
	end
	ok, e = df:write(cont)
	if not ok then
		sf:close()
		df:close()
		return nil, "Error writing output: "..(e or "")
	end
	sf:close()
	df:close()
	return true
end

function shutil.move_file(src, dst)
	local ok, e = shutil.copy_file(src, dst)
	if not ok then
		return nil, "Copy failed: "..(e or "")
	end
	ok, e = os.remove(src)
	if not ok then
		return nil, "Remove failed: "..(e or "")
	end
	return true
end