4 * \brief Header: Virtual File System switch code
10 #include <sys/types.h>
21 #include "lib/global.h"
22 #include "lib/fs.h" /* MC_MAXPATHLEN */
26 /*** typedefs(not structures) and defined constants **********************************************/
28 #if defined (ENABLE_VFS_FTP) || defined (ENABLE_VFS_FISH) || defined (ENABLE_VFS_SMB)
29 #define ENABLE_VFS_NET 1
33 * This is the type of callback function passed to vfs_fill_names.
34 * It gets the name of the virtual file system as its first argument.
39 #define VFS_ENCODING_PREFIX "#enc:"
41 #define O_ALL (O_CREAT | O_EXCL | O_NOCTTY | O_NDELAY | O_SYNC | O_WRONLY | O_RDWR | O_RDONLY)
42 /* Midnight commander code should _not_ use other flags than those
43 listed above and O_APPEND */
45 #if (O_ALL & O_APPEND)
46 #warning "Unexpected problem with flags, O_LINEAR disabled, contact pavel@ucw.cz"
48 #define IS_LINEAR(a) 0
49 #define NO_LINEAR(a) a
51 #define O_LINEAR O_APPEND
52 #define IS_LINEAR(a) ((a) == (O_RDONLY | O_LINEAR)) /* Return only 0 and 1 ! */
53 #define NO_LINEAR(a) (((a) == (O_RDONLY | O_LINEAR)) ? O_RDONLY : (a))
56 /* O_LINEAR is strange beast, be careful. If you open file asserting
57 * O_RDONLY | O_LINEAR, you promise:
59 * a) to read file linearly from beginning to the end
60 * b) not to open another file before you close this one
61 * (this will likely go away in future)
62 * as a special gift, you may
63 * c) lseek() immediately after open(), giving ftpfs chance to
64 * reget. Be warned that this lseek() can fail, and you _have_
65 * to handle that gratefully.
67 * O_LINEAR allows filesystems not to create temporary file in some
68 * cases (ftp transfer). -- pavel@ucw.cz
71 /* And now some defines for our errors. */
74 #define E_NOTSUPP ENOSYS /* for use in vfs when module does not provide function */
76 #define E_NOTSUPP EFAULT /* Does this happen? */
80 #define E_UNKNOWN ENOMSG /* if we do not know what error happened */
82 #define E_UNKNOWN EIO /* if we do not know what error happened */
86 #define E_REMOTE EREMOTEIO /* if other side of ftp/fish reports error */
88 #define E_REMOTE ENETUNREACH /* :-( there's no EREMOTEIO on some systems */
92 #define E_PROTO EPROTO /* if other side fails to follow protocol */
97 typedef void (*fill_names_f
) (const char *);
101 /*** enums ***************************************************************************************/
103 /* Flags of VFS classes */
107 VFSF_LOCAL
= 1 << 0, /* Class is local (not virtual) filesystem */
108 VFSF_NOLINKS
= 1 << 1 /* Hard links not supported */
111 /* Operations for mc_ctl - on open file */
117 /* Operations for mc_setctl - on path */
123 VFS_SETCTL_FLUSH
, /* invalidate directory cache */
125 /* Setting this makes vfs layer give out potentially incorrect data,
126 but it also makes some operations much faster. Use with caution. */
127 VFS_SETCTL_STALE_DATA
130 /*** structures declarations (and typedefs of structures)*****************************************/
132 typedef struct vfs_class
134 const char *name
; /* "FIles over SHell" */
135 vfs_class_flags_t flags
;
136 const char *prefix
; /* "fish:" */
137 void *data
; /* this is for filesystem's own use */
138 int verrno
; /* can't use errno because glibc2 might define errno as function */
140 int (*init
) (struct vfs_class
* me
);
141 void (*done
) (struct vfs_class
* me
);
144 * The fill_names method shall call the callback function for every
145 * filesystem name that this vfs module supports.
147 void (*fill_names
) (struct vfs_class
* me
, fill_names_f
);
150 * The which() method shall return the index of the vfs subsystem
151 * or -1 if this vfs cannot handle the given pathname.
153 int (*which
) (struct vfs_class
* me
, const char *path
);
155 void *(*open
) (const vfs_path_t
* vpath
, int flags
, mode_t mode
);
156 int (*close
) (void *vfs_info
);
157 ssize_t (*read
) (void *vfs_info
, char *buffer
, size_t count
);
158 ssize_t (*write
) (void *vfs_info
, const char *buf
, size_t count
);
160 void *(*opendir
) (const vfs_path_t
* vpath
);
161 void *(*readdir
) (void *vfs_info
);
162 int (*closedir
) (void *vfs_info
);
164 int (*stat
) (const vfs_path_t
* vpath
, struct stat
* buf
);
165 int (*lstat
) (const vfs_path_t
* vpath
, struct stat
* buf
);
166 int (*fstat
) (void *vfs_info
, struct stat
* buf
);
168 int (*chmod
) (const vfs_path_t
* vpath
, mode_t mode
);
169 int (*chown
) (const vfs_path_t
* vpath
, uid_t owner
, gid_t group
);
170 int (*utime
) (const vfs_path_t
* vpath
, struct utimbuf
* times
);
172 int (*readlink
) (const vfs_path_t
* vpath
, char *buf
, size_t size
);
173 int (*symlink
) (const vfs_path_t
* vpath1
, const vfs_path_t
* vpath2
);
174 int (*link
) (const vfs_path_t
* vpath1
, const vfs_path_t
* vpath2
);
175 int (*unlink
) (const vfs_path_t
* vpath
);
176 int (*rename
) (const vfs_path_t
* vpath1
, const vfs_path_t
* vpath2
);
177 int (*chdir
) (const vfs_path_t
* vpath
);
178 int (*ferrno
) (struct vfs_class
* me
);
179 off_t (*lseek
) (void *vfs_info
, off_t offset
, int whence
);
180 int (*mknod
) (const vfs_path_t
* vpath
, mode_t mode
, dev_t dev
);
182 vfsid (*getid
) (const vfs_path_t
* vpath
);
184 int (*nothingisopen
) (vfsid id
);
185 void (*free
) (vfsid id
);
187 vfs_path_t
*(*getlocalcopy
) (const vfs_path_t
* vpath
);
188 int (*ungetlocalcopy
) (const vfs_path_t
* vpath
, const vfs_path_t
* local_vpath
,
189 gboolean has_changed
);
191 int (*mkdir
) (const vfs_path_t
* vpath
, mode_t mode
);
192 int (*rmdir
) (const vfs_path_t
* vpath
);
194 int (*ctl
) (void *vfs_info
, int ctlop
, void *arg
);
195 int (*setctl
) (const vfs_path_t
* vpath
, int ctlop
, void *arg
);
199 * This union is used to ensure that there is enough space for the
200 * filename (d_name) when the dirent structure is created.
205 char _extra_buffer
[offsetof (struct dirent
, d_name
) + MC_MAXPATHLEN
+ 1];
208 /*** global variables defined in .c file *********************************************************/
210 extern int vfs_timeout
;
212 #ifdef ENABLE_VFS_NET
213 extern int use_netrc
;
216 /*** declarations of public functions ************************************************************/
218 /* lib/vfs/direntry.c: */
219 void *vfs_s_open (const vfs_path_t
* vpath
, int flags
, mode_t mode
);
221 vfsid
vfs_getid (const vfs_path_t
* vpath
);
223 void vfs_init (void);
224 void vfs_shut (void);
225 /* Register a file system class */
226 gboolean
vfs_register_class (struct vfs_class
*vfs
);
228 void vfs_setup_work_dir (void);
230 void vfs_timeout_handler (void);
231 int vfs_timeouts (void);
232 void vfs_expire (int now
);
234 char *vfs_get_current_dir (void);
235 vfs_path_t
*vfs_get_raw_current_dir (void);
236 void vfs_set_raw_current_dir (const vfs_path_t
* vpath
);
238 gboolean
vfs_current_is_local (void);
239 gboolean
vfs_file_is_local (const vfs_path_t
* vpath
);
241 char *vfs_strip_suffix_from_filename (const char *filename
);
243 vfs_class_flags_t
vfs_file_class_flags (const vfs_path_t
* vpath
);
245 /* translate path back to terminal encoding, remove all #enc:
246 * every invalid character is replaced with question mark
247 * return static buffer */
248 char *vfs_translate_path (const char *path
);
249 /* return new string */
250 char *vfs_translate_path_n (const char *path
);
252 void vfs_stamp_path (const char *path
);
254 void vfs_release_path (const char *dir
);
256 void vfs_fill_names (fill_names_f
);
258 void vfs_print_message (const char *msg
, ...) __attribute__ ((format (__printf__
, 1, 2)));
260 int vfs_ferrno (struct vfs_class
*vfs
);
262 int vfs_new_handle (struct vfs_class
*vclass
, void *fsinfo
);
264 struct vfs_class
*vfs_class_find_by_handle (int handle
);
266 void *vfs_class_data_find_by_handle (int handle
);
268 void vfs_free_handle (int handle
);
270 void vfs_setup_cwd (void);
271 char *_vfs_get_cwd (void);
273 vfs_path_t
*vfs_change_encoding (vfs_path_t
* vpath
, const char *encoding
);
275 int vfs_preallocate (int dest_desc
, off_t src_fsize
, off_t dest_fsize
);
278 * Interface functions described in interface.c
280 ssize_t
mc_read (int handle
, void *buffer
, size_t count
);
281 ssize_t
mc_write (int handle
, const void *buffer
, size_t count
);
282 int mc_utime (const vfs_path_t
* vpath
, struct utimbuf
*times
);
283 int mc_readlink (const vfs_path_t
* vpath
, char *buf
, size_t bufsiz
);
284 int mc_close (int handle
);
285 off_t
mc_lseek (int fd
, off_t offset
, int whence
);
286 DIR *mc_opendir (const vfs_path_t
* vpath
);
287 struct dirent
*mc_readdir (DIR * dirp
);
288 int mc_closedir (DIR * dir
);
289 int mc_stat (const vfs_path_t
* vpath
, struct stat
*buf
);
290 int mc_mknod (const vfs_path_t
* vpath
, mode_t mode
, dev_t dev
);
291 int mc_link (const vfs_path_t
* vpath1
, const vfs_path_t
* vpath2
);
292 int mc_mkdir (const vfs_path_t
* vpath
, mode_t mode
);
293 int mc_rmdir (const vfs_path_t
* vpath
);
294 int mc_fstat (int fd
, struct stat
*buf
);
295 int mc_lstat (const vfs_path_t
* vpath
, struct stat
*buf
);
296 int mc_symlink (const vfs_path_t
* vpath1
, const vfs_path_t
* vpath2
);
297 int mc_rename (const vfs_path_t
* vpath1
, const vfs_path_t
* vpath2
);
298 int mc_chmod (const vfs_path_t
* vpath
, mode_t mode
);
299 int mc_chown (const vfs_path_t
* vpath
, uid_t owner
, gid_t group
);
300 int mc_chdir (const vfs_path_t
* vpath
);
301 int mc_unlink (const vfs_path_t
* vpath
);
302 int mc_ctl (int fd
, int ctlop
, void *arg
);
303 int mc_setctl (const vfs_path_t
* vpath
, int ctlop
, void *arg
);
304 int mc_open (const vfs_path_t
* vpath
, int flags
, ...);
305 vfs_path_t
*mc_getlocalcopy (const vfs_path_t
* pathname_vpath
);
306 int mc_ungetlocalcopy (const vfs_path_t
* pathname_vpath
, const vfs_path_t
* local_vpath
,
307 gboolean has_changed
);
308 int mc_mkstemps (vfs_path_t
** pname_vpath
, const char *prefix
, const char *suffix
);
310 /* Creating temporary files safely */
311 const char *mc_tmpdir (void);
314 /*** inline functions ****************************************************************************/
316 #endif /* MC_VFS_VFS_H */