fix(setup): guard against overwriting existing problem files
If the destination source file already exists on disk and belongs to a different platform/contest/problem in the cache, abort with an error rather than silently clobbering it.
This commit is contained in:
parent
29af2df858
commit
86b3eb9582
1 changed files with 13 additions and 0 deletions
|
|
@ -278,6 +278,19 @@ function M.setup_problem(problem_id, language)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if vim.fn.filereadable(source_file) == 1 then
|
||||||
|
local existing = cache.get_file_state(vim.fn.fnamemodify(source_file, ':p'))
|
||||||
|
if existing and (existing.platform ~= platform or existing.contest_id ~= (state.get_contest_id() or '') or existing.problem_id ~= problem_id) then
|
||||||
|
logger.log(
|
||||||
|
('File %q already exists for %s/%s %s.'):format(
|
||||||
|
source_file, existing.platform, existing.contest_id, existing.problem_id
|
||||||
|
),
|
||||||
|
{ level = vim.log.levels.ERROR }
|
||||||
|
)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local contest_dir = vim.fn.fnamemodify(source_file, ':h')
|
local contest_dir = vim.fn.fnamemodify(source_file, ':h')
|
||||||
local is_new_dir = vim.fn.isdirectory(contest_dir) == 0
|
local is_new_dir = vim.fn.isdirectory(contest_dir) == 0
|
||||||
vim.fn.mkdir(contest_dir, 'p')
|
vim.fn.mkdir(contest_dir, 'p')
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue