aboutsummaryrefslogtreecommitdiffstats
path: root/lua/nvim-lsp-installer/fs.lua
diff options
context:
space:
mode:
authorWilliam Boman <william@redwill.se>2022-05-11 16:10:57 +0200
committerGitHub <noreply@github.com>2022-05-11 16:10:57 +0200
commit5e3385d90668c792919c7e2791620a6c0d569538 (patch)
tree4b0158d3ac7765db47411d57ec754a96f8eaf1f9 /lua/nvim-lsp-installer/fs.lua
parentchore!: remove zeta_note (diff)
downloadmason-5e3385d90668c792919c7e2791620a6c0d569538.tar
mason-5e3385d90668c792919c7e2791620a6c0d569538.tar.gz
mason-5e3385d90668c792919c7e2791620a6c0d569538.tar.bz2
mason-5e3385d90668c792919c7e2791620a6c0d569538.tar.lz
mason-5e3385d90668c792919c7e2791620a6c0d569538.tar.xz
mason-5e3385d90668c792919c7e2791620a6c0d569538.tar.zst
mason-5e3385d90668c792919c7e2791620a6c0d569538.zip
chore: further decouple module structure (#685)
Diffstat (limited to 'lua/nvim-lsp-installer/fs.lua')
-rw-r--r--lua/nvim-lsp-installer/fs.lua148
1 files changed, 0 insertions, 148 deletions
diff --git a/lua/nvim-lsp-installer/fs.lua b/lua/nvim-lsp-installer/fs.lua
deleted file mode 100644
index 3725f154..00000000
--- a/lua/nvim-lsp-installer/fs.lua
+++ /dev/null
@@ -1,148 +0,0 @@
-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
- log.fmt_error("assert_ownership() failed on path %s", path)
- error(
- ("Refusing to operate on path (%s) outside of the servers root dir (%s)."):format(
- path,
- settings.current.install_root_dir
- )
- )
- end
-end
-
----@param path string @The full path to the file/dir to recursively delete. Will refuse to operate on paths outside of the install_root dir setting.
-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
-
----@param path string @The full path to the file/dir to rename. Will refuse to operate on paths outside of the install_root dir setting.
----@param new_path string @The full path to the new file/dir name. Will refuse to operate on paths outside of the install_root dir setting.
-function M.rename(path, new_path)
- log.debug("fs: rename", path, new_path)
- assert_ownership(path)
- assert_ownership(new_path)
- assert(uv.fs_rename(path, new_path))
-end
-
----@param path string @The full path to the directory to create. Will refuse to operate on paths outside of the install_root dir setting.
-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
-
----@param path string @The full path to the directory to create. Will refuse to operate on paths outside of the install_root dir setting.
-function M.mkdir(path)
- log.debug("fs: mkdir", path)
- assert_ownership(path)
- assert(uv.fs_mkdir(path, 493)) -- 493(10) == 755(8)
-end
-
----Recursively removes the path if it exists before creating a directory.
----@param path string @The full path to the directory to create. Will refuse to operate on paths outside of the install_root dir setting.
-function M.rm_mkdirp(path)
- if M.dir_exists(path) then
- M.rmrf(path)
- end
- return M.mkdirp(path)
-end
-
----@param path string @The full path to check if it 1) exists, and 2) is a directory.
----@return boolean
-function M.dir_exists(path)
- local ok, stat = pcall(M.fstat, path)
- if not ok then
- return false
- end
- return stat.type == "directory"
-end
-
----@param path string @The full path to check if it 1) exists, and 2) is a file.
----@return boolean
-function M.file_exists(path)
- local ok, stat = pcall(M.fstat, path)
- if not ok then
- return false
- end
- return stat.type == "file"
-end
-
----@param path string @The full path to the file to get the file status from.
----@return table @Returns a struct of type uv_fs_t.
-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
-
----@param path string @The full path to the file to write.
----@param contents string @The contents to write.
-function M.write_file(path, contents)
- log.fmt_debug("fs: write_file %s", path)
- assert_ownership(path)
- local fd = assert(uv.fs_open(path, "w", 438))
- uv.fs_write(fd, contents, -1)
- assert(uv.fs_close(fd))
-end
-
----@param path string @The full path to the file to read.
-function M.read_file(path)
- log.fmt_debug("fs: read_file %s", path)
- assert_ownership(path)
- local fd = assert(uv.fs_open(path, "r", 438))
- local fstat = assert(uv.fs_fstat(fd))
- local contents = assert(uv.fs_read(fd, fstat.size, 0))
- assert(uv.fs_close(fd))
- return contents
-end
-
-function M.append_file(path, contents)
- log.fmt_debug("fs: append_file %s", path)
- assert_ownership(path)
- local fd = assert(uv.fs_open(path, "a", 438))
- uv.fs_write(fd, contents, -1)
- assert(uv.fs_close(fd))
-end
-
----@alias ReaddirEntry {name: string, type: string}
-
----@param path string @The full path to the directory to read.
----@return ReaddirEntry[]
-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