Back to index

avfs  1.0.1
Classes | Functions
local.c File Reference
#include "local.h"
#include "version.h"
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <utime.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>

Go to the source code of this file.

Classes

struct  localfile

Functions

static struct localfilelocal_vfile_file (vfile *vf)
static int avoflags_to_oflags (int avflags)
static int local_open (ventry *ve, int flags, avmode_t mode, void **resp)
static int local_close (vfile *vf)
static avssize_t local_read (vfile *vf, char *buf, avsize_t nbyte)
static avssize_t local_write (vfile *vf, const char *buf, avsize_t nbyte)
static avoff_t local_lseek (vfile *vf, avoff_t offset, int whence)
static int local_readdir (vfile *vf, struct avdirent *buf)
static int local_truncate (vfile *vf, avoff_t length)
static void stat_to_avstat (struct avstat *vbuf, struct stat *lbuf)
static int local_getattr (vfile *vf, struct avstat *buf, int attrmask)
static int local_set_time (struct localfile *fi, const struct avstat *buf, int attrmask)
static int local_set_mode (struct localfile *fi, avmode_t mode)
static int local_set_ugid (struct localfile *fi, const struct avstat *buf, int attrmask, int flags)
static int local_setattr (vfile *vf, struct avstat *buf, int attrmask)
static int local_access (ventry *ve, int amode)
static int local_readlink (ventry *ve, char **bufp)
static int local_unlink (ventry *ve)
static int local_rmdir (ventry *ve)
static int local_mkdir (ventry *ve, avmode_t mode)
static int local_mknod (ventry *ve, avmode_t mode, avdev_t dev)
static int local_rename (ventry *ve, ventry *newve)
static int local_link (ventry *ve, ventry *newve)
static int local_symlink (const char *path, ventry *newve)
static int local_lookup (ventry *ve, const char *name, void **newp)
int av_init_module_local ()

Class Documentation

struct localfile

Definition at line 20 of file local.c.

Class Members
DIR * dirp
avoff_t entctr
int fd
char * path

Function Documentation

Definition at line 450 of file local.c.

{
    int res;
    int flags = AVF_NEEDSLASH | AVF_NOLOCK;
    struct avfs *avfs;

    res = av_new_avfs("local", NULL, AV_VER, flags, NULL, &avfs);
    if(res < 0)
        return res;

    avfs->lookup     = local_lookup;

    avfs->open       = local_open;
    avfs->close      = local_close;
    avfs->read       = local_read;
    avfs->write      = local_write;
    avfs->lseek      = local_lseek;
    avfs->readdir    = local_readdir;
    avfs->access     = local_access;
    avfs->getattr    = local_getattr;
    avfs->setattr    = local_setattr;
    avfs->readlink   = local_readlink;
    avfs->unlink     = local_unlink;
    avfs->rmdir      = local_rmdir;
    avfs->mkdir      = local_mkdir;
    avfs->mknod      = local_mknod;
    avfs->rename     = local_rename;
    avfs->link       = local_link;
    avfs->symlink    = local_symlink;
    avfs->truncate   = local_truncate;

    av_add_avfs(avfs);
    
    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int avoflags_to_oflags ( int  avflags) [static]

Definition at line 32 of file local.c.

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

    return flags;
}

Here is the caller graph for this function:

static int local_access ( ventry *  ve,
int  amode 
) [static]

Definition at line 295 of file local.c.

