You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
1.6 KiB

  1. /*
  2. ISC License
  3. Copyright (c) 2023, Antonio SJ Musumeci <trapexit@spawn.link>
  4. Permission to use, copy, modify, and/or distribute this software for any
  5. purpose with or without fee is hereby granted, provided that the above
  6. copyright notice and this permission notice appear in all copies.
  7. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. */
  15. #pragma once
  16. #include "fs_mktemp.hpp"
  17. #include "fs_statvfs.hpp"
  18. #include "fs_unlink.hpp"
  19. #include "statvfs_util.hpp"
  20. #include "ugid.hpp"
  21. #include <string>
  22. namespace fs
  23. {
  24. static
  25. inline
  26. bool
  27. is_mounted_rofs(const std::string path_)
  28. {
  29. int rv;
  30. struct statvfs st;
  31. rv = fs::statvfs(path_,&st);
  32. return ((rv == 0) ? StatVFS::readonly(st) : false);
  33. }
  34. static
  35. inline
  36. bool
  37. is_rofs(std::string path_)
  38. {
  39. ugid::SetRootGuard const ugid;
  40. int fd;
  41. std::string tmp_filepath;
  42. std::tie(fd,tmp_filepath) = fs::mktemp_in_dir(path_,O_WRONLY);
  43. if(fd < 0)
  44. return (fd == -EROFS);
  45. fs::close(fd);
  46. fs::unlink(tmp_filepath);
  47. return false;
  48. }
  49. static
  50. inline
  51. bool
  52. is_rofs_but_not_mounted_ro(const std::string path_)
  53. {
  54. if(fs::is_mounted_rofs(path_))
  55. return false;
  56. return fs::is_rofs(path_);
  57. }
  58. }