utils.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2010 (see AUTHORS file)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <starpu.h>
  17. #include <common/config.h>
  18. #include <common/utils.h>
  19. #include <libgen.h>
  20. /* Function with behaviour like `mkdir -p'. This function was adapted from
  21. * http://niallohiggins.com/2009/01/08/mkpath-mkdir-p-alike-in-c-for-unix/ */
  22. int starpu_mkpath(const char *s, mode_t mode)
  23. {
  24. char *q, *r = NULL, *path = NULL, *up = NULL;
  25. int rv;
  26. rv = -1;
  27. if (strcmp(s, ".") == 0 || strcmp(s, "/") == 0)
  28. return 0;
  29. if ((path = strdup(s)) == NULL)
  30. STARPU_ABORT();
  31. if ((q = strdup(s)) == NULL)
  32. STARPU_ABORT();
  33. if ((r = dirname(q)) == NULL)
  34. goto out;
  35. if ((up = strdup(r)) == NULL)
  36. STARPU_ABORT();
  37. if ((starpu_mkpath(up, mode) == -1) && (errno != EEXIST))
  38. goto out;
  39. if ((mkdir(path, mode) == -1) && (errno != EEXIST))
  40. rv = -1;
  41. else
  42. rv = 0;
  43. out:
  44. if (up)
  45. free(up);
  46. free(q);
  47. free(path);
  48. return rv;
  49. }