vfs.h 5.6 KB

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