vfs.h 11 KB

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