core.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. "LSUP_NOACTION: No action or change of state occurred.",
  11. "LSUP_NORESULT: No result.",
  12. "LSUP_END: End of the loop reached.",
  13. "LSUP_CONFLICT: A resource conflict prevented an action from completing."
  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_ERROR: Runtime error.",
  21. "LSUP_PARSE_ERR: Error parsing input.",
  22. "LSUP_VALUE_ERR: Invalid input.",
  23. "LSUP_TXN_ERR: MDB transaction error.",
  24. "LSUP_DB_ERR: Database error.",
  25. "LSUP_NOT_IMPL_ERR: Feature is not implemented.",
  26. "LSUP_IO_ERR: Input/Output error.",
  27. "LSUP_MEM_ERR: Memory error.",
  28. "LSUP_CONFLICT_ERR: A resource conflict resulted in an invalid state.",
  29. "LSUP_ENV_ERR: Invalid environment. Did you call LSUP_init()?",
  30. };
  31. char *LSUP_root_path = __FILE__; // This is trimmed to root path on init.
  32. int mkdir_p(const char *path, mode_t mode)
  33. {
  34. /* Adapted from http://stackoverflow.com/a/2336245/119527 */
  35. const size_t len = strlen(path);
  36. char _path[PATH_MAX];
  37. char *p;
  38. errno = 0;
  39. /* Copy string so its mutable */
  40. if (len > sizeof(_path)-1) {
  41. errno = ENAMETOOLONG;
  42. return -1;
  43. }
  44. strcpy(_path, path);
  45. /* Iterate the string */
  46. for (p = _path + 1; *p; p++) {
  47. if (*p == '/') {
  48. /* Temporarily truncate */
  49. *p = '\0';
  50. if (mkdir(_path, mode) != 0) {
  51. if (errno != EEXIST)
  52. return -1;
  53. }
  54. *p = '/';
  55. }
  56. }
  57. if (mkdir(_path, mode) != 0) {
  58. if (errno != EEXIST)
  59. return -1;
  60. }
  61. return 0;
  62. }
  63. int
  64. unlink_cb(
  65. const char *fpath, const struct stat *sb, int typeflag,
  66. struct FTW *ftwbuf)
  67. {
  68. log_debug ("Removing %s", fpath);
  69. int rv = remove(fpath);
  70. if (rv)
  71. perror(fpath);
  72. return rv;
  73. }
  74. int rm_r(const char *path)
  75. { return nftw(path, unlink_cb, 64, FTW_DEPTH | FTW_PHYS); }
  76. const char *
  77. LSUP_strerror (LSUP_rc rc)
  78. {
  79. if (rc >= LSUP_MIN_ERROR && rc <= LSUP_MAX_ERROR)
  80. return err_msg[rc - LSUP_MIN_ERROR];
  81. if (rc >= LSUP_MIN_WARNING && rc <= LSUP_MAX_WARNING)
  82. return warning_msg[rc - LSUP_MIN_WARNING];
  83. return mdb_strerror (rc);
  84. }
  85. /* Inline extern functions. */
  86. int utf8_encode(const uint32_t utf, unsigned char *out);