-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.lua
More file actions
288 lines (230 loc) · 6.78 KB
/
Copy pathfile.lua
File metadata and controls
288 lines (230 loc) · 6.78 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
-- File utilities for diffusion.nvim
-- Provides secure file operations and path validation
local File = {}
-- Read file content safely
function File.read_file(file_path)
if not file_path or file_path == "" then
return nil, "Invalid file path"
end
local file = io.open(file_path, "r")
if not file then
return nil, "Could not open file: " .. file_path
end
local content = file:read("*a")
file:close()
return content
end
-- Write file content atomically
function File.write_file(file_path, content)
if not file_path or file_path == "" then
return false, "Invalid file path"
end
if content == nil then
content = ""
end
-- Write to temporary file first
local temp_path = file_path .. ".tmp"
local temp_file = io.open(temp_path, "w")
if not temp_file then
return false, "Could not create temporary file: " .. temp_path
end
local success, error = pcall(function()
temp_file:write(content)
temp_file:close()
end)
if not success then
pcall(function() temp_file:close() end)
pcall(os.remove, temp_path)
return false, "Error writing to file: " .. tostring(error)
end
-- Atomically replace original file
local rename_success = os.rename(temp_path, file_path)
if not rename_success then
pcall(os.remove, temp_path)
return false, "Could not replace file: " .. file_path
end
return true
end
-- Check if file exists and is readable
function File.file_exists(file_path)
if not file_path or file_path == "" then
return false
end
local file = io.open(file_path, "r")
if file then
file:close()
return true
end
return false
end
-- Get file information using vim.loop.fs_stat
function File.get_file_info(file_path)
if not file_path or file_path == "" then
return nil, "Invalid file path"
end
local stat = vim.loop.fs_stat(file_path)
if not stat then
return nil, "File not found or not accessible"
end
return {
size = stat.size,
modified = stat.mtime.sec,
accessed = stat.atime.sec,
created = stat.birthtime and stat.birthtime.sec or nil,
type = stat.type,
mode = stat.mode,
is_file = stat.type == "file",
is_directory = stat.type == "directory"
}
end
-- Ensure directory exists, creating it if necessary
function File.ensure_dir(dir_path)
if not dir_path or dir_path == "" then
return false, "Invalid directory path"
end
-- Check if directory already exists
local stat = vim.loop.fs_stat(dir_path)
if stat and stat.type == "directory" then
return true
end
-- Create directory with parents
local success = vim.fn.mkdir(dir_path, "p")
return success == 1, success == 1 and nil or "Failed to create directory"
end
-- Get absolute path
function File.get_absolute_path(file_path)
if not file_path or file_path == "" then
return nil
end
return vim.fn.fnamemodify(file_path, ":p")
end
-- Validate file path for security
function File.validate_file_path(file_path)
if not file_path or file_path == "" then
return false, "Empty file path"
end
-- Get absolute path
local abs_path = File.get_absolute_path(file_path)
if not abs_path then
return false, "Invalid file path"
end
-- Check for directory traversal attempts
if file_path:match("%.%.") then
return false, "Directory traversal not allowed"
end
-- Ensure path is within workspace or home directory
local workspace = vim.fn.getcwd()
local home = vim.fn.expand("~")
if not (vim.startswith(abs_path, workspace) or vim.startswith(abs_path, home)) then
return false, "Path outside allowed directories"
end
-- Additional security checks
local restricted_patterns = {
"/etc/",
"/proc/",
"/sys/",
"/dev/",
"/tmp/.*%.%./" -- No traversal in temp
}
for _, pattern in ipairs(restricted_patterns) do
if abs_path:match(pattern) then
return false, "Access to restricted path not allowed"
end
end
return true, abs_path
end
-- Create secure temporary file
function File.create_temp_file(prefix, suffix)
prefix = prefix or "diffusion"
suffix = suffix or ".tmp"
local temp_dir = vim.fn.stdpath("cache") .. "/diffusion/temp"
File.ensure_dir(temp_dir)
local timestamp = os.time()
local random = math.random(1000, 9999)
local temp_name = string.format("%s_%d_%d%s", prefix, timestamp, random, suffix)
local temp_path = temp_dir .. "/" .. temp_name
return temp_path
end
-- Clean up temporary files older than specified age
function File.cleanup_temp_files(max_age_seconds)
max_age_seconds = max_age_seconds or 3600 -- 1 hour default
local temp_dir = vim.fn.stdpath("cache") .. "/diffusion/temp"
local current_time = os.time()
if vim.fn.isdirectory(temp_dir) == 0 then
return 0 -- No temp directory
end
local files = vim.fn.glob(temp_dir .. "/*", false, true)
local cleaned = 0
for _, file_path in ipairs(files) do
local info = File.get_file_info(file_path)
if info and info.is_file then
local age = current_time - info.modified
if age > max_age_seconds then
local success = os.remove(file_path)
if success then
cleaned = cleaned + 1
end
end
end
end
return cleaned
end
-- Get file extension
function File.get_extension(file_path)
if not file_path or file_path == "" then
return ""
end
return vim.fn.fnamemodify(file_path, ":e")
end
-- Get filename without extension
function File.get_basename(file_path)
if not file_path or file_path == "" then
return ""
end
return vim.fn.fnamemodify(file_path, ":t:r")
end
-- Get directory of file
function File.get_directory(file_path)
if not file_path or file_path == "" then
return ""
end
return vim.fn.fnamemodify(file_path, ":h")
end
-- Check if path is a directory
function File.is_directory(path)
local stat = vim.loop.fs_stat(path)
return stat and stat.type == "directory"
end
-- List directory contents with filtering
function File.list_directory(dir_path, pattern, include_hidden)
if not File.is_directory(dir_path) then
return nil, "Not a directory"
end
local files = {}
local handle = vim.loop.fs_scandir(dir_path)
if not handle then
return nil, "Could not scan directory"
end
while true do
local name, type = vim.loop.fs_scandir_next(handle)
if not name then break end
-- Skip hidden files unless requested
if not include_hidden and vim.startswith(name, ".") then
goto continue
end
-- Apply pattern filter if provided
if pattern and not name:match(pattern) then
goto continue
end
table.insert(files, {
name = name,
type = type,
path = dir_path .. "/" .. name
})
::continue::
end
-- Sort files by name
table.sort(files, function(a, b) return a.name < b.name end)
return files
end
return File