/* StarPU --- Runtime system for heterogeneous multicore architectures. * * Copyright (C) 2010, 2012-2017 Université de Bordeaux * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 CNRS * * StarPU is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or (at * your option) any later version. * * StarPU is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * * See the GNU Lesser General Public License in COPYING.LGPL for more details. */ #include #include #include #include #include #ifdef HAVE_UNISTD_H #include #endif #include #if defined(_WIN32) && !defined(__CYGWIN__) #include #include #define mkdir(path, mode) mkdir(path) #if !defined(__MINGW32__) #define ftruncate(fd, length) _chsize(fd, length) #endif #endif #ifndef O_BINARY #define O_BINARY 0 #endif #if !defined(O_DIRECT) && defined(F_NOCACHE) #define O_DIRECT F_NOCACHE #endif int _starpu_silent; void _starpu_util_init(void) { _starpu_silent = starpu_get_env_number_default("STARPU_SILENT", 0); STARPU_HG_DISABLE_CHECKING(_starpu_silent); } #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__MINGW32__) #include static char * dirname(char * path) { char drive[_MAX_DRIVE]; char dir[_MAX_DIR]; /* Remove trailing slash */ while (strlen(path) > 0 && (*(path+strlen(path)-1) == '/' || *(path+strlen(path)-1) == '\\')) *(path+strlen(path)-1) = '\0'; _splitpath(path, drive, dir, NULL, NULL); _makepath(path, drive, dir, NULL, NULL); return path; } #else #include #endif /* Function with behaviour like `mkdir -p'. This function was adapted from * http://niallohiggins.com/2009/01/08/mkpath-mkdir-p-alike-in-c-for-unix/ */ int _starpu_mkpath(const char *s, mode_t mode) { int olderrno; char *q, *r = NULL, *path = NULL, *up = NULL; int rv; rv = -1; if (strcmp(s, ".") == 0 || strcmp(s, "/") == 0 #if defined(_WIN32) /* C:/ or C:\ */ || (s[0] && s[1] == ':' && (s[2] == '/' || s[2] == '\\') && !s[3]) #endif ) return 0; if ((path = strdup(s)) == NULL) STARPU_ABORT(); if ((q = strdup(s)) == NULL) STARPU_ABORT(); if ((r = dirname(q)) == NULL) goto out; if ((up = strdup(r)) == NULL) STARPU_ABORT(); if ((_starpu_mkpath(up, mode) == -1) && (errno != EEXIST)) goto out; struct stat sb; if (stat(path, &sb) == 0) { if (!S_ISDIR(sb.st_mode)) { _STARPU_MSG("Error: %s is not a directory:\n", path); STARPU_ABORT(); } /* It already exists and is a directory. */ rv = 0; } else { if ((mkdir(path, mode) == -1) && (errno != EEXIST)) rv = -1; else rv = 0; } out: olderrno = errno; if (up) free(up); free(q); free(path); errno = olderrno; return rv; } void _starpu_mkpath_and_check(const char *path, mode_t mode) { int ret; ret = _starpu_mkpath(path, mode); if (ret == -1 && errno != EEXIST) { _STARPU_MSG("Error making StarPU directory %s:\n", path); perror("mkdir"); STARPU_ABORT(); } } char *_starpu_mkdtemp_internal(char *tmpl) { int len = (int)strlen(tmpl); int i; int ret; struct stat sb; // Initialize template for(i=len-6 ; iworker_is_initialized) _STARPU_DISP( "getenv should not be called from running workers, only for main() or worker initialization, since it is not reentrant\n"); #endif #endif return getenv(str); }