Back to index

avfs  1.0.1
Classes | Functions
state.c File Reference
#include "state.h"
#include "version.h"

Go to the source code of this file.

Classes

struct  stentry
struct  stfile

Functions

static struct stentrystate_ventry_stentry (ventry *ve)
static struct namespacestate_ventry_namespace (ventry *ve)
static struct stfilestate_vfile_stfile (vfile *vf)
static struct namespacestate_vfile_namespace (vfile *vf)
static void state_free_stentry (struct stentry *stent)
static int state_lookup (ventry *ve, const char *name, void **newp)
static int state_getpath (ventry *ve, char **resp)
static void state_putent (ventry *ve)
static int state_copyent (ventry *ve, void **resp)
static int state_open (ventry *ve, int flags, avmode_t mode, void **resp)
static int state_close (vfile *vf)
static avssize_t state_read (vfile *vf, char *buf, avsize_t nbyte)
static avssize_t state_write (vfile *vf, const char *buf, avsize_t nbyte)
static int state_truncate (vfile *vf, avoff_t length)
static unsigned int state_paramhash (const char *param)
static int state_readdir (vfile *vf, struct avdirent *buf)
static int state_getattr (vfile *vf, struct avstat *buf, int attrmask)
static int state_access (ventry *ve, int amode)
static void state_free_tree (struct namespace *ns, struct entry *ent)
static void state_destroy (struct avfs *avfs)
int av_state_new (struct vmodule *module, const char *name, struct namespace **resp, struct avfs **avfsp)

Class Documentation

struct stentry

Definition at line 12 of file state.c.

Collaboration diagram for stentry:
Class Members
struct entry * ent
char * param
struct stfile

Definition at line 17 of file state.c.

Collaboration diagram for stfile:
Class Members
char * contents
int modif
struct stentry * stent

Function Documentation

int av_state_new ( struct vmodule module,
const char *  name,
struct namespace **  resp,
struct avfs **  avfsp 
)

Definition at line 385 of file state.c.

