Ticket #2361: VFS URI reimplementation
[midnight-commander.git] / lib / vfs / vfs.h
blob5319711f98d11e743bf220d62f321c72bff25a8e
2 /**
3 * \file
4 * \brief Header: Virtual File System switch code
5 */
7 #ifndef MC__VFS_VFS_H
8 #define MC__VFS_VFS_H
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <dirent.h>
13 #include <utime.h>
14 #include <stdio.h>
15 #include <fcntl.h>
16 #include <unistd.h>
17 #include <stddef.h>
19 #include "lib/global.h"
20 #include "lib/fs.h" /* MC_MAXPATHLEN */
22 #include "path.h"
24 /*** typedefs(not structures) and defined constants **********************************************/
26 #if defined (ENABLE_VFS_FTP) || defined (ENABLE_VFS_FISH) || defined (ENABLE_VFS_SMB)
27 #define ENABLE_VFS_NET 1
28 #endif
30 /**
31 * This is the type of callback function passed to vfs_fill_names.
32 * It gets the name of the virtual file system as its first argument.
33 * See also:
34 * vfs_fill_names().
37 #define VFS_ENCODING_PREFIX "#enc:"
39 #define O_ALL (O_CREAT | O_EXCL | O_NOCTTY | O_NDELAY | O_SYNC | O_WRONLY | O_RDWR | O_RDONLY)
40 /* Midnight commander code should _not_ use other flags than those
41 listed above and O_APPEND */
43 #if (O_ALL & O_APPEND)
44 #warning "Unexpected problem with flags, O_LINEAR disabled, contact pavel@ucw.cz"
45 #define O_LINEAR 0
46 #define IS_LINEAR(a) 0
47 #define NO_LINEAR(a) a
48 #else
49 #define O_LINEAR O_APPEND
50 #define IS_LINEAR(a) ((a) == (O_RDONLY | O_LINEAR)) /* Return only 0 and 1 ! */
51 #define NO_LINEAR(a) (((a) == (O_RDONLY | O_LINEAR)) ? O_RDONLY : (a))
52 #endif
54 /* O_LINEAR is strange beast, be careful. If you open file asserting
55 * O_RDONLY | O_LINEAR, you promise:
57 * a) to read file linearly from beginning to the end
58 * b) not to open another file before you close this one
59 * (this will likely go away in future)
60 * as a special gift, you may
61 * c) lseek() immediately after open(), giving ftpfs chance to
62 * reget. Be warned that this lseek() can fail, and you _have_
63 * to handle that gratefully.
65 * O_LINEAR allows filesystems not to create temporary file in some
66 * cases (ftp transfer). -- pavel@ucw.cz
69 /* And now some defines for our errors. */
71 #ifdef ENOSYS
72 #define E_NOTSUPP ENOSYS /* for use in vfs when module does not provide function */
73 #else
74 #define E_NOTSUPP EFAULT /* Does this happen? */
75 #endif
77 #ifdef ENOMSG
78 #define E_UNKNOWN ENOMSG /* if we do not know what error happened */
79 #else
80 #define E_UNKNOWN EIO /* if we do not know what error happened */
81 #endif
83 #ifdef EREMOTEIO
84 #define E_REMOTE EREMOTEIO /* if other side of ftp/fish reports error */
85 #else
86 #define E_REMOTE ENETUNREACH /* :-( there's no EREMOTEIO on some systems */
87 #endif
89 #ifdef EPROTO
90 #define E_PROTO EPROTO /* if other side fails to follow protocol */
91 #else
92 #define E_PROTO EIO
93 #endif
95 typedef void (*fill_names_f) (const char *);
97 typedef void *vfsid;
99 /*** enums ***************************************************************************************/
101 /* Flags of VFS classes */
102 typedef enum
104 VFSF_UNKNOWN = 0,
105 VFSF_LOCAL = 1 << 0, /* Class is local (not virtual) filesystem */
106 VFSF_NOLINKS = 1 << 1 /* Hard links not supported */
107 } vfs_class_flags_t;
109 /* Operations for mc_ctl - on open file */
110 enum
112 VFS_CTL_IS_NOTREADY
115 /* Operations for mc_setctl - on path */
116 enum
118 VFS_SETCTL_FORGET,
119 VFS_SETCTL_RUN,
120 VFS_SETCTL_LOGFILE,
121 VFS_SETCTL_FLUSH, /* invalidate directory cache */
123 /* Setting this makes vfs layer give out potentially incorrect data,
124 but it also makes some operations much faster. Use with caution. */
125 VFS_SETCTL_STALE_DATA
128 /*** structures declarations (and typedefs of structures)*****************************************/
130 typedef struct vfs_class
132 const char *name; /* "FIles over SHell" */
133 vfs_class_flags_t flags;
134 const char *prefix; /* "fish:" */
135 void *data; /* this is for filesystem's own use */
136 int verrno; /* can't use errno because glibc2 might define errno as function */
138 int (*init) (struct vfs_class * me);
139 void (*done) (struct vfs_class * me);
142 * The fill_names method shall call the callback function for every
143 * filesystem name that this vfs module supports.
145 void (*fill_names) (struct vfs_class * me, fill_names_f);
148 * The which() method shall return the index of the vfs subsystem
149 * or -1 if this vfs cannot handle the given pathname.
151 int (*which) (struct vfs_class * me, const char *path);
153 void *(*open) (const vfs_path_t * vpath, int flags, mode_t mode);
154 int (*close) (void *vfs_info);
155 ssize_t (*read) (void *vfs_info, char *buffer, size_t count);
156 ssize_t (*write) (void *vfs_info, const char *buf, size_t count);
158 void *(*opendir) (const vfs_path_t * vpath);
159 void *(*readdir) (void *vfs_info);
160 int (*closedir) (void *vfs_info);
162 int (*stat) (const vfs_path_t * vpath, struct stat * buf);
163 int (*lstat) (const vfs_path_t * vpath, struct stat * buf);
164 int (*fstat) (void *vfs_info, struct stat * buf);
166 int (*chmod) (const vfs_path_t * vpath, mode_t mode);
167 int (*chown) (const vfs_path_t * vpath, uid_t owner, gid_t group);
168 int (*utime) (const vfs_path_t * vpath, struct utimbuf * times);
170 int (*readlink) (const vfs_path_t * vpath, char *buf, size_t size);
171 int (*symlink) (const vfs_path_t * vpath1, const vfs_path_t * vpath2);
172 int (*link) (const vfs_path_t * vpath1, const vfs_path_t * vpath2);
173 int (*unlink) (const vfs_path_t * vpath);
174 int (*rename) (const vfs_path_t * vpath1, const vfs_path_t * vpath2);
175 int (*chdir) (const vfs_path_t * vpath);
176 int (*ferrno) (struct vfs_class * me);
177 off_t (*lseek) (void *vfs_info, off_t offset, int whence);
178 int (*mknod) (const vfs_path_t * vpath, mode_t mode, dev_t dev);
180 vfsid (*getid) (const vfs_path_t * vpath);
182 int (*nothingisopen) (vfsid id);
183 void (*free) (vfsid id);
185 char *(*getlocalcopy) (const vfs_path_t * vpath);
186 int (*ungetlocalcopy) (const vfs_path_t * vpath, const char *local, int has_changed);
188 int (*mkdir) (const vfs_path_t * vpath, mode_t mode);
189 int (*rmdir) (const vfs_path_t * vpath);
191 int (*ctl) (void *vfs_info, int ctlop, void *arg);
192 int (*setctl) (const vfs_path_t * vpath, int ctlop, void *arg);
193 } vfs_class;
196 * This union is used to ensure that there is enough space for the
197 * filename (d_name) when the dirent structure is created.
199 union vfs_dirent
201 struct dirent dent;
202 char _extra_buffer[offsetof (struct dirent, d_name) + MC_MAXPATHLEN + 1];
205 /*** global variables defined in .c file *********************************************************/
207 extern int vfs_timeout;
209 #ifdef ENABLE_VFS_NET
210 extern int use_netrc;
211 #endif
213 /*** declarations of public functions ************************************************************/
215 /* lib/vfs/direntry.c: */
216 void *vfs_s_open (const vfs_path_t * vpath, int flags, mode_t mode);
218 vfsid vfs_getid (const vfs_path_t * vpath);
220 void vfs_init (void);
221 void vfs_shut (void);
222 /* Register a file system class */
223 gboolean vfs_register_class (struct vfs_class *vfs);
225 void vfs_setup_work_dir (void);
227 void vfs_timeout_handler (void);
228 int vfs_timeouts (void);
229 void vfs_expire (int now);
231 char *vfs_get_current_dir (void);
232 vfs_path_t *vfs_get_raw_current_dir (void);
233 void vfs_set_raw_current_dir (const vfs_path_t * vpath);
235 gboolean vfs_current_is_local (void);
236 gboolean vfs_file_is_local (const vfs_path_t * vpath);
238 char *vfs_strip_suffix_from_filename (const char *filename);
240 vfs_class_flags_t vfs_file_class_flags (const vfs_path_t * vpath);
242 /* translate path back to terminal encoding, remove all #enc:
243 * every invalid character is replaced with question mark
244 * return static buffer */
245 char *vfs_translate_path (const char *path);
246 /* return new string */
247 char *vfs_translate_path_n (const char *path);
249 void vfs_stamp_path (const char *path);
251 void vfs_release_path (const char *dir);
253 void vfs_fill_names (fill_names_f);
255 void vfs_print_message (const char *msg, ...) __attribute__ ((format (__printf__, 1, 2)));
257 int vfs_ferrno (struct vfs_class *vfs);
259 int vfs_new_handle (struct vfs_class *vclass, void *fsinfo);
261 struct vfs_class *vfs_class_find_by_handle (int handle);
263 void *vfs_class_data_find_by_handle (int handle);
265 void vfs_free_handle (int handle);
267 char *_vfs_get_cwd (void);
269 vfs_path_t *vfs_change_encoding (vfs_path_t * vpath, const char *encoding);
272 * Interface functions described in interface.c
274 ssize_t mc_read (int handle, void *buffer, size_t count);
275 ssize_t mc_write (int handle, const void *buffer, size_t count);
276 int mc_utime (const char *path, struct utimbuf *times);
277 int mc_readlink (const char *path, char *buf, size_t bufsiz);
278 int mc_close (int handle);
279 off_t mc_lseek (int fd, off_t offset, int whence);
280 DIR *mc_opendir (const char *dirname);
281 struct dirent *mc_readdir (DIR * dirp);
282 int mc_closedir (DIR * dir);
283 int mc_stat (const char *path, struct stat *buf);
284 int mc_mknod (const char *path, mode_t mode, dev_t dev);
285 int mc_link (const char *name1, const char *name2);
286 int mc_mkdir (const char *path, mode_t mode);
287 int mc_rmdir (const char *path);
288 int mc_fstat (int fd, struct stat *buf);
289 int mc_lstat (const char *path, struct stat *buf);
290 int mc_symlink (const char *name1, const char *name2);
291 int mc_rename (const char *original, const char *target);
292 int mc_chmod (const char *path, mode_t mode);
293 int mc_chown (const char *path, uid_t owner, gid_t group);
294 int mc_chdir (const char *path);
295 int mc_unlink (const char *path);
296 int mc_ctl (int fd, int ctlop, void *arg);
297 int mc_setctl (const char *path, int ctlop, void *arg);
298 int mc_open (const char *filename, int flags, ...);
299 char *mc_get_current_wd (char *buffer, size_t bufsize);
300 char *mc_getlocalcopy (const char *pathname);
301 int mc_ungetlocalcopy (const char *pathname, const char *local, int has_changed);
304 /*** inline functions ****************************************************************************/
305 #endif /* MC_VFS_VFS_H */