From 8e6615916de6f7d2491e47f8a97870eef0ad36d3 Mon Sep 17 00:00:00 2001 From: William Boman Date: Sat, 30 Oct 2021 11:05:50 +0200 Subject: run installations in tmpdir (#199) Resolves #154. --- lua/nvim-lsp-installer/fs.lua | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) (limited to 'lua/nvim-lsp-installer/fs.lua') diff --git a/lua/nvim-lsp-installer/fs.lua b/lua/nvim-lsp-installer/fs.lua index ed018da2..023d1db5 100644 --- a/lua/nvim-lsp-installer/fs.lua +++ b/lua/nvim-lsp-installer/fs.lua @@ -5,8 +5,14 @@ local settings = require "nvim-lsp-installer.settings" local uv = vim.loop local M = {} +local tmpdir_root = vim.fn.fnamemodify(vim.fn.tempname(), ":h") + local function assert_ownership(path) - if not pathm.is_subdirectory(settings.current.install_root_dir, path) then + if + not pathm.is_subdirectory(settings.current.install_root_dir, path) + and not pathm.is_subdirectory(tmpdir_root, 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, @@ -16,6 +22,7 @@ local function assert_ownership(path) end end +---@param path @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) @@ -25,13 +32,16 @@ function M.rmrf(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) - uv.fs_rename(path, 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) @@ -41,6 +51,15 @@ function M.mkdirp(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 + +---@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 @@ -49,6 +68,8 @@ function M.dir_exists(path) 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 @@ -57,6 +78,8 @@ function M.file_exists(path) 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)) @@ -64,6 +87,20 @@ function M.fstat(path) 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 + +---@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 = {} -- cgit v1.2.3-70-g09d2