2 * Copyright (c) 2004 Teodor Sigaev <teodor@sigaev.ru>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the author nor the names of any co-contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43 "flatdbtest [-r] -f FILE [ -p INFO | -g OFFSET | -d OFFSET | -v [ ... ] ]\n"
44 "flatdbtest -f FILE -s"
53 main(int argn, char *argv[]) {
56 int isopened=0, readonly=0;
61 opentlog(TL_OPEN_STDERR,TL_DEBUG, NULL);
64 while((i=getopt(argn,argv,"srf:p:g:d:vh")) != EOF) {
67 if (isopened) {FDBClose(&db); usage();}
71 if (isopened) {FDBClose(&db); usage();}
72 if ( (rc=FDBOpen(&db, optarg, readonly)) != FDB_OK )
73 tlog(TL_CRIT|TL_EXIT, "FDBOpen failed: %d", rc);
77 if (!isopened) {usage();}
78 FDBVacuumFreeSpace(&db);
82 if (!isopened) {usage();}
83 len = RECHDRSZ + strlen(optarg);
84 record = (FDBRecord*)tmalloc( len );
85 record->length = (size_t)len;
86 memcpy( record->data, optarg, len-RECHDRSZ );
87 if ( (rc=FDBPut(&db, record, &offset)) != FDB_OK ) {
89 tlog(TL_CRIT|TL_EXIT, "FDBPut failed: %d", rc);
92 printf("Put: off:%d len:%d '%s'\n", (int)offset, len-RECHDRSZ, optarg);
95 if (!isopened) {usage();}
96 offset = atoi(optarg);
97 if ( (rc=FDBGet(&db, offset, 0, &record)) != FDB_OK ) {
99 tlog(TL_CRIT|TL_EXIT, "FDBGet failed: %d", rc);
101 printf("Get: off:%d len:%d '", (int)offset, record->length - RECHDRSZ);
102 fwrite(record->data, record->length - RECHDRSZ, 1, stdout);
107 if (!isopened) {usage();}
108 offset = atoi(optarg);
109 if ( (rc=FDBDelete(&db, offset, 0)) != FDB_OK ) {
111 tlog(TL_CRIT|TL_EXIT, "FDBDelete failed: %d", rc);
113 printf("Del: off:%d\n", (int)offset);
116 if (!isopened) {usage();}
118 puts("List of free space:");
119 for(i=0;i<db.listcur;i++)
120 printf("\toff: %d\t len: %d\n", (int)(db.space[i].offset), db.space[i].length);
122 puts("List of free space is void");
126 if (isopened) {FDBClose(&db);}