{
    int res;
    struct avfs *avfs;
    struct namespace *ns;

    res = av_new_avfs(name, NULL, AV_VER, AVF_ONLYROOT, module, &avfs);
    if(res < 0)
        return res;

    ns = av_namespace_new();

    av_ref_obj(ns);
    avfs->data = ns;
    avfs->destroy = state_destroy;

    avfs->lookup    = state_lookup;
    avfs->putent    = state_putent;
    avfs->copyent   = state_copyent;
    avfs->getpath   = state_getpath;

    avfs->open      = state_open;
    avfs->close     = state_close;
    avfs->read      = state_read;
    avfs->write     = state_write;
    avfs->truncate  = state_truncate;
    avfs->readdir   = state_readdir;
    avfs->getattr   = state_getattr;
    avfs->access    = state_access;

    av_add_avfs(avfs);

    *resp = ns;
    *avfsp = avfs;
    
    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

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

Definition at line 332 of file state.c.

{
    struct stentry *stent = state_ventry_stentry(ve);
    struct namespace *ns = state_ventry_namespace(ve);
    struct entry *subdir;
    struct statefile *stf;

    subdir = av_namespace_subdir(ns, stent->ent);
    if(stent->ent != NULL)
        stf = (struct statefile *) av_namespace_get(stent->ent);
    else
        stf = NULL;
    
    if(subdir == NULL && stf == NULL)
        return -ENOENT;

    if((amode & AVW_OK) != 0 && stf != NULL && stf->set == NULL)
        return -EACCES;

    av_unref_obj(subdir);

    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int state_close ( vfile *  vf) [static]

Definition at line 161 of file state.c.

{
    struct stfile *sf = state_vfile_stfile(vf);
    int res = 0;

    if(sf->modif && sf->stent->ent != NULL) {
        struct statefile *stf;

        stf = (struct statefile *) av_namespace_get(sf->stent->ent);

        res = stf->set(sf->stent->ent, sf->stent->param, sf->contents);
    }

    av_unref_obj(sf->stent);
    av_free(sf->contents);
    av_free(sf);

    return res;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int state_copyent ( ventry *  ve,
void **  resp 
) [static]

Definition at line 101 of file state.c.

{
    struct stentry *stent = state_ventry_stentry(ve);

    av_ref_obj(stent);
    *resp = stent;

    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void state_destroy ( struct avfs avfs) [static]

Definition at line 376 of file state.c.

{
    struct namespace *ns = (struct namespace *) avfs->data;

    state_free_tree(ns, NULL);

    av_unref_obj(ns);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void state_free_stentry ( struct stentry stent) [static]

Definition at line 43 of file state.c.

{
    av_free(stent->param);
    av_unref_obj(stent->ent);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void state_free_tree ( struct namespace ns,
struct entry ent 
) [static]

Definition at line 356 of file state.c.

{
    struct entry *next;
    void *data;

    ent = av_namespace_subdir(ns, ent);
    while(ent != NULL) {
        state_free_tree(ns, ent);
        data = av_namespace_get(ent);
        if(data != NULL) {
            av_free(data);
            av_unref_obj(ent);
        }
        next = av_namespace_next(ent);
        av_unref_obj(ent);
        ent = next;
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

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

Definition at line 289 of file state.c.

{
    struct stfile *sf = state_vfile_stfile(vf);
    struct statefile *stf;
    char *ent_name;

    if(sf->stent->ent != NULL)
        stf = (struct statefile *) av_namespace_get(sf->stent->ent);
    else
        stf = NULL;

    av_default_stat(buf);
    /* This isn't perfect, but... */
    buf->ino = (long) stf + state_paramhash(sf->stent->param);

    /* add hash of entry name to hash */
    if( sf->stent->ent != NULL ) {
      ent_name = av_namespace_name( sf->stent->ent );
      buf->ino += state_paramhash( ent_name );
      av_free( ent_name );
    }
    /* make sure ino is not 0 or 1 */
    buf->ino = (avino_t)((((unsigned int)buf->ino) % (~0U - 1)) + 2);
    
    buf->dev = vf->mnt->avfs->dev;
    if(stf != NULL) {
        if(stf->set != NULL)
            buf->mode = AV_IFREG | 0644;
        else
            buf->mode = AV_IFREG | 0444;
    }
    else
        buf->mode = AV_IFDIR | 0755;

    if(sf->contents != NULL) {
        buf->size = strlen(sf->contents);
        buf->blocks = AV_DIV(buf->size, 512);
    }
    buf->nlink = 1;

    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int state_getpath ( ventry *  ve,
char **  resp 
) [static]

Definition at line 76 of file state.c.

{
    char *path;
    char *nspath;
    struct stentry *stent = state_ventry_stentry(ve);

    path = av_strdup(stent->param);
    if(stent->ent != NULL) {
        nspath = av_namespace_getpath(stent->ent);
        path = av_stradd(path, "/", nspath, NULL);
        av_free(nspath);
    }

    *resp = path;

    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

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

Definition at line 49 of file state.c.

{
    struct stentry *stent = state_ventry_stentry(ve);
    struct namespace *ns = state_ventry_namespace(ve);
    struct stentry *newent;
 
    if(stent != NULL) {
        if(stent->ent == NULL && (name == NULL || strcmp(name, "..") == 0))
            newent = NULL;
        else {
            AV_NEW_OBJ(newent, state_free_stentry);
            newent->ent = av_namespace_lookup_all(ns, stent->ent, name);
            newent->param = av_strdup(stent->param);
        }
    }
    else {
        AV_NEW_OBJ(newent, state_free_stentry);
        newent->ent = NULL;
        newent->param = av_strdup(name);
    }
    av_unref_obj(stent);
    
    *newp = newent;

    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

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

Definition at line 111 of file state.c.

{
    int res;
    struct stentry *stent = state_ventry_stentry(ve);
    struct namespace *ns = state_ventry_namespace(ve);
    struct stfile *sf;
    struct entry *subdir;
    struct statefile *stf;
    char *contents;

    subdir = av_namespace_subdir(ns, stent->ent);
    if(stent->ent != NULL)
        stf = (struct statefile *) av_namespace_get(stent->ent);
    else
        stf = NULL;
    
    if(subdir == NULL && (stf == NULL || (flags & AVO_DIRECTORY) != 0))
        return -ENOENT;

    av_unref_obj(subdir);

    contents = NULL;
    if(!(flags & AVO_DIRECTORY) && stf != NULL) {
        if(AV_ISWRITE(flags) && stf->set == NULL)
            return -EACCES;
            
        if((flags & AVO_TRUNC) != 0 || stf->get == NULL)
            contents = av_strdup("");
        else {
            res = stf->get(stent->ent, stent->param, &contents);
            if(res < 0)
                return res;
        }
    }

    AV_NEW(sf);
    sf->stent = stent;
    sf->contents = contents;
    sf->modif = 0;
    av_ref_obj(stent);

    if((flags & AVO_TRUNC) != 0)
        sf->modif = 1;

    *resp = sf;
    
    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static unsigned int state_paramhash ( const char *  param) [static]

Definition at line 242 of file state.c.

{
    unsigned long hash = 0;

    for(; *param; param++) {
        unsigned long c = *(const unsigned char *) param;
        hash = (hash + (c << 4) + (c >> 4)) * 11;
    }
    return hash;
}

Here is the caller graph for this function:

static void state_putent ( ventry *  ve) [static]

Definition at line 94 of file state.c.

{
    struct stentry *stent = state_ventry_stentry(ve);

    av_unref_obj(stent);
}

Here is the call graph for this function:

Here is the caller graph for this function:

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

Definition at line 181 of file state.c.

{
    avoff_t nact;
    avoff_t size;
    struct stfile *sf = state_vfile_stfile(vf);

    if(sf->contents == NULL)
        return -EISDIR;

    size = strlen(sf->contents);
    if(vf->ptr >= size)
       return 0;
    
    nact = AV_MIN(nbyte, (avsize_t) (size - vf->ptr));
    
    memcpy(buf, sf->contents + vf->ptr, nact);
    
    vf->ptr += nact;
    
    return nact;
}

Here is the call graph for this function:

Here is the caller graph for this function:

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

Definition at line 253 of file state.c.

{
    struct stfile *sf = state_vfile_stfile(vf);
    struct namespace *ns = state_vfile_namespace(vf);
    struct statefile *stf;
    struct entry *ent;
    int n;

    ent = av_namespace_subdir(ns, sf->stent->ent);
    for(n = vf->ptr; n > 0 && ent != NULL; n--) {
        struct entry *next;
        next = av_namespace_next(ent);
        av_unref_obj(ent);
        ent = next;
    }
    if(ent == NULL)
        return 0;
    
    buf->name = av_namespace_name(ent);
    stf = av_namespace_get(ent);

    /* FIXME: Make ino be some hash function of param and entry */
    buf->ino = (long) stf + state_paramhash(sf->stent->param);
    /* add hash of entry name to hash */
    buf->ino += state_paramhash( buf->name );
    /* make sure ino is not 0 or 1 */
    buf->ino = (avino_t)((((unsigned int)buf->ino) % (~0U - 1)) + 2);
    
    buf->type = 0;
    av_unref_obj(ent);
    
    vf->ptr ++;
    
    return 1;
}

Here is the call graph for this function:

Here is the caller graph for this function:

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

Definition at line 227 of file state.c.

{
    struct stfile *sf = state_vfile_stfile(vf);
    avoff_t size;

    size = strlen(sf->contents);

    if(length < size)
        sf->contents[length] = '\0';

    sf->modif = 1;

    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static struct namespace* state_ventry_namespace ( ventry *  ve) [static, read]

Definition at line 28 of file state.c.

{
    return (struct namespace *) ve->mnt->avfs->data;
}

Here is the caller graph for this function:

static struct stentry* state_ventry_stentry ( ventry *  ve) [static, read]

Definition at line 23 of file state.c.

{
    return (struct stentry *) ve->data;
}

Here is the caller graph for this function:

static struct namespace* state_vfile_namespace ( vfile *  vf) [static, read]

Definition at line 38 of file state.c.

{
    return (struct namespace *) vf->mnt->avfs->data;
}

Here is the caller graph for this function:

static struct stfile* state_vfile_stfile ( vfile *  vf) [static, read]

Definition at line 33 of file state.c.

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

Here is the caller graph for this function:

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

Definition at line 203 of file state.c.

{
    avoff_t end;
    struct stfile *sf = state_vfile_stfile(vf);
    avoff_t size;

    size = strlen(sf->contents);
    if((vf->flags & AVO_APPEND) != 0)
        vf->ptr = size;

    end = vf->ptr + nbyte;
    if(end > size) {
        sf->contents = av_realloc(sf->contents, end + 1);
        sf->contents[end] = '\0';
    }

    memcpy(sf->contents + vf->ptr, buf, nbyte);

    vf->ptr = end;
    sf->modif = 1;

    return nbyte;
}

Here is the call graph for this function:

Here is the caller graph for this function: