justinhj / battery.nvim

Neovim plugin to detect and view battery information
MIT License
59 stars 9 forks source link

bug: `util.file.is_readable_directory()` does not work on Termux #41

Closed Agent-E11 closed 2 months ago

Agent-E11 commented 2 months ago

On Termux, the /sys/class/power_supply exists and "is readable", except it, well, isn't.

$ stat --format='%A' /sys/class/power_supply
drwxr-xr-x
#      ^ Everyone can read it
$ ls /sys/class/power_supply
ls: cannot open directory '/sys/class/power_supply': Permission denied
#                                                    ^^^^^^^^^^^^^^^^^ I can't read it?

I have found a method that I think works on both regular Linux and Termux (not Windows though):

$ test -d /sys/class/power_supply && test -r /sys/class/power_supply
#      ^^ Is directory                    ^^ Is readable

This is communicated through the exit code, which is returned by the os.execute function (I think Neovim uses Lua 5.1, so this version of os.execute only returns the exit code)

function is_readable_directory(file)
  return os.execute('test -d '..file..' && test -r '..file) == 0
end

The problem with this is that the input must be trustworthy, because the file could be something like /some/file; rm -rf /

Agent-E11 commented 2 months ago

I have created a draft pull request with some proposed changes #42

justinhj commented 2 months ago

Interesting. Since termux is a special case, is there an environment flag we can use to detect Termux and then use a different code path for that?

Agent-E11 commented 2 months ago

There is a $TERMUX_VERSION environment variable, and I am looking into whether that is a reliable way of detecting if it is running in Termux

Agent-E11 commented 2 months ago

I have made it so that it checks whether it is in Termux, and will use the "old" or "new" version of the function based on that

42