local.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. #include <config.h>
  2. #include <errno.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include "utilvfs.h"
  8. #include "vfs.h"
  9. #include "local.h"
  10. /* Note: Some of this functions are not static. This has rather good
  11. * reason: exactly same functions would have to appear in sfs.c. This
  12. * saves both computer's memory and my work. <pavel@ucw.cz>
  13. * */
  14. static void *
  15. local_open (vfs *me, char *file, int flags, int mode)
  16. {
  17. int *local_info;
  18. int fd;
  19. fd = open (file, NO_LINEAR(flags), mode);
  20. if (fd == -1)
  21. return 0;
  22. local_info = g_new (int, 1);
  23. *local_info = fd;
  24. return local_info;
  25. }
  26. int
  27. local_read (void *data, char *buffer, int count)
  28. {
  29. int n;
  30. if (!data)
  31. return -1;
  32. while ((n = read (*((int *) data), buffer, count)) == -1){
  33. #ifdef EAGAIN
  34. if (errno == EAGAIN) continue;
  35. #endif
  36. #ifdef EINTR
  37. if (errno == EINTR) continue;
  38. #endif
  39. return -1;
  40. }
  41. return n;
  42. }
  43. int
  44. local_close (void *data)
  45. {
  46. int fd;
  47. if (!data)
  48. return -1;
  49. fd = *(int *) data;
  50. g_free (data);
  51. return close (fd);
  52. }
  53. int
  54. local_errno (vfs *me)
  55. {
  56. return errno;
  57. }
  58. static void *
  59. local_opendir (vfs *me, char *dirname)
  60. {
  61. DIR **local_info;
  62. DIR *dir;
  63. dir = opendir (dirname);
  64. if (!dir)
  65. return 0;
  66. local_info = (DIR **) g_new (DIR *, 1);
  67. *local_info = dir;
  68. return local_info;
  69. }
  70. static int
  71. local_telldir (void *data)
  72. {
  73. #ifdef HAVE_TELLDIR
  74. return telldir( *(DIR **) data );
  75. #else
  76. #warning "Native telldir() not available, emulation not implemented"
  77. abort();
  78. return 0; /* for dumb compilers */
  79. #endif /* !HAVE_TELLDIR */
  80. }
  81. static void
  82. local_seekdir (void *data, int offset)
  83. {
  84. #ifdef HAVE_SEEKDIR
  85. seekdir( *(DIR **) data, offset );
  86. #else
  87. #warning "Native seekdir() not available, emulation not implemented"
  88. abort();
  89. #endif /* !HAVE_SEEKDIR */
  90. }
  91. static void *
  92. local_readdir (void *data)
  93. {
  94. return readdir (*(DIR **) data);
  95. }
  96. static int
  97. local_closedir (void *data)
  98. {
  99. int i;
  100. i = closedir (* (DIR **) data);
  101. if (data)
  102. g_free (data);
  103. return i;
  104. }
  105. static int
  106. local_stat (vfs *me, char *path, struct stat *buf)
  107. {
  108. return stat (path, buf);
  109. }
  110. static int
  111. local_lstat (vfs *me, char *path, struct stat *buf)
  112. {
  113. #ifndef HAVE_STATLSTAT
  114. return lstat (path,buf);
  115. #else
  116. return statlstat (path, buf);
  117. #endif
  118. }
  119. int
  120. local_fstat (void *data, struct stat *buf)
  121. {
  122. return fstat (*((int *) data), buf);
  123. }
  124. static int
  125. local_chmod (vfs *me, char *path, int mode)
  126. {
  127. return chmod (path, mode);
  128. }
  129. static int
  130. local_chown (vfs *me, char *path, int owner, int group)
  131. {
  132. return chown (path, owner, group);
  133. }
  134. static int
  135. local_utime (vfs *me, char *path, struct utimbuf *times)
  136. {
  137. return utime (path, times);
  138. }
  139. static int
  140. local_readlink (vfs *me, char *path, char *buf, int size)
  141. {
  142. return readlink (path, buf, size);
  143. }
  144. static int
  145. local_unlink (vfs *me, char *path)
  146. {
  147. return unlink (path);
  148. }
  149. static int
  150. local_symlink (vfs *me, char *n1, char *n2)
  151. {
  152. return symlink (n1, n2);
  153. }
  154. static int
  155. local_write (void *data, char *buf, int nbyte)
  156. {
  157. int fd;
  158. int n;
  159. if (!data)
  160. return -1;
  161. fd = * (int *) data;
  162. while ((n = write (fd, buf, nbyte)) == -1){
  163. #ifdef EAGAIN
  164. if (errno == EAGAIN) continue;
  165. #endif
  166. #ifdef EINTR
  167. if (errno == EINTR) continue;
  168. #endif
  169. break;
  170. }
  171. return n;
  172. }
  173. static int
  174. local_rename (vfs *me, char *a, char *b)
  175. {
  176. return rename (a, b);
  177. }
  178. static int
  179. local_chdir (vfs *me, char *path)
  180. {
  181. return chdir (path);
  182. }
  183. int
  184. local_lseek (void *data, off_t offset, int whence)
  185. {
  186. int fd = * (int *) data;
  187. return lseek (fd, offset, whence);
  188. }
  189. static int
  190. local_mknod (vfs *me, char *path, int mode, int dev)
  191. {
  192. return mknod (path, mode, dev);
  193. }
  194. static int
  195. local_link (vfs *me, char *p1, char *p2)
  196. {
  197. return link (p1, p2);
  198. }
  199. static int
  200. local_mkdir (vfs *me, char *path, mode_t mode)
  201. {
  202. return mkdir (path, mode);
  203. }
  204. static int
  205. local_rmdir (vfs *me, char *path)
  206. {
  207. return rmdir (path);
  208. }
  209. static vfsid
  210. local_getid (vfs *me, char *path, struct vfs_stamping **parent)
  211. {
  212. *parent = NULL;
  213. return (vfsid) -1; /* We do not free local fs stuff at all */
  214. }
  215. static int
  216. local_nothingisopen (vfsid id)
  217. {
  218. return 0;
  219. }
  220. static void
  221. local_free (vfsid id)
  222. {
  223. }
  224. static char *
  225. local_getlocalcopy (vfs *me, char *path)
  226. {
  227. return g_strdup (path);
  228. }
  229. static int
  230. local_ungetlocalcopy (vfs *me, char *path, char *local, int has_changed)
  231. {
  232. return 0;
  233. }
  234. #ifdef HAVE_MMAP
  235. caddr_t
  236. local_mmap (vfs *me, caddr_t addr, size_t len, int prot, int flags, void *data, off_t offset)
  237. {
  238. int fd = * (int *)data;
  239. return mmap (addr, len, prot, flags, fd, offset);
  240. }
  241. int
  242. local_munmap (vfs *me, caddr_t addr, size_t len, void *data)
  243. {
  244. return munmap (addr, len);
  245. }
  246. #endif
  247. static int
  248. local_which (vfs *me, char *path)
  249. {
  250. return 0; /* Every path which other systems do not like is expected to be ours */
  251. }
  252. vfs vfs_local_ops = {
  253. NULL, /* This is place of next pointer */
  254. "localfs",
  255. 0, /* flags */
  256. NULL, /* prefix */
  257. NULL, /* data */
  258. 0, /* errno */
  259. NULL,
  260. NULL,
  261. NULL,
  262. local_which,
  263. local_open,
  264. local_close,
  265. local_read,
  266. local_write,
  267. local_opendir,
  268. local_readdir,
  269. local_closedir,
  270. local_telldir,
  271. local_seekdir,
  272. local_stat,
  273. local_lstat,
  274. local_fstat,
  275. local_chmod,
  276. local_chown,
  277. local_utime,
  278. local_readlink,
  279. local_symlink,
  280. local_link,
  281. local_unlink,
  282. local_rename,
  283. local_chdir,
  284. local_errno,
  285. local_lseek,
  286. local_mknod,
  287. local_getid,
  288. local_nothingisopen,
  289. local_free,
  290. local_getlocalcopy,
  291. local_ungetlocalcopy,
  292. local_mkdir,
  293. local_rmdir,
  294. NULL,
  295. NULL
  296. #ifdef HAVE_MMAP
  297. ,local_mmap,
  298. local_munmap
  299. #endif
  300. };
  301. vfs vfs_nil_ops = {
  302. NULL, /* This is place of next pointer */
  303. "nullfs",
  304. 0, /* flags */
  305. NULL, /* prefix */
  306. NULL, /* data */
  307. 0, /* errno */
  308. NULL,
  309. NULL,
  310. NULL,
  311. NULL,
  312. NULL,
  313. NULL,
  314. NULL,
  315. NULL,
  316. NULL,
  317. NULL,
  318. NULL,
  319. NULL,
  320. NULL,
  321. NULL,
  322. NULL,
  323. NULL,
  324. NULL,
  325. NULL,
  326. NULL,
  327. NULL,
  328. NULL,
  329. NULL,
  330. NULL,
  331. NULL,
  332. NULL,
  333. NULL,
  334. NULL,
  335. NULL,
  336. local_getid,
  337. local_nothingisopen,
  338. local_free,
  339. NULL,
  340. NULL,
  341. NULL,
  342. NULL,
  343. NULL,
  344. NULL
  345. #ifdef HAVE_MMAP
  346. , NULL,
  347. NULL
  348. #endif
  349. };