local.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /**
  2. * \file
  3. * \brief Source: local FS
  4. */
  5. #include <config.h>
  6. #include <errno.h>
  7. #include <sys/types.h>
  8. #include <unistd.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <fcntl.h>
  12. #include "../src/global.h"
  13. #include "../src/wtools.h" /* message() */
  14. #include "../src/main.h" /* print_vfs_message */
  15. #include "utilvfs.h"
  16. #include "vfs.h"
  17. #include "local.h"
  18. /**
  19. * Note: Some of this functions are not static. This has rather good
  20. * reason: exactly same functions would have to appear in sfs.c. This
  21. * saves both computer's memory and my work. <pavel@ucw.cz>
  22. */
  23. static struct vfs_class vfs_local_ops;
  24. static void *
  25. local_open (struct vfs_class *me, const char *file, int flags, int mode)
  26. {
  27. int *local_info;
  28. int fd;
  29. (void) me;
  30. fd = open (file, NO_LINEAR (flags), mode);
  31. if (fd == -1)
  32. return 0;
  33. local_info = g_new (int, 1);
  34. *local_info = fd;
  35. return local_info;
  36. }
  37. ssize_t
  38. local_read (void *data, char *buffer, int count)
  39. {
  40. int n;
  41. if (!data)
  42. return -1;
  43. while ((n = read (*((int *) data), buffer, count)) == -1){
  44. #ifdef EAGAIN
  45. if (errno == EAGAIN) continue;
  46. #endif
  47. #ifdef EINTR
  48. if (errno == EINTR) continue;
  49. #endif
  50. return -1;
  51. }
  52. return n;
  53. }
  54. int
  55. local_close (void *data)
  56. {
  57. int fd;
  58. if (!data)
  59. return -1;
  60. fd = *(int *) data;
  61. g_free (data);
  62. return close (fd);
  63. }
  64. int
  65. local_errno (struct vfs_class *me)
  66. {
  67. (void) me;
  68. return errno;
  69. }
  70. static void *
  71. local_opendir (struct vfs_class *me, const char *dirname)
  72. {
  73. DIR **local_info;
  74. DIR *dir;
  75. (void) me;
  76. dir = opendir (dirname);
  77. if (!dir)
  78. return 0;
  79. local_info = (DIR **) g_new (DIR *, 1);
  80. *local_info = dir;
  81. return local_info;
  82. }
  83. static void *
  84. local_readdir (void *data)
  85. {
  86. return readdir (*(DIR **) data);
  87. }
  88. static int
  89. local_closedir (void *data)
  90. {
  91. int i;
  92. i = closedir (* (DIR **) data);
  93. g_free (data);
  94. return i;
  95. }
  96. static int
  97. local_stat (struct vfs_class *me, const char *path, struct stat *buf)
  98. {
  99. (void) me;
  100. return stat (path, buf);
  101. }
  102. static int
  103. local_lstat (struct vfs_class *me, const char *path, struct stat *buf)
  104. {
  105. (void) me;
  106. #ifndef HAVE_STATLSTAT
  107. return lstat (path,buf);
  108. #else
  109. return statlstat (path, buf);
  110. #endif
  111. }
  112. int
  113. local_fstat (void *data, struct stat *buf)
  114. {
  115. /* FIXME: avoid type cast */
  116. return fstat (*((int *) data), buf);
  117. }
  118. static int
  119. local_chmod (struct vfs_class *me, const char *path, int mode)
  120. {
  121. (void) me;
  122. return chmod (path, mode);
  123. }
  124. static int
  125. local_chown (struct vfs_class *me, const char *path, int owner, int group)
  126. {
  127. (void) me;
  128. return chown (path, owner, group);
  129. }
  130. static int
  131. local_utime (struct vfs_class *me, const char *path, struct utimbuf *times)
  132. {
  133. (void) me;
  134. return utime (path, times);
  135. }
  136. static int
  137. local_readlink (struct vfs_class *me, const char *path, char *buf, size_t size)
  138. {
  139. (void) me;
  140. return readlink (path, buf, size);
  141. }
  142. static int
  143. local_unlink (struct vfs_class *me, const char *path)
  144. {
  145. (void) me;
  146. return unlink (path);
  147. }
  148. static int
  149. local_symlink (struct vfs_class *me, const char *n1, const char *n2)
  150. {
  151. (void) me;
  152. return symlink (n1, n2);
  153. }
  154. static ssize_t
  155. local_write (void *data, const 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 (struct vfs_class *me, const char *a, const char *b)
  175. {
  176. (void) me;
  177. return rename (a, b);
  178. }
  179. static int
  180. local_chdir (struct vfs_class *me, const char *path)
  181. {
  182. (void) me;
  183. return chdir (path);
  184. }
  185. off_t
  186. local_lseek (void *data, off_t offset, int whence)
  187. {
  188. int fd = * (int *) data;
  189. return lseek (fd, offset, whence);
  190. }
  191. static int
  192. local_mknod (struct vfs_class *me, const char *path, int mode, int dev)
  193. {
  194. (void) me;
  195. return mknod (path, mode, dev);
  196. }
  197. static int
  198. local_link (struct vfs_class *me, const char *p1, const char *p2)
  199. {
  200. (void) me;
  201. return link (p1, p2);
  202. }
  203. static int
  204. local_mkdir (struct vfs_class *me, const char *path, mode_t mode)
  205. {
  206. (void) me;
  207. return mkdir (path, mode);
  208. }
  209. static int
  210. local_rmdir (struct vfs_class *me, const char *path)
  211. {
  212. (void) me;
  213. return rmdir (path);
  214. }
  215. static char *
  216. local_getlocalcopy (struct vfs_class *me, const char *path)
  217. {
  218. (void) me;
  219. return g_strdup (path);
  220. }
  221. static int
  222. local_ungetlocalcopy (struct vfs_class *me, const char *path,
  223. const char *local, int has_changed)
  224. {
  225. (void) me;
  226. (void) path;
  227. (void) local;
  228. (void) has_changed;
  229. return 0;
  230. }
  231. static int
  232. local_which (struct vfs_class *me, const char *path)
  233. {
  234. (void) me;
  235. (void) path;
  236. return 0; /* Every path which other systems do not like is expected to be ours */
  237. }
  238. void
  239. init_localfs (void)
  240. {
  241. vfs_local_ops.name = "localfs";
  242. vfs_local_ops.flags = VFSF_LOCAL;
  243. vfs_local_ops.which = local_which;
  244. vfs_local_ops.open = local_open;
  245. vfs_local_ops.close = local_close;
  246. vfs_local_ops.read = local_read;
  247. vfs_local_ops.write = local_write;
  248. vfs_local_ops.opendir = local_opendir;
  249. vfs_local_ops.readdir = local_readdir;
  250. vfs_local_ops.closedir = local_closedir;
  251. vfs_local_ops.stat = local_stat;
  252. vfs_local_ops.lstat = local_lstat;
  253. vfs_local_ops.fstat = local_fstat;
  254. vfs_local_ops.chmod = local_chmod;
  255. vfs_local_ops.chown = local_chown;
  256. vfs_local_ops.utime = local_utime;
  257. vfs_local_ops.readlink = local_readlink;
  258. vfs_local_ops.symlink = local_symlink;
  259. vfs_local_ops.link = local_link;
  260. vfs_local_ops.unlink = local_unlink;
  261. vfs_local_ops.rename = local_rename;
  262. vfs_local_ops.chdir = local_chdir;
  263. vfs_local_ops.ferrno = local_errno;
  264. vfs_local_ops.lseek = local_lseek;
  265. vfs_local_ops.mknod = local_mknod;
  266. vfs_local_ops.getlocalcopy = local_getlocalcopy;
  267. vfs_local_ops.ungetlocalcopy = local_ungetlocalcopy;
  268. vfs_local_ops.mkdir = local_mkdir;
  269. vfs_local_ops.rmdir = local_rmdir;
  270. vfs_register_class (&vfs_local_ops);
  271. }