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.

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