vfs.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 <sys/stat.h>
  9. #include <dirent.h>
  10. #include <utime.h>
  11. #include <stdio.h>
  12. #include <fcntl.h>
  13. #include <unistd.h>
  14. #include <stddef.h>
  15. #include "lib/global.h"
  16. #include "lib/fs.h" /* MC_MAXPATHLEN */
  17. #include "path.h"
  18. /*** typedefs(not structures) and defined constants **********************************************/
  19. #if defined (ENABLE_VFS_FTP) || defined (ENABLE_VFS_FISH) || defined (ENABLE_VFS_SMB)
  20. #define ENABLE_VFS_NET 1
  21. #endif
  22. /**
  23. * This is the type of callback function passed to vfs_fill_names.
  24. * It gets the name of the virtual file system as its first argument.
  25. * See also:
  26. * vfs_fill_names().
  27. */
  28. #define VFS_ENCODING_PREFIX "#enc:"
  29. #define O_ALL (O_CREAT | O_EXCL | O_NOCTTY | O_NDELAY | O_SYNC | O_WRONLY | O_RDWR | O_RDONLY)
  30. /* Midnight commander code should _not_ use other flags than those
  31. listed above and O_APPEND */
  32. #if (O_ALL & O_APPEND)
  33. #warning "Unexpected problem with flags, O_LINEAR disabled, contact pavel@ucw.cz"
  34. #define O_LINEAR 0
  35. #define IS_LINEAR(a) 0
  36. #define NO_LINEAR(a) a
  37. #else
  38. #define O_LINEAR O_APPEND
  39. #define IS_LINEAR(a) ((a) == (O_RDONLY | O_LINEAR)) /* Return only 0 and 1 ! */
  40. #define NO_LINEAR(a) (((a) == (O_RDONLY | O_LINEAR)) ? O_RDONLY : (a))
  41. #endif
  42. /* O_LINEAR is strange beast, be careful. If you open file asserting
  43. * O_RDONLY | O_LINEAR, you promise:
  44. *
  45. * a) to read file linearly from beginning to the end
  46. * b) not to open another file before you close this one
  47. * (this will likely go away in future)
  48. * as a special gift, you may
  49. * c) lseek() immediately after open(), giving ftpfs chance to
  50. * reget. Be warned that this lseek() can fail, and you _have_
  51. * to handle that gratefully.
  52. *
  53. * O_LINEAR allows filesystems not to create temporary file in some
  54. * cases (ftp transfer). -- pavel@ucw.cz
  55. */
  56. /* And now some defines for our errors. */
  57. #ifdef ENOSYS
  58. #define E_NOTSUPP ENOSYS /* for use in vfs when module does not provide function */
  59. #else
  60. #define E_NOTSUPP EFAULT /* Does this happen? */
  61. #endif
  62. #ifdef ENOMSG
  63. #define E_UNKNOWN ENOMSG /* if we do not know what error happened */
  64. #else
  65. #define E_UNKNOWN EIO /* if we do not know what error happened */
  66. #endif
  67. #ifdef EREMOTEIO
  68. #define E_REMOTE EREMOTEIO /* if other side of ftp/fish reports error */
  69. #else
  70. #define E_REMOTE ENETUNREACH /* :-( there's no EREMOTEIO on some systems */
  71. #endif
  72. #ifdef EPROTO
  73. #define E_PROTO EPROTO /* if other side fails to follow protocol */
  74. #else
  75. #define E_PROTO EIO
  76. #endif
  77. typedef void (*fill_names_f) (const char *);
  78. typedef void *vfsid;
  79. /*** enums ***************************************************************************************/
  80. /* Flags of VFS classes */
  81. typedef enum
  82. {
  83. VFSF_UNKNOWN = 0,
  84. VFSF_LOCAL = 1 << 0, /* Class is local (not virtual) filesystem */
  85. VFSF_NOLINKS = 1 << 1 /* Hard links not supported */
  86. } vfs_class_flags_t;
  87. /* Operations for mc_ctl - on open file */
  88. enum
  89. {
  90. VFS_CTL_IS_NOTREADY
  91. };
  92. /* Operations for mc_setctl - on path */
  93. enum
  94. {
  95. VFS_SETCTL_FORGET,
  96. VFS_SETCTL_RUN,
  97. VFS_SETCTL_LOGFILE,
  98. VFS_SETCTL_FLUSH, /* invalidate directory cache */
  99. /* Setting this makes vfs layer give out potentially incorrect data,
  100. but it also makes some operations much faster. Use with caution. */
  101. VFS_SETCTL_STALE_DATA
  102. };
  103. /*** structures declarations (and typedefs of structures)*****************************************/
  104. typedef struct vfs_class
  105. {
  106. const char *name; /* "FIles over SHell" */
  107. vfs_class_flags_t flags;
  108. const char *prefix; /* "fish:" */
  109. void *data; /* this is for filesystem's own use */
  110. int verrno; /* can't use errno because glibc2 might define errno as function */
  111. int (*init) (struct vfs_class * me);
  112. void (*done) (struct vfs_class * me);
  113. /**
  114. * The fill_names method shall call the callback function for every
  115. * filesystem name that this vfs module supports.
  116. */
  117. void (*fill_names) (struct vfs_class * me, fill_names_f);
  118. /**
  119. * The which() method shall return the index of the vfs subsystem
  120. * or -1 if this vfs cannot handle the given pathname.
  121. */
  122. int (*which) (struct vfs_class * me, const char *path);
  123. void *(*open) (const vfs_path_t * vpath, int flags, mode_t mode);
  124. int (*close) (void *vfs_info);
  125. ssize_t (*read) (void *vfs_info, char *buffer, size_t count);
  126. ssize_t (*write) (void *vfs_info, const char *buf, size_t count);
  127. void *(*opendir) (const vfs_path_t * vpath);
  128. void *(*readdir) (void *vfs_info);
  129. int (*closedir) (void *vfs_info);
  130. int (*stat) (const vfs_path_t * vpath, struct stat * buf);
  131. int (*lstat) (const vfs_path_t * vpath, struct stat * buf);
  132. int (*fstat) (void *vfs_info, struct stat * buf);
  133. int (*chmod) (const vfs_path_t * vpath, mode_t mode);
  134. int (*chown) (const vfs_path_t * vpath, uid_t owner, gid_t group);
  135. int (*utime) (const vfs_path_t * vpath, struct utimbuf * times);
  136. int (*readlink) (const vfs_path_t * vpath, char *buf, size_t size);
  137. int (*symlink) (const vfs_path_t * vpath1, const vfs_path_t * vpath2);
  138. int (*link) (const vfs_path_t * vpath1, const vfs_path_t * vpath2);
  139. int (*unlink) (const vfs_path_t * vpath);
  140. int (*rename) (const vfs_path_t * vpath1, const vfs_path_t * vpath2);
  141. int (*chdir) (const vfs_path_t * vpath);
  142. int (*ferrno) (struct vfs_class * me);
  143. off_t (*lseek) (void *vfs_info, off_t offset, int whence);
  144. int (*mknod) (const vfs_path_t * vpath, mode_t mode, dev_t dev);
  145. vfsid (*getid) (const vfs_path_t * vpath);
  146. int (*nothingisopen) (vfsid id);
  147. void (*free) (vfsid id);
  148. char *(*getlocalcopy) (const vfs_path_t * vpath);
  149. int (*ungetlocalcopy) (const vfs_path_t * vpath, const char *local, int has_changed);
  150. int (*mkdir) (const vfs_path_t * vpath, mode_t mode);
  151. int (*rmdir) (const vfs_path_t * vpath);
  152. int (*ctl) (void *vfs_info, int ctlop, void *arg);
  153. int (*setctl) (const vfs_path_t * vpath, int ctlop, void *arg);
  154. } vfs_class;
  155. /*
  156. * This union is used to ensure that there is enough space for the
  157. * filename (d_name) when the dirent structure is created.
  158. */
  159. union vfs_dirent
  160. {
  161. struct dirent dent;
  162. char _extra_buffer[offsetof (struct dirent, d_name) + MC_MAXPATHLEN + 1];
  163. };
  164. /*** global variables defined in .c file *********************************************************/
  165. extern int vfs_timeout;
  166. #ifdef ENABLE_VFS_NET
  167. extern int use_netrc;
  168. #endif
  169. /*** declarations of public functions ************************************************************/
  170. /* lib/vfs/direntry.c: */
  171. void *vfs_s_open (const vfs_path_t * vpath, int flags, mode_t mode);
  172. vfsid vfs_getid (const vfs_path_t * vpath);
  173. void vfs_init (void);
  174. void vfs_shut (void);
  175. /* Register a file system class */
  176. gboolean vfs_register_class (struct vfs_class *vfs);
  177. void vfs_setup_work_dir (void);
  178. void vfs_timeout_handler (void);
  179. int vfs_timeouts (void);
  180. void vfs_expire (int now);
  181. char *vfs_get_current_dir (void);
  182. vfs_path_t *vfs_get_raw_current_dir (void);
  183. void vfs_set_raw_current_dir (const vfs_path_t * vpath);
  184. gboolean vfs_current_is_local (void);
  185. gboolean vfs_file_is_local (const vfs_path_t * vpath);
  186. char *vfs_strip_suffix_from_filename (const char *filename);
  187. vfs_class_flags_t vfs_file_class_flags (const vfs_path_t * vpath);
  188. /* translate path back to terminal encoding, remove all #enc:
  189. * every invalid character is replaced with question mark
  190. * return static buffer */
  191. char *vfs_translate_path (const char *path);
  192. /* return new string */
  193. char *vfs_translate_path_n (const char *path);
  194. void vfs_stamp_path (const char *path);
  195. void vfs_release_path (const char *dir);
  196. void vfs_fill_names (fill_names_f);
  197. void vfs_print_message (const char *msg, ...) __attribute__ ((format (__printf__, 1, 2)));
  198. int vfs_ferrno (struct vfs_class *vfs);
  199. int vfs_new_handle (struct vfs_class *vclass, void *fsinfo);
  200. struct vfs_class *vfs_class_find_by_handle (int handle);
  201. void *vfs_class_data_find_by_handle (int handle);
  202. void vfs_free_handle (int handle);
  203. char *_vfs_get_cwd (void);
  204. vfs_path_t *vfs_change_encoding (vfs_path_t * vpath, const char *encoding);
  205. int vfs_preallocate (int dest_desc, off_t src_fsize, off_t dest_fsize);
  206. /**
  207. * Interface functions described in interface.c
  208. */
  209. ssize_t mc_read (int handle, void *buffer, size_t count);
  210. ssize_t mc_write (int handle, const void *buffer, size_t count);
  211. int mc_utime (const char *path, struct utimbuf *times);
  212. int mc_readlink (const char *path, char *buf, size_t bufsiz);
  213. int mc_close (int handle);
  214. off_t mc_lseek (int fd, off_t offset, int whence);
  215. DIR *mc_opendir (const char *dirname);
  216. struct dirent *mc_readdir (DIR * dirp);
  217. int mc_closedir (DIR * dir);
  218. int mc_stat (const char *path, struct stat *buf);
  219. int mc_mknod (const char *path, mode_t mode, dev_t dev);
  220. int mc_link (const char *name1, const char *name2);
  221. int mc_mkdir (const char *path, mode_t mode);
  222. int mc_rmdir (const char *path);
  223. int mc_fstat (int fd, struct stat *buf);
  224. int mc_lstat (const char *path, struct stat *buf);
  225. int mc_symlink (const char *name1, const char *name2);
  226. int mc_rename (const char *original, const char *target);
  227. int mc_chmod (const char *path, mode_t mode);
  228. int mc_chown (const char *path, uid_t owner, gid_t group);
  229. int mc_chdir (const char *path);
  230. int mc_unlink (const char *path);
  231. int mc_ctl (int fd, int ctlop, void *arg);
  232. int mc_setctl (const char *path, int ctlop, void *arg);
  233. int mc_open (const char *filename, int flags, ...);
  234. char *mc_get_current_wd (char *buffer, size_t bufsize);
  235. char *mc_getlocalcopy (const char *pathname);
  236. int mc_ungetlocalcopy (const char *pathname, const char *local, int has_changed);
  237. /*** inline functions ****************************************************************************/
  238. #endif /* MC_VFS_VFS_H */