Back to index

avfs  1.0.1
Classes | Defines | Functions
ugid.c File Reference
#include "avfs.h"
#include "config.h"
#include <string.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>

Go to the source code of this file.

Classes

struct  ugidcache

Defines

#define NAMLEN   256

Functions

static void free_ugidcache (struct ugidcache *cache)
struct ugidcacheav_new_ugidcache ()
char * av_finduname (struct ugidcache *cache, int uid, const char *deflt)
int av_finduid (struct ugidcache *cache, const char *uname, int deflt)
char * av_findgname (struct ugidcache *cache, int gid, const char *deflt)
int av_findgid (struct ugidcache *cache, const char *gname, int deflt)

Class Documentation

struct ugidcache

Definition at line 19 of file ugid.c.

Class Members
int gid
char * gname
int mygid
int myuid
int uid
char * uname

Define Documentation

#define NAMLEN   256

Definition at line 17 of file ugid.c.


Function Documentation

int av_findgid ( struct ugidcache cache,
const char *  gname,
int  deflt 
)

Definition at line 170 of file ugid.c.

{
    if(gname == NULL || !gname[0])
        return deflt == -1 ? cache->mygid : deflt;

    if(cache->gname == NULL || strcmp(gname, cache->gname) != 0) {
        int res;
        struct group gr;
        struct group *grres;
        char *buf = NULL;
        size_t bufsize = 0;

#ifdef HAVE_GETGRNAM_R
        do {
            bufsize += 256;
            buf = av_realloc(buf, bufsize);
            res = getgrnam_r(gname, &gr, buf, bufsize, &grres);
        } while(res == ERANGE);
#else
       grres = getgrnam(gname);
#endif

        if(grres == NULL)
            cache->gid = -1;
        else
            cache->gid = grres->gr_gid;

        av_free(cache->gname);
        cache->gname = av_strdup(gname);
#ifdef HAVE_GETGRNAM_R
        av_free(buf);
#endif
    }
 
    if(cache->gid == -1)
        return deflt == -1 ? cache->mygid : deflt;
    else
        return cache->gid;
}

Here is the call graph for this function:

Here is the caller graph for this function:

char* av_findgname ( struct ugidcache cache,
int  gid,
const char *  deflt 
)

Definition at line 130 of file ugid.c.

{
    if(gid == -1)
        return av_strdup(deflt);

    if(cache->gname == NULL || gid != cache->gid) {
        int res;
        struct group gr;
        struct group *grres;
        char *buf = NULL;
        size_t bufsize = 0;

#ifdef HAVE_GETGRGID_R
        do {
            bufsize += 256;
            buf = av_realloc(buf, bufsize);
            res = getgrgid_r(gid, &gr, buf, bufsize, &grres);
        } while(res == ERANGE);
#else
       grres = getgrgid(gid);
#endif

        av_free(cache->gname);
        if(grres == NULL)
            cache->gname = av_strdup("");
        else
            cache->gname = av_strdup(grres->gr_name);

        cache->gid = gid;
#ifdef HAVE_GETGRGID_R
        av_free(buf);
#endif
    }
    
    if(!cache->gname[0])
        return av_strdup(deflt);
    else
        return av_strdup(cache->gname);
}

Here is the call graph for this function:

int av_finduid ( struct ugidcache cache,
const char *  uname,
int  deflt 
)

Definition at line 90 of file ugid.c.

{
    if(uname == NULL || !uname[0])
        return deflt == -1 ? cache->myuid : deflt;

    if(cache->uname == NULL || strcmp(uname, cache->uname) != 0) {
        int res;
        struct passwd pw;
        struct passwd *pwres;
        char *buf = NULL;
        size_t bufsize = 0;

#ifdef HAVE_GETPWNAM_R
        do {
            bufsize += 256;
            buf = av_realloc(buf, bufsize);
            res = getpwnam_r(uname, &pw, buf, bufsize, &pwres);
        } while(res == ERANGE);
#else
       pwres = getpwnam(uname);
#endif

        if(pwres == NULL)
            cache->uid = -1;
        else
            cache->uid = pwres->pw_uid;

        av_free(cache->uname);
        cache->uname = av_strdup(uname);
#ifdef HAVE_GETPWNAM_R
        av_free(buf);
#endif
    }
    
    if(cache->uid == -1)
        return deflt == -1 ? cache->myuid : deflt;
    else
        return cache->uid;
}

Here is the call graph for this function:

Here is the caller graph for this function:

char* av_finduname ( struct ugidcache cache,
int  uid,
const char *  deflt 
)

Definition at line 50 of file ugid.c.

{
    if(uid == -1)
        return av_strdup(deflt);

    if(cache->uname == NULL || uid != cache->uid) {
        int res;
        struct passwd pw;
        struct passwd *pwres;
        char *buf = NULL;
        size_t bufsize = 0;

#ifdef HAVE_GETPWUID_R
        do {
            bufsize += 256;
            buf = av_realloc(buf, bufsize);
            res = getpwuid_r(uid, &pw, buf, bufsize, &pwres);
        } while(res == ERANGE);
#else
       pwres = getpwuid(uid);
#endif

        av_free(cache->uname);
        if(pwres == NULL)
            cache->uname = av_strdup("");
        else
            cache->uname = av_strdup(pwres->pw_name);

        cache->uid = uid;
#ifdef HAVE_GETPWUID_R
        av_free(buf);
#endif
    }

    if(!cache->uname[0])
        return av_strdup(deflt);
    else
        return av_strdup(cache->uname);
}

Here is the call graph for this function:

struct ugidcache* av_new_ugidcache ( ) [read]

Definition at line 35 of file ugid.c.

{
    struct ugidcache *cache;

    AV_NEW_OBJ(cache, free_ugidcache);

    cache->uname = NULL;
    cache->myuid = getuid();
  
    cache->gname = NULL;
    cache->mygid = getgid();

    return cache;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void free_ugidcache ( struct ugidcache cache) [static]

Definition at line 29 of file ugid.c.

{
    av_free(cache->uname);
    av_free(cache->gname);
}

Here is the call graph for this function:

Here is the caller graph for this function: