Welcome to mirror list, hosted at ThFree Co, Russian Federation.

tst_tmpdir.c « lib « libltp « testsuite « winsup - cygwin.com/git/newlib-cygwin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cd9d9c8b827914ab4de3a842465f1e61d0cbe1cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
 * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
 * 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 * 
 * This program is distributed in the hope that it would be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * 
 * Further, this software is distributed without any warranty that it is
 * free of the rightful claim of any third person regarding infringement
 * or the like.  Any license provided herein, whether implied or
 * otherwise, applies only to this software file.  Patent licenses, if
 * any, provided herein do not apply to combinations of this program with
 * other software, or any other product whatsoever.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write the Free Software Foundation, Inc., 59
 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
 * 
 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
 * Mountain View, CA  94043, or:
 * 
 * http://www.sgi.com 
 * 
 * For further information regarding this notice, see: 
 * 
 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
 */

/* $Id$ */

/**********************************************************
 *
 *    OS Testing - Silicon Graphics, Inc.
 *
 *    FUNCTION NAME     : tst_tmpdir, tst_rmdir
 *
 *    FUNCTION TITLE    : Create/remove a testing temp dir
 *
 *    SYNOPSIS:
 *      void tst_tmpdir();
 *      void tst_rmdir();
 *
 *    AUTHOR            : Dave Fenner
 *
 *    INITIAL RELEASE   : UNICOS 8.0
 *
 *    DESCRIPTION
 *      tst_tmpdir() is used to create a unique, temporary testing
 *      directory, and make it the current working directory.
 *      tst_rmdir() is used to remove the directory created by
 *      tst_tmpdir().
 *
 *      Setting the env variable "TDIRECTORY" will override the creation
 *      of a new temp dir.  The directory specified by TDIRECTORY will
 *      be used as the temporary directory, and no removal will be done
 *      in tst_rmdir().
 *
 *    RETURN VALUE
 *      Neither tst_tmpdir() or tst_rmdir() has a return value.
 *
 *#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#**/
#include <errno.h>
#include <sys/types.h>     /* for mkdir() */
#include <sys/stat.h>      /* for mkdir() */
#include "test.h"
#include "rmobj.h"

/*
 * Define some useful macros.
 */
#define PREFIX_SIZE     4
#define STRING_SIZE     256
#define DIR_MODE        0777  /* mode of tmp dir that will be created */

#ifndef PATH_MAX
#ifdef MAXPATHLEN
#define PATH_MAX MAXPATHLEN
#else
#define PATH_MAX 1024
#endif
#endif

/*
 * Define function prototypes.
 */
static void tmpdir_cleanup();

/*
 * Define global variables.
 */
extern char *TCID;            /* defined/initialized in main() */
extern int  TST_TOTAL;        /* defined/initialized in main() */
extern char *TESTDIR;         /* the directory created; defined in */
                              /* tst_res.c */

/*
 * tst_tmpdir() - Create a unique temporary directory and chdir() to it.
 *                It expects the caller to have defined/initialized the
 *                TCID/TST_TOTAL global variables.  The TESTDIR global
 *                variable will be set to the directory that gets used
 *                as the testing directory.
 *
 *                NOTE: This function must be called BEFORE any activity
 *                that would require CLEANUP.  If tst_tmpdir() fails, it
 *                cleans up afer itself and calls tst_exit() (i.e. does
 *                not return).
 */
#undef   FN_NAME
#define  FN_NAME  "tst_tmpdir()"

void
tst_tmpdir()
{
   char prefix[PREFIX_SIZE];     /* first three characters from TCID */
   int  no_cleanup = 0;          /* !0 means TDIRECTORY env var was set */

   /*
    * If the TDIRECTORY env variable is not set, a temp dir will be
    * created.
    */
   if ( (TESTDIR = getenv(TDIRECTORY)) == NULL ) {
      /*
       * Create a unique name based on the first three characters of the
       * TCID.  The last byte in "prefix" is for the null.
       */
      strncpy(prefix, TCID, PREFIX_SIZE - 1);
      prefix[PREFIX_SIZE-1] = '\0';
      if ( (TESTDIR = tempnam(TEMPDIR, prefix)) == NULL )
         tst_brkm(TBROK, tmpdir_cleanup, "%s: tempnam(%s, %s) failed",
                  FN_NAME, TEMPDIR, prefix);

      /*
       * Create the temporary directory.
       */
      if ( mkdir(TESTDIR, DIR_MODE) == -1 )
         tst_brkm(TBROK, tmpdir_cleanup,
                  "%s: mkdir(%s, %#o) failed; errno = %d: %s", 
                  FN_NAME, TESTDIR, DIR_MODE, errno, strerror(errno));

      /*
       * Change the group on this temporary directory to be that of the
       * gid of the person running the tests.
       */
      if ( chown(TESTDIR, -1, getgid()) == -1 )
         tst_brkm(TBROK, tmpdir_cleanup,
                  "chown(%s, -1, %d) failed; errno = %d: %s", 
                  TESTDIR, getgid(), errno, strerror(errno));
   } else {
      /*
       * The TDIRECTORY env. variable is set, so no temp dir is created.
       * Also, no clean up will be done via tst_rmdir().
       */
      no_cleanup++;
#if UNIT_TEST
      printf("TDIRECTORY env var is set\n");
#endif
   }

#if UNIT_TEST
      printf("TESTDIR = %s\n", TESTDIR);
#endif

   /*
    * Change to the temporary directory.  If the chdir() fails, issue
    * TBROK messages for all test cases, attempt to remove the
    * directory (if it was created), and exit.  If the removal also
    * fails, also issue a TWARN message.   
    */
   if ( chdir(TESTDIR) == -1 ) {
      tst_brkm(TBROK, NULL, "%s: chdir(%s) failed; errno = %d: %s",
               FN_NAME, TESTDIR, errno, strerror(errno) );

      /* Try to remove the directory */
      if ( !no_cleanup && rmdir(TESTDIR) == -1 )
         tst_resm(TWARN, "%s: rmdir(%s) failed; errno = %d: %s",
                  FN_NAME, TESTDIR, errno, strerror(errno) );

      tmpdir_cleanup();
   }

#if UNIT_TEST
   printf("CWD is %s\n", getcwd((char *)NULL, PATH_MAX));
#endif

   /*
    *  If we made through all this stuff, return.
    */
   return;
}  /* tst_tmpdir() */


