vfs.h 10 KB

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