local.c 9.7 KB

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