Back to index

avfs  1.0.1
Defines | Functions
realfile.c File Reference
#include "realfile.h"
#include "oper.h"
#include "cache.h"
#include <unistd.h>
#include <fcntl.h>

Go to the source code of this file.

Defines

#define COPY_BUFSIZE   16384

Functions

static int copy_file (ventry *ve, const char *destpath)
static void realfile_delete (struct realfile *rf)
int av_get_realfile (ventry *ve, struct realfile **resp)

Define Documentation

#define COPY_BUFSIZE   16384

Definition at line 16 of file realfile.c.


Function Documentation

int av_get_realfile ( ventry *  ve,
struct realfile **  resp 
)

Definition at line 81 of file realfile.c.

{
    int res;
    struct realfile *rf;

    AV_NEW_OBJ(rf, realfile_delete);
    rf->is_tmp = 0;
    rf->name = NULL;

    if(ve->mnt->base == NULL) {
        rf->name = av_strdup((char *) ve->data);
        rf->is_tmp = 0;

        *resp = rf;
        return 0;
    }

    res = av_get_tmpfile(&rf->name);
    if(res < 0) {
        av_unref_obj(rf);
        return res;
    }

    rf->is_tmp = 1;

    res = copy_file(ve, rf->name);
    if(res < 0) {
        av_unref_obj(rf);
        return res;
    }

    *resp = rf;
    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int copy_file ( ventry *  ve,
const char *  destpath 
) [static]

Definition at line 18 of file realfile.c.

{
    int res;
    avssize_t num;
    char buf[COPY_BUFSIZE];
    int ctr;
    vfile *vf;
    int destfd;

    res = av_open(ve, AVO_RDONLY, 0, &vf);
    if(res < 0)
        return res;
    
    destfd = open(destpath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
    if(destfd == -1) {
        res = -errno;
        av_close(vf);
        av_log(AVLOG_ERROR, "Error opening file %s: %s", destpath,
               strerror(errno));
        return res;
    }

    ctr = 0;
    while(1) {
        res = av_read(vf, buf, COPY_BUFSIZE);
        if(res <= 0)
            break;

        num = res;
        res = write(destfd, buf, num);
        if(res == -1 && (errno == ENOSPC || errno == EDQUOT)) {
            av_cache_diskfull();
            res = write(destfd, buf, num);
        }
        if(res == -1) {
            res = -errno;
            av_log(AVLOG_ERROR, "Error writing file %s: %s", destpath,
                   strerror(errno));
            break;
        }

        /* Check free space after each Meg */
        if((ctr++ % 64) == 0)
            av_cache_checkspace();
    }

    close(destfd);
    if(res == 0)
        res = av_close(vf);
    else
        av_close(vf);

    return res;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void realfile_delete ( struct realfile rf) [static]

Definition at line 73 of file realfile.c.

{
    if(!rf->is_tmp) 
        av_free(rf->name);
    else 
        av_del_tmpfile(rf->name);
}

Here is the call graph for this function:

Here is the caller graph for this function: