Back to index

avfs  1.0.1
Classes | Defines | Functions
virtual.c File Reference
#include "virtual.h"
#include "operutil.h"
#include "oper.h"
#include "internal.h"
#include <dirent.h>
#include <fcntl.h>
#include <utime.h>
#include <sys/types.h>
#include <sys/stat.h>

Go to the source code of this file.

Classes

struct  AVDIR

Defines

#define NAME_MAX   255
#define AVFS_DIR_RECLEN   256 /* just an arbitary number */

Functions

static int oflags_to_avfs (int flags)
int virt_open (const char *path, int flags, mode_t mode)
int virt_close (int fd)
ssize_t virt_write (int fd, const void *buf, size_t nbyte)
ssize_t virt_read (int fd, void *buf, size_t nbyte)
off_t virt_lseek (int fd, off_t offset, int whence)
static void avstat_to_stat (struct stat *buf, struct avstat *avbuf)
int virt_fstat (int fd, struct stat *buf)
static int open_path (vfile *vf, const char *path, int flags, avmode_t mode)
static int common_stat (const char *path, struct stat *buf, int flags)
int virt_stat (const char *path, struct stat *buf)
int virt_lstat (const char *path, struct stat *buf)
DIR * virt_opendir (const char *path)
int virt_closedir (DIR *dirp)
void virt_rewinddir (DIR *dirp)
static void avdirent_to_dirent (struct dirent *ent, struct avdirent *avent, avoff_t n)
struct dirent * virt_readdir (DIR *dirp)
int virt_truncate (const char *path, off_t length)
static int common_setattr (const char *path, struct avstat *buf, int attrmask, int flags)
int virt_utime (const char *path, struct utimbuf *buf)
int virt_chmod (const char *path, mode_t mode)
static int common_chown (const char *path, uid_t owner, gid_t grp, int flags)
int virt_chown (const char *path, uid_t owner, gid_t grp)
int virt_lchown (const char *path, uid_t owner, gid_t grp)
int virt_ftruncate (int fd, off_t length)
static int common_fsetattr (int fd, struct avstat *stbuf, int attrmask)
int virt_fchmod (int fd, mode_t mode)
int virt_fchown (int fd, uid_t owner, gid_t grp)
int virt_access (const char *path, int amode)
static int copy_readlink (char *buf, size_t bsiz, const char *avbuf)
int virt_readlink (const char *path, char *buf, size_t bsiz)
int virt_unlink (const char *path)
int virt_rmdir (const char *path)
int virt_mkdir (const char *path, mode_t mode)
int virt_mknod (const char *path, mode_t mode, dev_t dev)
int virt_symlink (const char *path, const char *newpath)
int virt_rename (const char *path, const char *newpath)
int virt_link (const char *path, const char *newpath)
int virt_remove (const char *path)
int virt_islocal (const char *path)

Class Documentation

struct AVDIR

Definition at line 204 of file virtual.c.

Class Members
char _trail
int fd

Define Documentation

#define AVFS_DIR_RECLEN   256 /* just an arbitary number */

Definition at line 271 of file virtual.c.

#define NAME_MAX   255

Definition at line 201 of file virtual.c.


Function Documentation

static void avdirent_to_dirent ( struct dirent *  ent,
struct avdirent avent,
avoff_t  n 
) [static]

Definition at line 273 of file virtual.c.

{
    ent->d_ino = avent->ino;
#ifdef HAVE_D_OFF
    ent->d_off = n * AVFS_DIR_RECLEN; 
#endif
    ent->d_reclen = AVFS_DIR_RECLEN;
#ifdef HAVE_D_TYPE
    ent->d_type = avent->type;
#endif
    strncpy(ent->d_name, avent->name, NAME_MAX);
    ent->d_name[NAME_MAX] = '\0';
}

Here is the caller graph for this function:

static void avstat_to_stat ( struct stat buf,
struct avstat avbuf 
) [static]

Definition at line 116 of file virtual.c.

{
    buf->st_dev     = avbuf->dev;
    buf->st_ino     = avbuf->ino;
    buf->st_mode    = avbuf->mode;
    buf->st_nlink   = avbuf->nlink;
    buf->st_uid     = avbuf->uid;
    buf->st_gid     = avbuf->gid;
    buf->st_rdev    = avbuf->rdev;
    buf->st_size    = avbuf->size;
    buf->st_blksize = avbuf->blksize;
    buf->st_blocks  = avbuf->blocks;
    buf->st_atime   = avbuf->atime.sec;
    buf->st_mtime   = avbuf->mtime.sec;
    buf->st_ctime   = avbuf->ctime.sec;
}

