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.

137 lines
3.0 KiB

  1. /*
  2. ISC License
  3. Copyright (c) 2018, 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. #include "errno.hpp"
  16. #include "fs_clonefile.hpp"
  17. #include "fs_close.hpp"
  18. #include "fs_lstat.hpp"
  19. #include "fs_mktemp.hpp"
  20. #include "fs_open.hpp"
  21. #include "fs_path.hpp"
  22. #include "fs_rename.hpp"
  23. #include "fs_unlink.hpp"
  24. #include <string>
  25. #include <fcntl.h>
  26. #include <sys/stat.h>
  27. #include <sys/types.h>
  28. #include <unistd.h>
  29. using std::string;
  30. namespace l
  31. {
  32. static
  33. int
  34. cleanup_on_error(const int src_fd_,
  35. const int dst_fd_ = -1,
  36. const string &dst_fullpath_ = string())
  37. {
  38. int error = errno;
  39. if(src_fd_ >= 0)
  40. fs::close(src_fd_);
  41. if(dst_fd_ >= 0)
  42. fs::close(dst_fd_);
  43. if(!dst_fullpath_.empty())
  44. fs::unlink(dst_fullpath_);
  45. errno = error;
  46. return -1;
  47. }
  48. }
  49. namespace fs
  50. {
  51. namespace cow
  52. {
  53. bool
  54. is_eligible(const int flags_)
  55. {
  56. int accmode;
  57. accmode = (flags_ & O_ACCMODE);
  58. return ((accmode == O_RDWR) ||
  59. (accmode == O_WRONLY));
  60. }
  61. bool
  62. is_eligible(const struct stat &st_)
  63. {
  64. return ((S_ISREG(st_.st_mode)) && (st_.st_nlink > 1));
  65. }
  66. bool
  67. is_eligible(const int flags_,
  68. const struct stat &st_)
  69. {
  70. return (is_eligible(flags_) && is_eligible(st_));
  71. }
  72. bool
  73. is_eligible(const char *fullpath_,
  74. const int flags_)
  75. {
  76. int rv;
  77. struct stat st;
  78. if(!fs::cow::is_eligible(flags_))
  79. return false;
  80. rv = fs::lstat(fullpath_,&st);
  81. if(rv == -1)
  82. return false;
  83. return fs::cow::is_eligible(st);
  84. }
  85. int
  86. break_link(const char *src_fullpath_)
  87. {
  88. int rv;
  89. int src_fd;
  90. int dst_fd;
  91. std::string dst_fullpath;
  92. src_fd = fs::open(src_fullpath_,O_RDONLY|O_NOFOLLOW);
  93. if(src_fd == -1)
  94. return -1;
  95. std::tie(dst_fd,dst_fullpath) = fs::mktemp(src_fullpath_,O_WRONLY);
  96. if(dst_fd < 0)
  97. return l::cleanup_on_error(src_fd);
  98. rv = fs::clonefile(src_fd,dst_fd);
  99. if(rv == -1)
  100. return l::cleanup_on_error(src_fd,dst_fd,dst_fullpath);
  101. rv = fs::rename(dst_fullpath,src_fullpath_);
  102. if(rv == -1)
  103. return l::cleanup_on_error(src_fd,dst_fd,dst_fullpath);
  104. fs::close(src_fd);
  105. fs::close(dst_fd);
  106. return 0;
  107. }
  108. }
  109. }