aboutsummaryrefslogtreecommitdiffstats
path: root/lua/nvim-lsp-installer/fs.lua
blob: 0cfacecaa1e961bd4ff51a9b13c870c9c49a8152 (plain) (blame)
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
local pathm = require("nvim-lsp-installer.path")

local uv = vim.loop
local M = {}

local function assert_ownership(path)
    if not pathm.is_subdirectory(pathm.SERVERS_ROOT_DIR, path) then
        error(("Refusing to operate on path outside of the servers root dir (%s)."):format(pathm.SERVERS_ROOT_DIR))
    end
end

function M.rmrf(path)
    assert_ownership(path)
    if vim.fn.delete(path, "rf") ~= 0 then
        error(("rmrf: Could not remove directory %q."):format(path))
    end
end

function M.mkdirp(path)
    assert_ownership(path)
    if vim.fn.mkdir(path, "p") ~= 1 then
        error(("mkdirp: Could not create directory %q."):format(path))
    end
end

function M.dir_exists(path)
    local ok, stat = pcall(M.fstat, path)
    if not ok then
        return false
    end
    return stat.type == "directory"
end

function M.file_exists(path)
    local ok, stat = pcall(M.fstat, path)
    if not ok then
        return false
    end
    return stat.type == "file"
end

function M.fstat(path)
    local fd = assert(uv.fs_open(path, "r", 438))
    return assert(uv.fs_fstat(fd))
end

return M