-
Notifications
You must be signed in to change notification settings - Fork 2
/
fsutils.lua
65 lines (52 loc) · 1.85 KB
/
fsutils.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
local core = require "core"
local fsutils = {}
--- Checks whether a file or directory exists
-- @param string path Path of object to be checked
function fsutils.is_object_exist(path)
local stat = system.get_file_info(path)
if not stat or (stat.type ~= "file" and stat.type ~= "dir") then
return false
end
return true
end
--- Checks whether an object is a directory
-- @param string path Path of object to be checked
function fsutils.is_dir(path)
local file_info = system.get_file_info(path)
if (file_info ~= nil) then
return file_info.type == "dir"
end
return false
end
--- Moves object (file or directory) to another path
-- @param string old_abs_filename Absolute old filename
-- @param string new_abs_filename Absolute new filename
function fsutils.move_object(old_abs_filename, new_abs_filename)
local res, err = os.rename(old_abs_filename, new_abs_filename)
if res then -- successfully renamed
core.log("[treeview-extender] Moved \"%s\" to \"%s\"", old_abs_filename, new_abs_filename)
else
core.error("[treeview-extender] Error while moving \"%s\" to \"%s\": %s", old_abs_filename, new_abs_filename, err)
end
end
--- Copy source file to destination path
-- @param string source_abs_filename Absolute source filename
-- @param string dest_abs_filename Absolute destination filename
function fsutils.copy_file(source_abs_filename, dest_abs_filename)
local source_file = io.open(source_abs_filename, "rb")
local dest_file = io.open(dest_abs_filename, "wb")
if source_file ~= nil and dest_file ~= nil then
local chunk_size = 2^13 -- 8KB
while true do
local chunk = source_file:read(chunk_size)
if not chunk then break end
dest_file:write(chunk)
end
source_file:close()
dest_file:close()
end
end
function fsutils.project_dir()
return core.project_dir or core.root_project().path
end
return fsutils