File: tmpfile.c

package info (click to toggle)
avfs 1.0.0-4
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 4,600 kB
  • sloc: ansic: 41,546; sh: 11,261; perl: 1,916; makefile: 527; lisp: 14; csh: 12
file content (196 lines) | stat: -rw-r--r-- 3,795 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
    AVFS: A Virtual File System Library
    Copyright (C) 1998-2001  Miklos Szeredi <miklos@szeredi.hu>
    Copyright (C) 2006       Ralf Hoffmann (ralf@boomerangsworld.de)

    This program can be distributed under the terms of the GNU GPL.
    See the file COPYING.
*/

#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>
#ifdef HAVE_SYS_STATVFS_H
#include <sys/statvfs.h>
#endif

struct tmpdir {
    char *path;
    int ctr;
};

static AV_LOCK_DECL(tmplock);
static struct tmpdir *tmpdir;

static int unlink_recursive(const char *file)
{
    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);
}


void av_delete_tmpdir()
{
    AV_LOCK(tmplock);
    if(tmpdir != NULL) {
        unlink_recursive(tmpdir->path);
        av_free(tmpdir->path);
        av_free(tmpdir);
        tmpdir = NULL;
    }
    AV_UNLOCK(tmplock);
}

#ifdef HAVE_MKDTEMP

static int make_tmp_dir(char *path)
{
    char *res;

    res = mkdtemp(path);
    if(res == NULL) {
        av_log(AVLOG_ERROR, "mkdtemp failed: %s", strerror(errno));
        return -EIO;
    }
    return 0;
}

#else /* HAVE_MKDTEMP */

static int make_tmp_dir(char *path)
{
    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;
}

#endif /* HAVE_MKDTEMP */

int av_get_tmpfile(char **retp)
{
    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;
}

void av_del_tmpfile(char *tmpf)
{
    if(tmpf != NULL) {
	if(unlink(tmpf) == -1)
	    rmdir(tmpf);
	
	av_free(tmpf);
    }
}

avoff_t av_tmp_free()
{
#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;
}

avoff_t av_tmpfile_blksize(const char *tmpf)
{
    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;
}