vfs.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /**
  2. * \file
  3. * \brief Header: Virtual File System switch code
  4. */
  5. #ifndef MC_VFS_VFS_H
  6. #define MC_VFS_VFS_H
  7. #include <sys/types.h>
  8. #include <dirent.h>
  9. #include <utime.h>
  10. #include <stdio.h>
  11. /* Flags of VFS classes */
  12. #define VFSF_LOCAL 1 /* Class is local (not virtual) filesystem */
  13. #define VFSF_NOLINKS 2 /* Hard links not supported */
  14. #ifdef ENABLE_VFS
  15. void vfs_init (void);
  16. void vfs_shut (void);
  17. int vfs_current_is_local (void);
  18. int vfs_file_is_local (const char *filename);
  19. ssize_t mc_read (int handle, void *buffer, int count);
  20. ssize_t mc_write (int handle, const void *buffer, int count);
  21. int mc_utime (const char *path, struct utimbuf *times);
  22. int mc_readlink (const char *path, char *buf, int bufsiz);
  23. int mc_ungetlocalcopy (const char *pathname, const char *local, int has_changed);
  24. int mc_close (int handle);
  25. off_t mc_lseek (int fd, off_t offset, int whence);
  26. DIR *mc_opendir (const char *dirname);
  27. struct dirent *mc_readdir (DIR * dirp);
  28. int mc_closedir (DIR * dir);
  29. int mc_stat (const char *path, struct stat *buf);
  30. int mc_mknod (const char *, mode_t, dev_t);
  31. int mc_link (const char *name1, const char *name2);
  32. int mc_mkdir (const char *path, mode_t mode);
  33. int mc_rmdir (const char *path);
  34. int mc_fstat (int fd, struct stat *buf);
  35. int mc_lstat (const char *path, struct stat *buf);
  36. int mc_symlink (const char *name1, const char *name2);
  37. int mc_rename (const char *original, const char *target);
  38. int mc_chmod (const char *path, mode_t mode);
  39. int mc_chown (const char *path, uid_t owner, gid_t group);
  40. int mc_chdir (const char *path);
  41. int mc_unlink (const char *path);
  42. int mc_ctl (int fd, int ctlop, void *arg);
  43. int mc_setctl (const char *path, int ctlop, void *arg);
  44. int mc_open (const char *filename, int flags, ...);
  45. char *mc_get_current_wd (char *buffer, int bufsize);
  46. char *vfs_canon (const char *path);
  47. char *mc_getlocalcopy (const char *pathname);
  48. char *vfs_strip_suffix_from_filename (const char *filename);
  49. char *vfs_translate_url (const char *url);
  50. /* return encoding after last #enc: or NULL, if part does not contain #enc:
  51. * return static buffer */
  52. const char *vfs_get_encoding (const char *path);
  53. /* return new string */
  54. char *vfs_translate_path_n (const char *path);
  55. /* canonize and translate path, return new string */
  56. char *vfs_canon_and_translate (const char *path);
  57. #else /* ENABLE_VFS */
  58. /* Only the routines outside of the VFS module need the emulation macros */
  59. #define vfs_init() do { } while (0)
  60. #define vfs_shut() do { } while (0)
  61. #define vfs_current_is_local() (1)
  62. #define vfs_file_is_local(x) (1)
  63. #define vfs_strip_suffix_from_filename(x) g_strdup(x)
  64. #define vfs_get_class(x) (struct vfs_class *)(NULL)
  65. #define vfs_translate_url(s) g_strdup(s)
  66. #define vfs_file_class_flags(x) (VFSF_LOCAL)
  67. #define vfs_release_path(x)
  68. #define vfs_add_current_stamps() do { } while (0)
  69. #define vfs_timeout_handler() do { } while (0)
  70. #define vfs_timeouts() 0
  71. #define mc_getlocalcopy(x) vfs_canon(x)
  72. #define mc_read read
  73. #define mc_write write
  74. #define mc_utime utime
  75. #define mc_readlink readlink
  76. #define mc_ungetlocalcopy(x,y,z) do { } while (0)
  77. #define mc_close close
  78. #define mc_lseek lseek
  79. #define mc_opendir opendir
  80. #define mc_readdir readdir
  81. #define mc_closedir closedir
  82. #define mc_stat stat
  83. #define mc_mknod mknod
  84. #define mc_link link
  85. #define mc_mkdir mkdir
  86. #define mc_rmdir rmdir
  87. #define mc_fstat fstat
  88. #define mc_lstat lstat
  89. #define mc_symlink symlink
  90. #define mc_rename rename
  91. #define mc_chmod chmod
  92. #define mc_chown chown
  93. #define mc_chdir chdir
  94. #define mc_unlink unlink
  95. #define mc_open open
  96. #define mc_get_current_wd(x,size) get_current_wd (x, size)
  97. static inline int mc_setctl (const char *path, int ctlop, void *arg)
  98. {
  99. (void) path;
  100. (void) ctlop;
  101. (void) arg;
  102. return 0;
  103. }
  104. static inline int mc_ctl (int fd, int ctlop, void *arg)
  105. {
  106. (void) fd;
  107. (void) ctlop;
  108. (void) arg;
  109. return 0;
  110. }
  111. static inline const char *vfs_get_encoding (const char *path)
  112. {
  113. (void) path;
  114. return NULL;
  115. }
  116. /* return new string */
  117. static inline char *vfs_translate_path_n (const char *path)
  118. {
  119. return ((path == NULL) ? g_strdup ("") : g_strdup (path));
  120. }
  121. static inline char* vfs_canon_and_translate(const char* path)
  122. {
  123. char *ret_str;
  124. if (path == NULL)
  125. return g_strdup("");
  126. if (path[0] == PATH_SEP)
  127. {
  128. char *curr_dir = g_get_current_dir();
  129. ret_str = g_strdup_printf("%s" PATH_SEP_STR "%s", curr_dir, path);
  130. g_free(curr_dir);
  131. }
  132. else
  133. ret_str = g_strdup(path);
  134. canonicalize_pathname (ret_str);
  135. return ret_str;
  136. }
  137. static inline char *
  138. vfs_canon (const char *path)
  139. {
  140. char *p = g_strdup (path);
  141. canonicalize_pathname(p);
  142. return p;
  143. }
  144. #endif /* ENABLE_VFS */
  145. char *vfs_get_current_dir (void);
  146. /* translate path back to terminal encoding, remove all #enc:
  147. * every invalid character is replaced with question mark
  148. * return static buffer */
  149. char *vfs_translate_path (const char *path);
  150. /* Operations for mc_ctl - on open file */
  151. enum {
  152. VFS_CTL_IS_NOTREADY
  153. };
  154. /* Operations for mc_setctl - on path */
  155. enum {
  156. VFS_SETCTL_FORGET,
  157. VFS_SETCTL_RUN,
  158. VFS_SETCTL_LOGFILE,
  159. VFS_SETCTL_FLUSH, /* invalidate directory cache */
  160. /* Setting this makes vfs layer give out potentially incorrect data,
  161. but it also makes some operations much faster. Use with caution. */
  162. VFS_SETCTL_STALE_DATA
  163. };
  164. #define O_ALL (O_CREAT | O_EXCL | O_NOCTTY | O_NDELAY | O_SYNC | O_WRONLY | O_RDWR | O_RDONLY)
  165. /* Midnight commander code should _not_ use other flags than those
  166. listed above and O_APPEND */
  167. #if (O_ALL & O_APPEND)
  168. #warning "Unexpected problem with flags, O_LINEAR disabled, contact pavel@ucw.cz"
  169. #define O_LINEAR 0
  170. #define IS_LINEAR(a) 0
  171. #define NO_LINEAR(a) a
  172. #else
  173. #define O_LINEAR O_APPEND
  174. #define IS_LINEAR(a) ((a) == (O_RDONLY | O_LINEAR)) /* Return only 0 and 1 ! */
  175. #define NO_LINEAR(a) (((a) == (O_RDONLY | O_LINEAR)) ? O_RDONLY : (a))
  176. #endif
  177. /* O_LINEAR is strange beast, be careful. If you open file asserting
  178. * O_RDONLY | O_LINEAR, you promise:
  179. *
  180. * a) to read file linearly from beginning to the end
  181. * b) not to open another file before you close this one
  182. * (this will likely go away in future)
  183. * as a special gift, you may
  184. * c) lseek() immediately after open(), giving ftpfs chance to
  185. * reget. Be warned that this lseek() can fail, and you _have_
  186. * to handle that gratefully.
  187. *
  188. * O_LINEAR allows filesystems not to create temporary file in some
  189. * cases (ftp transfer). -- pavel@ucw.cz
  190. */
  191. /* And now some defines for our errors. */
  192. #ifdef ENOSYS
  193. #define E_NOTSUPP ENOSYS /* for use in vfs when module does not provide function */
  194. #else
  195. #define E_NOTSUPP EFAULT /* Does this happen? */
  196. #endif
  197. #ifdef ENOMSG
  198. #define E_UNKNOWN ENOMSG /* if we do not know what error happened */
  199. #else
  200. #define E_UNKNOWN EIO /* if we do not know what error happened */
  201. #endif
  202. #ifdef EREMOTEIO
  203. #define E_REMOTE EREMOTEIO /* if other side of ftp/fish reports error */
  204. #else
  205. #define E_REMOTE ENETUNREACH /* :-( there's no EREMOTEIO on some systems */
  206. #endif
  207. #ifdef EPROTO
  208. #define E_PROTO EPROTO /* if other side fails to follow protocol */
  209. #else
  210. #define E_PROTO EIO
  211. #endif
  212. #endif