Back to index

avfs  1.0.1
Classes | Functions | Variables
tmpfile.c File Reference
#include "internal.h"
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>

Go to the source code of this file.

Classes

struct  tmpdir

Functions

static AV_LOCK_DECL (tmplock)
static int unlink_recursive (const char *file)
void av_delete_tmpdir ()
static int make_tmp_dir (char *path)
int av_get_tmpfile (char **retp)
void av_del_tmpfile (char *tmpf)
avoff_t av_tmp_free ()
avoff_t av_tmpfile_blksize (const char *tmpf)

Variables

static struct tmpdirtmpdir

Class Documentation

struct tmpdir

Definition at line 23 of file tmpfile.c.

Class Members
int ctr
char * path

Function Documentation

void av_del_tmpfile ( char *  tmpf)

Definition at line 142 of file tmpfile.c.

{
    if(tmpf != NULL) {
       if(unlink(tmpf) == -1)
           rmdir(tmpf);
       
       av_free(tmpf);
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void av_delete_tmpdir ( )

Definition at line 67 of file tmpfile.c.

Here is the call graph for this function:

Here is the caller graph for this function:

int av_get_tmpfile ( char **  retp)

Definition at line 114 of file tmpfile.c.

{
    int res = 0;
    char buf[64];
  
    AV_LOCK(tmplock);
    if(tmpdir == NULL) {
        char *path;

        path = av_strdup("/tmp/.avfs_tmp_XXXXXX");
        res = make_tmp_dir(path);
        if(res < 0)
            av_free(path);
        else {
           AV_NEW(tmpdir);
           tmpdir->path = path;
           tmpdir->ctr = 0;
       }
    }
    if(tmpdir != NULL) {
       sprintf(buf, "/atmp%06i", tmpdir->ctr++);
       *retp = av_stradd(NULL, tmpdir->path, buf, NULL);
    }
    AV_UNLOCK(tmplock);

    return res;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static AV_LOCK_DECL ( tmplock  ) [static]

Definition at line 152 of file tmpfile.c.

{
#ifdef HAVE_SYS_STATVFS_H
    int res;
    struct statvfs stbuf;
#endif
    avoff_t freebytes = -1;

#ifdef HAVE_SYS_STATVFS_H
    AV_LOCK(tmplock);
    if(tmpdir != NULL) {
        /* Check if fs supports df info (ramfs doesn't) */
        res = statvfs(tmpdir->path, &stbuf);
        if(res != -1 && stbuf.f_blocks != 0)
            freebytes = (avoff_t) stbuf.f_bavail * (avoff_t) stbuf.f_frsize;
    }
    AV_UNLOCK(tmplock);
#endif

#if 0    
    if(freebytes != -1)
        av_log(AVLOG_DEBUG, "free bytes in tmp directory: %lli", freebytes);
#endif

    return freebytes;
}

Here is the call graph for this function:

Here is the caller graph for this function:

avoff_t av_tmpfile_blksize ( const char *  tmpf)

Definition at line 179 of file tmpfile.c.

{
    int res;
    struct stat stbuf;
    
    if(tmpf == NULL)
        return -1;

    res = stat(tmpf, &stbuf);
    if(res == 0) {
        /* Ramfs returns 0 diskusage */
        if(stbuf.st_blocks == 0)
            return stbuf.st_size;
        else
            return stbuf.st_blocks * 512;
    } else
        return -1;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int make_tmp_dir ( char *  path) [static]

Definition at line 95 of file tmpfile.c.

{
    int res;

    mktemp(path);
    if(path[0] == '\0') {
        av_log(AVLOG_ERROR, "mktemp failed for temporary directory");
        return -EIO;
    }
    res = mkdir(path, 0700);
    if(res == -1) {
        av_log(AVLOG_ERROR, "mkdir(%s) failed: %s", path, strerror(errno));
        return -EIO;
    }
    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int unlink_recursive ( const char *  file) [static]

Definition at line 31 of file tmpfile.c.

{
    int res;
    DIR *dirp;
    struct dirent *ent;
    char *name;

    res = unlink(file);
    if(res == 0)
        return 0;

    res = rmdir(file);
    if(res == 0)
        return 0;

    dirp = opendir(file);
    if(dirp == NULL)
        return -1;

    while((ent = readdir(dirp)) != NULL) {
        name = ent->d_name;
    
        if(name[0] != '.' || (name[1] && (name[1] != '.' || name[2]))) {
            char *newname;

            newname = av_stradd(NULL, file, "/", name, NULL);
            unlink_recursive(newname);
            av_free(newname);
        }
    }
    closedir(dirp);

    return rmdir(file);
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

struct tmpdir* tmpdir [static]

Definition at line 29 of file tmpfile.c.