#define _XOPEN_SOURCE 500 #include #include "core.h" #include "lmdb.h" /* * The message corresponding to the rc is found by * warning_msg[rc - LSUP_MIN_WARNING]. */ char *warning_msg[] = { "LSUP_NOACTION: No action or change of state occurred.", "LSUP_NORESULT: No result.", "LSUP_END: End of the loop reached.", "LSUP_CONFLICT: A resource conflict prevented an action from completing." }; /* * The message corresponding to the rc is found by * err_msg[rc - LSUP_MIN_ERROR]. */ char *err_msg[] = { "LSUP_ERROR: Runtime error.", "LSUP_PARSE_ERR: Error parsing input.", "LSUP_VALUE_ERR: Invalid input.", "LSUP_TXN_ERR: MDB transaction error.", "LSUP_DB_ERR: Database error.", "LSUP_NOT_IMPL_ERR: Feature is not implemented.", "LSUP_IO_ERR: Input/Output error.", "LSUP_MEM_ERR: Memory error.", "LSUP_CONFLICT_ERR: A resource conflict resulted in an invalid state.", "LSUP_ENV_ERR: Invalid environment. Did you call LSUP_init()?", }; char *LSUP_root_path = __FILE__; // This is trimmed to root path on init. int mkdir_p(const char *path, mode_t mode) { /* Adapted from http://stackoverflow.com/a/2336245/119527 */ const size_t len = strlen(path); char _path[PATH_MAX]; char *p; errno = 0; /* Copy string so its mutable */ if (len > sizeof(_path)-1) { errno = ENAMETOOLONG; return -1; } strcpy(_path, path); /* Iterate the string */ for (p = _path + 1; *p; p++) { if (*p == '/') { /* Temporarily truncate */ *p = '\0'; if (mkdir(_path, mode) != 0) { if (errno != EEXIST) return -1; } *p = '/'; } } if (mkdir(_path, mode) != 0) { if (errno != EEXIST) return -1; } return 0; } int unlink_cb( const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf) { log_debug ("Removing %s", fpath); int rv = remove(fpath); if (rv) perror(fpath); return rv; } int rm_r(const char *path) { return nftw(path, unlink_cb, 64, FTW_DEPTH | FTW_PHYS); } const char * LSUP_strerror (LSUP_rc rc) { if (rc >= LSUP_MIN_ERROR && rc <= LSUP_MAX_ERROR) return err_msg[rc - LSUP_MIN_ERROR]; if (rc >= LSUP_MIN_WARNING && rc <= LSUP_MAX_WARNING) return warning_msg[rc - LSUP_MIN_WARNING]; return mdb_strerror (rc); } /* Inline extern functions. */ int utf8_encode(const uint32_t utf, unsigned char *out);