-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfs.lua
More file actions
145 lines (139 loc) · 4.27 KB
/
fs.lua
File metadata and controls
145 lines (139 loc) · 4.27 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
local fs = {}
fs.libPath = "tmpLibs/"
fs.progPath = "tmpProg/"
do
fs.open = function(path, mode)
assert("string" == type(path), tostring(path) .. "path must be a string")
assert("string" == type(mode), tostring(mode) .. " mode must be a string")
local file = io.open(path, mode)
if not file then
-- find all occurences of / in the path
local dirs = {}
for i = 1, #path do
if path:sub(i, i) == "/" then
table.insert(dirs, i)
end
end
-- create all directories
local dir = ""
for i = 1, #dirs do
dir = path:sub(1, dirs[i])
if not fs.exists(dir) then
os.execute("mkdir " .. dir)
end
end
file = io.open(path, mode)
end
if mode == "w" then
assert(file, "file could not be opened in : " .. path .. " Mode : " .. mode)
end
if not file then
return nil
end
local file2 = {}
setmetatable(file2, { __index = file })
file2.base = file
file2.readAll = function()
return file2.base:read("*a")
end
file2["readLine"] = function()
return file2.base:read("*l")
end
file2["write"] = function(content)
return file2.base:write(content)
end
file2["read"] = function(...)
return file2.base:read(...)
end
file2["close"] = function()
file2.base:close()
end
return file2
end
fs.exists = function(path)
assert("string" == type(path), "path must be a string")
local file = io.open(path, "r")
if not file then
return false
end
file:close()
return true
end
fs.copy = function(src, dest)
assert("string" == type(src), "src must be a string")
assert("string" == type(dest), "dest must be a string")
local file = io.open(src, "r")
if not file then
print("file " .. src .. " not found")
return false
end
local content = file:read("*a")
file:close()
file = io.open(dest, "w")
if not file then
if not file then
-- find all occurences of / in the path
local dirs = {}
for i = 1, #dest do
if dest:sub(i, i) == "/" then
table.insert(dirs, i)
end
end
-- create all directories
local dir = ""
for i = 1, #dirs do
dir = dest:sub(1, dirs[i])
if not fs.exists(dir) then
os.execute("mkdir " .. dir)
end
end
file = io.open(dest, "w")
if not file then
print("file" .. dest .. " not found")
return false
end
end
end
file:write(content)
file:close()
return true
end
fs.makeDir = function(path)
assert("string" == type(path), "path must be a string")
path = path .. "/"
local exists = os.rename(path, path)
if not exists then
local dirs = {}
for i = 1, #path do
if path:sub(i, i) == "/" then
table.insert(dirs, i)
end
end
-- create all directories
local dir = ""
for i = 1, #dirs do
dir = path:sub(1, dirs[i])
if not fs.exists(dir) then
os.execute("mkdir " .. dir)
end
end
end
end
fs.delete = function(path)
assert("string" == type(path), "path must be a string")
if fs.exists(path) then
os.execute("rm -rf " .. path)
end
end
fs.readAll = function(path)
assert("string" == type(path), "path must be a string")
local file = io.open(path, "r")
if not file then
return nil
end
local content = file:read("*a")
file:close()
return content
end
end
return fs