4 * \brief Header: Virtual File System switch code
10 #include <sys/types.h>
15 #elif defined (HAVE_UTIME_H)
22 #include "lib/global.h"
23 #include "lib/fs.h" /* MC_MAXPATHLEN */
27 /*** typedefs(not structures) and defined constants **********************************************/
29 #if defined (ENABLE_VFS_FTP) || defined (ENABLE_VFS_FISH) || defined (ENABLE_VFS_SMB)
30 #define ENABLE_VFS_NET 1
34 * This is the type of callback function passed to vfs_fill_names.
35 * It gets the name of the virtual file system as its first argument.
40 #define VFS_ENCODING_PREFIX "#enc:"
42 #define O_ALL (O_CREAT | O_EXCL | O_NOCTTY | O_NDELAY | O_SYNC | O_WRONLY | O_RDWR | O_RDONLY)
43 /* Midnight commander code should _not_ use other flags than those
44 listed above and O_APPEND */
46 #if (O_ALL & O_APPEND)
47 #warning "Unexpected problem with flags, O_LINEAR disabled, contact pavel@ucw.cz"
49 #define IS_LINEAR(a) 0
50 #define NO_LINEAR(a) a
52 #define O_LINEAR O_APPEND
53 #define IS_LINEAR(a) ((a) == (O_RDONLY | O_LINEAR)) /* Return only 0 and 1 ! */
54 #define NO_LINEAR(a) (((a) == (O_RDONLY | O_LINEAR)) ? O_RDONLY : (a))
57 /* O_LINEAR is strange beast, be careful. If you open file asserting
58 * O_RDONLY | O_LINEAR, you promise:
60 * a) to read file linearly from beginning to the end
61 * b) not to open another file before you close this one
62 * (this will likely go away in future)
63 * as a special gift, you may
64 * c) lseek() immediately after open(), giving ftpfs chance to
65 * reget. Be warned that this lseek() can fail, and you _have_
66 * to handle that gratefully.
68 * O_LINEAR allows filesystems not to create temporary file in some
69 * cases (ftp transfer). -- pavel@ucw.cz
72 /* And now some defines for our errors. */
75 #define E_NOTSUPP ENOSYS /* for use in vfs when module does not provide function */
77 #define E_NOTSUPP EFAULT /* Does this happen? */
81 #define E_UNKNOWN ENOMSG /* if we do not know what error happened */
83 #define E_UNKNOWN EIO /* if we do not know what error happened */
87 #define E_REMOTE EREMOTEIO /* if other side of ftp/fish reports error */
89 #define E_REMOTE ENETUNREACH /* :-( there's no EREMOTEIO on some systems */
93 #define E_PROTO EPROTO /* if other side fails to follow protocol */
98 typedef void (*fill_names_f
) (const char *);
102 #ifdef HAVE_UTIMENSAT
103 typedef struct timespec mc_timesbuf_t
[2];
105 typedef struct utimbuf mc_timesbuf_t
;
108 /*** enums ***************************************************************************************/
110 /* Flags of VFS classes */
114 VFSF_LOCAL
= 1 << 0, /* Class is local (not virtual) filesystem */
115 VFSF_NOLINKS
= 1 << 1 /* Hard links not supported */
118 /* Operations for mc_ctl - on open file */
124 /* Operations for mc_setctl - on path */
130 VFS_SETCTL_FLUSH
, /* invalidate directory cache */
132 /* Setting this makes vfs layer give out potentially incorrect data,
133 but it also makes some operations much faster. Use with caution. */
134 VFS_SETCTL_STALE_DATA
137 /*** structures declarations (and typedefs of structures)*****************************************/
139 typedef struct vfs_class
141 const char *name
; /* "FIles over SHell" */
142 vfs_class_flags_t flags
;
143 const char *prefix
; /* "fish:" */
144 void *data
; /* this is for filesystem's own use */
145 int verrno
; /* can't use errno because glibc2 might define errno as function */
148 int (*init
) (struct vfs_class
* me
);
149 void (*done
) (struct vfs_class
* me
);
152 * The fill_names method shall call the callback function for every
153 * filesystem name that this vfs module supports.
155 void (*fill_names
) (struct vfs_class
* me
, fill_names_f
);
158 * The which() method shall return the index of the vfs subsystem
159 * or -1 if this vfs cannot handle the given pathname.
161 int (*which
) (struct vfs_class
* me
, const char *path
);
163 void *(*open
) (const vfs_path_t
* vpath
, int flags
, mode_t mode
);
164 int (*close
) (void *vfs_info
);
165 ssize_t (*read
) (void *vfs_info
, char *buffer
, size_t count
);
166 ssize_t (*write
) (void *vfs_info
, const char *buf
, size_t count
);
168 void *(*opendir
) (const vfs_path_t
* vpath
);
169 void *(*readdir
) (void *vfs_info
);
170 int (*closedir
) (void *vfs_info
);
172 int (*stat
) (const vfs_path_t
* vpath
, struct stat
* buf
);
173 int (*lstat
) (const vfs_path_t
* vpath
, struct stat
* buf
);
174 int (*fstat
) (void *vfs_info
, struct stat
* buf
);
176 int (*chmod
) (const vfs_path_t
* vpath
, mode_t mode
);
177 int (*chown
) (const vfs_path_t
* vpath
, uid_t owner
, gid_t group
);
178 int (*utime
) (const vfs_path_t
* vpath
, mc_timesbuf_t
* times
);
180 int (*readlink
) (const vfs_path_t
* vpath
, char *buf
, size_t size
);
181 int (*symlink
) (const vfs_path_t
* vpath1
, const vfs_path_t
* vpath2
);
182 int (*link
) (const vfs_path_t
* vpath1
, const vfs_path_t
* vpath2
);
183 int (*unlink
) (const vfs_path_t
* vpath
);
184 int (*rename
) (const vfs_path_t
* vpath1
, const vfs_path_t
* vpath2
);
185 int (*chdir
) (const vfs_path_t
* vpath
);
186 int (*ferrno
) (struct vfs_class
* me
);
187 off_t (*lseek
) (void *vfs_info
, off_t offset
, int whence
);
188 int (*mknod
) (const vfs_path_t
* vpath
, mode_t mode
, dev_t dev
);
190 vfsid (*getid
) (const vfs_path_t
* vpath
);
192 int (*nothingisopen
) (vfsid id
);
193 void (*free
) (vfsid id
);
195 vfs_path_t
*(*getlocalcopy
) (const vfs_path_t
* vpath
);
196 int (*ungetlocalcopy
) (const vfs_path_t
* vpath
, const vfs_path_t
* local_vpath
,
197 gboolean has_changed
);
199 int (*mkdir
) (const vfs_path_t
* vpath
, mode_t mode
);
200 int (*rmdir
) (const vfs_path_t
* vpath
);
202 int (*ctl
) (void *vfs_info
, int ctlop
, void *arg
);
203 int (*setctl
) (const vfs_path_t
* vpath
, int ctlop
, void *arg
);
208 * This union is used to ensure that there is enough space for the
209 * filename (d_name) when the dirent structure is created.
214 char _extra_buffer
[offsetof (struct dirent
, d_name
) + MC_MAXPATHLEN
+ 1];
217 /*** global variables defined in .c file *********************************************************/
219 extern int vfs_timeout
;
221 #ifdef ENABLE_VFS_NET
222 extern int use_netrc
;
225 /*** declarations of public functions ************************************************************/
227 /* lib/vfs/direntry.c: */
228 void *vfs_s_open (const vfs_path_t
* vpath
, int flags
, mode_t mode
);
229 int vfs_s_stat (const vfs_path_t
* vpath
, struct stat
*buf
);
230 int vfs_s_lstat (const vfs_path_t
* vpath
, struct stat
*buf
);
231 int vfs_s_fstat (void *fh
, struct stat
*buf
);
233 void vfs_adjust_stat (struct stat
*s
);
235 vfsid
vfs_getid (const vfs_path_t
* vpath
);
237 void vfs_init (void);
238 void vfs_shut (void);
239 /* Register a file system class */
240 gboolean
vfs_register_class (struct vfs_class
*vfs
);
242 void vfs_setup_work_dir (void);
244 void vfs_timeout_handler (void);
245 int vfs_timeouts (void);
246 void vfs_expire (gboolean now
);
248 const char *vfs_get_current_dir (void);
249 char *vfs_get_current_dir_n (void);
250 const vfs_path_t
*vfs_get_raw_current_dir (void);
251 void vfs_set_raw_current_dir (const vfs_path_t
* vpath
);
253 gboolean
vfs_current_is_local (void);
254 gboolean
vfs_file_is_local (const vfs_path_t
* vpath
);
256 char *vfs_strip_suffix_from_filename (const char *filename
);
258 vfs_class_flags_t
vfs_file_class_flags (const vfs_path_t
* vpath
);
260 /* translate path back to terminal encoding, remove all #enc:
261 * every invalid character is replaced with question mark
262 * return static buffer */
263 const char *vfs_translate_path (const char *path
);
264 /* return new string */
265 char *vfs_translate_path_n (const char *path
);
267 void vfs_stamp_path (const char *path
);
269 void vfs_release_path (const vfs_path_t
* vpath
);
271 void vfs_fill_names (fill_names_f
);
274 void vfs_print_message (const char *msg
, ...) G_GNUC_PRINTF (1, 2);
277 int vfs_ferrno (struct vfs_class
*vfs
);
279 int vfs_new_handle (struct vfs_class
*vclass
, void *fsinfo
);
281 struct vfs_class
*vfs_class_find_by_handle (int handle
, void **fsinfo
);
283 void vfs_free_handle (int handle
);
285 void vfs_setup_cwd (void);
286 char *_vfs_get_cwd (void);
288 int vfs_preallocate (int dest_desc
, off_t src_fsize
, off_t dest_fsize
);
291 * Interface functions described in interface.c
293 ssize_t
mc_read (int handle
, void *buffer
, size_t count
);
294 ssize_t
mc_write (int handle
, const void *buffer
, size_t count
);
295 int mc_utime (const vfs_path_t
* vpath
, mc_timesbuf_t
* times
);
296 int mc_readlink (const vfs_path_t
* vpath
, char *buf
, size_t bufsiz
);
297 int mc_close (int handle
);
298 off_t
mc_lseek (int fd
, off_t offset
, int whence
);
299 DIR *mc_opendir (const vfs_path_t
* vpath
);
300 struct dirent
*mc_readdir (DIR * dirp
);
301 int mc_closedir (DIR * dir
);
302 int mc_stat (const vfs_path_t
* vpath
, struct stat
*buf
);
303 int mc_mknod (const vfs_path_t
* vpath
, mode_t mode
, dev_t dev
);
304 int mc_link (const vfs_path_t
* vpath1
, const vfs_path_t
* vpath2
);
305 int mc_mkdir (const vfs_path_t
* vpath
, mode_t mode
);
306 int mc_rmdir (const vfs_path_t
* vpath
);
307 int mc_fstat (int fd
, struct stat
*buf
);
308 int mc_lstat (const vfs_path_t
* vpath
, struct stat
*buf
);
309 int mc_symlink (const vfs_path_t
* vpath1
, const vfs_path_t
* vpath2
);
310 int mc_rename (const vfs_path_t
* vpath1
, const vfs_path_t
* vpath2
);
311 int mc_chmod (const vfs_path_t
* vpath
, mode_t mode
);
312 int mc_chown (const vfs_path_t
* vpath
, uid_t owner
, gid_t group
);
313 int mc_chdir (const vfs_path_t
* vpath
);
314 int mc_unlink (const vfs_path_t
* vpath
);
315 int mc_ctl (int fd
, int ctlop
, void *arg
);
316 int mc_setctl (const vfs_path_t
* vpath
, int ctlop
, void *arg
);
317 int mc_open (const vfs_path_t
* vpath
, int flags
, ...);
318 vfs_path_t
*mc_getlocalcopy (const vfs_path_t
* pathname_vpath
);
319 int mc_ungetlocalcopy (const vfs_path_t
* pathname_vpath
, const vfs_path_t
* local_vpath
,
320 gboolean has_changed
);
321 int mc_mkstemps (vfs_path_t
** pname_vpath
, const char *prefix
, const char *suffix
);
323 /* Creating temporary files safely */
324 const char *mc_tmpdir (void);
327 /*** inline functions ****************************************************************************/
329 #endif /* MC_VFS_VFS_H */