#include #include #include #include #include #include #include #include #include "util.h" void statistics(DB *db) { DB_BTREE_STAT *statp; int ret; assert(db); if ((ret = db->stat(db, &statp, 0)) != 0) { db->err(db, ret, "DB->stat"); return; } printf("Database contains %lu records\n", (long unsigned) statp->bt_ndata); free(statp); } char* normalize_path(char *s) { char *l, *p, *d; // deletes /./ and // if (*s == '/') l = p = d = s+1; else l = p = d = s; for (; *p; p++) { if (*p == '/') { if (l-p == 0) { l++; continue; } if (p-l == 1 && *l == '.') { l += 2; continue; } while (l <= p) *(d++) = *(l++); } } while (l <= p) *(d++) = *(l++); return s; } void rotdash(void) { static const char dashes[] = /* ".oOo"; */ "|/-\\"; const static char *d = dashes; if (isatty(fileno(stderr))) { fprintf(stderr, "%c\b", *d); d++; if (!*d) d = dashes; } } const char* get_attached_filename(const char *path, const char *fn) { static char npath[PATH_MAX]; struct stat st; if (stat(path, &st) < 0) { if (errno == ENOENT) return path; fprintf(stderr, "stat(%s) failed: %s\n", path, strerror(errno)); return NULL; } if (S_ISREG(st.st_mode)) return path; if (S_ISDIR(st.st_mode)) { snprintf(npath, sizeof(npath), "%s/.syrep", path); mkdir(npath, 0777); snprintf(npath, sizeof(npath), "%s/.syrep/%s", path, fn); return npath; } fprintf(stderr, "<%s> is not a valid syrep snapshot\n", path); return NULL; } int isdirectory(const char *path) { struct stat st; if (stat(path, &st) < 0) return -1; return !!S_ISDIR(st.st_mode); }