Back to index

avfs  1.0.1
avfs.h
Go to the documentation of this file.
00001 /*
00002     AVFS: A Virtual File System Library
00003     Copyright (C) 1998-2001  Miklos Szeredi <miklos@szeredi.hu>
00004 
00005     This program can be distributed under the terms of the GNU GPL.
00006     See the file COPYING.
00007 */
00008 
00009 #ifndef _AVFS_H
00010 #define _AVFS_H
00011 
00012 #include <errno.h>
00013 #include <string.h>
00014 #include <ctype.h>
00015 #include <pthread.h>
00016 
00017 /* The following 3 includes are not needed except for some
00018  * systems which redefine open to open64 etc. (namely Sun)
00019  */
00020 #include <fcntl.h>
00021 #include <unistd.h>
00022 #include <dirent.h>
00023 
00024 #ifndef __GNUC__
00025 #define __attribute__(x)
00026 #endif
00027 
00028 typedef unsigned char       avbyte;      /* 1 byte unsigned    */
00029 typedef unsigned short      avushort;    /* 2 bytes unsigned   */
00030 typedef unsigned int        avuint;      /* 4 bytes unsigned   */
00031 typedef unsigned long       avulong;     /* 4-8 bytes unsigned */
00032 typedef long long           avquad;      /* 8 bytes signed     */
00033 typedef unsigned long long  avuquad;     /* 8 bytes unsigned   */
00034 
00035 typedef avuquad avdev_t;
00036 typedef avuint  avmode_t;
00037 typedef avuint  avnlink_t;
00038 typedef avuint  avuid_t;
00039 typedef avuint  avgid_t;
00040 typedef avuquad avino_t;
00041 typedef avquad  avoff_t;
00042 typedef long    avtime_t;
00043 typedef avuint  avsize_t;
00044 typedef int     avssize_t;
00045 typedef avulong avblksize_t;
00046 typedef avquad  avblkcnt_t;
00047 
00048 typedef pthread_mutex_t avmutex;
00049 
00050 typedef struct _avtimestruc_t avtimestruc_t;
00051 struct _avtimestruc_t {
00052     avtime_t sec;
00053     long     nsec;
00054 };
00055 
00056 struct avstat {
00057     avdev_t          dev;
00058     avino_t          ino;
00059     avmode_t         mode;
00060     avnlink_t        nlink;
00061     avuid_t          uid;
00062     avgid_t          gid;
00063     avdev_t          rdev;
00064     avoff_t          size;
00065     avblksize_t      blksize;
00066     avblkcnt_t       blocks;
00067     avtimestruc_t    atime;
00068     avtimestruc_t    mtime;
00069     avtimestruc_t    ctime;
00070 };
00071 
00072 struct avdirent {
00073     avino_t ino;
00074     int type;
00075     char *name;
00076 };
00077 
00078 typedef struct _ventry ventry;
00079 struct _ventry {
00080     void *data;
00081     struct avmount *mnt;
00082 };
00083 
00084 typedef struct _vfile vfile;
00085 struct _vfile {
00086     void *data;
00087     struct avmount *mnt;
00088     int flags;
00089     avoff_t ptr;
00090     avmutex lock;
00091 };
00092 
00093 struct avmount {
00094     ventry *base;
00095     struct avfs *avfs;
00096     char *opts;
00097     int flags;
00098 };
00099 
00100 struct avfs {
00101     /* private */
00102     struct vmodule *module;
00103     avmutex lock;
00104     avino_t inoctr;
00105 
00106     /* read-only: */
00107     char *name;
00108     struct ext_info *exts;
00109     void *data;
00110     int version;
00111     int flags;
00112     avdev_t dev;
00113 
00114     void      (*destroy) (struct avfs *avfs);
00115     
00116     int       (*lookup)  (ventry *ve, const char *name, void **retp);
00117     void      (*putent)  (ventry *ve);
00118     int       (*copyent) (ventry *ve, void **retp);
00119     int       (*getpath) (ventry *ve, char **retp);
00120                          
00121     int       (*access)  (ventry *ve, int amode);
00122     int       (*readlink)(ventry *ve, char **bufp);
00123     int       (*symlink) (const char *path, ventry *newve);
00124     int       (*unlink)  (ventry *ve);
00125     int       (*rmdir)   (ventry *ve);
00126     int       (*mknod)   (ventry *ve, avmode_t mode, avdev_t dev);
00127     int       (*mkdir)   (ventry *ve, avmode_t mode);
00128     int       (*rename)  (ventry *ve, ventry *newve);
00129     int       (*link)    (ventry *ve, ventry *newve);
00130                          
00131     int       (*open)    (ventry *ve, int flags, avmode_t mode, void **retp);
00132     int       (*close)   (vfile *vf);
00133     avssize_t (*read)    (vfile *vf, char *buf, avsize_t nbyte);
00134     avssize_t (*write)   (vfile *vf, const char *buf, avsize_t nbyte);
00135     int       (*readdir) (vfile *vf, struct avdirent *buf);
00136     int       (*getattr) (vfile *vf, struct avstat *buf, int attrmask);
00137     int       (*setattr) (vfile *vf, struct avstat *buf, int attrmask);
00138     int       (*truncate)(vfile *vf, avoff_t length);
00139     avoff_t   (*lseek)   (vfile *vf, avoff_t offset, int whence);
00140 };
00141 
00142 struct ext_info {
00143     const char *from;
00144     const char *to;
00145 };
00146 
00147 struct avtm {
00148     int sec;    /* [0-59] (note: 61 _can_ happen) */
00149     int min;    /* [0-59] */
00150     int hour;   /* [0-23] */
00151     int day;    /* [1-31] */
00152     int mon;    /* [0-11] */
00153     int year;   /* Year - 1900 */
00154 };
00155 
00156 typedef struct {
00157     char *name;
00158     int is_tmp;
00159 } real_file;
00160 
00161 
00162 typedef struct {
00163     int outfd;
00164     ventry *ve;
00165 } rep_file;
00166 
00167 #define AV_NEW(ptr)   ptr = av_calloc(sizeof(*(ptr)))
00168 
00169 #define AV_NEW_OBJ(ptr, destr) \
00170    ptr = av_new_obj(sizeof(*(ptr)), (void (*)(void *)) destr)
00171 
00172 #define AV_LOCK_DECL(mutex) avmutex mutex
00173 #define AV_INITLOCK(mutex) pthread_mutex_init(&(mutex), NULL)
00174 #define AV_INIT_RECURSIVELOCK(mutex) { pthread_mutexattr_t attr;   \
00175         pthread_mutexattr_init(&attr);                                  \
00176         if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0) { \
00177             av_log(AVLOG_ERROR, "Couldn't init recursive mutex");       \
00178             exit(1);                                                    \
00179         }                                                               \
00180         if (pthread_mutex_init(&(mutex), &attr) != 0) {                 \
00181             av_log(AVLOG_ERROR, "Couldn't init recursive mutex");       \
00182             exit(1);                                                    \
00183         }                                                               \
00184         pthread_mutexattr_destroy(&attr);}
00185 #define AV_FREELOCK(mutex) pthread_mutex_destroy(&(mutex));
00186 #define AV_LOCK(mutex)     pthread_mutex_lock(&(mutex))
00187 #define AV_UNLOCK(mutex)   pthread_mutex_unlock(&(mutex))
00188 
00189 #define AV_INIT_EXT(e, f, t) (e).from = (f), (e).to = (t)
00190 
00191 #define AV_MAX(x, y) ((x) > (y) ? (x) : (y))
00192 #define AV_MIN(x, y) ((x) < (y) ? (x) : (y))
00193 #define AV_DIV(x, y) ((x) ? (((x) - 1) / (y) + 1) : 0)
00194 
00195 #define AV_TIME_LESS(t1, t2) ((t1).sec < (t2).sec || \
00196                               ((t1).sec == (t2).sec && (t1).nsec < (t2).nsec))
00197 
00198 #define AV_TIME_EQ(t1, t2) ((t1).sec == (t2).sec && (t1).nsec == (t2).nsec)
00199 
00200 #define AV_BLOCKS(x) AV_DIV(x, 512)
00201 
00202 #define AV_MAXTIME (~(1L << (sizeof(avtime_t) * 8 - 1)))
00203 #define AV_MAXOFF  0x7FFFFFFFFFFFFFFFLL
00204 
00205 #define AV_DIR_SEP_CHAR '/'
00206 #define AV_DIR_SEP_STR  "/"
00207 
00208 #ifndef NULL
00209 #define NULL ((void *) 0)
00210 #endif
00211 
00212 #define AVSEEK_SET   0
00213 #define AVSEEK_CUR   1
00214 #define AVSEEK_END   2
00215 
00216 #define AVR_OK       4
00217 #define AVW_OK       2
00218 #define AVX_OK       1
00219 #define AVF_OK       0
00220 
00221 #define AVO_ACCMODE  0x03
00222 
00223 #define AVO_RDONLY   0
00224 #define AVO_WRONLY   1
00225 #define AVO_RDWR     2
00226 #define AVO_NOPERM   3
00227 
00228 #define AV_ISWRITE(flags) ((flags & AVO_ACCMODE) == AVO_WRONLY || \
00229                            (flags & AVO_ACCMODE) == AVO_RDWR)
00230 
00231 #define AVO_CREAT      0x00000040
00232 #define AVO_EXCL       0x00000080
00233 #define AVO_TRUNC      0x00000200
00234 #define AVO_APPEND     0x00000400
00235 #define AVO_NONBLOCK   0x00000800
00236 #define AVO_SYNC       0x00001000
00237 #define AVO_DIRECTORY  0x00010000
00238 #define AVO_NOFOLLOW   0x00020000
00239 
00240 #define AVA_DEV     (1 << 0)
00241 #define AVA_INO     (1 << 1)
00242 #define AVA_MODE    (1 << 2)
00243 #define AVA_NLINK   (1 << 3)
00244 #define AVA_UID     (1 << 4)
00245 #define AVA_GID     (1 << 5)
00246 #define AVA_RDEV    (1 << 6)
00247 #define AVA_SIZE    (1 << 7)
00248 #define AVA_BLKSIZE (1 << 8)
00249 #define AVA_BLKCNT  (1 << 9)
00250 #define AVA_ATIME   (1 << 10)
00251 #define AVA_MTIME   (1 << 11)
00252 #define AVA_CTIME   (1 << 12)
00253 
00254 #define AVA_ALL     0x00001fff
00255 
00256 #define AV_IFMT   0170000
00257 #define AV_IFDIR  0040000
00258 #define AV_IFCHR  0020000
00259 #define AV_IFBLK  0060000
00260 #define AV_IFREG  0100000
00261 #define AV_IFIFO  0010000
00262 #define AV_IFLNK  0120000
00263 #define AV_IFSOCK 0140000
00264 
00265 #define AV_ISVTX  01000
00266 #define AV_ISGID  02000
00267 #define AV_ISUID  04000
00268 
00269 #define AV_ISTYPE(mode, mask)  (((mode) & AV_IFMT) == (mask))
00270  
00271 #define AV_ISDIR(mode)  AV_ISTYPE((mode), AV_IFDIR)
00272 #define AV_ISCHR(mode)  AV_ISTYPE((mode), AV_IFCHR)
00273 #define AV_ISBLK(mode)  AV_ISTYPE((mode), AV_IFBLK)
00274 #define AV_ISREG(mode)  AV_ISTYPE((mode), AV_IFREG)
00275 #define AV_ISFIFO(mode) AV_ISTYPE((mode), AV_IFIFO)
00276 #define AV_ISLNK(mode)  AV_ISTYPE((mode), AV_IFLNK)
00277 #define AV_ISSOCK(mode) AV_ISTYPE((mode), AV_IFSOCK)
00278 
00279 #define AV_TYPE(mode) (((mode) & AV_IFMT) >> 12)
00280 
00281 #define AVLOG_ERROR     001
00282 #define AVLOG_WARNING   002
00283 #define AVLOG_DEBUG     004
00284 #define AVLOG_SYSCALL   010
00285 
00286 #define AVF_NEEDSLASH  (1 << 0)
00287 #define AVF_ONLYROOT   (1 << 1)
00288 #define AVF_NOLOCK     (1 << 2)
00289 
00290 int        av_new_avfs(const char *name, struct ext_info *exts, int version,
00291                        int flags, struct vmodule *module, struct avfs **retp);
00292 void       av_add_avfs(struct avfs *avfs);
00293 avino_t    av_new_ino(struct avfs *avfs);
00294 
00295 int        av_check_version(const char *modname, const char *name, int version,
00296                             int need_ver, int provide_ver);
00297 
00298 int        av_copy_ventry(ventry *ve, ventry **retp);
00299 void       av_free_ventry(ventry *ve);
00300 int        av_generate_path(ventry *ve, char **pathp);
00301 
00302 avdev_t    av_mkdev(int major, int minor);
00303 void       av_splitdev(avdev_t dev, int *majorp, int *minorp);
00304 void       av_default_stat(struct avstat *stbuf);
00305           
00306 void       av_log(int level, const char *format, ...)
00307     __attribute__ ((format (printf, 2, 3)));
00308 char      *av_get_config(const char *param);
00309 void      *av_malloc(avsize_t nbyte);
00310 void      *av_calloc(avsize_t nbyte);
00311 void      *av_realloc(void *ptr, avsize_t nbyte);
00312 void       av_free(void *ptr);
00313          
00314 void      *av_new_obj(avsize_t nbyte, void (*destr)(void *));
00315 void       av_ref_obj(void *obj);
00316 void       av_unref_obj(void *obj);
00317 void       av_obj_set_ref_lock(void *obj, avmutex *lock);
00318 void       av_obj_set_destr_locked(void *obj, void (*destr)(void *));
00319           
00320 char      *av_strdup(const char *s);
00321 char      *av_strndup(const char *s, avsize_t len);
00322 char      *av_stradd(char *s1, ...);
00323           
00324 void       av_registerfd(int fd);
00325 void       av_curr_time(avtimestruc_t *tim);
00326 avtime_t   av_time();
00327 void       av_sleep(unsigned long msec);
00328 avtime_t   av_mktime(struct avtm *tp);
00329 void       av_localtime(avtime_t t, struct avtm *tp);
00330 int        av_get_tmpfile(char **retp);
00331 void       av_del_tmpfile(char *tmpfile);
00332 avoff_t    av_tmp_free();
00333 
00334 avssize_t  av_read_all(vfile *vf, char *buf, avsize_t nbyte);
00335 avssize_t av_pread_all(vfile *vf, char *buf, avsize_t nbyte, avoff_t offset);
00336 
00337 #endif /* _AVFS_H */