annotate lib/clean-temp.c @ 7044:4165bec5f3a2

New module 'clean-temp'.
author Bruno Haible <bruno@clisp.org>
date Mon, 24 Jul 2006 16:45:17 +0000
parents
children 1c4ed7637c24
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Temporary directories and temporary files with automatic cleanup.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
2 Copyright (C) 2001, 2003, 2006 Free Software Foundation, Inc.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3 Written by Bruno Haible <bruno@clisp.org>, 2006.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 This program is free software; you can redistribute it and/or modify
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 it under the terms of the GNU General Public License as published by
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 the Free Software Foundation; either version 2, or (at your option)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8 any later version.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13 GNU General Public License for more details.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16 along with this program; if not, write to the Free Software Foundation,
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 #ifdef HAVE_CONFIG_H
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 # include "config.h"
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 #endif
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 /* Specification. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 #include "clean-temp.h"
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 #include <errno.h>
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 #include <limits.h>
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 #include <stdbool.h>
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 #include <stdlib.h>
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 #include <string.h>
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 #include <unistd.h>
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 #include "error.h"
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 #include "fatal-signal.h"
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 #include "pathmax.h"
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 #include "tmpdir.h"
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 #include "mkdtemp.h"
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 #include "xalloc.h"
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 #include "xallocsa.h"
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 #include "gl_linkedhash_list.h"
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 #include "gettext.h"
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 #define _(str) gettext (str)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 /* The use of 'volatile' in the types below (and ISO C 99 section 5.1.2.3.(5))
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 ensure that while constructing or modifying the data structures, the field
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 values are written to memory in the order of the C statements. So the
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 signal handler can rely on these field values to be up to date. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 /* Registry for a single temporary directory.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 'struct temp_dir' from the public header file overlaps with this. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 struct tempdir
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 {
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 /* The absolute pathname of the directory. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 char * volatile dirname;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 /* Whether errors during explicit cleanup are reported to standard error. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 bool cleanup_verbose;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 /* Absolute pathnames of subdirectories. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 gl_list_t /* <char *> */ volatile subdirs;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 /* Absolute pathnames of files. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 gl_list_t /* <char *> */ volatile files;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 };
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 /* List of all temporary directories. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 static struct
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 {
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 struct tempdir * volatile * volatile tempdir_list;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 size_t volatile tempdir_count;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 size_t tempdir_allocated;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 } cleanup_list /* = { NULL, 0, 0 } */;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76 /* For the subdirs and for the files, we use a gl_list_t of type LINKEDHASH.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77 Why? We need a data structure that
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79 1) Can contain an arbitrary number of 'char *' values. The strings
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80 are compared via strcmp, not pointer comparison.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 2) Has insertion and deletion operations that are fast: ideally O(1),
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82 or possibly O(log n). This is important for GNU sort, which may
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83 create a large number of temporary files.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
84 3) Allows iteration through all elements from within a signal handler.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85 4) May or may not allow duplicates. It doesn't matter here, since
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86 any file or subdir can only be removed once.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
88 Criterion 1) would allow any gl_list_t or gl_oset_t implementation.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
90 Criterion 2) leaves only GL_LINKEDHASH_LIST, GL_TREEHASH_LIST, or
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
91 GL_TREE_OSET.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
92
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
93 Criterion 3) puts at disadvantage GL_TREEHASH_LIST and GL_TREE_OSET.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
94 Namely, iteration through the elements of a binary tree requires access
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
95 to many ->left, ->right, ->parent pointers. However, the rebalancing
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
96 code for insertion and deletion in an AVL or red-black tree is so
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
97 complicated that we cannot assume that >left, ->right, ->parent pointers
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
98 are in a consistent state throughout these operations. Therefore, to
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
99 avoid a crash in the signal handler, all destructive operations to the
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
100 lists would have to be protected by a
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
101 block_fatal_signals ();
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
102 ...
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
103 unblock_fatal_signals ();
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
104 pair. Which causes extra system calls.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
105
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
106 Criterion 3) would also discourage GL_ARRAY_LIST and GL_CARRAY_LIST,
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
107 if they were not already excluded. Namely, these implementations use
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
108 xrealloc(), leaving a time window in which in the list->elements pointer
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
109 points to already deallocated memory. To avoid a crash in the signal
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
110 handler at such a moment, all destructive operations would have to
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
111 protected by block/unblock_fatal_signals (), in this case too.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
112
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
113 A list of type GL_LINKEDHASH_LIST without duplicates fulfills all
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
114 requirements:
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
115 2) Insertion and deletion are O(1) on average.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
116 3) The gl_list_iterator, gl_list_iterator_next implementations do
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
117 not trigger memory allocations, nor other system calls, and are
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
118 therefore safe to be called from a signal handler.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
119 Furthermore, since SIGNAL_SAFE_LIST is defined, the implementation
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
120 of the destructive functions ensures that the list structure is
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
121 safe to be traversed at any moment, even when interrupted by an
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
122 asynchronous signal.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
123 */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
124
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
125 /* String equality and hash code functions used by the lists. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
126
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
127 static bool
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
128 string_equals (const void *x1, const void *x2)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
129 {
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
130 const char *s1 = x1;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
131 const char *s2 = x2;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
132 return strcmp (s1, s2) == 0;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
133 }
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
134
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
135 #define SIZE_BITS (sizeof (size_t) * CHAR_BIT)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
136
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
137 /* A hash function for NUL-terminated char* strings using
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
138 the method described by Bruno Haible.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
139 See http://www.haible.de/bruno/hashfunc.html. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
140 static size_t
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
141 string_hash (const void *x)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
142 {
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
143 const char *s = x;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
144 size_t h = 0;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
145
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
146 for (; *s; s++)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
147 h = *s + ((h << 9) | (h >> (SIZE_BITS - 9)));
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
148
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
149 return h;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
150 }
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
151
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
152
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
153 /* The signal handler. It gets called asynchronously. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
154 static void
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
155 cleanup ()
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
156 {
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
157 size_t i;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
158
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
159 for (i = 0; i < cleanup_list.tempdir_count; i++)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
160 {
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
161 struct tempdir *dir = cleanup_list.tempdir_list[i];
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
162
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
163 if (dir != NULL)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
164 {
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
165 gl_list_iterator_t iter;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
166 const void *element;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
167
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
168 /* First cleanup the files in the subdirectories. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
169 iter = gl_list_iterator (dir->files);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
170 while (gl_list_iterator_next (&iter, &element, NULL))
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
171 {
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
172 const char *file = (const char *) element;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
173 unlink (file);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
174 }
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
175 gl_list_iterator_free (&iter);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
176
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
177 /* Then cleanup the subdirectories. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
178 iter = gl_list_iterator (dir->subdirs);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
179 while (gl_list_iterator_next (&iter, &element, NULL))
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
180 {
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
181 const char *subdir = (const char *) element;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
182 rmdir (subdir);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
183 }
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
184 gl_list_iterator_free (&iter);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
185
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
186 /* Then cleanup the temporary directory itself. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
187 rmdir (dir->dirname);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
188 }
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
189 }
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
190 }
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
191
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
192 /* Create a temporary directory.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
193 PREFIX is used as a prefix for the name of the temporary directory. It
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
194 should be short and still give an indication about the program.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
195 PARENTDIR can be used to specify the parent directory; if NULL, a default
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
196 parent directory is used (either $TMPDIR or /tmp or similar).
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
197 CLEANUP_VERBOSE determines whether errors during explicit cleanup are
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
198 reported to standard error.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
199 Return a fresh 'struct temp_dir' on success. Upon error, an error message
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
200 is shown and NULL is returned. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
201 struct temp_dir *
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
202 create_temp_dir (const char *prefix, const char *parentdir,
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
203 bool cleanup_verbose)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
204 {
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
205 struct tempdir * volatile *tmpdirp = NULL;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
206 struct tempdir *tmpdir;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
207 size_t i;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
208 char *template;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
209 char *tmpdirname;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
210
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
211 /* See whether it can take the slot of an earlier temporary directory
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
212 already cleaned up. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
213 for (i = 0; i < cleanup_list.tempdir_count; i++)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
214 if (cleanup_list.tempdir_list[i] == NULL)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
215 {
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
216 tmpdirp = &cleanup_list.tempdir_list[i];
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
217 break;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
218 }
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
219 if (tmpdirp == NULL)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
220 {
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
221 /* See whether the array needs to be extended. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
222 if (cleanup_list.tempdir_count == cleanup_list.tempdir_allocated)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
223 {
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
224 /* Note that we cannot use xrealloc(), because then the cleanup()
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
225 function could access an already deallocated array. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
226 struct tempdir * volatile *old_array = cleanup_list.tempdir_list;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
227 size_t old_allocated = cleanup_list.tempdir_allocated;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
228 size_t new_allocated = 2 * cleanup_list.tempdir_allocated + 1;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
229 struct tempdir * volatile *new_array =
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
230 (struct tempdir * volatile *)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
231 xmalloc (new_allocated * sizeof (struct tempdir * volatile));
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
232
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
233 if (old_allocated == 0)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
234 /* First use of this facility. Register the cleanup handler. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
235 at_fatal_signal (&cleanup);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
236 else
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
237 {
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
238 /* Don't use memcpy() here, because memcpy takes non-volatile
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
239 arguments and is therefore not guaranteed to complete all
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
240 memory stores before the next statement. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
241 size_t k;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
242
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
243 for (k = 0; k < old_allocated; k++)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
244 new_array[k] = old_array[k];
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
245 }
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
246
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
247 cleanup_list.tempdir_list = new_array;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
248 cleanup_list.tempdir_allocated = new_allocated;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
249
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
250 /* Now we can free the old array. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
251 if (old_array != NULL)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
252 free ((struct tempdir **) old_array);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
253 }
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
254
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
255 tmpdirp = &cleanup_list.tempdir_list[cleanup_list.tempdir_count];
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
256 /* Initialize *tmpdirp before incrementing tempdir_count, so that
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
257 cleanup() will skip this entry before it is fully initialized. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
258 *tmpdirp = NULL;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
259 cleanup_list.tempdir_count++;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
260 }
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
261
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
262 /* Initialize a 'struct tempdir'. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
263 tmpdir = (struct tempdir *) xmalloc (sizeof (struct tempdir));
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
264 tmpdir->dirname = NULL;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
265 tmpdir->cleanup_verbose = cleanup_verbose;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
266 tmpdir->subdirs = gl_list_create_empty (GL_LINKEDHASH_LIST,
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
267 string_equals, string_hash, false);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
268 tmpdir->files = gl_list_create_empty (GL_LINKEDHASH_LIST,
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
269 string_equals, string_hash, false);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
270
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
271 /* Create the temporary directory. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
272 template = (char *) xallocsa (PATH_MAX);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
273 if (path_search (template, PATH_MAX, parentdir, prefix, parentdir == NULL))
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
274 {
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
275 error (0, errno,
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
276 _("cannot find a temporary directory, try setting $TMPDIR"));
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
277 goto quit;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
278 }
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
279 block_fatal_signals ();
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
280 tmpdirname = mkdtemp (template);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
281 if (tmpdirname != NULL)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
282 {
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
283 tmpdir->dirname = tmpdirname;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
284 *tmpdirp = tmpdir;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
285 }
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
286 unblock_fatal_signals ();
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
287 if (tmpdirname == NULL)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
288 {
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
289 error (0, errno,
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
290 _("cannot create a temporary directory using template \"%s\""),
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
291 template);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
292 goto quit;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
293 }
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
294 /* Replace tmpdir->dirname with a copy that has indefinite extent.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
295 We cannot do this inside the block_fatal_signals/unblock_fatal_signals
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
296 block because then the cleanup handler would not remove the directory
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
297 if xstrdup fails. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
298 tmpdir->dirname = xstrdup (tmpdirname);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
299 freesa (template);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
300 return (struct temp_dir *) tmpdir;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
301
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
302 quit:
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
303 freesa (template);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
304 return NULL;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
305 }
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
306
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
307 /* Register the given ABSOLUTE_FILE_NAME as being a file inside DIR, that
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
308 needs to be removed before DIR can be removed.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
309 Should be called before the file ABSOLUTE_FILE_NAME is created. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
310 void
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
311 register_temp_file (struct temp_dir *dir,
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
312 const char *absolute_file_name)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
313 {
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
314 struct tempdir *tmpdir = (struct tempdir *)dir;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
315
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
316 /* Add absolute_file_name to tmpdir->files, without duplicates. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
317 if (gl_list_search (tmpdir->files, absolute_file_name) == NULL)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
318 gl_list_add_first (tmpdir->files, xstrdup (absolute_file_name));
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
319 }
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
320
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
321 /* Unregister the given ABSOLUTE_FILE_NAME as being a file inside DIR, that
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
322 needs to be removed before DIR can be removed.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
323 Should be called when the file ABSOLUTE_FILE_NAME could not be created. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
324 void
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
325 unregister_temp_file (struct temp_dir *dir,
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
326 const char *absolute_file_name)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
327 {
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
328 struct tempdir *tmpdir = (struct tempdir *)dir;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
329 gl_list_t list = tmpdir->files;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
330 gl_list_node_t node;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
331
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
332 node = gl_list_search (list, absolute_file_name);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
333 if (node != NULL)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
334 {
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
335 char *old_string = (char *) gl_list_node_value (list, node);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
336
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
337 gl_list_remove_node (list, node);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
338 free (old_string);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
339 }
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
340 }
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
341
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
342 /* Register the given ABSOLUTE_DIR_NAME as being a subdirectory inside DIR,
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
343 that needs to be removed before DIR can be removed.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
344 Should be called before the subdirectory ABSOLUTE_DIR_NAME is created. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
345 void
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
346 register_temp_subdir (struct temp_dir *dir,
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
347 const char *absolute_dir_name)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
348 {
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
349 struct tempdir *tmpdir = (struct tempdir *)dir;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
350
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
351 /* Add absolute_dir_name to tmpdir->subdirs, without duplicates. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
352 if (gl_list_search (tmpdir->subdirs, absolute_dir_name) == NULL)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
353 gl_list_add_first (tmpdir->subdirs, xstrdup (absolute_dir_name));
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
354 }
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
355
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
356 /* Unregister the given ABSOLUTE_DIR_NAME as being a subdirectory inside DIR,
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
357 that needs to be removed before DIR can be removed.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
358 Should be called when the subdirectory ABSOLUTE_DIR_NAME could not be
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
359 created. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
360 void
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
361 unregister_temp_subdir (struct temp_dir *dir,
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
362 const char *absolute_dir_name)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
363 {
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
364 struct tempdir *tmpdir = (struct tempdir *)dir;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
365 gl_list_t list = tmpdir->subdirs;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
366 gl_list_node_t node;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
367
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
368 node = gl_list_search (list, absolute_dir_name);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
369 if (node != NULL)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
370 {
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
371 char *old_string = (char *) gl_list_node_value (list, node);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
372
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
373 gl_list_remove_node (list, node);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
374 free (old_string);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
375 }
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
376 }
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
377
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
378 /* Remove a file, with optional error message. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
379 static void
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
380 do_unlink (struct temp_dir *dir, const char *absolute_file_name)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
381 {
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
382 if (unlink (absolute_file_name) < 0 && dir->cleanup_verbose
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
383 && errno != ENOENT)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
384 error (0, errno, _("cannot remove temporary file %s"), absolute_file_name);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
385 }
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
386
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
387 /* Remove a directory, with optional error message. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
388 static void
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
389 do_rmdir (struct temp_dir *dir, const char *absolute_dir_name)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
390 {
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
391 if (rmdir (absolute_dir_name) < 0 && dir->cleanup_verbose
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
392 && errno != ENOENT)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
393 error (0, errno,
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
394 _("cannot remove temporary directory %s"), absolute_dir_name);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
395 }
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
396
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
397 /* Remove the given ABSOLUTE_FILE_NAME and unregister it. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
398 void
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
399 cleanup_temp_file (struct temp_dir *dir,
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
400 const char *absolute_file_name)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
401 {
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
402 do_unlink (dir, absolute_file_name);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
403 unregister_temp_file (dir, absolute_file_name);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
404 }
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
405
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
406 /* Remove the given ABSOLUTE_DIR_NAME and unregister it. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
407 void
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
408 cleanup_temp_subdir (struct temp_dir *dir,
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
409 const char *absolute_dir_name)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
410 {
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
411 do_rmdir (dir, absolute_dir_name);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
412 unregister_temp_subdir (dir, absolute_dir_name);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
413 }
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
414
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
415 /* Remove all registered files and subdirectories inside DIR. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
416 void
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
417 cleanup_temp_dir_contents (struct temp_dir *dir)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
418 {
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
419 struct tempdir *tmpdir = (struct tempdir *)dir;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
420 gl_list_t list;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
421 gl_list_iterator_t iter;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
422 const void *element;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
423 gl_list_node_t node;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
424
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
425 /* First cleanup the files in the subdirectories. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
426 list = tmpdir->files;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
427 iter = gl_list_iterator (list);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
428 while (gl_list_iterator_next (&iter, &element, &node))
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
429 {
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
430 char *file = (char *) element;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
431
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
432 do_unlink (dir, file);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
433 gl_list_remove_node (list, node);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
434 /* Now only we can free file. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
435 free (file);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
436 }
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
437 gl_list_iterator_free (&iter);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
438
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
439 /* Then cleanup the subdirectories. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
440 list = tmpdir->subdirs;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
441 iter = gl_list_iterator (list);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
442 while (gl_list_iterator_next (&iter, &element, &node))
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
443 {
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
444 char *subdir = (char *) element;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
445
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
446 do_rmdir (dir, subdir);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
447 gl_list_remove_node (list, node);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
448 /* Now only we can free subdir. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
449 free (subdir);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
450 }
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
451 gl_list_iterator_free (&iter);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
452 }
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
453
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
454 /* Remove all registered files and subdirectories inside DIR and DIR itself.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
455 DIR cannot be used any more after this call. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
456 void
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
457 cleanup_temp_dir (struct temp_dir *dir)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
458 {
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
459 struct tempdir *tmpdir = (struct tempdir *)dir;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
460 size_t i;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
461
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
462 cleanup_temp_dir_contents (dir);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
463 do_rmdir (dir, tmpdir->dirname);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
464
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
465 for (i = 0; i < cleanup_list.tempdir_count; i++)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
466 if (cleanup_list.tempdir_list[i] == tmpdir)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
467 {
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
468 /* Remove cleanup_list.tempdir_list[i]. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
469 if (i + 1 == cleanup_list.tempdir_count)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
470 {
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
471 while (i > 0 && cleanup_list.tempdir_list[i - 1] == NULL)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
472 i--;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
473 cleanup_list.tempdir_count = i;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
474 }
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
475 else
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
476 cleanup_list.tempdir_list[i] = NULL;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
477 /* Now only we can free the tmpdir->dirname and tmpdir itself. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
478 free (tmpdir->dirname);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
479 free (tmpdir);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
480 return;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
481 }
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
482
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
483 /* The user passed an invalid DIR argument. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
484 abort ();
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
485 }