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 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
|
/*
* libhfs - library for reading and writing Macintosh HFS volumes
* Copyright (C) 1996, 1997 Robert Leslie
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* $Id: btree.c,v 1.18 1997/11/07 16:48:34 rob Exp $
*/
# ifdef HAVE_CONFIG_H
# include "config.h"
# endif
# include <stdlib.h>
# include <string.h>
# include <errno.h>
# include "libhfs.h"
# include "btree.h"
# include "data.h"
# include "file.h"
# include "block.h"
# include "node.h"
/*
* NAME: btree->getnode()
* DESCRIPTION: retrieve a numbered node from a B*-tree file
*/
int bt_getnode(node *np)
{
btree *bt = np->bt;
block *bp = &np->data;
const byte *ptr;
int i;
/* verify the node exists and is marked as in-use */
if (np->nnum > 0 && np->nnum >= bt->hdr.bthNNodes)
ERROR(EIO, "read nonexistent b*-tree node");
else if (bt->map && ! BMTST(bt->map, np->nnum))
ERROR(EIO, "read unallocated b*-tree node");
if (f_getblock(&bt->f, np->nnum, bp) == -1)
goto fail;
ptr = *bp;
d_fetchul(&ptr, &np->nd.ndFLink);
d_fetchul(&ptr, &np->nd.ndBLink);
d_fetchsb(&ptr, &np->nd.ndType);
d_fetchsb(&ptr, &np->nd.ndNHeight);
d_fetchuw(&ptr, &np->nd.ndNRecs);
d_fetchsw(&ptr, &np->nd.ndResv2);
if (np->nd.ndNRecs > HFS_MAX_NRECS)
ERROR(EIO, "too many b*-tree node records");
i = np->nd.ndNRecs + 1;
ptr = *bp + HFS_BLOCKSZ - (2 * i);
while (i--)
d_fetchuw(&ptr, &np->roff[i]);
return 0;
fail:
return -1;
}
/*
* NAME: btree->putnode()
* DESCRIPTION: store a numbered node into a B*-tree file
*/
int bt_putnode(node *np)
{
btree *bt = np->bt;
block *bp = &np->data;
byte *ptr;
int i;
/* verify the node exists and is marked as in-use */
if (np->nnum > 0 && np->nnum >= bt->hdr.bthNNodes)
ERROR(EIO, "write nonexistent b*-tree node");
else if (bt->map && ! BMTST(bt->map, np->nnum))
ERROR(EIO, "write unallocated b*-tree node");
ptr = *bp;
d_storeul(&ptr, np->nd.ndFLink);
d_storeul(&ptr, np->nd.ndBLink);
d_storesb(&ptr, np->nd.ndType);
d_storesb(&ptr, np->nd.ndNHeight);
d_storeuw(&ptr, np->nd.ndNRecs);
d_storesw(&ptr, np->nd.ndResv2);
if (np->nd.ndNRecs > HFS_MAX_NRECS)
ERROR(EIO, "too many b*-tree node records");
i = np->nd.ndNRecs + 1;
ptr = *bp + HFS_BLOCKSZ - (2 * i);
while (i--)
d_storeuw(&ptr, np->roff[i]);
return f_putblock(&bt->f, np->nnum, bp);
fail:
return -1;
}
/*
* NAME: btree->readhdr()
* DESCRIPTION: read the header node of a B*-tree
*/
int bt_readhdr(btree *bt)
{
const byte *ptr;
byte *map = 0;
int i;
unsigned long nnum;
bt->hdrnd.bt = bt;
bt->hdrnd.nnum = 0;
if (bt_getnode(&bt->hdrnd) == -1)
goto fail;
if (bt->hdrnd.nd.ndType != ndHdrNode ||
bt->hdrnd.nd.ndNRecs != 3 ||
bt->hdrnd.roff[0] != 0x00e ||
bt->hdrnd.roff[1] != 0x078 ||
bt->hdrnd.roff[2] != 0x0f8 ||
bt->hdrnd.roff[3] != 0x1f8)
ERROR(EIO, "malformed b*-tree header node");
/* read header record */
ptr = HFS_NODEREC(bt->hdrnd, 0);
d_fetchuw(&ptr, &bt->hdr.bthDepth);
d_fetchul(&ptr, &bt->hdr.bthRoot);
d_fetchul(&ptr, &bt->hdr.bthNRecs);
d_fetchul(&ptr, &bt->hdr.bthFNode);
d_fetchul(&ptr, &bt->hdr.bthLNode);
d_fetchuw(&ptr, &bt->hdr.bthNodeSize);
d_fetchuw(&ptr, &bt->hdr.bthKeyLen);
d_fetchul(&ptr, &bt->hdr.bthNNodes);
d_fetchul(&ptr, &bt->hdr.bthFree);
for (i = 0; i < 76; ++i)
d_fetchsb(&ptr, &bt->hdr.bthResv[i]);
if (bt->hdr.bthNodeSize != HFS_BLOCKSZ)
ERROR(EINVAL, "unsupported b*-tree node size");
/* read map record; construct btree bitmap */
/* don't set bt->map until we're done, since getnode() checks it */
map = ALLOC(byte, HFS_MAP1SZ);
if (map == 0)
ERROR(ENOMEM, 0);
memcpy(map, HFS_NODEREC(bt->hdrnd, 2), HFS_MAP1SZ);
bt->mapsz = HFS_MAP1SZ;
/* read continuation map records, if any */
nnum = bt->hdrnd.nd.ndFLink;
while (nnum)
{
node n;
byte *newmap;
n.bt = bt;
n.nnum = nnum;
if (bt_getnode(&n) == -1)
goto fail;
if (n.nd.ndType != ndMapNode ||
n.nd.ndNRecs != 1 ||
n.roff[0] != 0x00e ||
n.roff[1] != 0x1fa)
ERROR(EIO, "malformed b*-tree map node");
newmap = REALLOC(map, byte, bt->mapsz + HFS_MAPXSZ);
if (newmap == 0)
ERROR(ENOMEM, 0);
map = newmap;
memcpy(map + bt->mapsz, HFS_NODEREC(n, 0), HFS_MAPXSZ);
bt->mapsz += HFS_MAPXSZ;
nnum = n.nd.ndFLink;
}
bt->map = map;
return 0;
fail:
FREE(map);
return -1;
}
/*
* NAME: btree->writehdr()
* DESCRIPTION: write the header node of a B*-tree
*/
int bt_writehdr(btree *bt)
{
byte *ptr, *map;
unsigned long mapsz, nnum;
int i;
# ifdef DEBUG
if (bt->hdrnd.bt != bt ||
bt->hdrnd.nnum != 0 ||
bt->hdrnd.nd.ndType != ndHdrNode ||
bt->hdrnd.nd.ndNRecs != 3)
abort();
# endif
ptr = HFS_NODEREC(bt->hdrnd, 0);
d_storeuw(&ptr, bt->hdr.bthDepth);
d_storeul(&ptr, bt->hdr.bthRoot);
d_storeul(&ptr, bt->hdr.bthNRecs);
d_storeul(&ptr, bt->hdr.bthFNode);
d_storeul(&ptr, bt->hdr.bthLNode);
d_storeuw(&ptr, bt->hdr.bthNodeSize);
d_storeuw(&ptr, bt->hdr.bthKeyLen);
d_storeul(&ptr, bt->hdr.bthNNodes);
d_storeul(&ptr, bt->hdr.bthFree);
for (i = 0; i < 76; ++i)
d_storesb(&ptr, bt->hdr.bthResv[i]);
memcpy(HFS_NODEREC(bt->hdrnd, 2), bt->map, HFS_MAP1SZ);
if (bt_putnode(&bt->hdrnd) == -1)
goto fail;
map = bt->map + HFS_MAP1SZ;
mapsz = bt->mapsz - HFS_MAP1SZ;
nnum = bt->hdrnd.nd.ndFLink;
while (mapsz)
{
node n;
if (nnum == 0)
ERROR(EIO, "truncated b*-tree map");
n.bt = bt;
n.nnum = nnum;
if (bt_getnode(&n) == -1)
goto fail;
if (n.nd.ndType != ndMapNode ||
n.nd.ndNRecs != 1 ||
n.roff[0] != 0x00e ||
n.roff[1] != 0x1fa)
ERROR(EIO, "malformed b*-tree map node");
memcpy(HFS_NODEREC(n, 0), map, HFS_MAPXSZ);
if (bt_putnode(&n) == -1)
goto fail;
map += HFS_MAPXSZ;
mapsz -= HFS_MAPXSZ;
nnum = n.nd.ndFLink;
}
bt->flags &= ~HFS_BT_UPDATE_HDR;
return 0;
fail:
return -1;
}
/* High-Level B*-Tree Routines ============================================= */
/*
* NAME: btree->space()
* DESCRIPTION: assert space for new records, or extend the file
*/
int bt_space(btree *bt, unsigned int nrecs)
{
unsigned int nnodes;
long space;
nnodes = nrecs * (bt->hdr.bthDepth + 1);
if (nnodes <= bt->hdr.bthFree)
goto done;
/* make sure the extents tree has room too */
if (bt != &bt->f.vol->ext)
{
if (bt_space(&bt->f.vol->ext, 1) == -1)
goto fail;
}
space = f_alloc(&bt->f);
if (space == -1)
goto fail;
nnodes = space * (bt->f.vol->mdb.drAlBlkSiz / bt->hdr.bthNodeSize);
bt->hdr.bthNNodes += nnodes;
bt->hdr.bthFree += nnodes;
bt->flags |= HFS_BT_UPDATE_HDR;
bt->f.vol->flags |= HFS_VOL_UPDATE_ALTMDB;
while (bt->hdr.bthNNodes > bt->mapsz * 8)
{
byte *newmap;
node mapnd;
/* extend tree map */
newmap = REALLOC(bt->map, byte, bt->mapsz + HFS_MAPXSZ);
if (newmap == 0)
ERROR(ENOMEM, 0);
memset(newmap + bt->mapsz, 0, HFS_MAPXSZ);
bt->map = newmap;
bt->mapsz += HFS_MAPXSZ;
n_init(&mapnd, bt, ndMapNode, 0);
if (n_new(&mapnd) == -1)
goto fail;
/* link the new map node */
if (bt->hdrnd.nd.ndFLink == 0)
{
bt->hdrnd.nd.ndFLink = mapnd.nnum;
mapnd.nd.ndBLink = 0;
}
else
{
node n;
n.bt = bt;
n.nnum = bt->hdrnd.nd.ndFLink;
while (1)
{
if (bt_getnode(&n) == -1)
goto fail;
if (n.nd.ndFLink == 0)
break;
n.nnum = n.nd.ndFLink;
}
n.nd.ndFLink = mapnd.nnum;
mapnd.nd.ndBLink = n.nnum;
if (bt_putnode(&n) == -1)
goto fail;
}
mapnd.nd.ndNRecs = 1;
mapnd.roff[1] = 0x1fa;
if (bt_putnode(&mapnd) == -1)
goto fail;
}
done:
return 0;
fail:
return -1;
}
/*
* NAME: insertx()
* DESCRIPTION: recursively locate a node and insert a record
*/
static
int insertx(node *np, byte *record, int *reclen)
{
node child;
byte *rec;
int result = 0;
if (n_search(np, record))
ERROR(EIO, "b*-tree record already exists");
switch (np->nd.ndType)
{
case ndIndxNode:
if (np->rnum == -1)
rec = HFS_NODEREC(*np, 0);
else
rec = HFS_NODEREC(*np, np->rnum);
child.bt = np->bt;
child.nnum = d_getul(HFS_RECDATA(rec));
if (bt_getnode(&child) == -1 ||
insertx(&child, record, reclen) == -1)
goto fail;
if (np->rnum == -1)
{
n_index(&child, rec, 0);
if (*reclen == 0)
{
result = bt_putnode(np);
goto done;
}
}
if (*reclen)
result = n_insert(np, record, reclen);
break;
case ndLeafNode:
result = n_insert(np, record, reclen);
break;
default:
ERROR(EIO, "unexpected b*-tree node");
}
done:
return result;
fail:
return -1;
}
/*
* NAME: btree->insert()
* DESCRIPTION: insert a new node record into a tree
*/
int bt_insert(btree *bt, const byte *record, int reclen)
{
node root;
byte newrec[HFS_MAX_RECLEN];
if (bt->hdr.bthRoot == 0)
{
/* create root node */
n_init(&root, bt, ndLeafNode, 1);
if (n_new(&root) == -1 ||
bt_putnode(&root) == -1)
goto fail;
bt->hdr.bthDepth = 1;
bt->hdr.bthRoot = root.nnum;
bt->hdr.bthFNode = root.nnum;
bt->hdr.bthLNode = root.nnum;
bt->flags |= HFS_BT_UPDATE_HDR;
}
else
{
root.bt = bt;
root.nnum = bt->hdr.bthRoot;
if (bt_getnode(&root) == -1)
goto fail;
}
memcpy(newrec, record, reclen);
if (insertx(&root, newrec, &reclen) == -1)
goto fail;
if (reclen)
{
byte oroot[HFS_MAX_RECLEN];
int orootlen;
/* root node was split; create a new root */
n_index(&root, oroot, &orootlen);
n_init(&root, bt, ndIndxNode, root.nd.ndNHeight + 1);
if (n_new(&root) == -1)
goto fail;
++bt->hdr.bthDepth;
bt->hdr.bthRoot = root.nnum;
bt->flags |= HFS_BT_UPDATE_HDR;
/* insert index records for new root */
n_search(&root, oroot);
n_insertx(&root, oroot, orootlen);
n_search(&root, newrec);
n_insertx(&root, newrec, reclen);
if (bt_putnode(&root) == -1)
goto fail;
}
++bt->hdr.bthNRecs;
bt->flags |= HFS_BT_UPDATE_HDR;
return 0;
fail:
return -1;
}
/*
* NAME: deletex()
* DESCRIPTION: recursively locate a node and delete a record
*/
static
int deletex(node *np, const byte *key, byte *record, int *flag)
{
node child;
byte *rec;
int found, result = 0;
found = n_search(np, key);
switch (np->nd.ndType)
{
case ndIndxNode:
if (np->rnum == -1)
ERROR(EIO, "b*-tree record not found");
rec = HFS_NODEREC(*np, np->rnum);
child.bt = np->bt;
child.nnum = d_getul(HFS_RECDATA(rec));
if (bt_getnode(&child) == -1 ||
deletex(&child, key, rec, flag) == -1)
goto fail;
if (*flag)
{
*flag = 0;
if (HFS_RECKEYLEN(rec) == 0)
{
result = n_delete(np, record, flag);
break;
}
if (np->rnum == 0)
{
/* propagate index record change into parent */
n_index(np, record, 0);
*flag = 1;
}
result = bt_putnode(np);
}
break;
case ndLeafNode:
if (found == 0)
ERROR(EIO, "b*-tree record not found");
result = n_delete(np, record, flag);
break;
default:
ERROR(EIO, "unexpected b*-tree node");
}
return result;
fail:
return -1;
}
/*
* NAME: btree->delete()
* DESCRIPTION: remove a node record from a tree
*/
int bt_delete(btree *bt, const byte *key)
{
node root;
byte record[HFS_MAX_RECLEN];
int flag = 0;
root.bt = bt;
root.nnum = bt->hdr.bthRoot;
if (root.nnum == 0)
ERROR(EIO, "empty b*-tree");
if (bt_getnode(&root) == -1 ||
deletex(&root, key, record, &flag) == -1)
goto fail;
if (bt->hdr.bthDepth > 1 && root.nd.ndNRecs == 1)
{
const byte *rec;
/* chop the root */
rec = HFS_NODEREC(root, 0);
--bt->hdr.bthDepth;
bt->hdr.bthRoot = d_getul(HFS_RECDATA(rec));
if (n_free(&root) == -1)
goto fail;
}
else if (bt->hdr.bthDepth == 1 && root.nd.ndNRecs == 0)
{
/* root node was deleted */
bt->hdr.bthDepth = 0;
bt->hdr.bthRoot = 0;
}
--bt->hdr.bthNRecs;
bt->flags |= HFS_BT_UPDATE_HDR;
return 0;
fail:
return -1;
}
/*
* NAME: btree->search()
* DESCRIPTION: locate a data record given a search key
*/
int bt_search(btree *bt, const byte *key, node *np)
{
int found = 0;
np->bt = bt;
np->nnum = bt->hdr.bthRoot;
if (np->nnum == 0)
ERROR(ENOENT, 0);
while (1)
{
const byte *rec;
if (bt_getnode(np) == -1)
{
found = -1;
goto fail;
}
found = n_search(np, key);
switch (np->nd.ndType)
{
case ndIndxNode:
if (np->rnum == -1)
ERROR(ENOENT, 0);
rec = HFS_NODEREC(*np, np->rnum);
np->nnum = d_getul(HFS_RECDATA(rec));
break;
case ndLeafNode:
if (! found)
ERROR(ENOENT, 0);
goto done;
default:
found = -1;
ERROR(EIO, "unexpected b*-tree node");
}
}
done:
fail:
return found;
}
|