robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40516 from justinmk/health05d7040425

master
576 B20 linesraw
1-- OS/process utils.
2
3local M = {}
4
5--- Counts (approximate) open file descriptors for this process. Works on Linux/macOS/BSD.
6---@return integer? # Number of open fds, or nil if `/dev/fd` failed (fds exhausted, or unavailable/Windows).
7function M.count_open_fds()
8  local n = 0
9  for _, _, err in vim.fs.dir('/dev/fd', { err = true }) do
10    -- If `/dev/fd` scan failed (e.g. EMFILE when fds exhausted), count would be misleading.
11    if err then
12      return nil
13    end
14    n = n + 1
15  end
16  -- Discount the scan's own descriptor.
17  return math.max(0, n - 1)
18end
19
20return M