annotate lib/clean-temp.c @ 40238:3ac749aa0041

fatal-signal: Pass the signal number to the action. * lib/fatal-signal.h (at_fatal_signal): Change the signature. * lib/fatal-signal.c (action_t): Take the signal number as parameter. (fatal_signal_handler): Pass the signal number to the action. * lib/clean-temp.c (cleanup_action): Renamed from cleanup. Take the signal number as parameter. (create_temp_dir): Update. * lib/wait-process.c (cleanup_slaves_action): New function. (register_slave_subprocess): Update at_fatal_signal invocation. * NEWS: Mention the change.
author Bruno Haible <bruno@clisp.org>
date Sat, 16 Mar 2019 22:56:21 +0100
parents 5a52ef2d4772
children
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.
40057
b06060465f09 maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents: 19595
diff changeset
2 Copyright (C) 2001, 2003, 2006-2007, 2009-2019 Free Software Foundation,
12559
c2cbabec01dd update nearly all FSF copyright year lists to include 2010
Jim Meyering <meyering@redhat.com>
parents: 12445
diff changeset
3 Inc.
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4 Written by Bruno Haible <bruno@clisp.org>, 2006.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8948
diff changeset
6 This program is free software: you can redistribute it and/or modify
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 it under the terms of the GNU General Public License as published by
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8948
diff changeset
8 the Free Software Foundation; either version 3 of the License, or
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8948
diff changeset
9 (at your option) any later version.
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 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
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14 GNU General Public License for more details.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
19190
9759915b2aca all: prefer https: URLs
Paul Eggert <eggert@cs.ucla.edu>
parents: 18626
diff changeset
17 along with this program. If not, see <https://www.gnu.org/licenses/>. */
7044
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
7304
1c4ed7637c24 Include <config.h> unconditionally.
Bruno Haible <bruno@clisp.org>
parents: 7044
diff changeset
20 #include <config.h>
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 /* Specification. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23 #include "clean-temp.h"
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 #include <errno.h>
7417
fa9e9b096831 * clean-temp.h (close_stream_temp): New declaration.
Eric Blake <ebb9@byu.net>
parents: 7415
diff changeset
26 #include <fcntl.h>
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 #include <limits.h>
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 #include <stdbool.h>
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 #include <stdlib.h>
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 #include <string.h>
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 #include <unistd.h>
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32
19595
beb2ad957aca Simplify code. Drop support for Borland C++ on Windows.
Bruno Haible <bruno@clisp.org>
parents: 19484
diff changeset
33 #if defined _WIN32 && ! defined __CYGWIN__
8161
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
34 # define WIN32_LEAN_AND_MEAN /* avoid including junk */
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
35 # include <windows.h>
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
36 #endif
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
37
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 #include "error.h"
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 #include "fatal-signal.h"
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 #include "pathmax.h"
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 #include "tmpdir.h"
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 #include "xalloc.h"
8948
a162347a0232 Update after allocsa -> malloca renaming.
Bruno Haible <bruno@clisp.org>
parents: 8438
diff changeset
43 #include "xmalloca.h"
12445
a8c91b846640 Move the malloc checking from module 'list' to new module 'xlist'.
Bruno Haible <bruno@clisp.org>
parents: 12421
diff changeset
44 #include "gl_xlist.h"
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 #include "gl_linkedhash_list.h"
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 #include "gettext.h"
7513
f53946778c11 Avoid a gcc warning.
Bruno Haible <bruno@clisp.org>
parents: 7422
diff changeset
47 #if GNULIB_FWRITEERROR
f53946778c11 Avoid a gcc warning.
Bruno Haible <bruno@clisp.org>
parents: 7422
diff changeset
48 # include "fwriteerror.h"
f53946778c11 Avoid a gcc warning.
Bruno Haible <bruno@clisp.org>
parents: 7422
diff changeset
49 #endif
7417
fa9e9b096831 * clean-temp.h (close_stream_temp): New declaration.
Eric Blake <ebb9@byu.net>
parents: 7415
diff changeset
50 #if GNULIB_CLOSE_STREAM
fa9e9b096831 * clean-temp.h (close_stream_temp): New declaration.
Eric Blake <ebb9@byu.net>
parents: 7415
diff changeset
51 # include "close-stream.h"
fa9e9b096831 * clean-temp.h (close_stream_temp): New declaration.
Eric Blake <ebb9@byu.net>
parents: 7415
diff changeset
52 #endif
fa9e9b096831 * clean-temp.h (close_stream_temp): New declaration.
Eric Blake <ebb9@byu.net>
parents: 7415
diff changeset
53 #if GNULIB_FCNTL_SAFER
fa9e9b096831 * clean-temp.h (close_stream_temp): New declaration.
Eric Blake <ebb9@byu.net>
parents: 7415
diff changeset
54 # include "fcntl--.h"
fa9e9b096831 * clean-temp.h (close_stream_temp): New declaration.
Eric Blake <ebb9@byu.net>
parents: 7415
diff changeset
55 #endif
fa9e9b096831 * clean-temp.h (close_stream_temp): New declaration.
Eric Blake <ebb9@byu.net>
parents: 7415
diff changeset
56 #if GNULIB_FOPEN_SAFER
fa9e9b096831 * clean-temp.h (close_stream_temp): New declaration.
Eric Blake <ebb9@byu.net>
parents: 7415
diff changeset
57 # include "stdio--.h"
fa9e9b096831 * clean-temp.h (close_stream_temp): New declaration.
Eric Blake <ebb9@byu.net>
parents: 7415
diff changeset
58 #endif
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 #define _(str) gettext (str)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61
15496
451ec8bd8a1c pathmax: Leave PATH_MAX undefined on the Hurd, and a constant otherwise.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
62 /* GNU Hurd doesn't have PATH_MAX. Use a fallback.
451ec8bd8a1c pathmax: Leave PATH_MAX undefined on the Hurd, and a constant otherwise.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
63 Temporary directory names are usually not that long. */
7414
368fbbd8e59d Provide a fallback for PATH_MAX.
Bruno Haible <bruno@clisp.org>
parents: 7411
diff changeset
64 #ifndef PATH_MAX
15496
451ec8bd8a1c pathmax: Leave PATH_MAX undefined on the Hurd, and a constant otherwise.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
65 # define PATH_MAX 1024
7414
368fbbd8e59d Provide a fallback for PATH_MAX.
Bruno Haible <bruno@clisp.org>
parents: 7411
diff changeset
66 #endif
368fbbd8e59d Provide a fallback for PATH_MAX.
Bruno Haible <bruno@clisp.org>
parents: 7411
diff changeset
67
7411
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
68 #ifndef uintptr_t
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
69 # define uintptr_t unsigned long
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
70 #endif
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
71
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 /* 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
74 ensure that while constructing or modifying the data structures, the field
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 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
76 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
77
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 /* Registry for a single temporary directory.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80 'struct temp_dir' from the public header file overlaps with this. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 struct tempdir
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82 {
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83 /* The absolute pathname of the directory. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
84 char * volatile dirname;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85 /* Whether errors during explicit cleanup are reported to standard error. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86 bool cleanup_verbose;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87 /* Absolute pathnames of subdirectories. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
88 gl_list_t /* <char *> */ volatile subdirs;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89 /* Absolute pathnames of files. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
90 gl_list_t /* <char *> */ volatile files;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
91 };
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 /* List of all temporary directories. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
94 static struct
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
95 {
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
96 struct tempdir * volatile * volatile tempdir_list;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
97 size_t volatile tempdir_count;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
98 size_t tempdir_allocated;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
99 } cleanup_list /* = { NULL, 0, 0 } */;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
100
7411
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
101 /* List of all open file descriptors to temporary files. */
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
102 static gl_list_t /* <int> */ volatile descriptors;
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
103
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
104
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
105 /* 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
106 Why? We need a data structure that
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
107
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
108 1) Can contain an arbitrary number of 'char *' values. The strings
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
109 are compared via strcmp, not pointer comparison.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
110 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
111 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
112 create a large number of temporary files.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
113 3) Allows iteration through all elements from within a signal handler.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
114 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
115 any file or subdir can only be removed once.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
116
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
117 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
118
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
119 Criterion 2) leaves only GL_LINKEDHASH_LIST, GL_TREEHASH_LIST, or
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
120 GL_TREE_OSET.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
121
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
122 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
123 Namely, iteration through the elements of a binary tree requires access
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
124 to many ->left, ->right, ->parent pointers. However, the rebalancing
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
125 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
126 complicated that we cannot assume that >left, ->right, ->parent pointers
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
127 are in a consistent state throughout these operations. Therefore, to
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
128 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
129 lists would have to be protected by a
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
130 block_fatal_signals ();
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
131 ...
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
132 unblock_fatal_signals ();
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
133 pair. Which causes extra system calls.
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 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
136 if they were not already excluded. Namely, these implementations use
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
137 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
138 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
139 handler at such a moment, all destructive operations would have to
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
140 protected by block/unblock_fatal_signals (), in this case too.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
141
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
142 A list of type GL_LINKEDHASH_LIST without duplicates fulfills all
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
143 requirements:
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
144 2) Insertion and deletion are O(1) on average.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
145 3) The gl_list_iterator, gl_list_iterator_next implementations do
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
146 not trigger memory allocations, nor other system calls, and are
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
147 therefore safe to be called from a signal handler.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
148 Furthermore, since SIGNAL_SAFE_LIST is defined, the implementation
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
149 of the destructive functions ensures that the list structure is
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
150 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
151 asynchronous signal.
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
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
154 /* String equality and hash code functions used by the lists. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
155
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
156 static bool
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
157 string_equals (const void *x1, const void *x2)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
158 {
7586
4a8b5467d8b2 Make it compile in C++ mode.
Bruno Haible <bruno@clisp.org>
parents: 7513
diff changeset
159 const char *s1 = (const char *) x1;
4a8b5467d8b2 Make it compile in C++ mode.
Bruno Haible <bruno@clisp.org>
parents: 7513
diff changeset
160 const char *s2 = (const char *) x2;
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
161 return strcmp (s1, s2) == 0;
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
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
164 #define SIZE_BITS (sizeof (size_t) * CHAR_BIT)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
165
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
166 /* A hash function for NUL-terminated char* strings using
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
167 the method described by Bruno Haible.
19192
d86e08b1f555 all: Replace many more http URLs by https URLs. Update stale URLs.
Bruno Haible <bruno@clisp.org>
parents: 19190
diff changeset
168 See https://www.haible.de/bruno/hashfunc.html. */
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
169 static size_t
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
170 string_hash (const void *x)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
171 {
7586
4a8b5467d8b2 Make it compile in C++ mode.
Bruno Haible <bruno@clisp.org>
parents: 7513
diff changeset
172 const char *s = (const char *) x;
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
173 size_t h = 0;
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 for (; *s; s++)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
176 h = *s + ((h << 9) | (h >> (SIZE_BITS - 9)));
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
177
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
178 return h;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
179 }
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
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
182 /* The signal handler. It gets called asynchronously. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
183 static void
40238
3ac749aa0041 fatal-signal: Pass the signal number to the action.
Bruno Haible <bruno@clisp.org>
parents: 40235
diff changeset
184 cleanup_action (int sig _GL_UNUSED)
7044
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 size_t i;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
187
7411
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
188 /* First close all file descriptors to temporary files. */
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
189 {
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
190 gl_list_t fds = descriptors;
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
191
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
192 if (fds != NULL)
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
193 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
194 gl_list_iterator_t iter;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
195 const void *element;
7411
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
196
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
197 iter = gl_list_iterator (fds);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
198 while (gl_list_iterator_next (&iter, &element, NULL))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
199 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
200 int fd = (int) (uintptr_t) element;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
201 close (fd);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
202 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
203 gl_list_iterator_free (&iter);
7411
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
204 }
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
205 }
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
206
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
207 for (i = 0; i < cleanup_list.tempdir_count; i++)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
208 {
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
209 struct tempdir *dir = cleanup_list.tempdir_list[i];
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 if (dir != NULL)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
212 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
213 gl_list_iterator_t iter;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
214 const void *element;
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
215
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
216 /* First cleanup the files in the subdirectories. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
217 iter = gl_list_iterator (dir->files);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
218 while (gl_list_iterator_next (&iter, &element, NULL))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
219 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
220 const char *file = (const char *) element;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
221 unlink (file);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
222 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
223 gl_list_iterator_free (&iter);
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
224
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
225 /* Then cleanup the subdirectories. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
226 iter = gl_list_iterator (dir->subdirs);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
227 while (gl_list_iterator_next (&iter, &element, NULL))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
228 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
229 const char *subdir = (const char *) element;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
230 rmdir (subdir);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
231 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
232 gl_list_iterator_free (&iter);
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
233
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
234 /* Then cleanup the temporary directory itself. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
235 rmdir (dir->dirname);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
236 }
7044
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 }
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
239
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
240 /* Create a temporary directory.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
241 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
242 should be short and still give an indication about the program.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
243 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
244 parent directory is used (either $TMPDIR or /tmp or similar).
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
245 CLEANUP_VERBOSE determines whether errors during explicit cleanup are
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
246 reported to standard error.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
247 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
248 is shown and NULL is returned. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
249 struct temp_dir *
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
250 create_temp_dir (const char *prefix, const char *parentdir,
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
251 bool cleanup_verbose)
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
252 {
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
253 struct tempdir * volatile *tmpdirp = NULL;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
254 struct tempdir *tmpdir;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
255 size_t i;
7586
4a8b5467d8b2 Make it compile in C++ mode.
Bruno Haible <bruno@clisp.org>
parents: 7513
diff changeset
256 char *xtemplate;
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
257 char *tmpdirname;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
258
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
259 /* 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
260 already cleaned up. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
261 for (i = 0; i < cleanup_list.tempdir_count; i++)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
262 if (cleanup_list.tempdir_list[i] == NULL)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
263 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
264 tmpdirp = &cleanup_list.tempdir_list[i];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
265 break;
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
266 }
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
267 if (tmpdirp == NULL)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
268 {
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
269 /* See whether the array needs to be extended. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
270 if (cleanup_list.tempdir_count == cleanup_list.tempdir_allocated)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
271 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
272 /* Note that we cannot use xrealloc(), because then the cleanup()
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
273 function could access an already deallocated array. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
274 struct tempdir * volatile *old_array = cleanup_list.tempdir_list;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
275 size_t old_allocated = cleanup_list.tempdir_allocated;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
276 size_t new_allocated = 2 * cleanup_list.tempdir_allocated + 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
277 struct tempdir * volatile *new_array =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
278 XNMALLOC (new_allocated, struct tempdir * volatile);
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
279
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
280 if (old_allocated == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
281 /* First use of this facility. Register the cleanup handler. */
40238
3ac749aa0041 fatal-signal: Pass the signal number to the action.
Bruno Haible <bruno@clisp.org>
parents: 40235
diff changeset
282 at_fatal_signal (&cleanup_action);
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
283 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
284 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
285 /* Don't use memcpy() here, because memcpy takes non-volatile
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
286 arguments and is therefore not guaranteed to complete all
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
287 memory stores before the next statement. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
288 size_t k;
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
289
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
290 for (k = 0; k < old_allocated; k++)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
291 new_array[k] = old_array[k];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
292 }
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
293
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
294 cleanup_list.tempdir_list = new_array;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
295 cleanup_list.tempdir_allocated = new_allocated;
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
296
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
297 /* Now we can free the old array. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
298 if (old_array != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
299 free ((struct tempdir **) old_array);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
300 }
7044
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 tmpdirp = &cleanup_list.tempdir_list[cleanup_list.tempdir_count];
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
303 /* Initialize *tmpdirp before incrementing tempdir_count, so that
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
304 cleanup() will skip this entry before it is fully initialized. */
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
305 *tmpdirp = NULL;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
306 cleanup_list.tempdir_count++;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
307 }
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
308
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
309 /* Initialize a 'struct tempdir'. */
7603
23f14c284219 Simplify xmalloc expressions. Add overflow check in xmalloc arguments.
Bruno Haible <bruno@clisp.org>
parents: 7586
diff changeset
310 tmpdir = XMALLOC (struct tempdir);
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
311 tmpdir->dirname = NULL;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
312 tmpdir->cleanup_verbose = cleanup_verbose;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
313 tmpdir->subdirs = gl_list_create_empty (GL_LINKEDHASH_LIST,
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
314 string_equals, string_hash, NULL,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
315 false);
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
316 tmpdir->files = gl_list_create_empty (GL_LINKEDHASH_LIST,
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
317 string_equals, string_hash, NULL,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
318 false);
7044
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 /* Create the temporary directory. */
8948
a162347a0232 Update after allocsa -> malloca renaming.
Bruno Haible <bruno@clisp.org>
parents: 8438
diff changeset
321 xtemplate = (char *) xmalloca (PATH_MAX);
7586
4a8b5467d8b2 Make it compile in C++ mode.
Bruno Haible <bruno@clisp.org>
parents: 7513
diff changeset
322 if (path_search (xtemplate, PATH_MAX, parentdir, prefix, parentdir == NULL))
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
323 {
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
324 error (0, errno,
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
325 _("cannot find a temporary directory, try setting $TMPDIR"));
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
326 goto quit;
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 block_fatal_signals ();
7586
4a8b5467d8b2 Make it compile in C++ mode.
Bruno Haible <bruno@clisp.org>
parents: 7513
diff changeset
329 tmpdirname = mkdtemp (xtemplate);
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
330 if (tmpdirname != NULL)
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 tmpdir->dirname = tmpdirname;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
333 *tmpdirp = tmpdir;
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 unblock_fatal_signals ();
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
336 if (tmpdirname == NULL)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
337 {
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
338 error (0, errno,
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
339 _("cannot create a temporary directory using template \"%s\""),
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
340 xtemplate);
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
341 goto quit;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
342 }
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
343 /* Replace tmpdir->dirname with a copy that has indefinite extent.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
344 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
345 block because then the cleanup handler would not remove the directory
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
346 if xstrdup fails. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
347 tmpdir->dirname = xstrdup (tmpdirname);
8948
a162347a0232 Update after allocsa -> malloca renaming.
Bruno Haible <bruno@clisp.org>
parents: 8438
diff changeset
348 freea (xtemplate);
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
349 return (struct temp_dir *) tmpdir;
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 quit:
8948
a162347a0232 Update after allocsa -> malloca renaming.
Bruno Haible <bruno@clisp.org>
parents: 8438
diff changeset
352 freea (xtemplate);
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
353 return NULL;
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 /* 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
357 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 before the file ABSOLUTE_FILE_NAME is created. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
359 void
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
360 register_temp_file (struct temp_dir *dir,
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
361 const char *absolute_file_name)
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
362 {
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
363 struct tempdir *tmpdir = (struct tempdir *)dir;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
364
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
365 /* Add absolute_file_name to tmpdir->files, without duplicates. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
366 if (gl_list_search (tmpdir->files, absolute_file_name) == NULL)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
367 gl_list_add_first (tmpdir->files, xstrdup (absolute_file_name));
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
368 }
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
369
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
370 /* 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
371 needs to be removed before DIR can be removed.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
372 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
373 void
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
374 unregister_temp_file (struct temp_dir *dir,
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
375 const char *absolute_file_name)
7044
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 struct tempdir *tmpdir = (struct tempdir *)dir;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
378 gl_list_t list = tmpdir->files;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
379 gl_list_node_t node;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
380
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
381 node = gl_list_search (list, absolute_file_name);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
382 if (node != NULL)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
383 {
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
384 char *old_string = (char *) gl_list_node_value (list, node);
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 gl_list_remove_node (list, node);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
387 free (old_string);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
388 }
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
389 }
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 /* 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
392 that needs to be removed before DIR can be removed.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
393 Should be called before the subdirectory ABSOLUTE_DIR_NAME is created. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
394 void
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
395 register_temp_subdir (struct temp_dir *dir,
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
396 const char *absolute_dir_name)
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
397 {
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
398 struct tempdir *tmpdir = (struct tempdir *)dir;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
399
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
400 /* Add absolute_dir_name to tmpdir->subdirs, without duplicates. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
401 if (gl_list_search (tmpdir->subdirs, absolute_dir_name) == NULL)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
402 gl_list_add_first (tmpdir->subdirs, xstrdup (absolute_dir_name));
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
403 }
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 /* 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
406 that needs to be removed before DIR can be removed.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
407 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
408 created. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
409 void
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
410 unregister_temp_subdir (struct temp_dir *dir,
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
411 const char *absolute_dir_name)
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
412 {
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
413 struct tempdir *tmpdir = (struct tempdir *)dir;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
414 gl_list_t list = tmpdir->subdirs;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
415 gl_list_node_t node;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
416
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
417 node = gl_list_search (list, absolute_dir_name);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
418 if (node != NULL)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
419 {
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
420 char *old_string = (char *) gl_list_node_value (list, node);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
421
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
422 gl_list_remove_node (list, node);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
423 free (old_string);
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 }
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
426
7415
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7414
diff changeset
427 /* Remove a file, with optional error message.
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7414
diff changeset
428 Return 0 upon success, or -1 if there was some problem. */
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7414
diff changeset
429 static int
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
430 do_unlink (struct temp_dir *dir, const char *absolute_file_name)
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 if (unlink (absolute_file_name) < 0 && dir->cleanup_verbose
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
433 && errno != ENOENT)
7415
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7414
diff changeset
434 {
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7414
diff changeset
435 error (0, errno, _("cannot remove temporary file %s"), absolute_file_name);
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7414
diff changeset
436 return -1;
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7414
diff changeset
437 }
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7414
diff changeset
438 return 0;
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
439 }
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
440
7415
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7414
diff changeset
441 /* Remove a directory, with optional error message.
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7414
diff changeset
442 Return 0 upon success, or -1 if there was some problem. */
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7414
diff changeset
443 static int
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
444 do_rmdir (struct temp_dir *dir, const char *absolute_dir_name)
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 if (rmdir (absolute_dir_name) < 0 && dir->cleanup_verbose
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
447 && errno != ENOENT)
7415
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7414
diff changeset
448 {
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7414
diff changeset
449 error (0, errno,
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
450 _("cannot remove temporary directory %s"), absolute_dir_name);
7415
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7414
diff changeset
451 return -1;
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7414
diff changeset
452 }
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7414
diff changeset
453 return 0;
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
454 }
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
455
7415
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7414
diff changeset
456 /* Remove the given ABSOLUTE_FILE_NAME and unregister it.
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7414
diff changeset
457 Return 0 upon success, or -1 if there was some problem. */
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7414
diff changeset
458 int
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
459 cleanup_temp_file (struct temp_dir *dir,
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
460 const char *absolute_file_name)
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
461 {
7415
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7414
diff changeset
462 int err;
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7414
diff changeset
463
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7414
diff changeset
464 err = do_unlink (dir, absolute_file_name);
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
465 unregister_temp_file (dir, absolute_file_name);
7415
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7414
diff changeset
466
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7414
diff changeset
467 return err;
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
468 }
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
469
7415
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7414
diff changeset
470 /* Remove the given ABSOLUTE_DIR_NAME and unregister it.
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7414
diff changeset
471 Return 0 upon success, or -1 if there was some problem. */
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7414
diff changeset
472 int
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
473 cleanup_temp_subdir (struct temp_dir *dir,
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
474 const char *absolute_dir_name)
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
475 {
7415
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7414
diff changeset
476 int err;
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7414
diff changeset
477
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7414
diff changeset
478 err = do_rmdir (dir, absolute_dir_name);
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
479 unregister_temp_subdir (dir, absolute_dir_name);
7415
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7414
diff changeset
480
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7414
diff changeset
481 return err;
7044
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
7415
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7414
diff changeset
484 /* Remove all registered files and subdirectories inside DIR.
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7414
diff changeset
485 Return 0 upon success, or -1 if there was some problem. */
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7414
diff changeset
486 int
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
487 cleanup_temp_dir_contents (struct temp_dir *dir)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
488 {
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
489 struct tempdir *tmpdir = (struct tempdir *)dir;
7415
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7414
diff changeset
490 int err = 0;
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
491 gl_list_t list;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
492 gl_list_iterator_t iter;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
493 const void *element;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
494 gl_list_node_t node;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
495
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
496 /* First cleanup the files in the subdirectories. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
497 list = tmpdir->files;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
498 iter = gl_list_iterator (list);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
499 while (gl_list_iterator_next (&iter, &element, &node))
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
500 {
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
501 char *file = (char *) element;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
502
7415
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7414
diff changeset
503 err |= do_unlink (dir, file);
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
504 gl_list_remove_node (list, node);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
505 /* Now only we can free file. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
506 free (file);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
507 }
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
508 gl_list_iterator_free (&iter);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
509
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
510 /* Then cleanup the subdirectories. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
511 list = tmpdir->subdirs;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
512 iter = gl_list_iterator (list);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
513 while (gl_list_iterator_next (&iter, &element, &node))
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
514 {
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
515 char *subdir = (char *) element;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
516
7415
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7414
diff changeset
517 err |= do_rmdir (dir, subdir);
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
518 gl_list_remove_node (list, node);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
519 /* Now only we can free subdir. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
520 free (subdir);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
521 }
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
522 gl_list_iterator_free (&iter);
7415
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7414
diff changeset
523
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7414
diff changeset
524 return err;
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
525 }
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
526
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
527 /* Remove all registered files and subdirectories inside DIR and DIR itself.
7415
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7414
diff changeset
528 DIR cannot be used any more after this call.
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7414
diff changeset
529 Return 0 upon success, or -1 if there was some problem. */
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7414
diff changeset
530 int
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
531 cleanup_temp_dir (struct temp_dir *dir)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
532 {
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
533 struct tempdir *tmpdir = (struct tempdir *)dir;
7415
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7414
diff changeset
534 int err = 0;
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
535 size_t i;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
536
7415
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7414
diff changeset
537 err |= cleanup_temp_dir_contents (dir);
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7414
diff changeset
538 err |= do_rmdir (dir, tmpdir->dirname);
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
539
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
540 for (i = 0; i < cleanup_list.tempdir_count; i++)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
541 if (cleanup_list.tempdir_list[i] == tmpdir)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
542 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
543 /* Remove cleanup_list.tempdir_list[i]. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
544 if (i + 1 == cleanup_list.tempdir_count)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
545 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
546 while (i > 0 && cleanup_list.tempdir_list[i - 1] == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
547 i--;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
548 cleanup_list.tempdir_count = i;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
549 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
550 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
551 cleanup_list.tempdir_list[i] = NULL;
17007
2d65f823588d clean-temp: Fix memory leak.
John Darrington <john@darrington.wattle.id.au>
parents: 16922
diff changeset
552 /* Now only we can free the tmpdir->dirname, tmpdir->subdirs,
2d65f823588d clean-temp: Fix memory leak.
John Darrington <john@darrington.wattle.id.au>
parents: 16922
diff changeset
553 tmpdir->files, and tmpdir itself. */
2d65f823588d clean-temp: Fix memory leak.
John Darrington <john@darrington.wattle.id.au>
parents: 16922
diff changeset
554 gl_list_free (tmpdir->files);
2d65f823588d clean-temp: Fix memory leak.
John Darrington <john@darrington.wattle.id.au>
parents: 16922
diff changeset
555 gl_list_free (tmpdir->subdirs);
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
556 free (tmpdir->dirname);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
557 free (tmpdir);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
558 return err;
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
559 }
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
560
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
561 /* The user passed an invalid DIR argument. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
562 abort ();
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
563 }
7411
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
564
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
565
19595
beb2ad957aca Simplify code. Drop support for Borland C++ on Windows.
Bruno Haible <bruno@clisp.org>
parents: 19484
diff changeset
566 #if defined _WIN32 && ! defined __CYGWIN__
8161
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
567
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
568 /* On Windows, opening a file with _O_TEMPORARY has the effect of passing
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
569 the FILE_FLAG_DELETE_ON_CLOSE flag to CreateFile(), which has the effect
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
570 of deleting the file when it is closed - even when the program crashes.
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
571 But (according to the Cygwin sources) it works only on Windows NT or newer.
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
572 So we cache the info whether we are running on Windows NT or newer. */
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
573
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
574 static bool
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
575 supports_delete_on_close ()
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
576 {
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
577 static int known; /* 1 = yes, -1 = no, 0 = unknown */
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
578 if (!known)
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
579 {
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
580 OSVERSIONINFO v;
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
581
16922
937fc0f34278 tmpfile, clean-temp: Fix invocation of GetVersionEx.
John Darrington <john@darrington.wattle.id.au>
parents: 16201
diff changeset
582 /* According to
40235
5a52ef2d4772 all: Update URLs to msdn.microsoft.com.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
583 <https://docs.microsoft.com/en-us/windows/desktop/api/sysinfoapi/nf-sysinfoapi-getversionexa>
18287
f853b35738d6 Prefer American spelling for "initialize"
Paul Eggert <eggert@cs.ucla.edu>
parents: 18189
diff changeset
584 this structure must be initialized as follows: */
16922
937fc0f34278 tmpfile, clean-temp: Fix invocation of GetVersionEx.
John Darrington <john@darrington.wattle.id.au>
parents: 16201
diff changeset
585 v.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
937fc0f34278 tmpfile, clean-temp: Fix invocation of GetVersionEx.
John Darrington <john@darrington.wattle.id.au>
parents: 16201
diff changeset
586
8161
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
587 if (GetVersionEx (&v))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
588 known = (v.dwPlatformId == VER_PLATFORM_WIN32_NT ? 1 : -1);
8161
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
589 else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
590 known = -1;
8161
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
591 }
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
592 return (known > 0);
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
593 }
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
594
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
595 #endif
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
596
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
597
7411
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
598 /* Register a file descriptor to be closed. */
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
599 static void
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
600 register_fd (int fd)
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
601 {
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
602 if (descriptors == NULL)
8438
238942284e2f Allow the use of a destructor for the values stored in the list.
Bruno Haible <bruno@clisp.org>
parents: 8192
diff changeset
603 descriptors = gl_list_create_empty (GL_LINKEDHASH_LIST, NULL, NULL, NULL,
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
604 false);
7411
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
605 gl_list_add_first (descriptors, (void *) (uintptr_t) fd);
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
606 }
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
607
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
608 /* Unregister a file descriptor to be closed. */
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
609 static void
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
610 unregister_fd (int fd)
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
611 {
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
612 gl_list_t fds = descriptors;
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
613 gl_list_node_t node;
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
614
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
615 if (fds == NULL)
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
616 /* descriptors should already contain fd. */
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
617 abort ();
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
618 node = gl_list_search (fds, (void *) (uintptr_t) fd);
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
619 if (node == NULL)
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
620 /* descriptors should already contain fd. */
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
621 abort ();
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
622 gl_list_remove_node (fds, node);
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
623 }
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
624
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
625 /* Open a temporary file in a temporary directory.
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
626 Registers the resulting file descriptor to be closed. */
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
627 int
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
628 open_temp (const char *file_name, int flags, mode_t mode)
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
629 {
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
630 int fd;
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
631 int saved_errno;
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
632
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
633 block_fatal_signals ();
8161
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
634 /* Note: 'open' here is actually open() or open_safer(). */
19595
beb2ad957aca Simplify code. Drop support for Borland C++ on Windows.
Bruno Haible <bruno@clisp.org>
parents: 19484
diff changeset
635 #if defined _WIN32 && ! defined __CYGWIN__
8161
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
636 /* Use _O_TEMPORARY when possible, to increase the chances that the
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
637 temporary file is removed when the process crashes. */
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
638 if (supports_delete_on_close ())
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
639 fd = open (file_name, flags | _O_TEMPORARY, mode);
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
640 else
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
641 #endif
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
642 fd = open (file_name, flags, mode);
7411
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
643 saved_errno = errno;
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
644 if (fd >= 0)
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
645 register_fd (fd);
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
646 unblock_fatal_signals ();
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
647 errno = saved_errno;
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
648 return fd;
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
649 }
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
650
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
651 /* Open a temporary file in a temporary directory.
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
652 Registers the resulting file descriptor to be closed. */
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
653 FILE *
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
654 fopen_temp (const char *file_name, const char *mode)
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
655 {
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
656 FILE *fp;
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
657 int saved_errno;
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
658
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
659 block_fatal_signals ();
8161
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
660 /* Note: 'fopen' here is actually fopen() or fopen_safer(). */
19595
beb2ad957aca Simplify code. Drop support for Borland C++ on Windows.
Bruno Haible <bruno@clisp.org>
parents: 19484
diff changeset
661 #if defined _WIN32 && ! defined __CYGWIN__
8161
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
662 /* Use _O_TEMPORARY when possible, to increase the chances that the
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
663 temporary file is removed when the process crashes. */
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
664 if (supports_delete_on_close ())
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
665 {
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
666 size_t mode_len = strlen (mode);
8948
a162347a0232 Update after allocsa -> malloca renaming.
Bruno Haible <bruno@clisp.org>
parents: 8438
diff changeset
667 char *augmented_mode = (char *) xmalloca (mode_len + 2);
8161
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
668 memcpy (augmented_mode, mode, mode_len);
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
669 memcpy (augmented_mode + mode_len, "D", 2);
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
670
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
671 fp = fopen (file_name, augmented_mode);
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
672 saved_errno = errno;
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
673
8948
a162347a0232 Update after allocsa -> malloca renaming.
Bruno Haible <bruno@clisp.org>
parents: 8438
diff changeset
674 freea (augmented_mode);
8161
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
675 }
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
676 else
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
677 #endif
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
678 {
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
679 fp = fopen (file_name, mode);
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
680 saved_errno = errno;
f7359fba603e Make use of _O_TEMPORARY on native Woe32 platforms.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
681 }
7411
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
682 if (fp != NULL)
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
683 {
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
684 /* It is sufficient to register fileno (fp) instead of the entire fp,
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
685 because at cleanup time there is no need to do an fflush (fp); a
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
686 close (fileno (fp)) will be enough. */
7411
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
687 int fd = fileno (fp);
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
688 if (!(fd >= 0))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
689 abort ();
7411
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
690 register_fd (fd);
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
691 }
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
692 unblock_fatal_signals ();
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
693 errno = saved_errno;
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
694 return fp;
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
695 }
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
696
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
697 /* Close a temporary file in a temporary directory.
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
698 Unregisters the previously registered file descriptor. */
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
699 int
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
700 close_temp (int fd)
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
701 {
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
702 if (fd >= 0)
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
703 {
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
704 /* No blocking of signals is needed here, since a double close of a
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
705 file descriptor is harmless. */
7411
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
706 int result = close (fd);
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
707 int saved_errno = errno;
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
708
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
709 /* No race condition here: we assume a single-threaded program, hence
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
710 fd cannot be re-opened here. */
7411
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
711
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
712 unregister_fd (fd);
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
713
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
714 errno = saved_errno;
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
715 return result;
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
716 }
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
717 else
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
718 return close (fd);
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
719 }
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
720
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
721 /* Close a temporary file in a temporary directory.
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
722 Unregisters the previously registered file descriptor. */
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
723 int
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
724 fclose_temp (FILE *fp)
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
725 {
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
726 int fd = fileno (fp);
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
727 /* No blocking of signals is needed here, since a double close of a
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
728 file descriptor is harmless. */
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
729 int result = fclose (fp);
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
730 int saved_errno = errno;
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
731
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
732 /* No race condition here: we assume a single-threaded program, hence
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
733 fd cannot be re-opened here. */
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
734
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
735 unregister_fd (fd);
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
736
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
737 errno = saved_errno;
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
738 return result;
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
739 }
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
740
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
741 #if GNULIB_FWRITEERROR
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
742 /* Like fwriteerror.
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
743 Unregisters the previously registered file descriptor. */
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
744 int
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
745 fwriteerror_temp (FILE *fp)
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
746 {
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
747 int fd = fileno (fp);
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
748 /* No blocking of signals is needed here, since a double close of a
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
749 file descriptor is harmless. */
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
750 int result = fwriteerror (fp);
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
751 int saved_errno = errno;
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
752
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
753 /* No race condition here: we assume a single-threaded program, hence
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
754 fd cannot be re-opened here. */
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
755
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
756 unregister_fd (fd);
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
757
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
758 errno = saved_errno;
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
759 return result;
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
760 }
67235cf9199a Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
761 #endif
7417
fa9e9b096831 * clean-temp.h (close_stream_temp): New declaration.
Eric Blake <ebb9@byu.net>
parents: 7415
diff changeset
762
fa9e9b096831 * clean-temp.h (close_stream_temp): New declaration.
Eric Blake <ebb9@byu.net>
parents: 7415
diff changeset
763 #if GNULIB_CLOSE_STREAM
fa9e9b096831 * clean-temp.h (close_stream_temp): New declaration.
Eric Blake <ebb9@byu.net>
parents: 7415
diff changeset
764 /* Like close_stream.
fa9e9b096831 * clean-temp.h (close_stream_temp): New declaration.
Eric Blake <ebb9@byu.net>
parents: 7415
diff changeset
765 Unregisters the previously registered file descriptor. */
fa9e9b096831 * clean-temp.h (close_stream_temp): New declaration.
Eric Blake <ebb9@byu.net>
parents: 7415
diff changeset
766 int
fa9e9b096831 * clean-temp.h (close_stream_temp): New declaration.
Eric Blake <ebb9@byu.net>
parents: 7415
diff changeset
767 close_stream_temp (FILE *fp)
fa9e9b096831 * clean-temp.h (close_stream_temp): New declaration.
Eric Blake <ebb9@byu.net>
parents: 7415
diff changeset
768 {
fa9e9b096831 * clean-temp.h (close_stream_temp): New declaration.
Eric Blake <ebb9@byu.net>
parents: 7415
diff changeset
769 int fd = fileno (fp);
fa9e9b096831 * clean-temp.h (close_stream_temp): New declaration.
Eric Blake <ebb9@byu.net>
parents: 7415
diff changeset
770 /* No blocking of signals is needed here, since a double close of a
fa9e9b096831 * clean-temp.h (close_stream_temp): New declaration.
Eric Blake <ebb9@byu.net>
parents: 7415
diff changeset
771 file descriptor is harmless. */
fa9e9b096831 * clean-temp.h (close_stream_temp): New declaration.
Eric Blake <ebb9@byu.net>
parents: 7415
diff changeset
772 int result = close_stream (fp);
fa9e9b096831 * clean-temp.h (close_stream_temp): New declaration.
Eric Blake <ebb9@byu.net>
parents: 7415
diff changeset
773 int saved_errno = errno;
fa9e9b096831 * clean-temp.h (close_stream_temp): New declaration.
Eric Blake <ebb9@byu.net>
parents: 7415
diff changeset
774
fa9e9b096831 * clean-temp.h (close_stream_temp): New declaration.
Eric Blake <ebb9@byu.net>
parents: 7415
diff changeset
775 /* No race condition here: we assume a single-threaded program, hence
fa9e9b096831 * clean-temp.h (close_stream_temp): New declaration.
Eric Blake <ebb9@byu.net>
parents: 7415
diff changeset
776 fd cannot be re-opened here. */
fa9e9b096831 * clean-temp.h (close_stream_temp): New declaration.
Eric Blake <ebb9@byu.net>
parents: 7415
diff changeset
777
fa9e9b096831 * clean-temp.h (close_stream_temp): New declaration.
Eric Blake <ebb9@byu.net>
parents: 7415
diff changeset
778 unregister_fd (fd);
fa9e9b096831 * clean-temp.h (close_stream_temp): New declaration.
Eric Blake <ebb9@byu.net>
parents: 7415
diff changeset
779
fa9e9b096831 * clean-temp.h (close_stream_temp): New declaration.
Eric Blake <ebb9@byu.net>
parents: 7415
diff changeset
780 errno = saved_errno;
fa9e9b096831 * clean-temp.h (close_stream_temp): New declaration.
Eric Blake <ebb9@byu.net>
parents: 7415
diff changeset
781 return result;
fa9e9b096831 * clean-temp.h (close_stream_temp): New declaration.
Eric Blake <ebb9@byu.net>
parents: 7415
diff changeset
782 }
fa9e9b096831 * clean-temp.h (close_stream_temp): New declaration.
Eric Blake <ebb9@byu.net>
parents: 7415
diff changeset
783 #endif