Main Page | Namespace List | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

tmparea.c

Go to the documentation of this file.
00001 /*!
00002  * @file tmparea.c
00003  *
00004  * @brief Create and manage temporary directory area for class files,
00005  * etc.
00006  *
00007  *
00008  * @section Control
00009  *
00010  * \$URL: https://svn.apache.org/path/name/tmparea.c $ \$Id: tmparea.c 0 09/28/2005 dlydick $
00011  *
00012  * Copyright 2005 The Apache Software Foundation
00013  * or its licensors, as applicable.
00014  *
00015  * Licensed under the Apache License, Version 2.0 ("the License");
00016  * you may not use this file except in compliance with the License.
00017  * You may obtain a copy of the License at
00018  *
00019  *     http://www.apache.org/licenses/LICENSE-2.0
00020  *
00021  * Unless required by applicable law or agreed to in writing,
00022  * software distributed under the License is distributed on an
00023  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
00024  * either express or implied.
00025  *
00026  * See the License for the specific language governing permissions
00027  * and limitations under the License.
00028  *
00029  * @version \$LastChangedRevision: 0 $
00030  *
00031  * @date \$LastChangedDate: 09/28/2005 $
00032  *
00033  * @author \$LastChangedBy: dlydick $
00034  *         Original code contributed by Daniel Lydick on 09/28/2005.
00035  *
00036  * @section Reference
00037  *
00038  */
00039 
00040 #include "arch.h"
00041 ARCH_COPYRIGHT_APACHE(tmparea, c, "$URL: https://svn.apache.org/path/name/tmparea.c $ $Id: tmparea.c 0 09/28/2005 dlydick $");
00042 
00043  
00044 #include <stdlib.h>
00045 #include <string.h>
00046 #include <unistd.h>
00047 #include <sys/stat.h>
00048 
00049 #include "jvmcfg.h"
00050 #include "exit.h"
00051 #include "heap.h" 
00052 #include "util.h"
00053 
00054 
00055 /*!
00056  * @brief Initialize the class area of the JVM model
00057  *
00058  *
00059  * @b Parameters: @link #rvoid rvoid@endlink
00060  *
00061  *
00062  *       @returns @link #rvoid rvoid@endlink
00063  *
00064  */
00065 static rchar *env_tmpdir = CHEAT_AND_USE_NULL_TO_INITIALIZE;
00066 
00067 static rchar tmparea[100];
00068 
00069 rvoid tmparea_init(char **argv)
00070 {
00071     struct stat statbfr;
00072     char *argv0name = strrchr(argv[0], JVMCFG_PATHNAME_DELIMITER_CHAR);
00073 
00074     if (rnull != argv0name)
00075     {
00076         argv0name++;
00077     }
00078     else
00079     {
00080         argv0name = argv[0];
00081     }
00082 
00083     int pid = getpid();
00084 
00085     env_tmpdir = getenv("TMPDIR");
00086 
00087     if (rnull == env_tmpdir)
00088     {
00089         env_tmpdir = JVMCFG_TMPAREA_DEFAULT;
00090     }
00091 
00092     sprintfLocal(tmparea,
00093                  "%s%ctmp.%s.%d",
00094                  env_tmpdir,
00095                  JVMCFG_PATHNAME_DELIMITER_CHAR,
00096                  argv0name,
00097                  pid);
00098 
00099     int rc = mkdir(tmparea, 0755); /* Could use <sys/stat.h> constants*/
00100 
00101     /* Verify existence of directory */
00102     rc = stat(tmparea, &statbfr);
00103 
00104     if (0 != rc)
00105     {
00106         sysErrMsg("tmparea_init",
00107                   "Cannot create temp directory %s",
00108                   tmparea);
00109         exit_jvm(EXIT_TMPAREA_MKDIR);
00110 /*NOTREACHED*/
00111     }
00112     /* Declare this module initialized */
00113     jvm_tmparea_initialized = rtrue;
00114 
00115     return;
00116 
00117 } /* END of tmparea_init() */
00118 
00119 
00120 /*!
00121  * Retrieve the temporary directory area path.
00122  *
00123  *
00124  * @b Parameters: @link #rvoid rvoid@endlink
00125  *
00126  *
00127  * @returns Null-terminated string to temp area.
00128  *
00129  */
00130 const rchar *tmparea_get()
00131 {
00132     return((const rchar *) tmparea);
00133 
00134 } /* END of tmparea_get() */
00135 
00136 
00137 /*!
00138  * @brief Shut down the temporary directory area after JVM execution.
00139  *
00140  *
00141  * @b Parameters: @link #rvoid rvoid@endlink
00142  *
00143  *
00144  *       @returns @link #rvoid rvoid@endlink
00145  *
00146  */
00147 rvoid tmparea_shutdown(rvoid)
00148 {
00149 
00150 /* Normal method requires directory to be empty:
00151  *
00152  *  int rc = rmdir(tmparea);
00153  *
00154  *  if (0 != rc)
00155  *  {
00156  *      sysErrMsg("tmparea_shutdown",
00157  *                "Cannot remove temp directory %s",
00158  *                tmparea);
00159  *      exit_jvm(EXIT_TMPAREA_RMDIR);
00160 **NOTREACHED**
00161  *  }
00162  *
00163  */
00164 
00165     struct stat statbfr;
00166 
00167 /* Since there will be temp files here, cheat just a @e little bit: */
00168 
00169 
00170     rchar *rmscript =        /* format spec %s make strlen longer than
00171                                 it needs to be, but it is benign */
00172         HEAP_GET_DATA(strlen(JVMCFG_TMPAREA_REMOVE_SCRIPT) +
00173                           strlen(tmparea) +
00174                           sizeof(rchar) /* NUL byte */,
00175                       rfalse);
00176 
00177     sprintfLocal(rmscript, JVMCFG_TMPAREA_REMOVE_SCRIPT, tmparea);
00178 
00179     int rc = system(rmscript);
00180 
00181     /* Verify missing directory */
00182     rc = stat(tmparea, &statbfr);
00183 
00184     if (0 == rc)
00185     {
00186         sysErrMsg("tmparea_shutdown",
00187                   "Cannot remove temp directory %s",
00188                   tmparea);
00189         exit_jvm(EXIT_TMPAREA_RMDIR);
00190 /*NOTREACHED*/
00191     }
00192 
00193     /* Declare this module uninitialized */
00194     jvm_tmparea_initialized = rfalse;
00195 
00196     return;
00197 
00198 } /* END of tmparea_shutdown() */
00199 
00200 
00201 /* EOF */
00202 

Generated on Fri Sep 30 18:59:35 2005 by  doxygen 1.4.4