aboutsummaryrefslogtreecommitdiffstats
path: root/lua/nvim-lsp-installer/fs.lua
blob: ed018da2d5f53547e58d61be255642d7a549f6e3 (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
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
local pathm = require "nvim-lsp-installer.path"
local log = require "nvim-lsp-installer.log"
local settings = require "nvim-lsp-installer.settings"

local uv = vim.loop
local M = {}

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

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

function M.rename(path, new_path)
    log.debug("fs: rename", path, new_path)
    assert_ownership(path)
    assert_ownership(new_path)
    uv.fs_rename(path, new_path)
end

function M.mkdirp(path)
    log.debug("fs: mkdirp", path)
    assert_ownership(path)
    if vim.fn.mkdir(path, "p") ~= 1 then
        log.debug "fs: mkdirp failed"
        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))
    local fstat = assert(uv.fs_fstat(fd))
    assert(uv.fs_close(fd))
    return fstat
end

function M.readdir(path)
    local dir = assert(uv.fs_opendir(path, nil, 25))
    local all_entries = {}
    local exhausted = false

    repeat
        local entries = uv.fs_readdir(dir)
        if entries and #entries > 0 then
            for i = 1, #entries do
                all_entries[#all_entries + 1] = entries[i]
            end
        else
            exhausted = true
        end
    until exhausted

    assert(uv.fs_closedir(dir))

    return all_entries
end

return M