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.

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