PageRenderTime 34ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/xlators/features/path-convertor/src/path.c

#
C | 1239 lines | 1021 code | 188 blank | 30 comment | 112 complexity | c04847951713d041162eac5f034e65a2 MD5 | raw file
Possible License(s): LGPL-2.0, GPL-2.0, BSD-3-Clause, GPL-3.0
  1. /*
  2. Copyright (c) 2008-2009 Gluster, Inc. <http://www.gluster.com>
  3. This file is part of GlusterFS.
  4. GlusterFS is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3 of the License,
  7. or (at your option) any later version.
  8. GlusterFS is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see
  14. <http://www.gnu.org/licenses/>.
  15. */
  16. /* TODO: add gf_log to all the cases returning errors */
  17. #ifndef _CONFIG_H
  18. #define _CONFIG_H
  19. #include "config.h"
  20. #endif
  21. /**
  22. * xlators/features/path-translator:
  23. * This translator converts the path it gets into user specified targets.
  24. */
  25. #include <sys/types.h>
  26. #include <regex.h>
  27. #include <time.h>
  28. #include <errno.h>
  29. #include "glusterfs.h"
  30. #include "xlator.h"
  31. #include "path-mem-types.h"
  32. typedef struct path_private
  33. {
  34. int32_t this_len;
  35. int32_t start_off;
  36. int32_t end_off;
  37. char *this;
  38. char *that;
  39. char *path;
  40. regex_t *preg;
  41. } path_private_t;
  42. static char *
  43. name_this_to_that (xlator_t *xl, const char *path, const char *name)
  44. {
  45. path_private_t *priv = xl->private;
  46. char priv_path[ZR_PATH_MAX] = {0,};
  47. char *tmp_name = NULL;
  48. int32_t path_len = strlen (path);
  49. int32_t name_len = strlen (name) - ZR_FILE_CONTENT_STRLEN;
  50. int32_t total_len = path_len + name_len;
  51. int32_t i = 0, j = 0;
  52. if (path_len >= priv->end_off)
  53. return (char *)name;
  54. if (priv->end_off && (total_len > priv->end_off)) {
  55. j = priv->start_off;
  56. tmp_name = GF_CALLOC (1, (total_len +
  57. ZR_FILE_CONTENT_STRLEN),
  58. gf_path_mt_char);
  59. ERR_ABORT (tmp_name);
  60. /* Get the complete path for the file first */
  61. strcpy (tmp_name, path);
  62. strcat (tmp_name, name + ZR_FILE_CONTENT_STRLEN);
  63. strncpy (priv_path, tmp_name, priv->start_off);
  64. for (i = priv->start_off; i < priv->end_off; i++) {
  65. if (tmp_name[i] == '/')
  66. continue;
  67. priv_path[j++] = tmp_name[i];
  68. }
  69. memcpy ((priv_path + j),
  70. (tmp_name + priv->end_off),
  71. (total_len - priv->end_off));
  72. priv_path[(total_len - (priv->end_off - j))] = '\0';
  73. strcpy (tmp_name, ZR_FILE_CONTENT_STR);
  74. strcat (tmp_name, priv_path);
  75. return tmp_name;
  76. }
  77. return (char *)name;
  78. }
  79. /* This function should return
  80. * NULL -
  81. * converted path - if path match
  82. * same path - if it doesn't match
  83. */
  84. static char *
  85. path_this_to_that (xlator_t *xl, const char *path)
  86. {
  87. path_private_t *priv = xl->private;
  88. char *priv_path = NULL;
  89. int32_t path_len = strlen (path);
  90. int32_t i = 0, j = 0;
  91. if (priv->end_off && (path_len > priv->start_off)) {
  92. priv_path = GF_CALLOC (1, path_len, gf_path_mt_char);
  93. ERR_ABORT (priv_path);
  94. if (priv->start_off && (path_len > priv->start_off))
  95. memcpy (priv_path, path, priv->start_off);
  96. if (path_len > priv->end_off) {
  97. j = priv->start_off;
  98. for (i = priv->start_off; i < priv->end_off; i++) {
  99. if (path[i] == '/')
  100. continue;
  101. priv_path[j++] = path[i];
  102. }
  103. memcpy ((priv_path + j),
  104. (path + priv->end_off),
  105. (path_len - priv->end_off));
  106. priv_path[(path_len - (priv->end_off - j))] = '\0';
  107. }
  108. return priv_path;
  109. }
  110. return (char *)path;
  111. }
  112. int32_t
  113. path_create_cbk (call_frame_t *frame,
  114. void *cookie,
  115. xlator_t *this,
  116. int32_t op_ret,
  117. int32_t op_errno,
  118. fd_t *fd,
  119. inode_t *inode,
  120. struct iatt *buf,
  121. struct iatt *preparent,
  122. struct iatt *postparent)
  123. {
  124. STACK_UNWIND (frame, op_ret, op_errno, fd, inode, buf);
  125. return 0;
  126. }
  127. int32_t
  128. path_open_cbk (call_frame_t *frame,
  129. void *cookie,
  130. xlator_t *this,
  131. int32_t op_ret,
  132. int32_t op_errno,
  133. fd_t *fd)
  134. {
  135. STACK_UNWIND (frame, op_ret, op_errno, fd);
  136. return 0;
  137. }
  138. int32_t
  139. path_getdents_cbk (call_frame_t *frame,
  140. void *cookie,
  141. xlator_t *this,
  142. int32_t op_ret,
  143. int32_t op_errno,
  144. dir_entry_t *entries,
  145. int32_t count)
  146. {
  147. STACK_UNWIND (frame, op_ret, op_errno, entries, count);
  148. return 0;
  149. }
  150. int32_t
  151. path_readdir_cbk (call_frame_t *frame,
  152. void *cookie,
  153. xlator_t *this,
  154. int32_t op_ret,
  155. int32_t op_errno,
  156. gf_dirent_t *buf)
  157. {
  158. STACK_UNWIND (frame, op_ret, op_errno, buf);
  159. return 0;
  160. }
  161. int32_t
  162. path_readlink_cbk (call_frame_t *frame,
  163. void *cookie,
  164. xlator_t *this,
  165. int32_t op_ret,
  166. int32_t op_errno,
  167. const char *buf,
  168. struct iatt *sbuf)
  169. {
  170. STACK_UNWIND (frame, op_ret, op_errno, buf, sbuf);
  171. return 0;
  172. }
  173. int32_t
  174. path_lookup_cbk (call_frame_t *frame,
  175. void *cookie,
  176. xlator_t *this,
  177. int32_t op_ret,
  178. int32_t op_errno,
  179. inode_t *inode,
  180. struct iatt *buf,
  181. dict_t *xattr,
  182. struct iatt *postparent)
  183. {
  184. STACK_UNWIND (frame, op_ret, op_errno, inode, buf, xattr);
  185. return 0;
  186. }
  187. int32_t
  188. path_symlink_cbk (call_frame_t *frame,
  189. void *cookie,
  190. xlator_t *this,
  191. int32_t op_ret,
  192. int32_t op_errno,
  193. inode_t *inode,
  194. struct iatt *buf,
  195. struct iatt *preparent,
  196. struct iatt *postparent)
  197. {
  198. STACK_UNWIND (frame, op_ret, op_errno, inode, buf);
  199. return 0;
  200. }
  201. int32_t
  202. path_mknod_cbk (call_frame_t *frame,
  203. void *cookie,
  204. xlator_t *this,
  205. int32_t op_ret,
  206. int32_t op_errno,
  207. inode_t *inode,
  208. struct iatt *buf,
  209. struct iatt *preparent,
  210. struct iatt *postparent)
  211. {
  212. STACK_UNWIND (frame, op_ret, op_errno, inode, buf);
  213. return 0;
  214. }
  215. int32_t
  216. path_mkdir_cbk (call_frame_t *frame,
  217. void *cookie,
  218. xlator_t *this,
  219. int32_t op_ret,
  220. int32_t op_errno,
  221. inode_t *inode,
  222. struct iatt *buf,
  223. struct iatt *preparent,
  224. struct iatt *postparent)
  225. {
  226. STACK_UNWIND (frame, op_ret, op_errno, inode, buf);
  227. return 0;
  228. }
  229. int32_t
  230. path_link_cbk (call_frame_t *frame,
  231. void *cookie,
  232. xlator_t *this,
  233. int32_t op_ret,
  234. int32_t op_errno,
  235. inode_t *inode,
  236. struct iatt *buf,
  237. struct iatt *preparent,
  238. struct iatt *postparent)
  239. {
  240. STACK_UNWIND (frame, op_ret, op_errno, inode, buf);
  241. return 0;
  242. }
  243. int32_t
  244. path_opendir_cbk (call_frame_t *frame,
  245. void *cookie,
  246. xlator_t *this,
  247. int32_t op_ret,
  248. int32_t op_errno,
  249. fd_t *fd)
  250. {
  251. STACK_UNWIND (frame, op_ret, op_errno, fd);
  252. return 0;
  253. }
  254. int32_t
  255. path_rename_buf_cbk (call_frame_t *frame,
  256. void *cookie,
  257. xlator_t *this,
  258. int32_t op_ret,
  259. int32_t op_errno,
  260. struct iatt *buf,
  261. struct iatt *preoldparent,
  262. struct iatt *postoldparent,
  263. struct iatt *prenewparent,
  264. struct iatt *postnewparent)
  265. {
  266. STACK_UNWIND (frame, op_ret, op_errno, buf);
  267. return 0;
  268. }
  269. int32_t
  270. path_common_buf_cbk (call_frame_t *frame,
  271. void *cookie,
  272. xlator_t *this,
  273. int32_t op_ret,
  274. int32_t op_errno,
  275. struct iatt *buf)
  276. {
  277. STACK_UNWIND (frame, op_ret, op_errno, buf);
  278. return 0;
  279. }
  280. int32_t
  281. path_common_dict_cbk (call_frame_t *frame,
  282. void *cookie,
  283. xlator_t *this,
  284. int32_t op_ret,
  285. int32_t op_errno,
  286. dict_t *dict)
  287. {
  288. STACK_UNWIND (frame, op_ret, op_errno, dict);
  289. return 0;
  290. }
  291. int32_t
  292. path_common_remove_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
  293. int32_t op_ret, int32_t op_errno,struct iatt *preparent,
  294. struct iatt *postparent)
  295. {
  296. STACK_UNWIND (frame, op_ret, op_errno);
  297. return 0;
  298. }
  299. int32_t
  300. path_truncate_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
  301. int32_t op_ret, int32_t op_errno,struct iatt *prebuf,
  302. struct iatt *postbuf)
  303. {
  304. STACK_UNWIND (frame, op_ret, op_errno, prebuf, postbuf);
  305. return 0;
  306. }
  307. int32_t
  308. path_common_cbk (call_frame_t *frame,
  309. void *cookie,
  310. xlator_t *this,
  311. int32_t op_ret,
  312. int32_t op_errno)
  313. {
  314. STACK_UNWIND (frame, op_ret, op_errno);
  315. return 0;
  316. }
  317. /* */
  318. int32_t
  319. path_lookup (call_frame_t *frame,
  320. xlator_t *this,
  321. loc_t *loc,
  322. dict_t *xattr_req)
  323. {
  324. char *loc_path = (char *)loc->path;
  325. char *tmp_path = NULL;
  326. if (!(tmp_path = path_this_to_that (this, loc->path))) {
  327. STACK_UNWIND (frame, -1, ENOENT, NULL, NULL);
  328. return 0;
  329. }
  330. loc->path = tmp_path;
  331. STACK_WIND (frame, path_lookup_cbk,
  332. FIRST_CHILD(this),
  333. FIRST_CHILD(this)->fops->lookup,
  334. loc, xattr_req);
  335. loc->path = loc_path;
  336. if (tmp_path != loc_path)
  337. GF_FREE (tmp_path);
  338. return 0;
  339. }
  340. int32_t
  341. path_stat (call_frame_t *frame,
  342. xlator_t *this,
  343. loc_t *loc)
  344. {
  345. char *loc_path = (char *)loc->path;
  346. char *tmp_path = NULL;
  347. if (!(tmp_path = path_this_to_that (this, loc->path))) {
  348. STACK_UNWIND (frame, -1, ENOENT, NULL, NULL);
  349. return 0;
  350. }
  351. loc->path = tmp_path;
  352. STACK_WIND (frame,
  353. path_common_buf_cbk,
  354. FIRST_CHILD(this),
  355. FIRST_CHILD(this)->fops->stat,
  356. loc);
  357. loc->path = loc_path;
  358. if (tmp_path != loc_path)
  359. GF_FREE (tmp_path);
  360. return 0;
  361. }
  362. int32_t
  363. path_readlink (call_frame_t *frame,
  364. xlator_t *this,
  365. loc_t *loc,
  366. size_t size)
  367. {
  368. char *loc_path = (char *)loc->path;
  369. char *tmp_path = NULL;
  370. if (!(tmp_path = path_this_to_that (this, loc->path))) {
  371. STACK_UNWIND (frame, -1, ENOENT, NULL, NULL);
  372. return 0;
  373. }
  374. loc->path = tmp_path;
  375. STACK_WIND (frame,
  376. path_readlink_cbk,
  377. FIRST_CHILD(this),
  378. FIRST_CHILD(this)->fops->readlink,
  379. loc,
  380. size);
  381. loc->path = loc_path;
  382. if (tmp_path != loc_path)
  383. GF_FREE (tmp_path);
  384. return 0;
  385. }
  386. int32_t
  387. path_mknod (call_frame_t *frame,
  388. xlator_t *this,
  389. loc_t *loc,
  390. mode_t mode,
  391. dev_t dev)
  392. {
  393. char *loc_path = (char *)loc->path;
  394. char *tmp_path = NULL;
  395. if (!(tmp_path = path_this_to_that (this, loc->path))) {
  396. STACK_UNWIND (frame, -1, ENOENT, NULL, NULL);
  397. return 0;
  398. }
  399. loc->path = tmp_path;
  400. STACK_WIND (frame,
  401. path_mknod_cbk,
  402. FIRST_CHILD(this),
  403. FIRST_CHILD(this)->fops->mknod,
  404. loc,
  405. mode,
  406. dev);
  407. loc->path = loc_path;
  408. if (tmp_path != loc_path)
  409. GF_FREE (tmp_path);
  410. return 0;
  411. }
  412. int32_t
  413. path_mkdir (call_frame_t *frame,
  414. xlator_t *this,
  415. loc_t *loc,
  416. mode_t mode)
  417. {
  418. char *loc_path = (char *)loc->path;
  419. char *tmp_path = NULL;
  420. if (!(tmp_path = path_this_to_that (this, loc->path))) {
  421. STACK_UNWIND (frame, -1, ENOENT, NULL, NULL);
  422. return 0;
  423. }
  424. loc->path = tmp_path;
  425. STACK_WIND (frame,
  426. path_mkdir_cbk,
  427. FIRST_CHILD(this),
  428. FIRST_CHILD(this)->fops->mkdir,
  429. loc,
  430. mode);
  431. loc->path = loc_path;
  432. if (tmp_path != loc_path)
  433. GF_FREE (tmp_path);
  434. return 0;
  435. }
  436. int32_t
  437. path_unlink (call_frame_t *frame,
  438. xlator_t *this,
  439. loc_t *loc)
  440. {
  441. char *loc_path = (char *)loc->path;
  442. char *tmp_path = NULL;
  443. if (!(tmp_path = path_this_to_that (this, loc->path))) {
  444. STACK_UNWIND (frame, -1, ENOENT, NULL, NULL);
  445. return 0;
  446. }
  447. loc->path = tmp_path;
  448. STACK_WIND (frame,
  449. path_common_remove_cbk,
  450. FIRST_CHILD(this),
  451. FIRST_CHILD(this)->fops->unlink,
  452. loc);
  453. loc->path = loc_path;
  454. if (tmp_path != loc_path)
  455. GF_FREE (tmp_path);
  456. return 0;
  457. }
  458. int32_t
  459. path_rmdir (call_frame_t *frame,
  460. xlator_t *this,
  461. loc_t *loc)
  462. {
  463. char *loc_path = (char *)loc->path;
  464. char *tmp_path = NULL;
  465. if (!(tmp_path = path_this_to_that (this, loc->path))) {
  466. STACK_UNWIND (frame, -1, ENOENT, NULL, NULL);
  467. return 0;
  468. }
  469. loc->path = tmp_path;
  470. STACK_WIND (frame,
  471. path_common_remove_cbk,
  472. FIRST_CHILD(this),
  473. FIRST_CHILD(this)->fops->rmdir,
  474. loc);
  475. loc->path = loc_path;
  476. if (tmp_path != loc_path)
  477. GF_FREE (tmp_path);
  478. return 0;
  479. }
  480. int32_t
  481. path_symlink (call_frame_t *frame,
  482. xlator_t *this,
  483. const char *linkpath,
  484. loc_t *loc)
  485. {
  486. char *loc_path = (char *)loc->path;
  487. char *tmp_path = NULL;
  488. if (!(tmp_path = path_this_to_that (this, loc->path))) {
  489. STACK_UNWIND (frame, -1, ENOENT, NULL, NULL);
  490. return 0;
  491. }
  492. loc->path = tmp_path;
  493. STACK_WIND (frame,
  494. path_symlink_cbk,
  495. FIRST_CHILD(this),
  496. FIRST_CHILD(this)->fops->symlink,
  497. linkpath,
  498. loc);
  499. loc->path = loc_path;
  500. if (tmp_path != loc_path)
  501. GF_FREE (tmp_path);
  502. return 0;
  503. }
  504. int32_t
  505. path_rename (call_frame_t *frame,
  506. xlator_t *this,
  507. loc_t *oldloc,
  508. loc_t *newloc)
  509. {
  510. char *oldloc_path = (char *)oldloc->path;
  511. char *tmp_oldloc_path = NULL;
  512. char *newloc_path = (char *)newloc->path;
  513. char *tmp_newloc_path = NULL;
  514. if (!(tmp_oldloc_path = path_this_to_that (this, oldloc->path))) {
  515. STACK_UNWIND (frame, -1, ENOENT, NULL, NULL);
  516. return 0;
  517. }
  518. oldloc->path = tmp_oldloc_path;
  519. if (!(tmp_newloc_path = path_this_to_that (this, newloc->path))) {
  520. STACK_UNWIND (frame, -1, ENOENT, NULL, NULL);
  521. return 0;
  522. }
  523. newloc->path = tmp_newloc_path;
  524. STACK_WIND (frame,
  525. path_rename_buf_cbk,
  526. FIRST_CHILD(this),
  527. FIRST_CHILD(this)->fops->rename,
  528. oldloc,
  529. newloc);
  530. oldloc->path = oldloc_path;
  531. if (tmp_oldloc_path != oldloc_path)
  532. GF_FREE (tmp_oldloc_path);
  533. newloc->path = newloc_path;
  534. if (tmp_newloc_path != newloc_path)
  535. GF_FREE (tmp_newloc_path);
  536. return 0;
  537. }
  538. int32_t
  539. path_link (call_frame_t *frame,
  540. xlator_t *this,
  541. loc_t *oldloc,
  542. loc_t *newloc)
  543. {
  544. char *oldloc_path = (char *)oldloc->path;
  545. char *tmp_oldloc_path = NULL;
  546. char *newloc_path = (char *)newloc->path;
  547. char *tmp_newloc_path = NULL;
  548. if (!(tmp_oldloc_path = path_this_to_that (this, oldloc->path))) {
  549. STACK_UNWIND (frame, -1, ENOENT, NULL, NULL);
  550. return 0;
  551. }
  552. oldloc->path = tmp_oldloc_path;
  553. if (!(tmp_newloc_path = path_this_to_that (this, newloc->path))) {
  554. STACK_UNWIND (frame, -1, ENOENT, NULL, NULL);
  555. return 0;
  556. }
  557. newloc->path = tmp_newloc_path;
  558. STACK_WIND (frame,
  559. path_link_cbk,
  560. FIRST_CHILD(this),
  561. FIRST_CHILD(this)->fops->link,
  562. oldloc,
  563. newloc);
  564. oldloc->path = oldloc_path;
  565. if (tmp_oldloc_path != oldloc_path)
  566. GF_FREE (tmp_oldloc_path);
  567. newloc->path = newloc_path;
  568. if (tmp_newloc_path != newloc_path)
  569. GF_FREE (tmp_newloc_path);
  570. return 0;
  571. }
  572. int32_t
  573. path_setattr_cbk (call_frame_t *frame,
  574. void *cookie,
  575. xlator_t *this,
  576. int32_t op_ret,
  577. int32_t op_errno,
  578. struct iatt *preop,
  579. struct iatt *postop)
  580. {
  581. STACK_UNWIND (frame, op_ret, op_errno, preop, postop);
  582. return 0;
  583. }
  584. int32_t
  585. path_setattr (call_frame_t *frame,
  586. xlator_t *this,
  587. loc_t *loc,
  588. struct iatt *stbuf,
  589. int32_t valid)
  590. {
  591. char *loc_path = (char *)loc->path;
  592. char *tmp_path = NULL;
  593. if (!(tmp_path = path_this_to_that (this, loc->path))) {
  594. STACK_UNWIND (frame, -1, ENOENT, NULL, NULL);
  595. return 0;
  596. }
  597. loc->path = tmp_path;
  598. STACK_WIND (frame,
  599. path_setattr_cbk,
  600. FIRST_CHILD(this),
  601. FIRST_CHILD(this)->fops->setattr,
  602. loc,
  603. stbuf, valid);
  604. loc->path = loc_path;
  605. if (tmp_path != loc_path)
  606. GF_FREE (tmp_path);
  607. return 0;
  608. }
  609. int32_t
  610. path_truncate (call_frame_t *frame,
  611. xlator_t *this,
  612. loc_t *loc,
  613. off_t offset)
  614. {
  615. char *loc_path = (char *)loc->path;
  616. char *tmp_path = NULL;
  617. if (!(tmp_path = path_this_to_that (this, loc->path))) {
  618. STACK_UNWIND (frame, -1, ENOENT, NULL, NULL);
  619. return 0;
  620. }
  621. loc->path = tmp_path;
  622. STACK_WIND (frame,
  623. path_truncate_cbk,
  624. FIRST_CHILD(this),
  625. FIRST_CHILD(this)->fops->truncate,
  626. loc,
  627. offset);
  628. loc->path = loc_path;
  629. if (tmp_path != loc_path)
  630. GF_FREE (tmp_path);
  631. return 0;
  632. }
  633. int32_t
  634. path_open (call_frame_t *frame,
  635. xlator_t *this,
  636. loc_t *loc,
  637. int32_t flags,
  638. fd_t *fd,
  639. int32_t wbflags)
  640. {
  641. char *loc_path = (char *)loc->path;
  642. char *tmp_path = NULL;
  643. if (!(tmp_path = path_this_to_that (this, loc->path))) {
  644. STACK_UNWIND (frame, -1, ENOENT, NULL, NULL);
  645. return 0;
  646. }
  647. loc->path = tmp_path;
  648. STACK_WIND (frame,
  649. path_open_cbk,
  650. FIRST_CHILD(this),
  651. FIRST_CHILD(this)->fops->open,
  652. loc,
  653. flags,
  654. fd,
  655. wbflags);
  656. loc->path = loc_path;
  657. if (tmp_path != loc_path)
  658. GF_FREE (tmp_path);
  659. return 0;
  660. }
  661. int32_t
  662. path_create (call_frame_t *frame,
  663. xlator_t *this,
  664. loc_t *loc,
  665. int32_t flags,
  666. mode_t mode,
  667. fd_t *fd)
  668. {
  669. char *loc_path = (char *)loc->path;
  670. char *tmp_path = NULL;
  671. if (!(tmp_path = path_this_to_that (this, loc->path))) {
  672. STACK_UNWIND (frame, -1, ENOENT, NULL, NULL);
  673. return 0;
  674. }
  675. loc->path = tmp_path;
  676. STACK_WIND (frame,
  677. path_create_cbk,
  678. FIRST_CHILD(this),
  679. FIRST_CHILD(this)->fops->create,
  680. loc,
  681. flags,
  682. mode,
  683. fd);
  684. loc->path = loc_path;
  685. if (tmp_path != loc_path)
  686. GF_FREE (tmp_path);
  687. return 0;
  688. }
  689. int32_t
  690. path_setxattr (call_frame_t *frame,
  691. xlator_t *this,
  692. loc_t *loc,
  693. dict_t *dict,
  694. int32_t flags)
  695. {
  696. char *tmp_name = NULL;
  697. data_pair_t *trav = dict->members_list;
  698. char *loc_path = (char *)loc->path;
  699. char *tmp_path = NULL;
  700. if (!(tmp_path = path_this_to_that (this, loc->path))) {
  701. STACK_UNWIND (frame, -1, ENOENT, NULL, NULL);
  702. return 0;
  703. }
  704. loc->path = tmp_path;
  705. if (ZR_FILE_CONTENT_REQUEST(trav->key)) {
  706. tmp_name = name_this_to_that (this, loc->path, trav->key);
  707. if (tmp_name != trav->key) {
  708. trav->key = tmp_name;
  709. } else {
  710. tmp_name = NULL;
  711. }
  712. }
  713. STACK_WIND (frame,
  714. path_common_cbk,
  715. FIRST_CHILD(this),
  716. FIRST_CHILD(this)->fops->setxattr,
  717. loc,
  718. dict,
  719. flags);
  720. loc->path = loc_path;
  721. if (tmp_path != loc_path)
  722. GF_FREE (tmp_path);
  723. if (tmp_name)
  724. GF_FREE (tmp_name);
  725. return 0;
  726. }
  727. int32_t
  728. path_getxattr (call_frame_t *frame,
  729. xlator_t *this,
  730. loc_t *loc,
  731. const char *name)
  732. {
  733. char *tmp_name = (char *)name;
  734. char *loc_path = (char *)loc->path;
  735. char *tmp_path = NULL;
  736. if (!(tmp_path = path_this_to_that (this, loc->path))) {
  737. STACK_UNWIND (frame, -1, ENOENT, NULL, NULL);
  738. return 0;
  739. }
  740. loc->path = tmp_path;
  741. if (ZR_FILE_CONTENT_REQUEST(name)) {
  742. tmp_name = name_this_to_that (this, loc->path, name);
  743. }
  744. STACK_WIND (frame,
  745. path_common_dict_cbk,
  746. FIRST_CHILD(this),
  747. FIRST_CHILD(this)->fops->getxattr,
  748. loc,
  749. tmp_name);
  750. loc->path = loc_path;
  751. if (tmp_path != loc_path)
  752. GF_FREE (tmp_path);
  753. if (tmp_name != name)
  754. GF_FREE (tmp_name);
  755. return 0;
  756. }
  757. int32_t
  758. path_removexattr (call_frame_t *frame,
  759. xlator_t *this,
  760. loc_t *loc,
  761. const char *name)
  762. {
  763. char *tmp_name = (char *)name;
  764. char *loc_path = (char *)loc->path;
  765. char *tmp_path = NULL;
  766. if (!(tmp_path = path_this_to_that (this, loc->path))) {
  767. STACK_UNWIND (frame, -1, ENOENT, NULL, NULL);
  768. return 0;
  769. }
  770. loc->path = tmp_path;
  771. if (ZR_FILE_CONTENT_REQUEST(name)) {
  772. tmp_name = name_this_to_that (this, loc->path, name);
  773. }
  774. STACK_WIND (frame,
  775. path_common_cbk,
  776. FIRST_CHILD(this),
  777. FIRST_CHILD(this)->fops->removexattr,
  778. loc,
  779. tmp_name);
  780. loc->path = loc_path;
  781. if (tmp_path != loc_path)
  782. GF_FREE (tmp_path);
  783. if (tmp_name != name)
  784. GF_FREE (tmp_name);
  785. return 0;
  786. }
  787. int32_t
  788. path_opendir (call_frame_t *frame,
  789. xlator_t *this,
  790. loc_t *loc,
  791. fd_t *fd)
  792. {
  793. char *loc_path = (char *)loc->path;
  794. char *tmp_path = NULL;
  795. if (!(tmp_path = path_this_to_that (this, loc->path))) {
  796. STACK_UNWIND (frame, -1, ENOENT, NULL, NULL);
  797. return 0;
  798. }
  799. loc->path = tmp_path;
  800. STACK_WIND (frame,
  801. path_opendir_cbk,
  802. FIRST_CHILD(this),
  803. FIRST_CHILD(this)->fops->opendir,
  804. loc,
  805. fd);
  806. loc->path = loc_path;
  807. if (tmp_path != loc_path)
  808. GF_FREE (tmp_path);
  809. return 0;
  810. }
  811. int32_t
  812. path_access (call_frame_t *frame,
  813. xlator_t *this,
  814. loc_t *loc,
  815. int32_t mask)
  816. {
  817. char *loc_path = (char *)loc->path;
  818. char *tmp_path = NULL;
  819. if (!(tmp_path = path_this_to_that (this, loc->path))) {
  820. STACK_UNWIND (frame, -1, ENOENT, NULL, NULL);
  821. return 0;
  822. }
  823. loc->path = tmp_path;
  824. STACK_WIND (frame,
  825. path_common_cbk,
  826. FIRST_CHILD(this),
  827. FIRST_CHILD(this)->fops->access,
  828. loc,
  829. mask);
  830. loc->path = loc_path;
  831. if (tmp_path != loc_path)
  832. GF_FREE (tmp_path);
  833. return 0;
  834. }
  835. int32_t
  836. path_checksum_cbk (call_frame_t *frame,
  837. void *cookie,
  838. xlator_t *this,
  839. int32_t op_ret,
  840. int32_t op_errno,
  841. uint8_t *fchecksum,
  842. uint8_t *dchecksum)
  843. {
  844. STACK_UNWIND (frame, op_ret, op_errno, fchecksum, dchecksum);
  845. return 0;
  846. }
  847. int32_t
  848. path_checksum (call_frame_t *frame,
  849. xlator_t *this,
  850. loc_t *loc,
  851. int32_t flag)
  852. {
  853. char *loc_path = (char *)loc->path;
  854. char *tmp_path = NULL;
  855. if (!(tmp_path = path_this_to_that (this, loc->path))) {
  856. STACK_UNWIND (frame, -1, ENOENT, NULL, NULL);
  857. return 0;
  858. }
  859. loc->path = tmp_path;
  860. STACK_WIND (frame,
  861. path_checksum_cbk,
  862. FIRST_CHILD(this),
  863. FIRST_CHILD(this)->fops->checksum,
  864. loc,
  865. flag);
  866. loc->path = loc_path;
  867. if (tmp_path != loc_path)
  868. GF_FREE (tmp_path);
  869. return 0;
  870. }
  871. int32_t
  872. path_entrylk (call_frame_t *frame, xlator_t *this,
  873. const char *volume, loc_t *loc, const char *basename,
  874. entrylk_cmd cmd, entrylk_type type)
  875. {
  876. char *loc_path = (char *)loc->path;
  877. char *tmp_path = NULL;
  878. if (!(tmp_path = path_this_to_that (this, loc->path))) {
  879. STACK_UNWIND (frame, -1, ENOENT, NULL, NULL);
  880. return 0;
  881. }
  882. loc->path = tmp_path;
  883. STACK_WIND (frame, path_common_cbk,
  884. FIRST_CHILD(this),
  885. FIRST_CHILD(this)->fops->entrylk,
  886. volume, loc, basename, cmd, type);
  887. loc->path = loc_path;
  888. if (tmp_path != loc_path)
  889. GF_FREE (tmp_path);
  890. return 0;
  891. }
  892. int32_t
  893. path_inodelk (call_frame_t *frame, xlator_t *this,
  894. const char *volume, loc_t *loc, int32_t cmd, struct flock *lock)
  895. {
  896. char *loc_path = (char *)loc->path;
  897. char *tmp_path = NULL;
  898. if (!(tmp_path = path_this_to_that (this, loc->path))) {
  899. STACK_UNWIND (frame, -1, ENOENT, NULL, NULL);
  900. return 0;
  901. }
  902. loc->path = tmp_path;
  903. STACK_WIND (frame,
  904. path_common_cbk,
  905. FIRST_CHILD(this),
  906. FIRST_CHILD(this)->fops->inodelk,
  907. volume, loc, cmd, lock);
  908. loc->path = loc_path;
  909. if (tmp_path != loc_path)
  910. GF_FREE (tmp_path);
  911. return 0;
  912. }
  913. int32_t
  914. path_xattrop (call_frame_t *frame,
  915. xlator_t *this,
  916. loc_t *loc,
  917. gf_xattrop_flags_t flags,
  918. dict_t *dict)
  919. {
  920. char *loc_path = (char *)loc->path;
  921. char *tmp_path = NULL;
  922. if (!(tmp_path = path_this_to_that (this, loc->path))) {
  923. STACK_UNWIND (frame, -1, ENOENT, NULL, NULL);
  924. return 0;
  925. }
  926. loc->path = tmp_path;
  927. STACK_WIND (frame,
  928. path_common_dict_cbk,
  929. FIRST_CHILD(this),
  930. FIRST_CHILD(this)->fops->xattrop,
  931. loc,
  932. flags,
  933. dict);
  934. loc->path = loc_path;
  935. if (tmp_path != loc_path)
  936. GF_FREE (tmp_path);
  937. return 0;
  938. }
  939. int32_t
  940. mem_acct_init (xlator_t *this)
  941. {
  942. int ret = -1;
  943. if (!this)
  944. return ret;
  945. ret = xlator_mem_acct_init (this, gf_path_mt_end + 1);
  946. if (ret != 0) {
  947. gf_log (this->name, GF_LOG_ERROR, "Memory accounting init"
  948. "failed");
  949. return ret;
  950. }
  951. return ret;
  952. }
  953. int32_t
  954. init (xlator_t *this)
  955. {
  956. dict_t *options = this->options;
  957. path_private_t *priv = NULL;
  958. if (!this->children || this->children->next) {
  959. gf_log (this->name, GF_LOG_ERROR,
  960. "path translator requires exactly one subvolume");
  961. return -1;
  962. }
  963. if (!this->parents) {
  964. gf_log (this->name, GF_LOG_WARNING,
  965. "dangling volume. check volfile ");
  966. }
  967. priv = GF_CALLOC (1, sizeof (*priv), gf_path_mt_path_private_t);
  968. ERR_ABORT (priv);
  969. if (dict_get (options, "start-offset")) {
  970. priv->start_off = data_to_int32 (dict_get (options,
  971. "start-offset"));
  972. }
  973. if (dict_get (options, "end-offset")) {
  974. priv->end_off = data_to_int32 (dict_get (options,
  975. "end-offset"));
  976. }
  977. if (dict_get (options, "regex")) {
  978. int32_t ret = 0;
  979. priv->preg = GF_CALLOC (1, sizeof (regex_t),
  980. gf_path_mt_regex_t);
  981. ERR_ABORT (priv->preg);
  982. ret = regcomp (priv->preg,
  983. data_to_str (dict_get (options, "regex")),
  984. REG_EXTENDED);
  985. if (ret) {
  986. gf_log (this->name, GF_LOG_ERROR,
  987. "Failed to compile the 'option regex'");
  988. GF_FREE (priv);
  989. return -1;
  990. }
  991. if (dict_get (options, "replace-with")) {
  992. priv->that = data_to_str (dict_get (options,
  993. "replace-with"));
  994. } else {
  995. priv->that = "";
  996. }
  997. }
  998. this->private = priv;
  999. return 0;
  1000. }
  1001. void
  1002. fini (xlator_t *this)
  1003. {
  1004. return;
  1005. }
  1006. struct xlator_fops fops = {
  1007. .stat = path_stat,
  1008. .readlink = path_readlink,
  1009. .mknod = path_mknod,
  1010. .mkdir = path_mkdir,
  1011. .unlink = path_unlink,
  1012. .rmdir = path_rmdir,
  1013. .symlink = path_symlink,
  1014. .rename = path_rename,
  1015. .link = path_link,
  1016. .truncate = path_truncate,
  1017. .open = path_open,
  1018. .setxattr = path_setxattr,
  1019. .getxattr = path_getxattr,
  1020. .removexattr = path_removexattr,
  1021. .opendir = path_opendir,
  1022. .access = path_access,
  1023. .create = path_create,
  1024. .lookup = path_lookup,
  1025. .checksum = path_checksum,
  1026. .xattrop = path_xattrop,
  1027. .entrylk = path_entrylk,
  1028. .inodelk = path_inodelk,
  1029. .setattr = path_setattr,
  1030. };
  1031. struct xlator_cbks cbks = {
  1032. };
  1033. struct volume_options options[] = {
  1034. { .key = {"start-offset"},
  1035. .type = GF_OPTION_TYPE_INT,
  1036. .min = 0,
  1037. .max = 4095
  1038. },
  1039. { .key = {"end-offset"},
  1040. .type = GF_OPTION_TYPE_INT,
  1041. .min = 1,
  1042. .max = 4096
  1043. },
  1044. { .key = {"replace-with"},
  1045. .type = GF_OPTION_TYPE_ANY
  1046. },
  1047. { .key = {NULL} },
  1048. };