2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these libraries and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /*%% (c) Copyright 1993, 1994 Hewlett-Packard Company */
24 /*%% (c) Copyright 1993, 1994 International Business Machines Corp. */
25 /*%% (c) Copyright 1993, 1994 Sun Microsystems, Inc. */
26 /*%% (c) Copyright 1993, 1994 Novell, Inc. */
27 /*%% $XConsortium: isvarrec.c /main/3 1995/10/23 11:45:36 rswiston $ */
29 * Copyright (c) 1988 by Sun Microsystems, Inc.
36 * Fixed length record access (VLRA) module.
39 #include "isam_impl.h"
42 long _vl_getpos(); /* Get offset in .rec file */
43 int _vl_deleted(); /* 0/1 returns 1 if record is deleted */
44 static void remove_from_chain2(); /* used by _vlrec_wrrec() */
45 long _istail_insert();
46 static void _istail_delete();
47 static _istail_read();
50 * _vlrec_write(fcb, record, recnum, reclen)
55 * FCB File Control Block
56 * record record buffer
57 * reclen record length (NOT USED)
60 * recnum record number of the new record
62 * Returns 0 if record was written successfully, or -1 if any error.
67 _vlrec_write(Fcb *fcb, char *record, Recno *recnum, int reclen)
71 long tailoff = VL_RECNOTAIL;
72 char recnobuf [RECNOSIZE];
73 char tailoffbuf[LONGSIZE];
76 * Reuse a deleted record if one exits.
77 * Otherwise, extend .rec file by a record.
79 if (fcb->freerecno != NULL_RECNO) {
80 recnum2 = fcb->freerecno;
83 * Remove record from the chain of deleted records.
85 rec_position = _vl_getpos(fcb, recnum2); /* Offset in .rec file */
86 _cp_fromfile(fcb, fcb->datfd, recnobuf, rec_position + LONGSIZE, RECNOSIZE);
87 fcb->freerecno = ldrecno(recnobuf);
90 recnum2 = ++(fcb->lastrecno);
93 * Extend .rec file size if necessary.
95 while (_vl_getpos(fcb, recnum2 + 1) > fcb->datsize * ISPAGESIZE) {
96 fcb->datsize = _extend_file(fcb, fcb->datfd, fcb->datsize);
98 rec_position = _vl_getpos(fcb, recnum2); /* Offset in .rec file */
102 * Store variable part of record (the 'tail') in .var file.
104 tailoff = _istail_insert(fcb, record + fcb->minreclen,
105 reclen - fcb->minreclen);
108 * Copy record to the .rec file. Mark record as undeleted.
110 stlong(tailoff, tailoffbuf);
111 _cp_tofile(fcb, fcb->datfd, tailoffbuf, rec_position, LONGSIZE);
112 _cp_tofile(fcb, fcb->datfd, record, rec_position + LONGSIZE, fcb->minreclen);
120 * _vlrec_read(fcb, record, recnum, reclen)
125 * FCB File Control Block
126 * recnum record number of the record
127 * reclen filled with the record size for compatibilty with
128 * variable length records
131 * record record buffer is filled with data
133 * Returns 0 if record was read successfully, or error code if any error.
137 _vlrec_read(Fcb *fcb, char *record, Recno recnum, int *reclen)
141 char tailoffbuf[LONGSIZE];
144 * Check that recnum is within the range of existing record numbers.
146 if (recnum < 1 || recnum > fcb->lastrecno)
149 rec_position = _vl_getpos(fcb, recnum); /* Offset in .rec file */
152 * Check that the record is not marked as deleted.
154 _cp_fromfile(fcb, fcb->datfd, tailoffbuf, rec_position, LONGSIZE);
155 tailoff = ldlong(tailoffbuf);
156 if (tailoff == VL_RECDELETED) {
161 * Copy record from the .at file.
163 _cp_fromfile(fcb, fcb->datfd, record, rec_position + LONGSIZE, fcb->minreclen);
165 *reclen = fcb->minreclen;
168 * Get the 'tail' of the record if any.
170 *reclen += _istail_read(fcb, tailoff, record + fcb->minreclen);
172 if (*reclen > fcb->maxreclen)
173 _isfatal_error("Corrupted file: too long variable length record");
179 * pos = _vl_getpos(fcb, recnum)
181 * Calculate the position of record in .rec file.
185 _vl_getpos(Fcb *fcb, Recno recnum)
187 return ((long)(ISCNTLSIZE + (fcb->minreclen + LONGSIZE) * (recnum -1)));
191 * _vlrec_rewrite(fcb, record, recnum, reclen)
196 * FCB File Control Block
197 * recnum record number of the record
199 * int reclen (NOT USED)
201 * Returns 0 if record was rewritten successfully, or error code if any error.
206 _vlrec_rewrite(Fcb *fcb, char *record, Recno recnum, int reclen)
210 char tailoffbuf[LONGSIZE];
213 * Check that recnum is within the range of existing record numbers.
215 if (recnum < 1 || recnum > fcb->lastrecno)
218 rec_position = _vl_getpos(fcb, recnum); /* Offset in .rec file */
221 * Check that the record is not marked as deleted.
223 _cp_fromfile(fcb, fcb->datfd, tailoffbuf, rec_position, LONGSIZE);
224 tailoff = ldlong(tailoffbuf);
225 if (tailoff == VL_RECDELETED) {
230 * Store variable part of record (the 'tail') in .var file.
232 tailoff = _istail_modify(fcb, tailoff, record + fcb->minreclen,
233 reclen - fcb->minreclen);
236 * Copy new record to the .rec file.
238 stlong(tailoff, tailoffbuf);
239 _cp_tofile(fcb, fcb->datfd, tailoffbuf, rec_position, LONGSIZE);
240 _cp_tofile(fcb, fcb->datfd, record, rec_position + LONGSIZE, fcb->minreclen);
246 * _vlrec_delete;(fcb, recnum)
251 * FCB File Control Block
252 * recnum record number of the record
254 * Returns 0 if record was rewritten successfully, or error code if any error.
258 _vlrec_delete(Fcb *fcb, Recno recnum)
262 char tailoffbuf[LONGSIZE];
263 char recnobuf [RECNOSIZE];
266 * Check that recnum is within the range of existing record numbers.
268 if (recnum < 1 || recnum > fcb->lastrecno)
271 rec_position = _vl_getpos(fcb, recnum); /* Offset in .rec file */
274 * Check that the record is not marked as deleted.
276 _cp_fromfile(fcb, fcb->datfd, tailoffbuf, rec_position, LONGSIZE);
277 tailoff = ldlong(tailoffbuf);
278 if (tailoff == VL_RECDELETED) {
283 * Set the delete flag to VL_RECDELETED.
285 tailoff = VL_RECDELETED;
286 stlong(tailoff, tailoffbuf);
287 _cp_tofile(fcb, fcb->datfd, tailoffbuf, rec_position, LONGSIZE);
290 * Insert record into chain of deleted records.
292 strecno(fcb->freerecno, recnobuf);
293 _cp_tofile(fcb, fcb->datfd, recnobuf, rec_position + LONGSIZE, RECNOSIZE);
294 fcb->freerecno = recnum;
297 * Delete tail from .var file.
304 * _vlrec_wrrec(fcb, record, recnum, reclen)
306 * Write a record by record number.
309 * FCB File Control Block
310 * recnum record number of the record
311 * record record buffer
312 * int reclen (NOT USED)
314 * Returns 0 if record was written successfully, or error code if any error.
316 * Note that _vlrec_wrrec() commits updates and syncs the FCB to avoid
317 * buffer pool overflow.
322 _vlrec_wrrec(Fcb *fcb, char *record, Recno recnum, int reclen)
326 char tailoffbuf[LONGSIZE];
328 char recnumbuf [RECNOSIZE];
331 * Check that recnum is not negative.
336 rec_position = _vl_getpos(fcb, recnum); /* Offset in .rec file */
338 if (recnum > fcb->lastrecno) {
341 * If the recnum is bigger than the highest record number in the .rec
342 * file, extend the .rec file.
344 while (_vl_getpos(fcb, recnum + 1) > fcb->datsize * ISPAGESIZE) {
345 fcb->datsize = _extend_file(fcb, fcb->datfd, fcb->datsize);
347 /* Sync the updates to avoid buffer pool overflow. */
350 (void)_isfcb_cntlpg_w2(fcb);
354 * Mark all records in the range <fcb->lastrecno+1, recnum> as
357 tailoff = VL_RECDELETED;
358 stlong(tailoff, tailoffbuf);
359 for (recnum2 = fcb->lastrecno + 1; recnum2 <= recnum; recnum2++) {
360 _cp_tofile(fcb, fcb->datfd, tailoffbuf, _vl_getpos(fcb, recnum2), LONGSIZE);
361 strecno(fcb->freerecno, recnumbuf);
362 _cp_tofile(fcb, fcb->datfd, recnumbuf,
363 _vl_getpos(fcb, recnum2) + LONGSIZE, RECNOSIZE);
364 fcb->freerecno = recnum2;
366 /* Sync the updates to avoid buffer pool overflow. */
369 fcb->lastrecno = recnum;
370 (void)_isfcb_cntlpg_w2(fcb);
374 * Note that the disk structures are in a consistent state now,
375 * the .rec was extended by a few records marked as 'deleted'.
376 * This is important for subsequent rollbacks.
381 * If recnum specifies a record that has existed, check whether it
382 * has been deleted. _vlrec_wrrec() does not override existing record.
384 _cp_fromfile(fcb, fcb->datfd, tailoffbuf, rec_position, LONGSIZE);
385 tailoff = ldlong(tailoffbuf);
386 if (tailoff != VL_RECDELETED) {
391 * Remove the record from the chain of deleted records.
393 remove_from_chain2(fcb, recnum);
396 * Store variable part of record (the 'tail') in .var file.
398 tailoff = _istail_insert(fcb, record + fcb->minreclen, reclen - fcb->minreclen);
401 * Copy new record to the .rec file.
403 stlong(tailoff, tailoffbuf);
404 _cp_tofile(fcb, fcb->datfd, tailoffbuf, rec_position, LONGSIZE);
405 _cp_tofile(fcb, fcb->datfd, record, rec_position + LONGSIZE, fcb->minreclen);
411 * remove_from_chain(fcb, recnum)
413 * Remove record from the chain of deleted records.
417 remove_from_chain2(Fcb *fcb, Recno recnum)
419 char recnobuf1 [RECNOSIZE] , recnobuf2 [RECNOSIZE];
423 pos1 = _vl_getpos(fcb, recnum);
424 _cp_fromfile(fcb, fcb->datfd, recnobuf1, pos1 + LONGSIZE, RECNOSIZE);
426 if (fcb->freerecno == recnum) {
427 fcb->freerecno = ldrecno(recnobuf1);
430 recnum2 = fcb->freerecno;
432 pos2 = _vl_getpos(fcb, recnum2);
433 _cp_fromfile(fcb, fcb->datfd, recnobuf2, pos2 + LONGSIZE, RECNOSIZE);
434 recnum2 = ldrecno(recnobuf2);
435 } while (recnum2 != recnum && recnum2 != NULL_RECNO);
437 _cp_tofile(fcb, fcb->datfd, recnobuf1, pos2 + LONGSIZE, RECNOSIZE);
443 * The following are functions that manipulate the 'tails' of variable
444 * records. The tail is the actual record with the fixed part removed
445 * (fixed part starts at offset zero and its length is minimum record
446 * length. The tails are stored in the .var file.
450 /* Insert tail into .var file. Return offset in .var file */
452 long _istail_insert(Fcb *fcb, char *tailp, int taillen)
454 char frameheadbuf [2 * SHORTSIZE];
458 /* printf ("_insert called, taillen %d\n", taillen); */
461 return (VL_RECNOTAIL);
463 framelen = taillen + 2 * SHORTSIZE;
466 * Set up frame header.
468 stshort((short)taillen, frameheadbuf + VR_FRAMELEN_OFF);
469 stshort((short)taillen, frameheadbuf + VR_TAILLEN_OFF);
471 offset = fcb->varend;
474 * Extend .var file if that is necesary.
476 while (offset + framelen > fcb->varsize * ISPAGESIZE)
477 fcb->varsize = _extend_file(fcb, fcb->varfd, fcb->varsize);
480 * Copy frame head and tail to .var file.
482 _cp_tofile(fcb, fcb->varfd, frameheadbuf, offset, 2 * SHORTSIZE);
483 _cp_tofile(fcb, fcb->varfd, tailp, offset + 2 * SHORTSIZE, taillen);
485 fcb->varend += taillen + 2 * SHORTSIZE;
490 /* Remove tail from .var file */
493 Static void _istail_delete(Fcb *fcb, long offset)
496 * Don't do anything in NetISAM 1.0. The tails are lost, the space
497 * will be re-used after next restructuring: "copy -c file" command
502 /* Read tail from .var file */
504 Static _istail_read(Fcb *fcb, long offset, char *buffer)
506 char frameheadbuf [2 * SHORTSIZE];
509 /* printf ("_read called, offset %d\n", offset); */
511 if (offset == VL_RECNOTAIL)
517 _cp_fromfile(fcb, fcb->varfd, frameheadbuf, offset, 2 * SHORTSIZE);
518 taillen = ldshort(frameheadbuf + VR_TAILLEN_OFF);
519 _cp_fromfile(fcb, fcb->varfd, buffer, offset + 2 * SHORTSIZE, taillen);
524 /* Rewrite tail. Returns -1 if the new tail is longer than the original frame */
526 int _istail_modify(Fcb *fcb, long offset, char *tailp, int taillen)
528 char frameheadbuf [2 * SHORTSIZE];
532 * Trivial case: no old frame, no new tail.
534 if (offset == VL_RECNOTAIL && taillen == 0)
537 if (offset != VL_RECNOTAIL) {
541 _cp_fromfile(fcb, fcb->varfd, frameheadbuf, offset, 2 * SHORTSIZE);
542 framelen = ldshort(frameheadbuf + VR_FRAMELEN_OFF);
547 if (taillen > framelen) {
549 * Delete the old frame if the new tail does not fit.
550 * Insert the new tail at the end of .var file.
553 _istail_delete(fcb, offset);
554 return (_istail_insert(fcb, tailp, taillen));
558 * The new tail fits in the existing frame.
560 stshort((short)taillen, frameheadbuf + VR_TAILLEN_OFF);
561 _cp_tofile(fcb, fcb->varfd, frameheadbuf, offset, 2 * SHORTSIZE);
562 _cp_tofile(fcb, fcb->varfd, tailp, offset + 2 * SHORTSIZE, taillen);