int main(int argc, char **argv) { MDBM* db; char fn[MAXPATHLEN]; MDBM_ITER iter; kvpair kv; snprintf(fn,sizeof(fn),"%s%s",yax_getroot(),"/tmp/example.mdbm"); db = mdbm_open(fn,MDBM_O_RDWR,0666,0,0); if (!db) { perror("Unable to open database"); exit(2); } /* Start at the beginning */ kv = mdbm_first_r( db, &iter ); if ((kv.key.dsize == 0) && (kv.val.dsize == 0)) { printf("Database was empty!\n"); mdbm_close(db); exit(3); } while (! ((kv.key.dsize == 0) && (kv.val.dsize == 0))) { printf("Key [%.*s] Val [%.*s]\n", kv.key.dsize, kv.key.dptr, kv.val.dsize, kv.val.dptr); kv = mdbm_next_r( db, &iter ); } printf("End of db reached.\n"); mdbm_close(db); exit(1); }
int r2c_main(int ac, char **av) { int c; char *file = 0; char *p; int rc = 1; int product = 1; MDBM *idDB, *goneDB; MDBM *md5DB = 0; RANGE rargs = {0}; char *sfile; char **revs = 0; longopt lopts[] = { { "standalone", 'S' }, /* treat comps as standalone */ { 0, 0 } }; while ((c = getopt(ac, av, "PRr;S", lopts)) != -1) { switch (c) { case 'P': break; // do not doc case 'R': // do not doc case 'S': product = 0; break; case 'r': if (range_addArg(&rargs, optarg, 0)) usage(); break; default: usage(); break; } } unless ((file = av[optind]) && !av[optind+1]) usage(); sfile = name2sccs(file); if (!isreg(sfile) && isKey(file)) { proj_cd2root(); idDB = loadDB(IDCACHE, 0, DB_IDCACHE); goneDB = loadDB(GONE, 0, DB_GONE); file = key2path(file, idDB, goneDB, &md5DB); mdbm_close(idDB); mdbm_close(goneDB); mdbm_close(md5DB); unless (file) goto out; }
void ldbm_close( LDBM ldbm ) { /* Open and close are not reentrant so we need to use locks here */ #ifdef MDBM_DEBUG fprintf( stdout, "==>(mdbm)ldbm_close(db=%p)\n", ldbm ); fflush( stdout ); #endif LDBM_WLOCK; mdbm_close( ldbm ); LDBM_WUNLOCK; #ifdef MDBM_DEBUG fprintf( stdout, "<==(mdbm)ldbm_close()\n" ); fflush( stdout ); #endif }
int main(int argc, char** argv) { MDBM* db; int opt; int oflags = 0; while ((opt = getopt(argc,argv,"hL")) != -1) { switch (opt) { case 'h': usage(0); case 'L': oflags |= MDBM_OPEN_NOLOCK; break; default: usage(1); } } if ((argc - optind) != 1) { usage(1); } if (!(oflags&MDBM_OPEN_NOLOCK)) { oflags |= MDBM_ANY_LOCKS; } if ((db = mdbm_open( argv[optind], MDBM_O_RDONLY|oflags, 0, 0, 0)) == NULL) { fprintf(stderr, PREFIX "mdbm_open(%s): %s\n", argv[optind], strerror(errno)); exit(1); } if (mdbm_sync(db)) { fprintf(stderr, PREFIX "mdbm_sync(%s): %s\n", argv[optind], strerror(errno)); exit(1); } mdbm_close(db); return 0; }
int main(int argc, char **argv) { char buf[2048]; MDBM* db; char fn[MAXPATHLEN]; int notfound; snprintf(fn,sizeof(fn),"%s%s",yax_getroot(),"/tmp/example.mdbm"); db = mdbm_open(fn,MDBM_O_RDWR,0666,0,0); if (!db) { perror("Unable to open database"); exit(2); } notfound = 0; while (fgets(buf,sizeof(buf),stdin)) { datum key, val; static char DELETED[] = "deleted"; int len = strlen(buf); int ret; if (buf[len-1] == '\n') { buf[--len] = 0; } key.dptr = buf; key.dsize = len; val.dptr = DELETED; val.dsize = sizeof(DELETED)-1; mdbm_lock(db); ret = mdbm_store(db,key,val,MDBM_REPLACE); mdbm_unlock(db); if (ret) { perror("Database store failed"); } } mdbm_close(db); return 0; }
int main (int argc, char** argv) { MDBM* db; mdbm_dump_info_t di; int opt; int chunks = 0; int flags = 0; int oflags = 0; int pno = -1; uint64_t winsize = 0; int legacy = 0; di.flags = 0; while ((opt = getopt(argc,argv,"CcdhfklLp:Pvw:")) != -1) { switch (opt) { case 'C': di.flags |= MDBM_DUMP_CACHEINFO; break; case 'c': chunks = 1; break; case 'd': di.flags |= MDBM_DUMP_DELETED; break; case 'f': di.flags |= MDBM_DUMP_FREE; break; case 'h': usage(0); case 'k': di.flags |= MDBM_DUMP_KEYS; break; case 'l': legacy = 1; break; case 'L': flags |= MDBM_ITERATE_NOLOCK; oflags |= MDBM_OPEN_NOLOCK; break; case 'P': di.flags |= MDBM_DUMP_PAGES_ONLY; break; case 'p': pno = atoi(optarg); break; case 'v': di.flags |= MDBM_DUMP_VALUES; break; case 'w': winsize = mdbm_util_get_size(optarg,1); if (winsize == 0) { printf("Window size must be positive, size=%lu\n\n", (unsigned long)winsize); usage(1); } oflags |= (MDBM_OPEN_WINDOWED | MDBM_O_RDWR); /* Windowed mode fails with read-only access */ break; } } if (optind+1 != argc) { usage(1); } if (!winsize) { oflags |= MDBM_O_RDONLY; } if (!(oflags&MDBM_OPEN_NOLOCK)) { oflags |= MDBM_ANY_LOCKS; } db = mdbm_open(argv[optind],oflags,0,0,0); if (!db) { perror(argv[optind]); return 1; } if (legacy || mdbm_get_version(db)==2) { if (pno < 0) { mdbm_dump_all_page(db); } else { mdbm_dump_page(db,pno); } return 0; } if (winsize) { errno = 0; if (mdbm_set_window_size(db, winsize) != 0) { printf("Unable to set window size to %llu (%s), exiting\n", (unsigned long long) winsize, strerror(errno)); mdbm_close(db); exit(1); } } if (chunks) { di.free = -1; mdbm_chunk_iterate(db,chunk,flags,&di); } else { di.db = db; if (!(di.flags & MDBM_DUMP_PAGES_ONLY)) { flags |= MDBM_ITERATE_ENTRIES; } mdbm_iterate(db,pno,dump,flags,&di); } return 0; }
int main(int argc, char** argv) { int c, err_flag = 0, opt_help = 0, opt_fast = 0, opt_cdbdump = 0; char *opt_outfile = NULL; char *opt_locktype = NULL; FILE *fp = stdout; MDBM *db; int lock_flags = MDBM_ANY_LOCKS; int opt_recordInfo = 0; while ((c = getopt(argc, argv, "hfL:o:cr")) != EOF) { switch (c) { case 'h': opt_help = 1; break; case 'f': fprintf(stderr, "Option \"-f\" is deprecated, use \"-L none\" \n"); opt_fast = 1; break; case 'c': opt_cdbdump = 1; break; case 'o': opt_outfile = optarg; break; case 'L': opt_locktype = optarg; break; case 'r': opt_recordInfo = 1; break; default: err_flag = 1; } } if (opt_help) { usage(); return 0; } if (err_flag || ((argc - optind) != 1)) { usage(); return 1; } if (opt_locktype && mdbm_util_lockstr_to_flags(opt_locktype, &lock_flags)) { fprintf(stderr, "Invalid locking argument %s\n", opt_locktype); usage(); return 1; } if (lock_flags == (int)MDBM_OPEN_NOLOCK) { opt_fast = 1; } if (opt_recordInfo && opt_cdbdump) { fprintf(stderr, "Options \"-c\" and \"-r\" are not compatible.\n"); return(1); } if ((db = mdbm_open(argv[optind], MDBM_O_RDONLY|lock_flags, 0, 0, 0)) == NULL) { fprintf(stderr, "%s: %s\n", argv[optind], strerror(errno)); return(1); } if (opt_outfile) { if ((fp = fopen(opt_outfile, "w")) == NULL) { perror(opt_outfile); return(1); } } /* Buffer output a little more */ setvbuf(fp, NULL, _IOFBF, BUFSIZ * 16); if (!opt_fast) { mdbm_lock(db); } if (opt_recordInfo) { do_recordInfoDump(db, fp); } else if (opt_cdbdump) { do_cdbdump(db, fp); } else { do_dbdump(db, fp); } fclose(fp); if (!opt_fast) { mdbm_unlock(db); } mdbm_close(db); return(0); }
int main (int argc, char** argv) { MDBM* db; int opt; int oflags = 0; uint64_t winsize = 0; int verbose = 0, print_page_num = 0; while ((opt = getopt(argc,argv,"hLpPvw:")) != -1) { switch (opt) { case 'h': usage(0); case 'L': oflags |= MDBM_OPEN_NOLOCK; break; case 'p': print_page_num = 1; break; case 'P': oflags |= MDBM_PARTITIONED_LOCKS; break; case 'v': ++verbose; break; case 'w': winsize = mdbm_util_get_size(optarg,1); if (winsize == 0) { printf("Window size must be positive, size=%lu\n\n", (unsigned long)winsize); usage(1); } oflags |= (MDBM_OPEN_WINDOWED | MDBM_O_RDWR); /* Windowed mode fails with read-only access */ break; default: usage(1); } } if (!(oflags&MDBM_OPEN_NOLOCK)) { oflags |= MDBM_ANY_LOCKS; } if ((argc - optind) != 2) { usage(1); } if (!winsize) { oflags |= MDBM_O_RDONLY; } if (( db = mdbm_open( argv[optind], oflags, 0, 0, 0 )) == NULL ) { fprintf(stderr, PREFIX ": mdbm_open(%s): %s\n", argv[optind], strerror(errno) ); exit(1); } if (winsize) { errno = 0; if (mdbm_set_window_size(db, winsize) != 0) { printf("Unable to set window size to %llu (%s) exiting\n", (unsigned long long) winsize, strerror(errno)); mdbm_close(db); exit(1); } } /* ASSERT: db is open. Fetch key. */ { datum key, val; MDBM_ITER iter; key.dptr = argv[optind+1]; key.dsize = strlen( key.dptr ); if (oflags & MDBM_PARTITIONED_LOCKS) { mdbm_plock(db,&key,0); } else { mdbm_lock(db); } MDBM_ITER_INIT( &iter ); while (mdbm_fetch_dup_r( db, &key, &val, &iter ) == 0) { if (verbose) { printf("key (%4d bytes): %.*s\n",key.dsize,key.dsize,key.dptr); printf("val (%4d bytes): %.*s\n",val.dsize,val.dsize,val.dptr); if (print_page_num) { printf("Page-number is: %u\n", mdbm_get_page(db, &key)); } } else { fwrite(key.dptr,key.dsize,1,stdout); fputs(" :: ",stdout); fwrite(val.dptr,val.dsize,1,stdout); if (print_page_num) { fputs(" :: ",stdout); fprintf(stdout, "page-number= %u", mdbm_get_page(db, &key)); } fputc('\n',stdout); } } mdbm_unlock(db); } mdbm_close(db); return 0; }