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.

368 lines
7.3 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_fgetxattr.hpp"
  17. #include "fs_flistxattr.hpp"
  18. #include "fs_fsetxattr.hpp"
  19. #include "fs_lgetxattr.hpp"
  20. #include "fs_llistxattr.hpp"
  21. #include "fs_lremovexattr.hpp"
  22. #include "fs_lsetxattr.hpp"
  23. #include "fs_open.hpp"
  24. #include "str.hpp"
  25. #include <map>
  26. #include <sstream>
  27. #include <string>
  28. #include <vector>
  29. using std::string;
  30. using std::vector;
  31. using std::map;
  32. using std::istringstream;
  33. namespace fs
  34. {
  35. namespace xattr
  36. {
  37. int
  38. list(const int fd_,
  39. vector<char> *attrs_)
  40. {
  41. ssize_t rv;
  42. rv = -1;
  43. errno = ERANGE;
  44. while((rv == -1) && (errno == ERANGE))
  45. {
  46. rv = fs::flistxattr(fd_,NULL,0);
  47. if(rv <= 0)
  48. return rv;
  49. attrs_->resize(rv);
  50. rv = fs::flistxattr(fd_,&(*attrs_)[0],rv);
  51. }
  52. return rv;
  53. }
  54. int
  55. list(const string &path_,
  56. vector<char> *attrs_)
  57. {
  58. ssize_t rv;
  59. rv = -1;
  60. errno = ERANGE;
  61. while((rv == -1) && (errno == ERANGE))
  62. {
  63. rv = fs::llistxattr(path_,NULL,0);
  64. if(rv <= 0)
  65. return rv;
  66. attrs_->resize(rv);
  67. rv = fs::llistxattr(path_,&(*attrs_)[0],rv);
  68. }
  69. return rv;
  70. }
  71. int
  72. list(const int fd_,
  73. vector<string> *attrvector_)
  74. {
  75. int rv;
  76. vector<char> attrs;
  77. rv = fs::xattr::list(fd_,&attrs);
  78. if(rv != -1)
  79. {
  80. string tmp(attrs.begin(),attrs.end());
  81. str::split(tmp,'\0',attrvector_);
  82. }
  83. return rv;
  84. }
  85. int
  86. list(const string &path_,
  87. vector<string> *attrvector_)
  88. {
  89. int rv;
  90. vector<char> attrs;
  91. rv = fs::xattr::list(path_,&attrs);
  92. if(rv != -1)
  93. {
  94. string tmp(attrs.begin(),attrs.end());
  95. str::split(tmp,'\0',attrvector_);
  96. }
  97. return rv;
  98. }
  99. int
  100. list(const int fd_,
  101. string *attrstr_)
  102. {
  103. int rv;
  104. vector<char> attrs;
  105. rv = fs::xattr::list(fd_,&attrs);
  106. if(rv != -1)
  107. *attrstr_ = string(attrs.begin(),attrs.end());
  108. return rv;
  109. }
  110. int
  111. list(const string &path_,
  112. string *attrstr_)
  113. {
  114. int rv;
  115. vector<char> attrs;
  116. rv = fs::xattr::list(path_,&attrs);
  117. if(rv != -1)
  118. *attrstr_ = string(attrs.begin(),attrs.end());
  119. return rv;
  120. }
  121. int
  122. get(const int fd_,
  123. const string &attr_,
  124. vector<char> *value_)
  125. {
  126. ssize_t rv;
  127. rv = -1;
  128. errno = ERANGE;
  129. while((rv == -1) && (errno == ERANGE))
  130. {
  131. rv = fs::fgetxattr(fd_,attr_,NULL,0);
  132. if(rv <= 0)
  133. return rv;
  134. value_->resize(rv);
  135. rv = fs::fgetxattr(fd_,attr_,&(*value_)[0],rv);
  136. }
  137. return rv;
  138. }
  139. int
  140. get(const string &path_,
  141. const string &attr_,
  142. vector<char> *value_)
  143. {
  144. ssize_t rv;
  145. rv = -1;
  146. errno = ERANGE;
  147. while((rv == -1) && (errno == ERANGE))
  148. {
  149. rv = fs::lgetxattr(path_,attr_,NULL,0);
  150. if(rv <= 0)
  151. return rv;
  152. value_->resize(rv);
  153. rv = fs::lgetxattr(path_,attr_,&(*value_)[0],rv);
  154. }
  155. return rv;
  156. }
  157. int
  158. get(const int fd_,
  159. const string &attr_,
  160. string *value_)
  161. {
  162. int rv;
  163. vector<char> tmpvalue;
  164. rv = get(fd_,attr_,&tmpvalue);
  165. if(rv != -1)
  166. *value_ = string(tmpvalue.begin(),tmpvalue.end());
  167. return rv;
  168. }
  169. int
  170. get(const string &path_,
  171. const string &attr_,
  172. string *value_)
  173. {
  174. int rv;
  175. vector<char> tmpvalue;
  176. rv = fs::xattr::get(path_,attr_,&tmpvalue);
  177. if(rv != -1)
  178. *value_ = string(tmpvalue.begin(),tmpvalue.end());
  179. return rv;
  180. }
  181. int
  182. get(const int fd_,
  183. map<string,string> *attrs_)
  184. {
  185. int rv;
  186. string attrstr;
  187. rv = fs::xattr::list(fd_,&attrstr);
  188. if(rv == -1)
  189. return -1;
  190. {
  191. string key;
  192. istringstream ss(attrstr);
  193. while(getline(ss,key,'\0'))
  194. {
  195. string value;
  196. rv = fs::xattr::get(fd_,key,&value);
  197. if(rv != -1)
  198. (*attrs_)[key] = value;
  199. }
  200. }
  201. return 0;
  202. }
  203. int
  204. get(const string &path_,
  205. map<string,string> *attrs_)
  206. {
  207. int rv;
  208. string attrstr;
  209. rv = fs::xattr::list(path_,&attrstr);
  210. if(rv == -1)
  211. return -1;
  212. {
  213. string key;
  214. istringstream ss(attrstr);
  215. while(getline(ss,key,'\0'))
  216. {
  217. string value;
  218. rv = fs::xattr::get(path_,key,&value);
  219. if(rv != -1)
  220. (*attrs_)[key] = value;
  221. }
  222. }
  223. return 0;
  224. }
  225. int
  226. set(const int fd_,
  227. const string &key_,
  228. const string &value_,
  229. const int flags_)
  230. {
  231. return fs::fsetxattr(fd_,
  232. key_,
  233. value_.data(),
  234. value_.size(),
  235. flags_);
  236. }
  237. int
  238. set(const string &path_,
  239. const string &key_,
  240. const string &value_,
  241. const int flags_)
  242. {
  243. return fs::lsetxattr(path_,
  244. key_,
  245. value_.data(),
  246. value_.size(),
  247. flags_);
  248. }
  249. int
  250. set(const int fd_,
  251. const map<string,string> &attrs_)
  252. {
  253. int rv;
  254. for(map<string,string>::const_iterator
  255. i = attrs_.begin(), ei = attrs_.end(); i != ei; ++i)
  256. {
  257. rv = fs::xattr::set(fd_,i->first,i->second,0);
  258. if(rv == -1)
  259. return -1;
  260. }
  261. return 0;
  262. }
  263. int
  264. set(const string &path_,
  265. const map<string,string> &attrs_)
  266. {
  267. int fd;
  268. fd = fs::open(path_,O_RDONLY|O_NONBLOCK);
  269. if(fd == -1)
  270. return -1;
  271. fs::xattr::set(fd,attrs_);
  272. return fs::close(fd);
  273. }
  274. int
  275. copy(const int fdin_,
  276. const int fdout_)
  277. {
  278. int rv;
  279. map<string,string> attrs;
  280. rv = fs::xattr::get(fdin_,&attrs);
  281. if(rv == -1)
  282. return -1;
  283. return fs::xattr::set(fdout_,attrs);
  284. }
  285. int
  286. copy(const string &from_,
  287. const string &to_)
  288. {
  289. int rv;
  290. map<string,string> attrs;
  291. rv = fs::xattr::get(from_,&attrs);
  292. if(rv == -1)
  293. return -1;
  294. return fs::xattr::set(to_,attrs);
  295. }
  296. }
  297. }