Guest User

Untitled

a guest
Dec 15th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.08 KB | None | 0 0
  1. /* $NetBSD: datefs.c,v 1.18 2008/11/26 14:03:48 pooka Exp $ */
  2.  
  3. /*
  4. * Copyright (c) 2007 Antti Kantee. All Rights Reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
  16. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. * SUCH DAMAGE.
  26. */
  27.  
  28. /*
  29. * datefs: puffs nullfs example
  30. */
  31.  
  32. #include <err.h>
  33. #include <puffs.h>
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <unistd.h>
  38. #include <time.h>
  39.  
  40. static void usage(void);
  41.  
  42. PUFFSOP_PROTOS(datefs)
  43.  
  44. static void
  45. usage()
  46. {
  47.  
  48. errx(1, "usage: %s [-s] [-o mntopts] nullpath mountpath",
  49. getprogname());
  50. }
  51.  
  52. int
  53. main(int argc, char *argv[])
  54. {
  55. struct puffs_usermount *pu;
  56. struct puffs_ops *pops;
  57. struct puffs_pathobj *po_root;
  58. struct puffs_node *node;
  59. struct stat sb;
  60. mntoptparse_t mp;
  61. int mntflags, pflags;
  62. int ch;
  63. int detach;
  64.  
  65. setprogname(argv[0]);
  66.  
  67. if (argc < 3)
  68. usage();
  69.  
  70. pflags = mntflags = 0;
  71. detach = 1;
  72. while ((ch = getopt(argc, argv, "o:s")) != -1) {
  73. switch (ch) {
  74. case 'o':
  75. mp = getmntopts(optarg, puffsmopts, &mntflags, &pflags);
  76. if (mp == NULL)
  77. err(1, "getmntopts");
  78. freemntopts(mp);
  79. break;
  80. case 's':
  81. detach = 0;
  82. break;
  83. }
  84. }
  85. pflags |= PUFFS_FLAG_BUILDPATH;
  86. argv += optind;
  87. argc -= optind;
  88.  
  89. if (pflags & PUFFS_FLAG_OPDUMP)
  90. detach = 0;
  91.  
  92. if (argc != 2)
  93. usage();
  94.  
  95. if (lstat(argv[0], &sb) == -1)
  96. err(1, "stat %s", argv[0]);
  97. if ((sb.st_mode & S_IFDIR) == 0)
  98. errx(1, "%s is not a directory", argv[0]);
  99.  
  100. PUFFSOP_INIT(pops);
  101. puffs_null_setops(pops);
  102.  
  103. PUFFSOP_SET(pops, datefs, node, readdir);
  104. PUFFSOP_SET(pops, datefs, node, read);
  105. PUFFSOP_SET(pops, datefs, node, write);
  106.  
  107. if ((pu = puffs_init(pops, argv[0], "datefs", NULL, pflags)) == NULL)
  108. err(1, "init");
  109.  
  110. node = puffs_pn_new(pu, NULL);
  111. if (node == NULL)
  112. err(1, "puffs_pn_new");
  113. puffs_setroot(pu, node);
  114. puffs_setfhsize(pu, 0, PUFFS_FHFLAG_PASSTHROUGH);
  115.  
  116. po_root = puffs_getrootpathobj(pu);
  117. if (po_root == NULL)
  118. err(1, "getrootpathobj");
  119. po_root->po_path = argv[0];
  120. po_root->po_len = strlen(argv[0]);
  121. puffs_stat2vattr(&node->pn_va, &sb);
  122.  
  123. if (detach)
  124. if (puffs_daemon(pu, 1, 1) == -1)
  125. err(1, "puffs_daemon");
  126.  
  127. if (puffs_mount(pu, argv[1], mntflags, node) == -1)
  128. err(1, "puffs_mount");
  129. if (puffs_mainloop(pu) == -1)
  130. err(1, "mainloop");
  131.  
  132. return 0;
  133. }
  134.  
  135. static void
  136. get_date(char *buf, size_t bufsz)
  137. {
  138. static time_t tval;
  139. struct tm *tm;
  140.  
  141. if (time(&tval) == -1) {
  142. strncpy(buf, "unknown_date.txt", bufsz);
  143. return;
  144. }
  145.  
  146. if ((tm = localtime(&tval)) == NULL) {
  147. strncpy(buf, "unknown_date.txt", bufsz);
  148. }
  149.  
  150. strftime(buf, bufsz, "%Y%m%d-%H%M%S.txt", tm);
  151.  
  152. return;
  153. }
  154.  
  155. int
  156. datefs_node_readdir(
  157. struct puffs_usermount *pu,
  158. void *opc, struct dirent *dent,
  159. off_t *readoff,
  160. size_t *reslen,
  161. const struct puffs_cred *pcr,
  162. int *eofflag,
  163. off_t *cookies,
  164. size_t *ncookies)
  165. {
  166. struct dirent *dp;
  167. size_t rl;
  168. int rv;
  169. char buf[BUFSIZ];
  170.  
  171. dp = dent;
  172. rl = *reslen;
  173.  
  174. rv = puffs_null_node_readdir(pu, opc, dent, readoff, reslen, pcr,
  175. eofflag, cookies, ncookies);
  176. if (rv)
  177. return rv;
  178.  
  179. get_date(buf, BUFSIZ);
  180.  
  181. while (rl > *reslen) {
  182. strcpy(dp->d_name, buf);
  183. dp->d_namlen = strlen(buf);
  184.  
  185. rl -= _DIRENT_SIZE(dp);
  186. dp = _DIRENT_NEXT(dp);
  187. }
  188.  
  189. return 0;
  190. }
  191.  
  192. int
  193. datefs_node_read(
  194. struct puffs_usermount *pu,
  195. void *opc,
  196. uint8_t *buf,
  197. off_t offset,
  198. size_t *resid,
  199. const struct puffs_cred *pcr,
  200. int ioflag)
  201. {
  202. uint8_t *prebuf = buf;
  203. size_t preres = *resid;
  204. int rv;
  205.  
  206. rv = puffs_null_node_read(pu, opc, buf, offset, resid, pcr, ioflag);
  207. if (rv)
  208. return rv;
  209.  
  210. get_date((char *)prebuf, preres - *resid);
  211.  
  212. return rv;
  213. }
  214.  
  215. int
  216. datefs_node_write(
  217. struct puffs_usermount *pu,
  218. void *opc,
  219. uint8_t *buf,
  220. off_t offset,
  221. size_t *resid,
  222. const struct puffs_cred *pcr,
  223. int ioflag)
  224. {
  225. get_date((char *)buf, *resid);
  226. return puffs_null_node_write(pu, opc, buf, offset, resid, pcr, ioflag);
  227. }
Add Comment
Please, Sign In to add comment