-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.lua
162 lines (140 loc) · 3.68 KB
/
api.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
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
-- lnmount adapted to be an API
-- lnmount can be found here: https://github.com/lyqyd/lnfs-client
local args = {...}
if #args < 3 then
print("Usage:")
print(fs.getName(shell.getRunningProgram()).." <host> <share> <path>")
print("<host> The name of the computer to connect to.")
print(" Use i:<id> in place of host to connect by ID.")
print("<share> The name of the share to connect to on the host.")
print("<path> An empty folder on the local machine to mount the share to.")
return
end
if fs.exists(args[3]) and fs.isDir(args[3]) then
local list = fs.list(args[3])
if #list > 0 then
print("Can only mount to empty directory!")
return
end
else
print("Must mount to empty directory!")
return
end
local conn = connection.new(args[1], 21)
local response = conn:open(0.5, args[2])
if not response then
print("Connection to server failed!")
return
end
local mount = {}
mount.path = shell.resolve(args[3])
if string.sub(mount.path, 1, 1) ~= "/" then mount.path = "/"..mount.path end
mount.mount = {}
mount.mount.list = function(path)
conn:send("fileList", path)
local result = conn:listen(0.3)
if result then
return result.payload
else
return {}
end
end
mount.mount.exists = function(path)
conn:send("fileStatus", path)
local result = conn:listen(0.3)
if result and result.payload then
return result.payload.exists
else
return false
end
end
mount.mount.isDir = function(path)
conn:send("fileStatus", path)
local result = conn:listen(0.3)
if result and result.payload then
return result.payload.isDir
else
return false
end
end
mount.mount.isReadOnly = function(path)
conn:send("fileStatus", path)
local result = conn:listen(0.3)
if result and result.payload then
return result.payload.isReadOnly
else
return false
end
end
mount.mount.getDrive = function(path)
return args[1].."."..args[2]
end
mount.mount.getSize = function(path)
conn:send("fileStatus", path)
local result = conn:listen(0.3)
if result and result.payload then
return result.payload.size
else
return false
end
end
mount.mount.getFreeSpace = function(path)
conn:send("fileStatus", path)
local result = conn:listen(0.3)
if result and result.payload then
return result.payload.space
else
return 0
end
end
mount.mount.makeDir = function(path)
conn:send("fileMakeDirectory", path)
end
mount.mount.move = function(origin, destination)
conn:send("fileMove", {origin = origin, destination = destination})
end
mount.mount.copy = function(origin, destination)
conn:send("fileCopy", {origin = origin, destination = destination})
end
mount.mount.delete = function(path)
conn:send("fileDelete", path)
end
mount.mount.find = function(path)
conn:send("fileFind", path)
local result = conn:listen(0.3)
if result and result.payload and result.type == "FI" then
return result.payload
else
return {}
end
end
mount.mount.get = function(path)
conn:send("fileQuery", path)
local received = false
local data = {}
repeat
local pack = conn:listen(0.3)
if pack and pack.type == "FH" then
if pack.payload ~= path then
return false
end
elseif pack and pack.type == "FD" then
for i = 1, #pack.payload do
data[#data + 1] = pack.payload[i]
end
elseif pack and pack.type == "FE" then
received = true
end
until received
return data
end
mount.mount.put = function(data, path)
conn:send("fileSend", path)
local response = conn:listen(0.3)
if response and response.type == "FR" then
conn:send("fileHeader", path)
conn:send("fileData", data)
conn:send("fileEnd", "eof")
end
end
table.insert(LyqydOS.fs.mounts, mount)