vfs.h 7.0 KB

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