{
    int res;
    const char *path = (char *) ve->data;
    
    res = access(path, amode);
    if(res == -1)
        return -errno;

    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int local_close ( vfile *  vf) [static]

Definition at line 83 of file local.c.

{
    int res = 0;
    struct localfile *fi = local_vfile_file(vf);

    if(fi->fd != -1)
        res = close(fi->fd);
    if(fi->dirp != NULL)
        res = closedir(fi->dirp);
    
    if(res == -1)
        res = -errno;

    av_free(fi->path);
    av_free(fi);

    return res;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int local_getattr ( vfile *  vf,
struct avstat buf,
int  attrmask 
) [static]

Definition at line 214 of file local.c.

{
    int res;
    struct stat stbuf;
    struct localfile *fi = local_vfile_file(vf);

    if(fi->fd != -1)
        res = fstat(fi->fd, &stbuf);
    else if((vf->flags & AVO_NOFOLLOW) != 0)
        res = lstat(fi->path, &stbuf);
    else
        res = stat(fi->path, &stbuf);

    if(res == -1)
        return -errno;
    
    stat_to_avstat(buf, &stbuf);
    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int local_link ( ventry *  ve,
ventry *  newve 
) [static]

Definition at line 393 of file local.c.

{
    int res;
    char *path = (char *) ve->data;
    char *newpath = (char *) newve->data;

    res = link(path, newpath);
    if(res == -1)
        return -errno;

    return 0;
}

Here is the caller graph for this function:

static int local_lookup ( ventry *  ve,
const char *  name,
void **  newp 
) [static]

Definition at line 418 of file local.c.

{
    char *path = (char *) ve->data;

    
    if(path == NULL) {
        /* You can't access the local handler directly*/
        return -ENOENT;
    }
    else if(name != NULL) {
        if(path[0] == '/' && path[1] == '\0' && name[0] == '/')
            path[0] = '\0';
        path = av_stradd(path, name, NULL);
    }
    else {
        char *s;
        s = strrchr(path, AV_DIR_SEP_CHAR);
        if(s == NULL) {
            path[0] = '\0';
            path = av_stradd(path, ".", NULL);
        }
        else if(s != path)
            s[0] = '\0';
        else
            s[1] = '\0';
    }

    *newp = path;
    
    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static avoff_t local_lseek ( vfile *  vf,
avoff_t  offset,
int  whence 
) [static]

Definition at line 132 of file local.c.

{
    avoff_t res;
    struct localfile *fi = local_vfile_file(vf);

    res = lseek(fi->fd, offset, whence);
    if(res == -1)
        return -errno;
    
    vf->ptr = res;

    return res;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int local_mkdir ( ventry *  ve,
avmode_t  mode 
) [static]

Definition at line 356 of file local.c.

{
    int res;
    char *path = (char *) ve->data;

    res = mkdir(path, mode);
    if(res == -1)
        return -errno;

    return 0;
}

Here is the caller graph for this function:

static int local_mknod ( ventry *  ve,
avmode_t  mode,
avdev_t  dev 
) [static]

Definition at line 368 of file local.c.

{
    int res;
    char *path = (char *) ve->data;

    res = mknod(path, mode, dev);
    if(res == -1)
        return -errno;

    return 0;
}

Here is the caller graph for this function:

static int local_open ( ventry *  ve,
int  flags,
avmode_t  mode,
void **  resp 
) [static]

Definition at line 49 of file local.c.

{
    struct localfile *fi;
    const char *path = (char *) ve->data;
    int fd;
    DIR *dirp;
    
    fd = -1;
    dirp = NULL;
    if((flags & AVO_ACCMODE) != AVO_NOPERM) {
        if(!(flags & AVO_DIRECTORY))
            fd = open(path, avoflags_to_oflags(flags), mode);
        else
            dirp = opendir(path);

        if(fd == -1 && dirp == NULL)
            return -errno;
        
        if(fd != -1)
            av_registerfd(fd);
    }
    
    AV_NEW(fi);

    fi->fd = fd;
    fi->dirp = dirp;
    fi->entctr = 0;
    fi->path = av_strdup(path);

    *resp = fi;

    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static avssize_t local_read ( vfile *  vf,
char *  buf,
avsize_t  nbyte 
) [static]

Definition at line 103 of file local.c.

{
    avssize_t res;
    struct localfile *fi = local_vfile_file(vf);

    res = read(fi->fd, buf, nbyte);
    if(res == -1)
        return -errno;

    vf->ptr += res;
    
    return res;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int local_readdir ( vfile *  vf,
struct avdirent buf 
) [static]

Definition at line 146 of file local.c.

{
    struct dirent *de;
    struct localfile *fi = local_vfile_file(vf);

    if(vf->ptr < fi->entctr) {
       rewinddir(fi->dirp);
       fi->entctr = 0;
    }

    do {
       errno = 0;
       de = readdir(fi->dirp);
       if(de == NULL)
            return -errno;

       fi->entctr ++;
    } while(vf->ptr >= fi->entctr);

    buf->name = av_strdup(de->d_name);
    buf->ino = de->d_ino;
#ifdef HAVE_D_TYPE
    buf->type = de->d_type;
#else
    buf->type = 0;
#endif
    vf->ptr ++;

    return 1;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int local_readlink ( ventry *  ve,
char **  bufp 
) [static]

Definition at line 307 of file local.c.

{
    int res;
    unsigned int bufsize;
    char *buf;
    const char *path = (char *) ve->data;
    
    bufsize = 0;
    buf = NULL;
    do {
        bufsize += 1024;
        buf = av_realloc(buf, bufsize + 1);
        res = readlink(path, buf, bufsize);
        if(res == -1) {
            av_free(buf);
            return -errno;
        }
    } while(res >= bufsize);
    
    buf[res] = '\0';
    *bufp = buf;

    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int local_rename ( ventry *  ve,
ventry *  newve 
) [static]

Definition at line 380 of file local.c.

{
    int res;
    char *path = (char *) ve->data;
    char *newpath = (char *) newve->data;

    res = rename(path, newpath);
    if(res == -1)
        return -errno;

    return 0;
}

Here is the caller graph for this function:

static int local_rmdir ( ventry *  ve) [static]

Definition at line 344 of file local.c.

{
    int res;
    char *path = (char *) ve->data;

    res = rmdir(path);
    if(res == -1)
        return -errno;

    return 0;
}

Here is the caller graph for this function:

static int local_set_mode ( struct localfile fi,
avmode_t  mode 
) [static]

Definition at line 250 of file local.c.

{
    if(fi->fd != -1)
        return  fchmod(fi->fd, mode);
    else
        return chmod(fi->path, mode);
}

Here is the caller graph for this function:

static int local_set_time ( struct localfile fi,
const struct avstat buf,
int  attrmask 
) [static]

Definition at line 234 of file local.c.

{
    struct utimbuf utbuf;
    
    utbuf.actime = buf->atime.sec;
    utbuf.modtime  = buf->mtime.sec;
    
    if(!(attrmask & AVA_ATIME))
        utbuf.actime = utbuf.modtime;
    if(!(attrmask & AVA_MTIME))
        utbuf.modtime = utbuf.actime;
    
    return utime(fi->path, &utbuf);
}

Here is the caller graph for this function:

static int local_set_ugid ( struct localfile fi,
const struct avstat buf,
int  attrmask,
int  flags 
) [static]

Definition at line 258 of file local.c.

{
    uid_t uid = (uid_t) -1;
    gid_t gid = (gid_t) -1;
    
    if((attrmask & AVA_UID) != 0)
        uid = buf->uid;
    if((attrmask & AVA_GID) != 0)
        gid = buf->gid;
    
    if(fi->fd != -1)
        return fchown(fi->fd, uid, gid);
    else if((flags & AVO_NOFOLLOW) != 0)
        return lchown(fi->path, uid, gid);
    else
        return chown(fi->path, uid, gid);
}

Here is the caller graph for this function:

static int local_setattr ( vfile *  vf,
struct avstat buf,
int  attrmask 
) [static]

Definition at line 277 of file local.c.

{
    int res = 0;
    struct localfile *fi = local_vfile_file(vf);

    if((attrmask & (AVA_ATIME | AVA_MTIME)) != 0) 
        res = local_set_time(fi, buf, attrmask);
    if((attrmask & AVA_MODE) != 0)
        res = local_set_mode(fi, buf->mode);
    if((attrmask & (AVA_UID | AVA_GID)) != 0)
        res = local_set_ugid(fi, buf, attrmask, vf->flags);

    if(res == -1)
        return -errno;

    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int local_symlink ( const char *  path,
ventry *  newve 
) [static]

Definition at line 406 of file local.c.

{
    int res;
    char *newpath = (char *) newve->data;

    res = symlink(path, newpath);
    if(res == -1)
        return -errno;

    return 0;
}

Here is the caller graph for this function:

static int local_truncate ( vfile *  vf,
avoff_t  length 
) [static]

Definition at line 177 of file local.c.

{
    int res;
    struct localfile *fi = local_vfile_file(vf);
    
    if(fi->fd != -1)
        res = ftruncate(fi->fd, length);
    else
        res = truncate(fi->path, length);

    if(res == -1)
        return -errno;

    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int local_unlink ( ventry *  ve) [static]

Definition at line 332 of file local.c.

{
    int res;
    char *path = (char *) ve->data;

    res = unlink(path);
    if(res == -1)
        return -errno;

    return 0;
}

Here is the caller graph for this function:

static struct localfile* local_vfile_file ( vfile *  vf) [static, read]

Definition at line 27 of file local.c.

{
    return (struct localfile *) vf->data;
}

Here is the caller graph for this function:

static avssize_t local_write ( vfile *  vf,
const char *  buf,
avsize_t  nbyte 
) [static]

Definition at line 117 of file local.c.

{
    avssize_t res;
    struct localfile *fi = local_vfile_file(vf);

    res = write(fi->fd, buf, nbyte);
    if(res == -1)
        return -errno;

    /* NOTE: ptr will go astray if file is opened with O_APPEND */
    vf->ptr += res;

    return res;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void stat_to_avstat ( struct avstat vbuf,
struct stat lbuf 
) [static]

Definition at line 193 of file local.c.

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

Here is the caller graph for this function: