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.

140 lines
2.9 KiB

  1. /*
  2. Copyright (c) 2016, Antonio SJ Musumeci <trapexit@spawn.link>
  3. Permission to use, copy, modify, and/or distribute this software for any
  4. purpose with or without fee is hereby granted, provided that the above
  5. copyright notice and this permission notice appear in all copies.
  6. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  7. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  8. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  9. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  10. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  11. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  12. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  13. */
  14. #include "errno.hpp"
  15. #include "fs_close.hpp"
  16. #include "fs_open.hpp"
  17. #include "fs_ioctl.hpp"
  18. #include <string>
  19. #include <fcntl.h>
  20. #include <linux/fs.h>
  21. using std::string;
  22. namespace fs
  23. {
  24. namespace attr
  25. {
  26. static
  27. int
  28. get_fs_ioc_flags(const int fd,
  29. int &flags)
  30. {
  31. int rv;
  32. rv = fs::ioctl(fd,FS_IOC_GETFLAGS,(void*)&flags);
  33. if((rv == -1) && (errno == EINVAL))
  34. errno = ENOTSUP;
  35. return rv;
  36. }
  37. static
  38. int
  39. get_fs_ioc_flags(const string &file,
  40. int &flags)
  41. {
  42. int fd;
  43. int rv;
  44. const int openflags = O_RDONLY|O_NONBLOCK;
  45. fd = fs::open(file,openflags);
  46. if(fd == -1)
  47. return -1;
  48. rv = get_fs_ioc_flags(fd,flags);
  49. if(rv == -1)
  50. {
  51. int error = errno;
  52. fs::close(fd);
  53. errno = error;
  54. return -1;
  55. }
  56. return fs::close(fd);
  57. }
  58. static
  59. int
  60. set_fs_ioc_flags(const int fd,
  61. const int flags)
  62. {
  63. int rv;
  64. rv = fs::ioctl(fd,FS_IOC_SETFLAGS,(void*)&flags);
  65. if((rv == -1) && (errno == EINVAL))
  66. errno = ENOTSUP;
  67. return rv;
  68. }
  69. static
  70. int
  71. set_fs_ioc_flags(const string &file,
  72. const int flags)
  73. {
  74. int fd;
  75. int rv;
  76. const int openflags = O_RDONLY|O_NONBLOCK;
  77. fd = fs::open(file,openflags);
  78. if(fd == -1)
  79. return -1;
  80. rv = set_fs_ioc_flags(fd,flags);
  81. if(rv == -1)
  82. {
  83. int error = errno;
  84. fs::close(fd);
  85. errno = error;
  86. return -1;
  87. }
  88. return fs::close(fd);
  89. }
  90. int
  91. copy(const int fdin,
  92. const int fdout)
  93. {
  94. int rv;
  95. int flags;
  96. rv = get_fs_ioc_flags(fdin,flags);
  97. if(rv == -1)
  98. return -1;
  99. return set_fs_ioc_flags(fdout,flags);
  100. }
  101. int
  102. copy(const string &from,
  103. const string &to)
  104. {
  105. int rv;
  106. int flags;
  107. rv = get_fs_ioc_flags(from,flags);
  108. if(rv == -1)
  109. return -1;
  110. return set_fs_ioc_flags(to,flags);
  111. }
  112. }
  113. }