Here is the caller graph for this function:

static int common_chown ( const char *  path,
uid_t  owner,
gid_t  grp,
int  flags 
) [static]

Definition at line 387 of file virtual.c.

{
    struct avstat stbuf;
    int attrmask = 0;
    
    stbuf.uid = owner;
    stbuf.gid = grp;

    if(owner != (uid_t) -1)
       attrmask |= AVA_UID;
    if(grp != (gid_t) -1)
       attrmask |= AVA_GID;

    return common_setattr(path, &stbuf, attrmask, flags);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int common_fsetattr ( int  fd,
struct avstat stbuf,
int  attrmask 
) [static]

Definition at line 428 of file virtual.c.

{
    int res;
    int errno_save = errno;
    
    res = av_fd_setattr(fd, stbuf, attrmask);
    if(res < 0) {
        errno = -res;
        return -1;
    }

    errno = errno_save;
    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int common_setattr ( const char *  path,
struct avstat buf,
int  attrmask,
int  flags 
) [static]

Definition at line 337 of file virtual.c.

{
    int res;
    int errno_save = errno;
    vfile vf;

    res = open_path(&vf, path, AVO_NOPERM | flags, 0);
    if(res == 0) {
        res = av_file_setattr(&vf, buf, attrmask);
       av_file_close(&vf);
    }
    if(res < 0) {
        errno = -res;
        return -1;
    }

    errno = errno_save;
    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int common_stat ( const char *  path,
struct stat buf,
int  flags 
) [static]

Definition at line 166 of file virtual.c.

{
    int res;
    vfile vf;
    struct avstat avbuf;
    int errno_save = errno;

    res = open_path(&vf, path, AVO_NOPERM | flags, 0);
    if(res == 0) {
        res = av_file_getattr(&vf, &avbuf, AVA_ALL);
        av_file_close(&vf);
       if(res == 0)
           avstat_to_stat(buf, &avbuf);
    }
    if(res < 0) {
        errno = -res;
        return -1;
    }
    
    errno = errno_save;
    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int copy_readlink ( char *  buf,
size_t  bsiz,
const char *  avbuf 
) [static]

Definition at line 488 of file virtual.c.

{
    size_t nact;

    nact = strlen(avbuf);
    nact = AV_MIN(nact, bsiz);

    strncpy(buf, avbuf, nact);

    return (int) nact;
}

Here is the caller graph for this function:

static int oflags_to_avfs ( int  flags) [static]

Definition at line 20 of file virtual.c.

{
    int avflags;
  
    avflags = flags & O_ACCMODE;
    if(avflags == AVO_NOPERM)
       avflags = AVO_RDWR;

    if(flags & O_CREAT)    avflags |= AVO_CREAT;
    if(flags & O_EXCL)     avflags |= AVO_EXCL;
    if(flags & O_TRUNC)    avflags |= AVO_TRUNC;
    if(flags & O_APPEND)   avflags |= AVO_APPEND;
    if(flags & O_NONBLOCK) avflags |= AVO_NONBLOCK;
#ifdef O_SYNC
    if(flags & O_SYNC)     avflags |= AVO_SYNC;
#endif

    return avflags;
}

Here is the caller graph for this function:

static int open_path ( vfile *  vf,
const char *  path,
int  flags,
avmode_t  mode 
) [static]

Definition at line 150 of file virtual.c.

{
    int res;
    ventry *ve;

    res = av_get_ventry(path, !(flags & AVO_NOFOLLOW), &ve);
    if(res < 0)
        return res;

    res = av_file_open(vf, ve, flags, mode);
    av_free_ventry(ve);

    return res;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int virt_access ( const char *  path,
int  amode 
)

Definition at line 468 of file virtual.c.

{
    int res;
    ventry *ve;
    int errno_save = errno;

    res = av_get_ventry(path, 1, &ve);
    if(res == 0) {
       res = av_access(ve, amode);
       av_free_ventry(ve);
    }
    if(res < 0) {
        errno = -res;
        return -1;
    }

    errno = errno_save;
    return 0;
}

Here is the call graph for this function:

int virt_chmod ( const char *  path,
mode_t  mode 
)

Definition at line 378 of file virtual.c.

{
    struct avstat stbuf;

    stbuf.mode = mode & 07777;

    return common_setattr(path, &stbuf, AVA_MODE, 0);
}

Here is the call graph for this function:

Here is the caller graph for this function:

int virt_chown ( const char *  path,
uid_t  owner,
gid_t  grp 
)

Definition at line 403 of file virtual.c.

{
    return common_chown(path, owner, grp, 0);
}

Here is the call graph for this function:

int virt_close ( int  fd)

Definition at line 55 of file virtual.c.

{
    int res;
    int errno_save = errno;

    res = av_fd_close(fd);
    if(res < 0) {
        errno = -res;
        return -1;
    }
    
    errno = errno_save;
    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int virt_closedir ( DIR *  dirp)

Definition at line 229 of file virtual.c.

{
    int res;
    AVDIR *dp = (AVDIR *) dirp;
    int errno_save = errno;
    int fd;

    if(dp == NULL) {
       errno = EINVAL;
       return -1;
    }
    
    fd = dp->fd;
    av_free(dp);
    res = av_fd_close(fd);
    if(res < 0) {
        errno = -res;
        return -1;
    }

    errno = errno_save;
    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int virt_fchmod ( int  fd,
mode_t  mode 
)

Definition at line 443 of file virtual.c.

{
    struct avstat stbuf;
    
    stbuf.mode = mode & 07777;

    return common_fsetattr(fd, &stbuf, AVA_MODE);
}

Here is the call graph for this function:

int virt_fchown ( int  fd,
uid_t  owner,
gid_t  grp 
)

Definition at line 452 of file virtual.c.

{
    struct avstat stbuf;
    int attrmask = 0;
        
    stbuf.uid = owner;
    stbuf.gid = grp;

    if(owner != (uid_t) -1)
       attrmask |= AVA_UID;
    if(grp != (gid_t) -1)
       attrmask |= AVA_GID;

    return common_fsetattr(fd, &stbuf, attrmask);
}

Here is the call graph for this function:

int virt_fstat ( int  fd,
struct stat buf 
)

Definition at line 133 of file virtual.c.

{
    int res;
    struct avstat avbuf;
    int errno_save = errno;

    res = av_fd_getattr(fd, &avbuf, AVA_ALL);
    if(res < 0) {
        errno = -res;
        return -1;
    }
    avstat_to_stat(buf, &avbuf);

    errno = errno_save;
    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int virt_ftruncate ( int  fd,
off_t  length 
)

Definition at line 413 of file virtual.c.

{
    int res;
    int errno_save = errno;
    
    res = av_fd_truncate(fd, length);
    if(res < 0) {
        errno = -res;
        return -1;
    }

    errno = errno_save;
    return 0;
}

Here is the call graph for this function:

int virt_islocal ( const char *  path)

Definition at line 693 of file virtual.c.

{
    int res;
    ventry *ve;
    int errno_save = errno;
    int erg = 0;

    res = av_get_ventry(path, 0, &ve);
    if(res == 0) {
        if(ve->mnt->base == NULL)
            erg = 1;
        else
            erg = 0;
        av_free_ventry(ve);
    }
    if(res < 0) {
        errno = -res;
        return -1;
    }

    errno = errno_save;
    return erg;
}

Here is the call graph for this function:

int virt_lchown ( const char *  path,
uid_t  owner,
gid_t  grp 
)

Definition at line 408 of file virtual.c.

{
    return common_chown(path, owner, grp, AVO_NOFOLLOW);
}

Here is the call graph for this function:

Here is the caller graph for this function:

int virt_link ( const char *  path,
const char *  newpath 
)

Definition at line 650 of file virtual.c.

{
    int res;
    ventry *ve;
    ventry *newve;
    int errno_save = errno;

    res = av_get_ventry(path, 0, &ve);
    if(res == 0) {
       res = av_get_ventry(newpath, 0, &newve);
       if(res == 0) {
            res = av_link(ve, newve);
           av_free_ventry(newve);
       }
       av_free_ventry(ve);
    }
    if(res < 0) {
        errno = -res;
        return -1;
    }
    
    errno = errno_save;
    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

off_t virt_lseek ( int  fd,
off_t  offset,
int  whence 
)

Definition at line 101 of file virtual.c.

{
    off_t res;
    int errno_save = errno;

    res = av_fd_lseek(fd, offset, whence);
    if(res < 0) {
        errno = -res;
        return -1;
    }

    errno = errno_save;
    return res;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int virt_lstat ( const char *  path,
struct stat buf 
)

Definition at line 194 of file virtual.c.

{
    return common_stat(path, buf, AVO_NOFOLLOW); 
}

Here is the call graph for this function:

int virt_mkdir ( const char *  path,
mode_t  mode 
)

Definition at line 565 of file virtual.c.

{
    int res;
    ventry *ve;
    int errno_save = errno;

    res = av_get_ventry(path, 0, &ve);
    if(res == 0) {
        res = av_mkdir(ve, mode);
        av_free_ventry(ve);
    }
    if(res < 0) {
        errno = -res;
        return -1;
    }
    
    errno = errno_save;
    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int virt_mknod ( const char *  path,
mode_t  mode,
dev_t  dev 
)

Definition at line 585 of file virtual.c.

{
    int res;
    ventry *ve;
    int errno_save = errno;

    res = av_get_ventry(path, 0, &ve);
    if(res == 0) {
        res = av_mknod(ve, mode, dev);
       av_free_ventry(ve);
    }
    if(res < 0) {
        errno = -res;
        return -1;
    }
    
    errno = errno_save;
    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int virt_open ( const char *  path,
int  flags,
mode_t  mode 
)

Definition at line 40 of file virtual.c.

{
    int res;
    int errno_save = errno;

    res = av_fd_open(path, oflags_to_avfs(flags), mode & 07777);
    if(res < 0) {
        errno = -res;
        return -1;
    }

    errno = errno_save;
    return res;
}

Here is the call graph for this function:

DIR* virt_opendir ( const char *  path)

Definition at line 210 of file virtual.c.

{
    AVDIR *dp;
    int res;
    int errno_save = errno;

    res = av_fd_open(path, AVO_DIRECTORY, 0);
    if(res < 0) {
        errno = -res;
        return NULL;
    }

    AV_NEW(dp);
    dp->fd = res;

    errno = errno_save;
    return (DIR *) dp;
}

Here is the call graph for this function:

Here is the caller graph for this function:

ssize_t virt_read ( int  fd,
void *  buf,
size_t  nbyte 
)

Definition at line 86 of file virtual.c.

{
    ssize_t res;
    int errno_save = errno;

    res = av_fd_read(fd, buf, nbyte);
    if(res < 0) {
        errno = -res;
        return -1;
    }

    errno = errno_save;
    return res;
}

Here is the call graph for this function:

Here is the caller graph for this function:

struct dirent* virt_readdir ( DIR *  dirp) [read]

Definition at line 288 of file virtual.c.

{
    int res;
    struct avdirent buf;
    avoff_t n;
    AVDIR *dp = (AVDIR *) dirp;
    int errno_save = errno;

    if(dp == NULL) {
       errno = EINVAL;
       return NULL;
    }
    res = av_fd_readdir(dp->fd, &buf, &n);
    if(res <= 0) {
        if(res < 0)
            errno = -res;
        else
            errno = errno_save;
        return NULL;
    }

    avdirent_to_dirent(&dp->entry, &buf, n);
    av_free(buf.name);

    errno = errno_save;
    return &dp->entry;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int virt_readlink ( const char *  path,
char *  buf,
size_t  bsiz 
)

Definition at line 500 of file virtual.c.

{
    int res;
    ventry *ve;
    char *avbuf;
    int errno_save = errno;
   
    res = av_get_ventry(path, 0, &ve);
    if(res == 0) {
        res = av_readlink(ve, &avbuf);
       if(res == 0) {
           res = copy_readlink(buf, bsiz, avbuf);
           av_free(avbuf);
       }
       av_free_ventry(ve);
    }
    if(res < 0) {
        errno = -res;
        return -1;
    }

    errno = errno_save;
    return res;
}

Here is the call graph for this function:

int virt_remove ( const char *  path)

Definition at line 675 of file virtual.c.

{
    struct stat stbuf;

    if(path != NULL) {
        if(virt_lstat(path, &stbuf) == 0) {
            if(S_ISDIR(stbuf.st_mode)) {
                return virt_rmdir(path);
            } else {
                return virt_unlink(path);
            }
        }
    }

    errno = EFAULT;
    return -1;
}

Here is the call graph for this function:

int virt_rename ( const char *  path,
const char *  newpath 
)

Definition at line 625 of file virtual.c.

{
    int res;
    ventry *ve;
    ventry *newve;
    int errno_save = errno;

    res = av_get_ventry(path, 0, &ve);
    if(res == 0) {
       res = av_get_ventry(newpath, 0, &newve);
       if(res == 0) {
            res = av_rename(ve, newve);
           av_free_ventry(newve);
       }
       av_free_ventry(ve);
    }
    if(res < 0) {
        errno = -res;
        return -1;
    }
    
    errno = errno_save;
    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void virt_rewinddir ( DIR *  dirp)

Definition at line 253 of file virtual.c.

{
    int res;
    AVDIR *dp = (AVDIR *) dirp;
    int errno_save = errno;

    if(dp == NULL) {
       errno = EINVAL;
       return;
    }

    res = av_fd_lseek(dp->fd, 0, AVSEEK_SET);
    if(res < 0)
        errno = -res;

    errno = errno_save;
}

Here is the call graph for this function:

int virt_rmdir ( const char *  path)

Definition at line 545 of file virtual.c.

{
    int res;
    ventry *ve;
    int errno_save = errno;

    res = av_get_ventry(path, 0, &ve);
    if(res == 0) {
        res = av_rmdir(ve);
       av_free_ventry(ve);
    }
    if(res < 0) {
        errno = -res;
        return -1;
    }
    
    errno = errno_save;
    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int virt_stat ( const char *  path,
struct stat buf 
)

Definition at line 189 of file virtual.c.

{
    return common_stat(path, buf, 0); 
}

Here is the call graph for this function:

int virt_symlink ( const char *  path,
const char *  newpath 
)

Definition at line 605 of file virtual.c.

{
    int res;
    ventry *newve;
    int errno_save = errno;

    res = av_get_ventry(newpath, 0, &newve);
    if(res == 0) {
        res = av_symlink(path, newve);
       av_free_ventry(newve);
    }
    if(res < 0) {
        errno = -res;
        return -1;
    }
    
    errno = errno_save;
    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int virt_truncate ( const char *  path,
off_t  length 
)

Definition at line 317 of file virtual.c.

{
    int res;
    vfile vf;
    int errno_save = errno;

    res = open_path(&vf, path, AVO_WRONLY, 0);
    if(res == 0) {
        av_file_truncate(&vf, length);
        av_file_close(&vf);
    }
    if(res < 0) {
        errno = -res;
        return -1;
    }

    errno = errno_save;
    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int virt_unlink ( const char *  path)

Definition at line 525 of file virtual.c.

{
    int res;
    ventry *ve;
    int errno_save = errno;

    res = av_get_ventry(path, 0, &ve);
    if(res == 0) {
        res = av_unlink(ve);
       av_free_ventry(ve);
    }
    if(res < 0) {
        errno = -res;
        return -1;
    }
    
    errno = errno_save;
    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int virt_utime ( const char *  path,
struct utimbuf *  buf 
)

Definition at line 358 of file virtual.c.

{
    struct avstat stbuf;

    if(buf == NULL) {
        int errno_save = errno;
       av_curr_time(&stbuf.mtime);
        errno = errno_save;
       stbuf.atime = stbuf.mtime;
    }
    else {
       stbuf.mtime.sec = buf->modtime;
       stbuf.mtime.nsec = 0;
       stbuf.atime.sec = buf->actime;
       stbuf.atime.nsec = 0;
    }
    
    return common_setattr(path, &stbuf, AVA_MTIME | AVA_ATIME, 0);
}

Here is the call graph for this function:

Here is the caller graph for this function:

ssize_t virt_write ( int  fd,
const void *  buf,
size_t  nbyte 
)

Definition at line 71 of file virtual.c.

{
    ssize_t res;
    int errno_save = errno;

    res = av_fd_write(fd, buf, nbyte);
    if(res < 0) {
        errno = -res;
        return -1;
    }

    errno = errno_save;
    return res;
}

Here is the call graph for this function:

Here is the caller graph for this function: