Skip to content

Commit 254bc66

Browse files
committed
fix: guard against nil metadata values (#548)
1 parent c6a39a6 commit 254bc66

File tree

4 files changed

+23
-23
lines changed

4 files changed

+23
-23
lines changed

lua/oil/adapters/files.lua

+6-6
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ file_columns.size = {
5656

5757
render = function(entry, conf)
5858
local meta = entry[FIELD_META]
59-
local stat = meta.stat
59+
local stat = meta and meta.stat
6060
if not stat then
6161
return columns.EMPTY
6262
end
@@ -73,7 +73,7 @@ file_columns.size = {
7373

7474
get_sort_value = function(entry)
7575
local meta = entry[FIELD_META]
76-
local stat = meta.stat
76+
local stat = meta and meta.stat
7777
if stat then
7878
return stat.size
7979
else
@@ -93,7 +93,7 @@ if not fs.is_windows then
9393

9494
render = function(entry, conf)
9595
local meta = entry[FIELD_META]
96-
local stat = meta.stat
96+
local stat = meta and meta.stat
9797
if not stat then
9898
return columns.EMPTY
9999
end
@@ -106,7 +106,7 @@ if not fs.is_windows then
106106

107107
compare = function(entry, parsed_value)
108108
local meta = entry[FIELD_META]
109-
if parsed_value and meta.stat and meta.stat.mode then
109+
if parsed_value and meta and meta.stat and meta.stat.mode then
110110
local mask = bit.lshift(1, 12) - 1
111111
local old_mode = bit.band(meta.stat.mode, mask)
112112
if parsed_value ~= old_mode then
@@ -156,7 +156,7 @@ for _, time_key in ipairs({ "ctime", "mtime", "atime", "birthtime" }) do
156156

157157
render = function(entry, conf)
158158
local meta = entry[FIELD_META]
159-
local stat = meta.stat
159+
local stat = meta and meta.stat
160160
if not stat then
161161
return columns.EMPTY
162162
end
@@ -188,7 +188,7 @@ for _, time_key in ipairs({ "ctime", "mtime", "atime", "birthtime" }) do
188188

189189
get_sort_value = function(entry)
190190
local meta = entry[FIELD_META]
191-
local stat = meta.stat
191+
local stat = meta and meta.stat
192192
if stat then
193193
return stat[time_key].sec
194194
else

lua/oil/adapters/ssh.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ ssh_columns.permissions = {
126126

127127
compare = function(entry, parsed_value)
128128
local meta = entry[FIELD_META]
129-
if parsed_value and meta.mode then
129+
if parsed_value and meta and meta.mode then
130130
local mask = bit.lshift(1, 12) - 1
131131
local old_mode = bit.band(meta.mode, mask)
132132
if parsed_value ~= old_mode then
@@ -169,7 +169,7 @@ ssh_columns.size = {
169169

170170
get_sort_value = function(entry)
171171
local meta = entry[FIELD_META]
172-
if meta.size then
172+
if meta and meta.size then
173173
return meta.size
174174
else
175175
return 0

lua/oil/adapters/trash/freedesktop.lua

+7-7
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ end
131131
---@param cb fun(path: string)
132132
M.get_entry_path = function(url, entry, cb)
133133
local internal_entry = assert(cache.get_entry_by_id(entry.id))
134-
local meta = internal_entry[FIELD_META]
134+
local meta = assert(internal_entry[FIELD_META])
135135
---@type oil.TrashInfo
136136
local trash_info = meta.trash_info
137137
if not trash_info then
@@ -381,7 +381,7 @@ file_columns.mtime = {
381381
get_sort_value = function(entry)
382382
local meta = entry[FIELD_META]
383383
---@type nil|oil.TrashInfo
384-
local trash_info = meta.trash_info
384+
local trash_info = meta and meta.trash_info
385385
if trash_info then
386386
return trash_info.deletion_date
387387
else
@@ -417,7 +417,7 @@ M.filter_action = function(action)
417417
elseif action.type == "delete" then
418418
local entry = assert(cache.get_entry_by_url(action.url))
419419
local meta = entry[FIELD_META]
420-
return meta.trash_info ~= nil
420+
return meta ~= nil and meta.trash_info ~= nil
421421
elseif action.type == "move" then
422422
local src_adapter = assert(config.get_adapter_by_scheme(action.src_url))
423423
local dest_adapter = assert(config.get_adapter_by_scheme(action.dest_url))
@@ -447,7 +447,7 @@ M.render_action = function(action)
447447
local entry = assert(cache.get_entry_by_url(action.url))
448448
local meta = entry[FIELD_META]
449449
---@type oil.TrashInfo
450-
local trash_info = meta.trash_info
450+
local trash_info = meta and meta.trash_info
451451
local short_path = fs.shorten_path(trash_info.original_path)
452452
return string.format(" PURGE %s", short_path)
453453
elseif action.type == "move" then
@@ -561,7 +561,7 @@ M.perform_action = function(action, cb)
561561
local entry = assert(cache.get_entry_by_url(action.url))
562562
local meta = entry[FIELD_META]
563563
---@type oil.TrashInfo
564-
local trash_info = meta.trash_info
564+
local trash_info = meta and meta.trash_info
565565
purge(trash_info, cb)
566566
elseif action.type == "move" then
567567
local src_adapter = assert(config.get_adapter_by_scheme(action.src_url))
@@ -576,7 +576,7 @@ M.perform_action = function(action, cb)
576576
local entry = assert(cache.get_entry_by_url(action.src_url))
577577
local meta = entry[FIELD_META]
578578
---@type oil.TrashInfo
579-
local trash_info = meta.trash_info
579+
local trash_info = meta and meta.trash_info
580580
fs.recursive_move(action.entry_type, trash_info.trash_file, dest_path, function(err)
581581
if err then
582582
return cb(err)
@@ -608,7 +608,7 @@ M.perform_action = function(action, cb)
608608
local entry = assert(cache.get_entry_by_url(action.src_url))
609609
local meta = entry[FIELD_META]
610610
---@type oil.TrashInfo
611-
local trash_info = meta.trash_info
611+
local trash_info = meta and meta.trash_info
612612
fs.recursive_copy(action.entry_type, trash_info.trash_file, dest_path, cb)
613613
else
614614
error("Must be moving files into or out of trash")

lua/oil/adapters/trash/windows.lua

+8-8
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,8 @@ file_columns.mtime = {
164164

165165
get_sort_value = function(entry)
166166
local meta = entry[FIELD_META]
167-
---@type oil.WindowsTrashInfo
168-
local trash_info = meta.trash_info
167+
---@type nil|oil.WindowsTrashInfo
168+
local trash_info = meta and meta.trash_info
169169
if trash_info and trash_info.deletion_date then
170170
return trash_info.deletion_date
171171
else
@@ -199,7 +199,7 @@ M.filter_action = function(action)
199199
elseif action.type == "delete" then
200200
local entry = assert(cache.get_entry_by_url(action.url))
201201
local meta = entry[FIELD_META]
202-
return meta.trash_info ~= nil
202+
return meta ~= nil and meta.trash_info ~= nil
203203
elseif action.type == "move" then
204204
local src_adapter = assert(config.get_adapter_by_scheme(action.src_url))
205205
local dest_adapter = assert(config.get_adapter_by_scheme(action.dest_url))
@@ -235,7 +235,7 @@ end
235235
M.get_entry_path = function(url, entry, cb)
236236
local internal_entry = assert(cache.get_entry_by_id(entry.id))
237237
local meta = internal_entry[FIELD_META] --[[@as {stat: uv_fs_t, trash_info: oil.WindowsTrashInfo, display_name: string}]]
238-
local trash_info = meta.trash_info
238+
local trash_info = meta and meta.trash_info
239239
if not trash_info then
240240
-- This is a subpath in the trash
241241
M.normalize_url(url, cb)
@@ -265,7 +265,7 @@ M.render_action = function(action)
265265
local entry = assert(cache.get_entry_by_url(action.url))
266266
local meta = entry[FIELD_META]
267267
---@type oil.WindowsTrashInfo
268-
local trash_info = meta.trash_info
268+
local trash_info = meta and meta.trash_info
269269
local short_path = fs.shorten_path(trash_info.original_path)
270270
return string.format(" PURGE %s", short_path)
271271
elseif action.type == "move" then
@@ -348,7 +348,7 @@ M.perform_action = function(action, cb)
348348
if action.type == "delete" then
349349
local entry = assert(cache.get_entry_by_url(action.url))
350350
local meta = entry[FIELD_META] --[[@as {stat: uv_fs_t, trash_info: oil.WindowsTrashInfo, display_name: string}]]
351-
local trash_info = meta.trash_info
351+
local trash_info = meta and meta.trash_info
352352

353353
purge(trash_info, cb)
354354
elseif action.type == "move" then
@@ -364,7 +364,7 @@ M.perform_action = function(action, cb)
364364
dest_path = fs.posix_to_os_path(dest_path)
365365
local entry = assert(cache.get_entry_by_url(action.src_url))
366366
local meta = entry[FIELD_META] --[[@as {stat: uv_fs_t, trash_info: oil.WindowsTrashInfo, display_name: string}]]
367-
local trash_info = meta.trash_info
367+
local trash_info = meta and meta.trash_info
368368
fs.recursive_move(action.entry_type, trash_info.trash_file, dest_path, function(err)
369369
if err then
370370
return cb(err)
@@ -388,7 +388,7 @@ M.perform_action = function(action, cb)
388388
dest_path = fs.posix_to_os_path(dest_path)
389389
local entry = assert(cache.get_entry_by_url(action.src_url))
390390
local meta = entry[FIELD_META] --[[@as {stat: uv_fs_t, trash_info: oil.WindowsTrashInfo, display_name: string}]]
391-
local trash_info = meta.trash_info
391+
local trash_info = meta and meta.trash_info
392392
fs.recursive_copy(action.entry_type, trash_info.trash_file, dest_path, cb)
393393
else
394394
error("Must be moving files into or out of trash")

0 commit comments

Comments
 (0)