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.

513 lines
10 KiB

  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. #include <string>
  21. #include <vector>
  22. #include <map>
  23. #include <sstream>
  24. #include <cstdlib>
  25. #include <iterator>
  26. #include <dirent.h>
  27. #include <errno.h>
  28. #include <fcntl.h>
  29. #include <linux/fs.h>
  30. #include <sys/ioctl.h>
  31. #include <sys/stat.h>
  32. #include <sys/statvfs.h>
  33. #include <sys/types.h>
  34. #include <unistd.h>
  35. #include <glob.h>
  36. #include "fs.hpp"
  37. #include "xattr.hpp"
  38. #include "str.hpp"
  39. using std::string;
  40. using std::vector;
  41. using std::map;
  42. using std::istringstream;
  43. template <typename Iter>
  44. Iter
  45. random_element(Iter begin,
  46. Iter end)
  47. {
  48. const unsigned long n = std::distance(begin, end);
  49. std::advance(begin, (std::rand() % n));
  50. return begin;
  51. }
  52. namespace fs
  53. {
  54. namespace path
  55. {
  56. string
  57. dirname(const string &path)
  58. {
  59. string parent = path;
  60. string::reverse_iterator i;
  61. string::reverse_iterator bi;
  62. bi = parent.rend();
  63. i = parent.rbegin();
  64. while(*i == '/' && i != bi)
  65. i++;
  66. while(*i != '/' && i != bi)
  67. i++;
  68. while(*i == '/' && i != bi)
  69. i++;
  70. parent.erase(i.base(),parent.end());
  71. return parent;
  72. }
  73. string
  74. basename(const string &path)
  75. {
  76. return path.substr(path.find_last_of('/')+1);
  77. }
  78. bool
  79. is_empty(const string &path)
  80. {
  81. DIR *dir;
  82. struct dirent *de;
  83. dir = ::opendir(path.c_str());
  84. if(!dir)
  85. return false;
  86. while((de = ::readdir(dir)))
  87. {
  88. const char *d_name = de->d_name;
  89. if(d_name[0] == '.' &&
  90. ((d_name[1] == '\0') ||
  91. (d_name[1] == '.' && d_name[2] == '\0')))
  92. continue;
  93. ::closedir(dir);
  94. return false;
  95. }
  96. ::closedir(dir);
  97. return true;
  98. }
  99. bool
  100. exists(const vector<string> &paths,
  101. const string &fusepath)
  102. {
  103. for(size_t i = 0, ei = paths.size(); i != ei; i++)
  104. {
  105. int rv;
  106. string path;
  107. struct stat st;
  108. path = fs::path::make(paths[i],fusepath);
  109. rv = ::lstat(path.c_str(),&st);
  110. if(rv == 0)
  111. return true;
  112. }
  113. return false;
  114. }
  115. }
  116. void
  117. findallfiles(const vector<string> &srcmounts,
  118. const string &fusepath,
  119. vector<string> &paths)
  120. {
  121. for(size_t i = 0, ei = srcmounts.size(); i != ei; i++)
  122. {
  123. int rv;
  124. string fullpath;
  125. struct stat st;
  126. fullpath = fs::path::make(srcmounts[i],fusepath);
  127. rv = ::lstat(fullpath.c_str(),&st);
  128. if(rv == 0)
  129. paths.push_back(fullpath);
  130. }
  131. }
  132. int
  133. listxattr(const string &path,
  134. vector<char> &attrs)
  135. {
  136. #ifndef WITHOUT_XATTR
  137. int rv;
  138. int size;
  139. rv = -1;
  140. errno = ERANGE;
  141. while(rv == -1 && errno == ERANGE)
  142. {
  143. size = ::listxattr(path.c_str(),NULL,0);
  144. attrs.resize(size);
  145. rv = ::listxattr(path.c_str(),&attrs[0],size);
  146. }
  147. return rv;
  148. #else
  149. errno = ENOTSUP;
  150. return -1;
  151. #endif
  152. }
  153. int
  154. listxattr(const string &path,
  155. vector<string> &attrvector)
  156. {
  157. int rv;
  158. vector<char> attrs;
  159. rv = listxattr(path,attrs);
  160. if(rv != -1)
  161. {
  162. string tmp(attrs.begin(),attrs.end());
  163. str::split(attrvector,tmp,'\0');
  164. }
  165. return rv;
  166. }
  167. int
  168. listxattr(const string &path,
  169. string &attrstr)
  170. {
  171. int rv;
  172. vector<char> attrs;
  173. rv = listxattr(path,attrs);
  174. if(rv != -1)
  175. attrstr = string(attrs.begin(),attrs.end());
  176. return rv;
  177. }
  178. int
  179. getxattr(const string &path,
  180. const string &attr,
  181. vector<char> &value)
  182. {
  183. #ifndef WITHOUT_XATTR
  184. int rv;
  185. int size;
  186. rv = -1;
  187. errno = ERANGE;
  188. while(rv == -1 && errno == ERANGE)
  189. {
  190. size = ::getxattr(path.c_str(),attr.c_str(),NULL,0);
  191. value.resize(size);
  192. rv = ::getxattr(path.c_str(),attr.c_str(),&value[0],size);
  193. }
  194. return rv;
  195. #else
  196. errno = ENOTSUP;
  197. return -1;
  198. #endif
  199. }
  200. int
  201. getxattr(const string &path,
  202. const string &attr,
  203. string &value)
  204. {
  205. int rv;
  206. vector<char> tmpvalue;
  207. rv = getxattr(path,attr,tmpvalue);
  208. if(rv != -1)
  209. value = string(tmpvalue.begin(),tmpvalue.end());
  210. return rv;
  211. }
  212. int
  213. getxattrs(const string &path,
  214. map<string,string> &attrs)
  215. {
  216. int rv;
  217. string attrstr;
  218. rv = listxattr(path,attrstr);
  219. if(rv == -1)
  220. return -1;
  221. {
  222. string key;
  223. istringstream ss(attrstr);
  224. while(getline(ss,key,'\0'))
  225. {
  226. string value;
  227. rv = getxattr(path,key,value);
  228. if(rv != -1)
  229. attrs[key] = value;
  230. }
  231. }
  232. return 0;
  233. }
  234. int
  235. setxattr(const string &path,
  236. const string &key,
  237. const string &value,
  238. const int flags)
  239. {
  240. #ifndef WITHOUT_XATTR
  241. return ::setxattr(path.c_str(),
  242. key.c_str(),
  243. value.data(),
  244. value.size(),
  245. flags);
  246. #else
  247. errno = ENOTSUP;
  248. return -1;
  249. #endif
  250. }
  251. int
  252. setxattr(const int fd,
  253. const string &key,
  254. const string &value,
  255. const int flags)
  256. {
  257. #ifndef WITHOUT_XATTR
  258. return ::fsetxattr(fd,
  259. key.c_str(),
  260. value.data(),
  261. value.size(),
  262. flags);
  263. #else
  264. errno = ENOTSUP;
  265. return -1;
  266. #endif
  267. }
  268. int
  269. setxattrs(const string &path,
  270. const map<string,string> &attrs)
  271. {
  272. int fd;
  273. fd = ::open(path.c_str(),O_RDONLY|O_NONBLOCK);
  274. if(fd == -1)
  275. return -1;
  276. for(map<string,string>::const_iterator
  277. i = attrs.begin(), ei = attrs.end(); i != ei; ++i)
  278. {
  279. setxattr(fd,i->first,i->second,0);
  280. }
  281. return ::close(fd);
  282. }
  283. int
  284. copyxattrs(const string &from,
  285. const string &to)
  286. {
  287. int rv;
  288. map<string,string> attrs;
  289. rv = getxattrs(from,attrs);
  290. if(rv == -1)
  291. return -1;
  292. return setxattrs(to,attrs);
  293. }
  294. static
  295. int
  296. get_fs_ioc_flags(const string &file,
  297. int &flags)
  298. {
  299. int fd;
  300. int rv;
  301. const int openflags = O_RDONLY|O_NONBLOCK;
  302. fd = ::open(file.c_str(),openflags);
  303. if(fd == -1)
  304. return -1;
  305. rv = ::ioctl(fd,FS_IOC_GETFLAGS,&flags);
  306. if(rv == -1)
  307. {
  308. int error = errno;
  309. ::close(fd);
  310. errno = error;
  311. return -1;
  312. }
  313. return ::close(fd);
  314. }
  315. static
  316. int
  317. set_fs_ioc_flags(const string &file,
  318. const int flags)
  319. {
  320. int fd;
  321. int rv;
  322. const int openflags = O_RDONLY|O_NONBLOCK;
  323. fd = ::open(file.c_str(),openflags);
  324. if(fd == -1)
  325. return -1;
  326. rv = ::ioctl(fd,FS_IOC_SETFLAGS,&flags);
  327. if(rv == -1)
  328. {
  329. int error = errno;
  330. ::close(fd);
  331. errno = error;
  332. return -1;
  333. }
  334. return ::close(fd);
  335. }
  336. int
  337. copyattr(const string &from,
  338. const string &to)
  339. {
  340. int rv;
  341. int flags;
  342. rv = get_fs_ioc_flags(from,flags);
  343. if(rv == -1)
  344. return -1;
  345. return set_fs_ioc_flags(to,flags);
  346. }
  347. static
  348. bool
  349. ignorable_error(const int err)
  350. {
  351. switch(err)
  352. {
  353. case ENOTTY:
  354. case ENOTSUP:
  355. #if ENOTSUP != EOPNOTSUPP
  356. case EOPNOTSUPP:
  357. #endif
  358. return true;
  359. }
  360. return false;
  361. }
  362. int
  363. clonepath(const string &fromsrc,
  364. const string &tosrc,
  365. const string &relative)
  366. {
  367. int rv;
  368. struct stat st;
  369. string topath;
  370. string frompath;
  371. string dirname;
  372. dirname = fs::path::dirname(relative);
  373. if(!dirname.empty())
  374. {
  375. rv = clonepath(fromsrc,tosrc,dirname);
  376. if(rv == -1)
  377. return -1;
  378. }
  379. frompath = fs::path::make(fromsrc,relative);
  380. rv = ::stat(frompath.c_str(),&st);
  381. if(rv == -1)
  382. return -1;
  383. else if(!S_ISDIR(st.st_mode))
  384. return (errno = ENOTDIR,-1);
  385. topath = fs::path::make(tosrc,relative);
  386. rv = ::mkdir(topath.c_str(),st.st_mode);
  387. if(rv == -1)
  388. {
  389. if(errno != EEXIST)
  390. return -1;
  391. rv = ::chmod(topath.c_str(),st.st_mode);
  392. if(rv == -1)
  393. return -1;
  394. }
  395. rv = ::chown(topath.c_str(),st.st_uid,st.st_gid);
  396. if(rv == -1)
  397. return -1;
  398. // It may not support it... it's fine...
  399. rv = copyattr(frompath,topath);
  400. if(rv == -1 && !ignorable_error(errno))
  401. return -1;
  402. rv = copyxattrs(frompath,topath);
  403. if(rv == -1 && !ignorable_error(errno))
  404. return -1;
  405. return 0;
  406. }
  407. void
  408. glob(const vector<string> &patterns,
  409. vector<string> &strs)
  410. {
  411. int flags;
  412. glob_t gbuf;
  413. flags = 0;
  414. for(size_t i = 0; i < patterns.size(); i++)
  415. {
  416. glob(patterns[i].c_str(),flags,NULL,&gbuf);
  417. flags = GLOB_APPEND;
  418. }
  419. for(size_t i = 0; i < gbuf.gl_pathc; ++i)
  420. strs.push_back(gbuf.gl_pathv[i]);
  421. globfree(&gbuf);
  422. }
  423. };