core.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #define _XOPEN_SOURCE 500
  2. #include <ftw.h>
  3. #include "core.h"
  4. #include "lmdb.h"
  5. /*
  6. * The message corresponding to the rc is found by
  7. * warning_msg[rc - LSUP_MIN_WARNING].
  8. */
  9. char *warning_msg[] = {
  10. "No action or change of state occurred.",
  11. "No result.",
  12. "End of the loop reached.",
  13. "A resource conflict prevented some actions from being completed.",
  14. };
  15. /*
  16. * The message corresponding to the rc is found by
  17. * err_msg[rc - LSUP_MIN_ERROR].
  18. */
  19. char *err_msg[] = {
  20. "LSUP runtime error.",
  21. "Error parsing input.",
  22. "Invalid input.",
  23. "MDB transaction error.",
  24. "Database error.",
  25. "Not implemented.",
  26. "Input/Output error.",
  27. "Memory error.",
  28. "A resource conflict resulted in an invalid state.",
  29. };
  30. int mkdir_p(const char *path, mode_t mode)
  31. {
  32. /* Adapted from http://stackoverflow.com/a/2336245/119527 */
  33. const size_t len = strlen(path);
  34. char _path[PATH_MAX];
  35. char *p;
  36. errno = 0;
  37. /* Copy string so its mutable */
  38. if (len > sizeof(_path)-1) {
  39. errno = ENAMETOOLONG;
  40. return -1;
  41. }
  42. strcpy(_path, path);
  43. /* Iterate the string */
  44. for (p = _path + 1; *p; p++) {
  45. if (*p == '/') {
  46. /* Temporarily truncate */
  47. *p = '\0';
  48. if (mkdir(_path, mode) != 0) {
  49. if (errno != EEXIST)
  50. return -1;
  51. }
  52. *p = '/';
  53. }
  54. }
  55. if (mkdir(_path, mode) != 0) {
  56. if (errno != EEXIST)
  57. return -1;
  58. }
  59. return 0;
  60. }
  61. int
  62. unlink_cb(
  63. const char *fpath, const struct stat *sb, int typeflag,
  64. struct FTW *ftwbuf)
  65. {
  66. log_debug ("Removing %s", fpath);
  67. int rv = remove(fpath);
  68. if (rv)
  69. perror(fpath);
  70. return rv;
  71. }
  72. int rm_r(const char *path)
  73. { return nftw(path, unlink_cb, 64, FTW_DEPTH | FTW_PHYS); }
  74. const char *
  75. LSUP_strerror (LSUP_rc rc)
  76. {
  77. if (rc >= LSUP_MIN_ERROR && rc <= LSUP_MAX_ERROR)
  78. return err_msg[rc - LSUP_MIN_ERROR];
  79. if (rc >= LSUP_MIN_WARNING && rc <= LSUP_MAX_WARNING)
  80. return warning_msg[rc - LSUP_MIN_WARNING];
  81. return mdb_strerror (rc);
  82. }
  83. /* Inline extern functions. */
  84. int utf8_encode(const uint32_t utf, unsigned char *out);