small sisez
[tedtools.git] / flatdbtest.c
1 /*
2  * Copyright (c) 2004 Teodor Sigaev <teodor@sigaev.ru>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
16  *
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.
28  */
29
30 #include <stdio.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34
35 #include "tlog.h"
36 #include "tmalloc.h"
37 #include "flatdb.h"
38
39 static void
40 usage() {
41         puts(
42         "Usage:\n"
43         "flatdbtest [-r] -f FILE  [ -p INFO | -g OFFSET | -d OFFSET | -v [ ... ] ]\n"
44         "flatdbtest -f FILE  -s"
45         );
46         exit(1);
47 }
48
49 extern char *optarg;
50 extern int opterr;
51
52 int
53 main(int argn, char *argv[]) {
54         FDB db;
55         int i;
56         int isopened=0, readonly=0;
57         off_t offset;
58         int len,rc;
59         FDBRecord       *record;        
60
61         opentlog(TL_OPEN_STDERR,TL_DEBUG, NULL);
62         opterr=0;
63
64         while((i=getopt(argn,argv,"srf:p:g:d:vh")) != EOF) {
65                 switch(i) {
66                         case 'r':
67                                 if (isopened) {FDBClose(&db); usage();}
68                                 readonly=1;
69                                 break;
70                         case 'f':
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);
74                                 isopened=1;
75                                 break;
76                         case 'v':
77                                 if (!isopened) {usage();}
78                                 FDBVacuumFreeSpace(&db);
79                                 puts("Vacuum"); 
80                                 break;
81                         case 'p':
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 ) {
88                                         FDBClose(&db);
89                                         tlog(TL_CRIT|TL_EXIT, "FDBPut failed: %d", rc);
90                                 }
91                                 tfree(record);
92                                 printf("Put: off:%d len:%d '%s'\n", (int)offset, len-RECHDRSZ, optarg);
93                                 break;
94                         case 'g': 
95                                 if (!isopened) {usage();}
96                                 offset = atoi(optarg);
97                                 if ( (rc=FDBGet(&db, offset, 0, &record)) != FDB_OK ) {
98                                         FDBClose(&db);
99                                         tlog(TL_CRIT|TL_EXIT, "FDBGet failed: %d", rc);
100                                 }
101                                 printf("Get: off:%d len:%d '", (int)offset, record->length - RECHDRSZ);
102                                 fwrite(record->data, record->length - RECHDRSZ, 1, stdout);
103                                 puts("'");
104                                 tfree(record);
105                                 break;
106                         case 'd':
107                                 if (!isopened) {usage();}
108                                 offset = atoi(optarg);
109                                 if ( (rc=FDBDelete(&db, offset, 0)) != FDB_OK ) {
110                                         FDBClose(&db);
111                                         tlog(TL_CRIT|TL_EXIT, "FDBDelete failed: %d", rc);
112                                 }
113                                 printf("Del: off:%d\n", (int)offset);
114                                 break;
115                         case 's':
116                                 if (!isopened) {usage();}
117                                 if ( db.listcur ) { 
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);
121                                 } else
122                                          puts("List of free space is void");
123                                 break;
124                         case 'h':
125                         default:
126                                 if (isopened) {FDBClose(&db);}
127                                 usage();
128                                 break;
129                 }
130         }
131                                 
132         if (isopened) 
133                 FDBClose(&db);
134         else
135                 usage();
136
137         return 0;
138 }
139
140         
141