root/lib/vfs/vfs.h

/* [previous][next][first][last][top][bottom][index][help]  */

INCLUDED FROM


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

/* [previous][next][first][last][top][bottom][index][help]  */