local.c 6.0 KB

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