From 3b252585d071820e9d4811c2d77776697221042f Mon Sep 17 00:00:00 2001 From: Bill Wendling Date: Tue, 22 May 2001 18:27:38 -0500 Subject: [svn-r3936] Purpose: Feature Add Description: Added the feature (not a bug, a FEATURE!) that, if the person has the env variables USER or LOGIN set, then it will place the temporary files in the "/tmp/$USER" or "/tmp/$LOGIN" directory (in that order). This is only if the prefix the user gives is the default one "/tmp". After the tests are finished, it will remove the directory for the user. Platforms tested: Linux --- test/h5test.c | 191 +++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 142 insertions(+), 49 deletions(-) diff --git a/test/h5test.c b/test/h5test.c index 0d17f18..c8a7ed5 100644 --- a/test/h5test.c +++ b/test/h5test.c @@ -128,11 +128,14 @@ h5_cleanup(const char *base_name[], hid_t fapl) #endif /* H5_WANT_H5_V1_2_COMPAT */ if (!getenv("HDF5_NOCLEANUP")) { - for (i=0; base_name[i]; i++) { - if (NULL==h5_fixname(base_name[i], fapl, filename, - sizeof filename)) { + char *user, *login; + + user = getenv("USER"); + login = getenv("LOGIN"); + + for (i = 0; base_name[i]; i++) { + if (h5_fixname(base_name[i], fapl, filename, sizeof(filename)) == NULL) continue; - } #ifdef H5_WANT_H5_V1_2_COMPAT switch (H5Pget_driver(fapl)) { @@ -140,18 +143,22 @@ h5_cleanup(const char *base_name[], hid_t fapl) break; /*nothing to remove*/ case H5F_LOW_SPLIT: - HDsnprintf(temp, sizeof temp, "%s.raw", filename); + HDsnprintf(temp, sizeof(temp), "%s.raw", filename); remove(temp); - HDsnprintf(temp, sizeof temp, "%s.meta", filename); + HDsnprintf(temp, sizeof(temp), "%s.meta", filename); remove(temp); break; case H5F_LOW_FAMILY: - for (j=0; /*void*/; j++) { - HDsnprintf(temp, sizeof temp, filename, j); - if (access(temp, F_OK)<0) break; + for (j = 0; /*void*/; j++) { + HDsnprintf(temp, sizeof(temp), filename, j); + + if (access(temp, F_OK) < 0) + break; + remove(temp); } + break; default: @@ -159,19 +166,25 @@ h5_cleanup(const char *base_name[], hid_t fapl) break; } #else /* H5_WANT_H5_V1_2_COMPAT */ + driver = H5Pget_driver(fapl); - if (H5FD_FAMILY==driver) { - for (j=0; /*void*/; j++) { + + if (driver == H5FD_FAMILY) { + for (j = 0; /*void*/; j++) { HDsnprintf(temp, sizeof temp, filename, j); - if (access(temp, F_OK)<0) break; + + if (access(temp, F_OK) < 0) + break; + remove(temp); } - } else if (H5FD_CORE==driver) { + } else if (driver == H5FD_CORE) { /*void*/ - } else if (H5FD_MULTI==driver) { + } else if (driver == H5FD_MULTI) { H5FD_mem_t mt; assert(strlen(multi_letters)==H5FD_MEM_NTYPES); - for (mt=H5FD_MEM_DEFAULT; mt=size) { - return NULL; /*buffer is too small*/ + char *subdir; + + if (!strcmp(prefix, "/tmp")) { + /* If the prefix specifies the "/tmp" directory, then default to + * using the "/tmp/$USER" or "/tmp/$LOGIN" directory instead. */ + char *user, *login; + + user = getenv("USER"); + login = getenv("LOGIN"); + subdir = (user ? user : login); + + if (subdir) { + for (i = 0; i < size && prefix[i]; i++) + fullname[i] = prefix[i]; + + fullname[i++] = '/'; + + for (j = 0; i < size && subdir[j]; i++, j++) + fullname[i] = subdir[j]; + } + } + + if (!fullname[0]) + /* We didn't append the prefix yet */ + strncpy(fullname, prefix, MIN(strlen(prefix), size)); + + if (strlen(fullname) + strlen(base_name) + 1 < size) { + /* Append the base_name with a slash first. Multiple slashes are + * handled below. */ + struct stat buf; + + if (stat(fullname, &buf) < 0) + /* The directory doesn't exist just yet */ + if (mkdir(fullname, 0755) < 0 && errno != EEXIST) + /* We couldn't make the "/tmp/${USER,LOGIN}" subdirectory. + * Default to PREFIX's original prefix value. */ + strcpy(fullname, prefix); + + strcat(fullname, "/"); + strcat(fullname, base_name); + } else { + /* Buffer is too small */ + return NULL; + } + } else if (strlen(base_name) >= size) { + /* Buffer is too small */ + return NULL; } else { strcpy(fullname, base_name); } #ifdef H5_WANT_H5_V1_2_COMPAT /* Append a suffix */ - if ((driver=H5Pget_driver(fapl))<0) return NULL; + if ((driver = H5Pget_driver(fapl)) < 0) + return NULL; + switch (driver) { case H5F_LOW_SPLIT: case H5F_LOW_CORE: @@ -360,11 +442,22 @@ h5_fixname(const char *base_name, hid_t fapl, char *fullname, size_t size) break; } #endif /* H5_WANT_H5_V1_2_COMPAT */ + /* Append a suffix */ if (suffix) { - if (strlen(fullname)+strlen(suffix)>=size) return NULL; + if (strlen(fullname) + strlen(suffix) >= size) + return NULL; + strcat(fullname, suffix); } + + /* Remove any double slashes in the filename */ + for (ptr = fullname, i = j = 0; ptr && i < size; i++, ptr++) { + if (*ptr != '/' || last != '/') + fullname[j++] = *ptr; + + last = *ptr; + } return fullname; } -- cgit v0.12