core.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. int mkdir_p(const char *path, mode_t mode)
  32. {
  33. /* Adapted from http://stackoverflow.com/a/2336245/119527 */
  34. const size_t len = strlen(path);
  35. char _path[PATH_MAX];
  36. char *p;
  37. errno = 0;
  38. /* Copy string so its mutable */
  39. if (len > sizeof(_path)-1) {
  40. errno = ENAMETOOLONG;
  41. return -1;
  42. }
  43. strcpy(_path, path);
  44. /* Iterate the string */
  45. for (p = _path + 1; *p; p++) {
  46. if (*p == '/') {
  47. /* Temporarily truncate */
  48. *p = '\0';
  49. if (mkdir(_path, mode) != 0) {
  50. if (errno != EEXIST)
  51. return -1;
  52. }
  53. *p = '/';
  54. }
  55. }
  56. if (mkdir(_path, mode) != 0) {
  57. if (errno != EEXIST)
  58. return -1;
  59. }
  60. return 0;
  61. }
  62. int
  63. unlink_cb(
  64. const char *fpath, const struct stat *sb, int typeflag,
  65. struct FTW *ftwbuf)
  66. {
  67. log_debug ("Removing %s", fpath);
  68. int rv = remove(fpath);
  69. if (rv)
  70. perror(fpath);
  71. return rv;
  72. }
  73. int rm_r(const char *path)
  74. { return nftw(path, unlink_cb, 64, FTW_DEPTH | FTW_PHYS); }
  75. const char *
  76. LSUP_strerror (LSUP_rc rc)
  77. {
  78. if (rc >= LSUP_MIN_ERROR && rc <= LSUP_MAX_ERROR)
  79. return err_msg[rc - LSUP_MIN_ERROR];
  80. if (rc >= LSUP_MIN_WARNING && rc <= LSUP_MAX_WARNING)
  81. return warning_msg[rc - LSUP_MIN_WARNING];
  82. return mdb_strerror (rc);
  83. }
  84. /* Inline extern functions. */
  85. int utf8_encode(const uint32_t utf, unsigned char *out);