/*
 *
 * tst_rmdir() - Recursively remove the temporary directory created by
 *               tst_tmpdir().  This function is intended ONLY as a
 *               companion to tst_tmpdir().  If the TDIRECTORY
 *               environment variable is set, no cleanup will be
 *               attempted.
 */ 
#undef   FN_NAME
#define  FN_NAME  "tst_rmdir()"

void
tst_rmdir()
{
   char *errmsg;
   char *tdirectory;
   char current_dir[PATH_MAX];   /* current working directory */
   char parent_dir[PATH_MAX];    /* directory above TESTDIR */
   char *basename;               /* basename of the TESTDIR */

   /*
    * If the TDIRECTORY env variable is set, this indicates that no
    * temp dir was created by tst_tmpdir().  Thus no cleanup will be
    * necessary.
    */
   if ( (tdirectory = getenv(TDIRECTORY)) != NULL ) {
#if UNIT_TEST
      printf("\"TDIRECORY\" env variable is set; no cleanup was performed\n");
#endif
      return;
   }
   
   /*
    * Check that TESTDIR is not NULL.
    */
   if ( TESTDIR == NULL ) {
      tst_resm(TWARN, "%s: TESTDIR was NULL; no removal attempted",
               FN_NAME);
      return;
   }

   /*
    * Check that the value of TESTDIR is not "*" or "/".  These could
    * have disastrous effects in a test run by root.
    */
   if ( strcmp(TESTDIR, "/") == 0 ) {
      tst_resm(TWARN,
               "%s: Recursive remove of root directory not attempted",
               FN_NAME);
      return;
   }

   if ( strchr(TESTDIR, '*') != NULL ) {
      tst_resm(TWARN, "%s: Recursive remove of '*' not attempted",
               FN_NAME);
      return;
   }

   /*
    * Get the directory name of TESTDIR.  If TESTDIR is a relative path,
    * get full path.
    */
   if ( TESTDIR[0] != '/' ) {
      if ( getcwd(current_dir,PATH_MAX) == NULL )
         strcpy(parent_dir, TESTDIR);
      else
         sprintf(parent_dir, "%s/%s", current_dir, TESTDIR);
   } else {
      strcpy(parent_dir, TESTDIR);
   }
   if ( (basename = strrchr(parent_dir, '/')) != NULL ) {
      *basename='\0';   /* terminate at end of parent_dir */
   }

   /*
    * Change directory to parent_dir (The dir above TESTDIR).
    */
   if ( chdir(parent_dir) != 0 )
      tst_resm(TWARN,
               "%s: chdir(%s) failed; errno = %d: %s\nAttempting to remove temp dir anyway",
               FN_NAME, parent_dir, errno, strerror(errno));
   
   /*
    * Attempt to remove the "TESTDIR" directory, using rmobj().
    */
   if ( rmobj(TESTDIR, &errmsg) == -1 )
      tst_resm(TWARN, "%s: rmobj(%s) failed: %s",
               FN_NAME, TESTDIR, errmsg);

   return;
}  /* tst_rmdir() */


/*
 * tmpdir_cleanup() - This function is used when tst_tmpdir()
 *                    encounters an error, and must cleanup and exit.
 *                    It prints a warning message via tst_resm(), and
 *                    then calls tst_exit().
 */
#undef  FN_NAME
#define FN_NAME "tst_tmpdir()"

static void
tmpdir_cleanup()
{
   /*
    * Print a warning message and call tst_exit() to exit the test.
    */
   tst_resm(TWARN, "%s: No user cleanup function called before exiting",
            FN_NAME);
   tst_exit();
}  /* tmpdir_cleanup() */


#ifdef UNIT_TEST
/****************************************************************************
 * Unit test code: Takes input from stdin and can make the following
 *                 calls: tst_tmpdir(), tst_rmdir().
 ****************************************************************************/
int  TST_TOTAL = 10;
char *TCID = "TESTTCID";

main()
{
   int  option;
   char *chrptr;

   printf("UNIT TEST of tst_tmpdir.c.  Options to try:\n\
   -1 : call tst_exit()\n\
    0 : call tst_tmpdir()\n\
    1 : call tst_rmdir()\n\n");

   while ( 1 ) {
      printf("Enter options (-1, 0, 1): ");
      (void) scanf("%d%c", &option, &chrptr);

      switch ( option ) {
      case -1:
         tst_exit();
         break;

      case 0:
         tst_tmpdir();
         break;

      case 1:
         tst_rmdir();
         break;
      }  /* switch() */
   }  /* while() */
}
#endif  /* UNIT_TEST */