1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 1996, 1997, 1998
* Sleepycat Software. All rights reserved.
*/
#include "config.h"
#ifndef lint
static const char sccsid[] = "@(#)tcl_log.c 10.19 (Sleepycat) 12/17/98";
#endif /* not lint */
/*
* This file is divided up into 5 sets of functions:
* 1. The log command and its support functions.
* 2. The logunlink command.
* 3. The log manager widget commands.
*/
#ifndef NO_SYSTEM_INCLUDES
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#endif
#include <tcl.h>
#include "db_int.h"
#include "dbtest.h"
#include "test_ext.h"
/* Internal functions */
static int get_lsn __P((Tcl_Interp *, char *, DB_LSN *));
static int log_compare_cmd __P((Tcl_Interp *interp, int, char *argv[]));
static int log_file_cmd __P((DB_LOG *, Tcl_Interp *interp, int, char *argv[]));
static int log_flush_cmd __P((DB_LOG *, Tcl_Interp *interp, int, char *argv[]));
static int log_get_cmd __P((DB_LOG *, Tcl_Interp *interp, int, char *argv[]));
static int log_put_cmd __P((DB_LOG *, Tcl_Interp *interp, int, char *argv[]));
static int log_reg_cmd __P((DB_LOG *, Tcl_Interp *interp, int, char *argv[]));
/*
* log_cmd --
* Implements log_open for dbtest. Log_open creates a log manager
* and all the necessary files in the file system. It then creates
* a command that implements the other log functions.
*/
#define LOG_USAGE "log path flags mode [options]\n\toptions:\n"
typedef struct _log_data {
DB_LOG *logp;
DB_ENV *env;
} log_data;
int
log_cmd(notused, interp, argc, argv)
ClientData notused;
Tcl_Interp *interp;
int argc;
char *argv[];
{
static int log_number = 0;
DB_LOG *lp;
DB_ENV *env;
log_data *ld;
u_int32_t flags;
int mode, tclint;
char logname[50];
u_int8_t *conflicts;
notused = NULL;
debug_check();
/* Check number of arguments. */
USAGE_GE(argc, 4, LOG_USAGE, DO_ENV);
if (Tcl_GetInt(interp, argv[2], &tclint) != TCL_OK)
return (TCL_ERROR);
flags = (u_int32_t)tclint;
if (Tcl_GetInt(interp, argv[3], &mode) != TCL_OK)
return (TCL_ERROR);
if (process_env_options(interp, argc, argv, &env))
return (TCL_ERROR);
if (F_ISSET(env, DB_ENV_STANDALONE))
lp = env->lg_info;
else if (log_open(argv[1], flags, mode, env, &lp) != 0) {
db_appexit(env);
Tcl_SetResult(interp, "NULL", TCL_STATIC);
return (TCL_OK);
} else
env->lg_info = lp;
if ((ld = (log_data *)malloc(sizeof(log_data))) == NULL) {
if (!F_ISSET(env, DB_ENV_STANDALONE)) {
(void)db_appexit(env);
conflicts = (u_int8_t *)env->lk_conflicts;
if (conflicts != NULL)
free(conflicts);
free(env);
}
Tcl_SetResult(interp, "lock_open: ", TCL_STATIC);
errno = ENOMEM;
Tcl_AppendResult(interp, Tcl_PosixError(interp), 0);
return (TCL_ERROR);
}
ld->logp = lp;
ld->env = env;
/* Create new command name. */
snprintf(logname, sizeof(logname), "log%d", log_number);
log_number++;
/* Create widget command. */
Tcl_CreateCommand(interp, logname, logwidget_cmd, (int *)ld, NULL);
Tcl_SetResult(interp, logname, TCL_VOLATILE);
return (TCL_OK);
}
/*
* logunlink_cmd --
* Implements txn_unlink for dbtest.
*/
#define LOGUNLINK_USAGE "log_unlink path force"
int
logunlink_cmd(notused, interp, argc, argv)
ClientData notused;
Tcl_Interp *interp;
int argc;
char *argv[];
{
int force;
notused = NULL;
debug_check();
USAGE_GE(argc, 3, LOGUNLINK_USAGE, 0);
if (Tcl_GetInt(interp, argv[2], &force) != TCL_OK)
return (TCL_ERROR);
if (log_unlink(argv[1], force, NULL) != 0)
Tcl_SetResult(interp, "-1", TCL_STATIC);
else
Tcl_SetResult(interp, "0", TCL_STATIC);
return (TCL_OK);
}
/*
* logwidget --
* This is that command that implements the log widget. If we
* ever add new "methods" we add new widget commands here.
*/
#define LOGWIDGET_USAGE "logN option ?arg arg ...?"
#define LOGCLOSE_USAGE "logN close"
#define LOGFLUSH_USAGE "logN flush lsn"
#define LOGGET_USAGE "logN get lsn flags"
#define LOGCMP_USAGE "logN compare lsn1 lsn2"
#define LOGFILE_USAGE "logN file lsn"
#define LOGLAST_USAGE "logN last"
#define LOGPUT_USAGE "logN put record flags"
#define LOGREG_USAGE "logN register db name type"
#define LOG_UNREG "logN unregister id"
int
logwidget_cmd(cd_lp, interp, argc, argv)
ClientData cd_lp;
Tcl_Interp *interp;
int argc;
char *argv[];
{
DB_LOG *lp;
DB_ENV *env;
DB_LSN lsn;
DBT rec;
int id, ret;
char fstr[16], dstr[16];
u_int8_t *conflicts;
debug_check();
lp = ((log_data *)cd_lp)->logp;
USAGE_GE(argc, 2, LOGWIDGET_USAGE, 0);
if (strcmp(argv[1], "close") == 0) {
USAGE(argc, 2, LOGCLOSE_USAGE, 0);
env = ((log_data *)cd_lp)->env;
if (!F_ISSET(env, DB_ENV_STANDALONE)) {
(void)db_appexit(env);
conflicts = (u_int8_t *)env->lk_conflicts;
if (conflicts != NULL)
free(conflicts);
free(env);
}
free(cd_lp);
ret = Tcl_DeleteCommand(interp, argv[0]);
} else if (strcmp(argv[1], "flush") == 0) {
ret = log_flush_cmd(lp, interp, argc, argv);
} else if (strcmp(argv[1], "get") == 0) {
return (log_get_cmd(lp, interp, argc, argv));
} else if (strcmp(argv[1], "last") == 0) {
memset(&rec, 0, sizeof(rec));
ret = log_get(lp, &lsn, &rec, DB_LAST);
if (ret == 0) {
snprintf(fstr, sizeof(fstr), "%lu", (u_long)lsn.file);
snprintf(dstr, sizeof(dstr), "%lu", (u_long)lsn.offset);
Tcl_ResetResult(interp);
Tcl_AppendResult(interp, fstr, " ", dstr, 0);
return (TCL_OK);
}
} else if (strcmp(argv[1], "compare") == 0) {
return (log_compare_cmd(interp, argc, argv));
} else if (strcmp(argv[1], "file") == 0) {
return (log_file_cmd(lp, interp, argc, argv));
} else if (strcmp(argv[1], "put") == 0) {
return (log_put_cmd(lp, interp, argc, argv));
} else if (strcmp(argv[1], "register") == 0) {
return (log_reg_cmd(lp, interp, argc, argv));
} else if (strcmp(argv[1], "unregister") == 0) {
if (Tcl_GetInt(interp, argv[2], &id) != TCL_OK)
return (TCL_ERROR);
ret = log_unregister(lp, id);
} else {
Tcl_SetResult(interp, LOGWIDGET_USAGE, TCL_STATIC);
return (TCL_ERROR);
}
if (ret != 0) {
Tcl_SetResult(interp, "log_cmd: ", TCL_STATIC);
errno = ret;
Tcl_AppendResult(interp, argv[2], " ",
Tcl_PosixError(interp), 0);
} else
Tcl_SetResult(interp, "0", TCL_STATIC);
return (TCL_OK);
}
/*
* Implementation of all the individual widget commands.
*/
static int
log_compare_cmd(interp, argc, argv)
Tcl_Interp *interp;
int argc;
char *argv[];
{
DB_LSN lsn1, lsn2;
char resbuf[32];
int cmp;
USAGE(argc, 4, LOGCMP_USAGE, 0);
if (get_lsn(interp, argv[2], &lsn1) != TCL_OK ||
get_lsn(interp, argv[3], &lsn2) != TCL_OK)
return (TCL_ERROR);
cmp = log_compare(&lsn1, &lsn2);
snprintf(resbuf, sizeof(resbuf), "%d", cmp);
Tcl_SetResult(interp, resbuf, TCL_VOLATILE);
return (TCL_OK);
}
static int
log_flush_cmd(lp, interp, argc, argv)
DB_LOG *lp;
Tcl_Interp *interp;
int argc;
char *argv[];
{
DB_LSN lsn;
int ret;
USAGE(argc, 3, LOGFLUSH_USAGE, 0);
if (get_lsn(interp, argv[2], &lsn) != TCL_OK)
return (TCL_ERROR);
if (lsn.file == 0 && lsn.offset == 0)
ret = log_flush(lp, NULL);
else
ret = log_flush(lp, &lsn);
if (ret != 0)
return (TCL_ERROR);
Tcl_SetResult(interp, "0", TCL_STATIC);
return (TCL_OK);
}
static int
log_file_cmd(lp, interp, argc, argv)
DB_LOG *lp;
Tcl_Interp *interp;
int argc;
char *argv[];
{
DB_LSN lsn;
int ret;
char name[1024];
USAGE(argc, 3, LOGFILE_USAGE, 0);
if (get_lsn(interp, argv[2], &lsn) != TCL_OK)
return (TCL_ERROR);
if ((ret = log_file(lp, &lsn, name, sizeof(name))) != 0) {
Tcl_SetResult(interp, "log_file: ", TCL_STATIC);
errno = ret;
Tcl_AppendResult(interp, Tcl_PosixError(interp), 0);
return (TCL_OK);
}
Tcl_SetResult(interp, name, TCL_VOLATILE);
return (TCL_OK);
}
static int
log_get_cmd(lp, interp, argc, argv)
DB_LOG *lp;
Tcl_Interp *interp;
int argc;
char *argv[];
{
DB_LSN lsn;
DBT data;
u_int32_t flags;
int ret, tclint;
USAGE(argc, 4, LOGGET_USAGE, 0);
if (get_lsn(interp, argv[2], &lsn) != TCL_OK)
return (TCL_ERROR);
if (Tcl_GetInt(interp, argv[3], &tclint) != TCL_OK)
return (TCL_ERROR);
flags = (u_int32_t)tclint;
memset(&data, 0, sizeof(data));
switch (ret = log_get(lp, &lsn, &data, flags)) {
case 0: Tcl_SetResult(interp, data.data, TCL_VOLATILE);
break;
case DB_NOTFOUND: Tcl_ResetResult(interp);
break;
default:
Tcl_SetResult(interp, "log get: ", TCL_STATIC);
errno = ret;
Tcl_AppendResult(interp, Tcl_PosixError(interp), 0);
break;
}
return (TCL_OK);
}
static int
log_put_cmd(lp, interp, argc, argv)
DB_LOG *lp;
Tcl_Interp *interp;
int argc;
char *argv[];
{
DB_LSN lsn;
DBT data;
u_int32_t flags;
int ret, tclint;
char resbuf[64];
USAGE(argc, 4, LOGPUT_USAGE, 0);
if (Tcl_GetInt(interp, argv[3], &tclint) != TCL_OK)
return (TCL_ERROR);
flags = (u_int32_t)tclint;
memset(&data, 0, sizeof(data));
data.data = argv[2];
data.size = strlen(argv[2]) + 1;
if ((ret = log_put(lp, &lsn, &data, flags)) != 0) {
errno = ret;
Tcl_SetResult(interp, "ERROR: ", TCL_STATIC);
Tcl_AppendResult(interp, Tcl_PosixError(interp), NULL);
return (TCL_ERROR);
}
snprintf(resbuf, sizeof(resbuf), "%lu %lu", (unsigned long)lsn.offset,
(unsigned long)lsn.file);
Tcl_SetResult(interp, resbuf, TCL_VOLATILE);
return (TCL_OK);
}
static int
log_reg_cmd(lp, interp, argc, argv)
DB_LOG *lp;
Tcl_Interp *interp;
int argc;
char *argv[];
{
DB *dbp;
DBTYPE type;
Tcl_CmdInfo cmd_info;
u_int32_t regid;
int ret, tclint;
USAGE(argc, 5, LOGREG_USAGE, 0);
if (Tcl_GetInt(interp, argv[4], &tclint) != TCL_OK)
return (TCL_ERROR);
type = (DBTYPE)tclint;
if (Tcl_GetCommandInfo(interp, argv[2], &cmd_info) != TCL_OK)
dbp = NULL;
else
dbp = (DB *)cmd_info.clientData;
ret = log_register(lp, dbp, argv[3], type, ®id);
if (ret != 0) {
errno = ret;
Tcl_SetResult(interp, "ERROR: ", TCL_STATIC);
Tcl_AppendResult(interp, Tcl_PosixError(interp), NULL);
return (TCL_ERROR);
}
/*
* !!!
* Safe: interp->result is guaranteed to be at least 200 bytes.
*/
sprintf(interp->result, "%lu", (unsigned long)regid);
return (TCL_OK);
}
static int
get_lsn(interp, str, lsnp)
Tcl_Interp *interp;
char *str;
DB_LSN *lsnp;
{
int largc, ret, tclint;
char **largv;
if (Tcl_SplitList(interp, str, &largc, &largv) != TCL_OK || largc != 2)
return (TCL_ERROR);
if (Tcl_GetInt(interp, largv[0], &tclint) != TCL_OK)
goto err;
lsnp->offset = (u_int32_t)tclint;
if (Tcl_GetInt(interp, largv[1], &tclint) != TCL_OK)
goto err;
lsnp->file = (u_int32_t)tclint;
ret = TCL_OK;
if (0) {
err: ret = TCL_ERROR;
}
FREE_TCL(largv);
return (ret);
}
|