Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

os_tmpdir.c

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 1998-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: os_tmpdir.c,v 12.1 2005/06/16 20:23:26 bostic Exp $
00008  */
00009 
00010 #include "db_config.h"
00011 
00012 #ifndef NO_SYSTEM_INCLUDES
00013 #include <sys/types.h>
00014 
00015 #include <stdlib.h>
00016 #endif
00017 
00018 #include "db_int.h"
00019 
00020 #ifdef macintosh
00021 #include <TFileSpec.h>
00022 #endif
00023 
00024 /*
00025  * __os_tmpdir --
00026  *      Set the temporary directory path.
00027  *
00028  * The order of items in the list structure and the order of checks in
00029  * the environment are documented.
00030  *
00031  * PUBLIC: int __os_tmpdir __P((DB_ENV *, u_int32_t));
00032  */
00033 int
00034 __os_tmpdir(dbenv, flags)
00035         DB_ENV *dbenv;
00036         u_int32_t flags;
00037 {
00038         int isdir;
00039 
00040         /*
00041          * !!!
00042          * Don't change this to:
00043          *
00044          *      static const char * const list[]
00045          *
00046          * because it creates a text relocation in position independent code.
00047          */
00048         static const char * list[] = {
00049                 "/var/tmp",
00050                 "/usr/tmp",
00051                 "/temp",                /* Windows. */
00052                 "/tmp",
00053                 "C:/temp",              /* Windows. */
00054                 "C:/tmp",               /* Windows. */
00055                 NULL
00056         };
00057         const char * const *lp, *p;
00058 
00059         /* Use the environment if it's permitted and initialized. */
00060         if (LF_ISSET(DB_USE_ENVIRON) ||
00061             (LF_ISSET(DB_USE_ENVIRON_ROOT) && __os_isroot())) {
00062                 if ((p = getenv("TMPDIR")) != NULL && p[0] == '\0') {
00063                         __db_err(dbenv, "illegal TMPDIR environment variable");
00064                         return (EINVAL);
00065                 }
00066                 /* Windows */
00067                 if (p == NULL && (p = getenv("TEMP")) != NULL && p[0] == '\0') {
00068                         __db_err(dbenv, "illegal TEMP environment variable");
00069                         return (EINVAL);
00070                 }
00071                 /* Windows */
00072                 if (p == NULL && (p = getenv("TMP")) != NULL && p[0] == '\0') {
00073                         __db_err(dbenv, "illegal TMP environment variable");
00074                         return (EINVAL);
00075                 }
00076                 /* Macintosh */
00077                 if (p == NULL &&
00078                     (p = getenv("TempFolder")) != NULL && p[0] == '\0') {
00079                         __db_err(dbenv,
00080                             "illegal TempFolder environment variable");
00081                         return (EINVAL);
00082                 }
00083                 if (p != NULL)
00084                         return (__os_strdup(dbenv, p, &dbenv->db_tmp_dir));
00085         }
00086 
00087 #ifdef macintosh
00088         /* Get the path to the temporary folder. */
00089         {FSSpec spec;
00090 
00091                 if (!Special2FSSpec(kTemporaryFolderType,
00092                     kOnSystemDisk, 0, &spec))
00093                         return (__os_strdup(dbenv,
00094                             FSp2FullPath(&spec), &dbenv->db_tmp_dir));
00095         }
00096 #endif
00097 #ifdef DB_WIN32
00098         /* Get the path to the temporary directory. */
00099         {
00100                 int ret;
00101                 _TCHAR tpath[MAXPATHLEN + 1];
00102                 char *path, *eos;
00103 
00104                 if (GetTempPath(MAXPATHLEN, tpath) > 2) {
00105                         FROM_TSTRING(dbenv, tpath, path, ret);
00106                         if (ret != 0)
00107                                 return (ret);
00108                         eos = path + strlen(path) - 1;
00109                         if (*eos == '\\' || *eos == '/')
00110                                 *eos = '\0';
00111                         if (__os_exists(path, &isdir) == 0 && isdir) {
00112                                 ret = __os_strdup(dbenv,
00113                                     path, &dbenv->db_tmp_dir);
00114                                 FREE_STRING(dbenv, path);
00115                                 return (ret);
00116                         }
00117                         FREE_STRING(dbenv, path);
00118                 }
00119         }
00120 #endif
00121 
00122         /* Step through the static list looking for a possibility. */
00123         for (lp = list; *lp != NULL; ++lp)
00124                 if (__os_exists(*lp, &isdir) == 0 && isdir != 0)
00125                         return (__os_strdup(dbenv, *lp, &dbenv->db_tmp_dir));
00126         return (0);
00127 }

Generated on Sun Dec 25 12:14:42 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2