aboutsummaryrefslogtreecommitdiffstats
path: root/e-util/e-mktemp.c
blob: 1d878b1ba02136c9a224fdc4fb61cf72140efce6 (plain) (blame)
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 *  Authors: Jeffrey Stedfast <fejj@ximian.com>
 *
 *  Copyright 2001 Ximian, Inc. (www.ximian.com)
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
 *
 */


#include "e-mktemp.h"

#include <config.h>

#include <glib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <errno.h>

#ifdef ENABLE_THREADS
#include <pthread.h>
#endif


static gboolean initialized = FALSE;
static GSList *temp_files = NULL;
static GSList *temp_dirs = NULL;
#ifdef ENABLE_THREADS
static pthread_mutex_t temp_files_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t temp_dirs_lock = PTHREAD_MUTEX_INITIALIZER;
#define TEMP_FILES_LOCK() pthread_mutex_lock (&temp_files_lock)
#define TEMP_FILES_UNLOCK() pthread_mutex_unlock (&temp_files_lock)
#define TEMP_DIRS_LOCK() pthread_mutex_lock (&temp_dirs_lock)
#define TEMP_DIRS_UNLOCK() pthread_mutex_unlock (&temp_dirs_lock)
#else
#define TEMP_FILES_LOCK()
#define TEMP_FILES_UNLOCK()
#define TEMP_DIRS_LOCK()
#define TEMP_DIRS_UNLOCK()
#endif /* ENABLE_THREADS */


static GString *
get_path (gboolean make)
{
    GString *path;
    
    path = g_string_new ("/tmp/evolution-");
    g_string_sprintfa (path, "%d-%d", (int) getuid (), (int) getpid ());
    
    if (make) {
        int ret;
        
        /* shoot now, ask questions later */
        ret = mkdir (path->str, S_IRWXU);
        if (ret == -1) {
            if (errno == EEXIST) {
                struct stat st;
                
                if (stat (path->str, &st) == -1) {
                    /* reset errno */
                    errno = EEXIST;
                    g_string_free (path, TRUE);
                    return NULL;
                }
                
                /* make sure this is a directory and belongs to us... */
                if (!S_ISDIR (st.st_mode) || st.st_uid != getuid ()) {
                    /* eek! this is bad... */
                    g_string_free (path, TRUE);
                    return NULL;
                }
            } else {
                /* some other error...do not pass go, do not collect $200 */
                g_string_free (path, TRUE);
                return NULL;
            }
        }
    }
    
    return path;
}

static void
e_mktemp_cleanup (void)
{
    GString *path;
    GSList *node;
    
    TEMP_FILES_LOCK ();
    if (temp_files) {
        node = temp_files;
        while (node) {
            unlink (node->data);
            g_free (node->data);
            node = node->next;
        }
        g_slist_free (temp_files);
        temp_files = NULL;
    }
    TEMP_FILES_UNLOCK ();
    
    TEMP_DIRS_LOCK ();
    if (temp_dirs) {
        node = temp_dirs;
        while (node) {
            /* perform the equivalent of a rm -rf */
            struct dirent *dent;
            DIR *dir;
            
            /* first empty out this directory of it's files... */
            dir = opendir (node->data);
            if (dir) {
                while ((dent = readdir (dir)) != NULL) {
                    /* yea...so if we contain
                       subdirectories this won't work, but
                       it shouldn't so we won't
                       bother caring... */
                    if (strcmp (dent->d_name, ".") && strcmp (dent->d_name, ".."))
                        unlink (dent->d_name);
                }
                closedir (dir);
            }
            
            /* ...then rmdir the directory */
            rmdir (node->data);
            g_free (node->data);
            node = node->next;
        }
        g_slist_free (temp_dirs);
        temp_dirs = NULL;
    }
    TEMP_DIRS_UNLOCK ();
    
    path = get_path (FALSE);
    rmdir (path->str);
    
    g_string_free (path, TRUE);
}


const char *
e_mktemp (const char *template)
{
    GString *path;
    char *ret;
    
    path = get_path (TRUE);
    if (!path)
        return NULL;
    
    g_string_append_c (path, '/');
    if (template)
        g_string_append (path, template);
    else
        g_string_append (path, "unknown-XXXXXX");
    
    ret = mktemp (path->str);
    if (ret) {
        TEMP_FILES_LOCK ();
        if (!initialized) {
            g_atexit (e_mktemp_cleanup);
            initialized = TRUE;
        }
        temp_files = g_slist_prepend (temp_files, ret);
        g_string_free (path, FALSE);
        TEMP_FILES_UNLOCK ();
    } else {
        g_string_free (path, TRUE);
    }
    
    return ret;
}


int
e_mkstemp (const char *template)
{
    GString *path;
    int fd;
    
    path = get_path (TRUE);
    if (!path)
        return -1;
    
    g_string_append_c (path, '/');
    if (template)
        g_string_append (path, template);
    else
        g_string_append (path, "unknown-XXXXXX");
    
    fd = mkstemp (path->str);
    if (fd != -1) {
        TEMP_FILES_LOCK ();
        if (!initialized) {
            g_atexit (e_mktemp_cleanup);
            initialized = TRUE;
        }
        temp_files = g_slist_prepend (temp_files, path->str);
        g_string_free (path, FALSE);
        TEMP_FILES_UNLOCK ();
    } else {
        g_string_free (path, TRUE);
    }
    
    return fd;
}


const char *
e_mkdtemp (const char *template)
{
    GString *path;
    char *tmpdir;
    
    path = get_path (TRUE);
    if (!path)
        return NULL;
    
    g_string_append_c (path, '/');
    if (template)
        g_string_append (path, template);
    else
        g_string_append (path, "unknown-XXXXXX");
    
#ifdef HAVE_MKDTEMP
    tmpdir = mkdtemp (path->str);
#else
    tmpdir = mktemp (path->str);
    if (tmpdir) {
        if (mkdir (tmpdir, S_IRWXU) == -1)
            tmpdir = NULL;
    }
#endif
    
    if (tmpdir) {
        TEMP_DIRS_LOCK ();
        if (!initialized) {
            g_atexit (e_mktemp_cleanup);
            initialized = TRUE;
        }
        temp_dirs = g_slist_prepend (temp_dirs, tmpdir);
        g_string_free (path, FALSE);
        TEMP_DIRS_UNLOCK ();
    } else {
        g_string_free (path, TRUE);
    }
    
    return tmpdir;
}