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.

110 lines
2.3 KiB

  1. /*
  2. ISC License
  3. Copyright (c) 2016, 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. #ifndef __FS_BASE_CHMOD_HPP__
  16. #define __FS_BASE_CHMOD_HPP__
  17. #include <sys/stat.h>
  18. #include "fs_base_stat.hpp"
  19. #define MODE_BITS (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)
  20. namespace fs
  21. {
  22. static
  23. inline
  24. int
  25. chmod(const std::string &path,
  26. const mode_t mode)
  27. {
  28. return ::chmod(path.c_str(),mode);
  29. }
  30. static
  31. inline
  32. int
  33. fchmod(const int fd,
  34. const mode_t mode)
  35. {
  36. return ::fchmod(fd,mode);
  37. }
  38. static
  39. inline
  40. int
  41. fchmod(const int fd,
  42. const struct stat &st)
  43. {
  44. return ::fchmod(fd,st.st_mode);
  45. }
  46. static
  47. inline
  48. int
  49. chmod_check_on_error(const std::string &path,
  50. const mode_t mode)
  51. {
  52. int rv;
  53. rv = fs::chmod(path,mode);
  54. if(rv == -1)
  55. {
  56. int error;
  57. struct stat st;
  58. error = errno;
  59. rv = fs::stat(path,st);
  60. if(rv == -1)
  61. return -1;
  62. if((st.st_mode & MODE_BITS) != (mode & MODE_BITS))
  63. return (errno=error,-1);
  64. }
  65. return 0;
  66. }
  67. static
  68. inline
  69. int
  70. fchmod_check_on_error(const int fd,
  71. const struct stat &st)
  72. {
  73. int rv;
  74. rv = fs::fchmod(fd,st);
  75. if(rv == -1)
  76. {
  77. int error;
  78. struct stat tmpst;
  79. error = errno;
  80. rv = fs::fstat(fd,tmpst);
  81. if(rv == -1)
  82. return -1;
  83. if((st.st_mode & MODE_BITS) != (tmpst.st_mode & MODE_BITS))
  84. return (errno=error,-1);
  85. }
  86. return 0;
  87. }
  88. }
  89. #endif