core.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. };
  14. /*
  15. * The message corresponding to the rc is found by
  16. * err_msg[rc - LSUP_MIN_ERROR].
  17. */
  18. char *err_msg[] = {
  19. "LSUP runtime error.",
  20. "Error parsing input.",
  21. "Invalid input.",
  22. "MDB transaction error.",
  23. "Database error.",
  24. "Not implemented.",
  25. "Input/Output error.",
  26. "Memory error.",
  27. };
  28. int mkdir_p(const char *path, mode_t mode)
  29. {
  30. /* Adapted from http://stackoverflow.com/a/2336245/119527 */
  31. const size_t len = strlen(path);
  32. char _path[PATH_MAX];
  33. char *p;
  34. errno = 0;
  35. /* Copy string so its mutable */
  36. if (len > sizeof(_path)-1) {
  37. errno = ENAMETOOLONG;
  38. return -1;
  39. }
  40. strcpy(_path, path);
  41. /* Iterate the string */
  42. for (p = _path + 1; *p; p++) {
  43. if (*p == '/') {
  44. /* Temporarily truncate */
  45. *p = '\0';
  46. if (mkdir(_path, mode) != 0) {
  47. if (errno != EEXIST)
  48. return -1;
  49. }
  50. *p = '/';
  51. }
  52. }
  53. if (mkdir(_path, mode) != 0) {
  54. if (errno != EEXIST)
  55. return -1;
  56. }
  57. return 0;
  58. }
  59. int
  60. unlink_cb(
  61. const char *fpath, const struct stat *sb, int typeflag,
  62. struct FTW *ftwbuf)
  63. {
  64. log_debug ("Removing %s", fpath);
  65. int rv = remove(fpath);
  66. if (rv)
  67. perror(fpath);
  68. return rv;
  69. }
  70. int rm_r(const char *path)
  71. { return nftw(path, unlink_cb, 64, FTW_DEPTH | FTW_PHYS); }
  72. const char *
  73. LSUP_strerror (LSUP_rc rc)
  74. {
  75. if (rc >= LSUP_MIN_ERROR && rc <= LSUP_MAX_ERROR)
  76. return err_msg[rc - LSUP_MIN_ERROR];
  77. if (rc >= LSUP_MIN_WARNING && rc <= LSUP_MAX_WARNING)
  78. return warning_msg[rc - LSUP_MIN_WARNING];
  79. return mdb_strerror (rc);
  80. }
  81. /* Inline extern functions. */
  82. int utf8_encode(const uint32_t utf, unsigned char *out);