2
2
#include <sys/syslimits.h>
3
3
#endif
4
4
5
- static char _clar_path [CLAR_MAX_PATH ];
5
+ /*
6
+ * The tempdir is the temporary directory for the entirety of the clar
7
+ * process execution. The sandbox is an individual temporary directory
8
+ * for the execution of an individual test. Sandboxes are deleted
9
+ * entirely after test execution to avoid pollution across tests.
10
+ */
11
+
12
+ static char _clar_tempdir [CLAR_MAX_PATH ];
13
+ size_t _clar_tempdir_len ;
14
+
15
+ static char _clar_sandbox [CLAR_MAX_PATH ];
6
16
7
17
static int
8
18
is_valid_tmp_path (const char * path )
@@ -108,17 +118,17 @@ static int canonicalize_tmp_path(char *buffer)
108
118
#endif
109
119
}
110
120
111
- static void clar_unsandbox (void )
121
+ static void clar_tempdir_shutdown (void )
112
122
{
113
- if (_clar_path [0 ] == '\0' )
123
+ if (_clar_tempdir [0 ] == '\0' )
114
124
return ;
115
125
116
126
cl_must_pass (chdir (".." ));
117
127
118
- fs_rm (_clar_path );
128
+ fs_rm (_clar_tempdir );
119
129
}
120
130
121
- static int build_sandbox_path (void )
131
+ static int build_tempdir_path (void )
122
132
{
123
133
#ifdef CLAR_TMPDIR
124
134
const char path_tail [] = CLAR_TMPDIR "_XXXXXX" ;
@@ -128,57 +138,116 @@ static int build_sandbox_path(void)
128
138
129
139
size_t len ;
130
140
131
- if (find_tmp_path (_clar_path , sizeof (_clar_path )) < 0 ||
132
- canonicalize_tmp_path (_clar_path ) < 0 )
141
+ if (find_tmp_path (_clar_tempdir , sizeof (_clar_tempdir )) < 0 ||
142
+ canonicalize_tmp_path (_clar_tempdir ) < 0 )
133
143
return -1 ;
134
144
135
- len = strlen (_clar_path );
145
+ len = strlen (_clar_tempdir );
136
146
137
147
if (len + strlen (path_tail ) + 1 > CLAR_MAX_PATH )
138
148
return -1 ;
139
149
140
- if (_clar_path [len - 1 ] != '/' )
141
- _clar_path [len ++ ] = '/' ;
150
+ if (_clar_tempdir [len - 1 ] != '/' )
151
+ _clar_tempdir [len ++ ] = '/' ;
142
152
143
- strncpy (_clar_path + len , path_tail , sizeof (_clar_path ) - len );
153
+ strncpy (_clar_tempdir + len , path_tail , sizeof (_clar_tempdir ) - len );
144
154
145
155
#if defined(__MINGW32__ )
146
- if (_mktemp (_clar_path ) == NULL )
156
+ if (_mktemp (_clar_tempdir ) == NULL )
147
157
return -1 ;
148
158
149
- if (mkdir (_clar_path , 0700 ) != 0 )
159
+ if (mkdir (_clar_tempdir , 0700 ) != 0 )
150
160
return -1 ;
151
161
#elif defined(_WIN32 )
152
- if (_mktemp_s (_clar_path , sizeof (_clar_path )) != 0 )
162
+ if (_mktemp_s (_clar_tempdir , sizeof (_clar_tempdir )) != 0 )
153
163
return -1 ;
154
164
155
- if (mkdir (_clar_path , 0700 ) != 0 )
165
+ if (mkdir (_clar_tempdir , 0700 ) != 0 )
156
166
return -1 ;
157
167
#elif defined(__sun ) || defined(__TANDEM )
158
- if (mktemp (_clar_path ) == NULL )
168
+ if (mktemp (_clar_tempdir ) == NULL )
159
169
return -1 ;
160
170
161
- if (mkdir (_clar_path , 0700 ) != 0 )
171
+ if (mkdir (_clar_tempdir , 0700 ) != 0 )
162
172
return -1 ;
163
173
#else
164
- if (mkdtemp (_clar_path ) == NULL )
174
+ if (mkdtemp (_clar_tempdir ) == NULL )
165
175
return -1 ;
166
176
#endif
167
177
178
+ _clar_tempdir_len = strlen (_clar_tempdir );
168
179
return 0 ;
169
180
}
170
181
171
- static void clar_sandbox (void )
182
+ static void clar_tempdir_init (void )
183
+ {
184
+ if (_clar_tempdir [0 ] == '\0' && build_tempdir_path () < 0 )
185
+ clar_abort ("Failed to build tempdir path.\n" );
186
+
187
+ if (chdir (_clar_tempdir ) != 0 )
188
+ clar_abort ("Failed to change into tempdir '%s': %s.\n" ,
189
+ _clar_tempdir , strerror (errno ));
190
+ }
191
+
192
+ static void append (char * dst , const char * src )
193
+ {
194
+ char * d ;
195
+ const char * s ;
196
+
197
+ for (d = dst ; * d ; d ++ )
198
+ ;
199
+
200
+ for (s = src ; * s ; d ++ , s ++ )
201
+ if (* s == ':' )
202
+ * d = '_' ;
203
+ else
204
+ * d = * s ;
205
+
206
+ * d = '\0' ;
207
+ }
208
+
209
+ static int clar_sandbox_create (const char * suite_name , const char * test_name )
172
210
{
173
- if (_clar_path [0 ] == '\0' && build_sandbox_path () < 0 )
174
- clar_abort ("Failed to build sandbox path.\n" );
211
+ cl_assert (_clar_sandbox [0 ] == '\0' );
175
212
176
- if (chdir (_clar_path ) != 0 )
177
- clar_abort ("Failed to change into sandbox directory '%s': %s.\n" ,
178
- _clar_path , strerror (errno ));
213
+ cl_assert (strlen (_clar_tempdir ) + strlen (suite_name ) + strlen (test_name ) + 2 < CLAR_MAX_PATH );
214
+
215
+ strcpy (_clar_sandbox , _clar_tempdir );
216
+ _clar_sandbox [_clar_tempdir_len ] = '/' ;
217
+ _clar_sandbox [_clar_tempdir_len + 1 ] = '\0' ;
218
+
219
+ append (_clar_sandbox , suite_name );
220
+ append (_clar_sandbox , "__" );
221
+ append (_clar_sandbox , test_name );
222
+
223
+ if (mkdir (_clar_sandbox , 0700 ) != 0 )
224
+ return -1 ;
225
+
226
+ if (chdir (_clar_sandbox ) != 0 )
227
+ return -1 ;
228
+
229
+ return 0 ;
230
+ }
231
+
232
+ static int clar_sandbox_cleanup (void )
233
+ {
234
+ cl_assert (_clar_sandbox [0 ] != '\0' );
235
+
236
+ fs_rm (_clar_sandbox );
237
+ _clar_sandbox [0 ] = '\0' ;
238
+
239
+ if (chdir (_clar_tempdir ) != 0 )
240
+ return -1 ;
241
+
242
+ return 0 ;
243
+ }
244
+
245
+ const char * clar_tempdir_path (void )
246
+ {
247
+ return _clar_tempdir ;
179
248
}
180
249
181
250
const char * clar_sandbox_path (void )
182
251
{
183
- return _clar_path ;
252
+ return _clar_sandbox ;
184
253
}
0 commit comments