local.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. #include <config.h>
  2. #include <errno.h>
  3. #include <sys/types.h>
  4. #include <malloc.h>
  5. #include <unistd.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include "../src/mad.h"
  9. #include "../src/fs.h"
  10. #include <fcntl.h>
  11. #include "../src/util.h"
  12. #include "vfs.h"
  13. /* Note: Some of this functions are not static. This has rather good
  14. * reason: exactly same functions would have to appear in sfs.c. This
  15. * saves both computer's memory and my work. <pavel@ucw.cz>
  16. * */
  17. static void *local_open (vfs *me, char *file, int flags, int mode)
  18. {
  19. int *local_info;
  20. int fd;
  21. fd = open (file, NO_LINEAR(flags), mode);
  22. if (fd == -1)
  23. return 0;
  24. local_info = (int *) xmalloc (sizeof (int), "Local fs");
  25. *local_info = fd;
  26. return local_info;
  27. }
  28. int local_read (void *data, char *buffer, int count)
  29. {
  30. int n;
  31. if (!data)
  32. return -1;
  33. while ((n = read (*((int *) data), buffer, count)) == -1){
  34. #ifdef EAGAIN
  35. if (errno == EAGAIN) continue;
  36. #endif
  37. #ifdef EINTR
  38. if (errno == EINTR) continue;
  39. #endif
  40. return -1;
  41. }
  42. return n;
  43. }
  44. int local_close (void *data)
  45. {
  46. int fd;
  47. if (!data)
  48. return -1;
  49. fd = *(int *) data;
  50. free (data);
  51. return close (fd);
  52. }
  53. int local_errno (vfs *me)
  54. {
  55. return errno;
  56. }
  57. static void *local_opendir (vfs *me, char *dirname)
  58. {
  59. DIR **local_info;
  60. DIR *dir;
  61. dir = opendir (dirname);
  62. if (!dir)
  63. return 0;
  64. local_info = (DIR **) xmalloc (sizeof (DIR *), "Local fs");
  65. *local_info = dir;
  66. return local_info;
  67. }
  68. static int local_telldir (void *data)
  69. {
  70. return telldir( *(DIR **) data );
  71. }
  72. static void local_seekdir (void *data, int offset)
  73. {
  74. seekdir( *(DIR **) data, offset );
  75. }
  76. static void *local_readdir (void *data)
  77. {
  78. return readdir (*(DIR **) data);
  79. }
  80. static int local_closedir (void *data)
  81. {
  82. int i;
  83. i = closedir (* (DIR **) data);
  84. if (data)
  85. free (data);
  86. return i;
  87. }
  88. static int local_stat (vfs *me, char *path, struct stat *buf)
  89. {
  90. return stat (path, buf);
  91. }
  92. static int local_lstat (vfs *me, char *path, struct stat *buf)
  93. {
  94. #ifndef HAVE_STATLSTAT
  95. return lstat (path,buf);
  96. #else
  97. return statlstat (path, buf);
  98. #endif
  99. }
  100. int local_fstat (void *data, struct stat *buf)
  101. {
  102. return fstat (*((int *) data), buf);
  103. }
  104. static int local_chmod (vfs *me, char *path, int mode)
  105. {
  106. return chmod (path, mode);
  107. }
  108. static int local_chown (vfs *me, char *path, int owner, int group)
  109. {
  110. return chown (path, owner, group);
  111. }
  112. static int local_utime (vfs *me, char *path, struct utimbuf *times)
  113. {
  114. return utime (path, times);
  115. }
  116. static int local_readlink (vfs *me, char *path, char *buf, int size)
  117. {
  118. return readlink (path, buf, size);
  119. }
  120. static int local_unlink (vfs *me, char *path)
  121. {
  122. return unlink (path);
  123. }
  124. static int local_symlink (vfs *me, char *n1, char *n2)
  125. {
  126. return symlink (n1, n2);
  127. }
  128. static int local_write (void *data, char *buf, int nbyte)
  129. {
  130. int fd;
  131. int n;
  132. if (!data)
  133. return -1;
  134. fd = * (int *) data;
  135. while ((n = write (fd, buf, nbyte)) == -1){
  136. #ifdef EAGAIN
  137. if (errno == EAGAIN) continue;
  138. #endif
  139. #ifdef EINTR
  140. if (errno == EINTR) continue;
  141. #endif
  142. break;
  143. }
  144. return n;
  145. }
  146. static int local_rename (vfs *me, char *a, char *b)
  147. {
  148. return rename (a, b);
  149. }
  150. static int local_chdir (vfs *me, char *path)
  151. {
  152. return chdir (path);
  153. }
  154. int local_lseek (void *data, off_t offset, int whence)
  155. {
  156. int fd = * (int *) data;
  157. return lseek (fd, offset, whence);
  158. }
  159. static int local_mknod (vfs *me, char *path, int mode, int dev)
  160. {
  161. return mknod (path, mode, dev);
  162. }
  163. static int local_link (vfs *me, char *p1, char *p2)
  164. {
  165. return link (p1, p2);
  166. }
  167. static int local_mkdir (vfs *me, char *path, mode_t mode)
  168. {
  169. return mkdir (path, mode);
  170. }
  171. static int local_rmdir (vfs *me, char *path)
  172. {
  173. return rmdir (path);
  174. }
  175. static vfsid local_getid (vfs *me, char *path, struct vfs_stamping **parent)
  176. {
  177. *parent = NULL;
  178. return (vfsid) -1; /* We do not free local fs stuff at all */
  179. }
  180. static int local_nothingisopen (vfsid id)
  181. {
  182. return 0;
  183. }
  184. static void local_free (vfsid id)
  185. {
  186. }
  187. static char *local_getlocalcopy (vfs *me, char *path)
  188. {
  189. return strdup (path);
  190. }
  191. static void local_ungetlocalcopy (vfs *me, char *path, char *local, int has_changed)
  192. {
  193. }
  194. #ifdef HAVE_MMAP
  195. caddr_t local_mmap (vfs *me, caddr_t addr, size_t len, int prot, int flags, void *data, off_t offset)
  196. {
  197. int fd = * (int *)data;
  198. return mmap (addr, len, prot, flags, fd, offset);
  199. }
  200. int local_munmap (vfs *me, caddr_t addr, size_t len, void *data)
  201. {
  202. return munmap (addr, len);
  203. }
  204. #endif
  205. static int local_which(vfs *me, char *path)
  206. {
  207. return 0; /* Every path which other systems do not like is expected to be ours */
  208. }
  209. vfs vfs_local_ops = {
  210. NULL, /* This is place of next pointer */
  211. "Local filesystem",
  212. 0, /* flags */
  213. NULL, /* prefix */
  214. NULL, /* data */
  215. 0, /* errno */
  216. NULL,
  217. NULL,
  218. NULL,
  219. local_which,
  220. local_open,
  221. local_close,
  222. local_read,
  223. local_write,
  224. local_opendir,
  225. local_readdir,
  226. local_closedir,
  227. local_telldir,
  228. local_seekdir,
  229. local_stat,
  230. local_lstat,
  231. local_fstat,
  232. local_chmod,
  233. local_chown,
  234. local_utime,
  235. local_readlink,
  236. local_symlink,
  237. local_link,
  238. local_unlink,
  239. local_rename,
  240. local_chdir,
  241. local_errno,
  242. local_lseek,
  243. local_mknod,
  244. local_getid,
  245. local_nothingisopen,
  246. local_free,
  247. local_getlocalcopy,
  248. local_ungetlocalcopy,
  249. local_mkdir,
  250. local_rmdir,
  251. NULL,
  252. NULL
  253. #ifdef HAVE_MMAP
  254. ,local_mmap,
  255. local_munmap
  256. #endif
  257. };