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.

92 lines
2.0 KiB

  1. /*
  2. ISC License
  3. Copyright (c) 2020, 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_lstat.hpp"
  17. #include <unistd.h>
  18. namespace fs
  19. {
  20. static
  21. inline
  22. int
  23. lchown(const char *pathname_,
  24. const uid_t uid_,
  25. const gid_t gid_)
  26. {
  27. return ::lchown(pathname_,uid_,gid_);
  28. }
  29. static
  30. inline
  31. int
  32. lchown(const std::string &pathname_,
  33. const uid_t uid_,
  34. const gid_t gid_)
  35. {
  36. return fs::lchown(pathname_.c_str(),uid_,gid_);
  37. }
  38. static
  39. inline
  40. int
  41. lchown(const char *pathname_,
  42. const struct stat &st_)
  43. {
  44. return fs::lchown(pathname_,st_.st_uid,st_.st_gid);
  45. }
  46. static
  47. inline
  48. int
  49. lchown(const std::string &pathname_,
  50. const struct stat &st_)
  51. {
  52. return fs::lchown(pathname_.c_str(),st_);
  53. }
  54. static
  55. inline
  56. int
  57. lchown_check_on_error(const std::string &path_,
  58. const struct stat &st_)
  59. {
  60. int rv;
  61. rv = fs::lchown(path_,st_);
  62. if(rv == -1)
  63. {
  64. int error;
  65. struct stat tmpst;
  66. error = errno;
  67. rv = fs::lstat(path_,&tmpst);
  68. if(rv == -1)
  69. return -1;
  70. if((st_.st_uid != tmpst.st_uid) ||
  71. (st_.st_gid != tmpst.st_gid))
  72. return (errno=error,-1);
  73. }
  74. return 0;
  75. }
  76. }