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 librararies 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: isfixrec.c /main/3 1995/10/23 11:39:42 rswiston $ */
29 static char sccsid[] = "@(#)isfixrec.c 1.5 89/07/17 Copyr 1988 Sun Micro";
32 * Copyright (c) 1988 by Sun Microsystems, Inc.
39 * Fixed length record access (FLRA) module.
42 #include "isam_impl.h"
45 long _fl_getpos(); /* Get offset in .rec file */
46 int _fl_deleted(); /* 0/1 returns 1 if record is deleted */
47 static void remove_from_chain(); /* used by _flrec_wrrec() */
50 * _flrec_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 succesfully, or -1 if any error.
67 _flrec_write(fcb, record, recnum, reclen)
75 char delflag = FL_RECEXISTS;
76 char recnobuf [RECNOSIZE];
79 * Reuse a deleted record if one exits.
80 * Otherwise, extend .rec file by a record.
82 if (fcb->freerecno != NULL_RECNO) {
83 recnum2 = fcb->freerecno;
86 * Remove record from the chain of deleted records.
88 rec_position = _fl_getpos(fcb, recnum2); /* Offset in .rec file */
89 _cp_fromfile(fcb, fcb->datfd, recnobuf, rec_position + 1, RECNOSIZE);
90 fcb->freerecno = ldrecno(recnobuf);
93 recnum2 = ++(fcb->lastrecno);
96 * Extend .rec file size if necessary.
98 while (_fl_getpos(fcb, recnum2 + 1) > fcb->datsize * ISPAGESIZE) {
99 fcb->datsize = _extend_file(fcb, fcb->datfd, fcb->datsize);
101 rec_position = _fl_getpos(fcb, recnum2); /* Offset in .rec file */
106 * Copy record to the .at file. Mark record as undeleted.
108 _cp_tofile(fcb, fcb->datfd, &delflag, rec_position, 1);
109 _cp_tofile(fcb, fcb->datfd, record, rec_position + 1, fcb->minreclen);
117 * _flrec_read(fcb, record, recnum, reclen)
122 * FCB File Control Block
123 * recnum record number of the record
124 * reclen filled with the record size for compatibilty with
125 * variable length records
128 * record record buffer is filled with data
130 * Returns 0 if record was read succesfully, or error code if any error.
134 _flrec_read(fcb, record, recnum, reclen)
144 * Check that recnum is within the range of existing record numbers.
146 if (recnum < 1 || recnum > fcb->lastrecno)
149 rec_position = _fl_getpos(fcb, recnum); /* Offset in .rec file */
152 * Check that the record is not marked as deleted.
154 _cp_fromfile(fcb, fcb->datfd, &delflag, rec_position, 1);
155 if (delflag == FL_RECDELETED) {
160 * Copy record from the .at file.
162 _cp_fromfile(fcb, fcb->datfd, record, rec_position + 1, fcb->minreclen);
164 *reclen = fcb->minreclen;
170 * pos = _fl_getpos(fcb, recnum)
172 * Calculate the position of record in .rec file.
176 _fl_getpos(fcb, recnum)
180 return ((long)(ISCNTLSIZE + (fcb->minreclen + 1) * (recnum -1)));
184 * _flrec_rewrite(fcb, record, recnum, reclen)
189 * FCB File Control Block
190 * recnum record number of the record
192 * int reclen (NOT USED)
194 * Returns 0 if record was rewritten succesfully, or error code if any error.
199 _flrec_rewrite(fcb, record, recnum, reclen)
209 * Check that recnum is within the range of existing record numbers.
211 if (recnum < 1 || recnum > fcb->lastrecno)
214 rec_position = _fl_getpos(fcb, recnum); /* Offset in .rec file */
217 * Check that the record is not marked as deleted.
219 _cp_fromfile(fcb, fcb->datfd, &delflag, rec_position, 1);
220 if (delflag == FL_RECDELETED) {
225 * Copy new record to the .rec file.
227 _cp_tofile(fcb, fcb->datfd, record, rec_position + 1, fcb->minreclen);
233 * _flrec_delete(fcb, recnum)
238 * FCB File Control Block
239 * recnum record number of the record
241 * Returns 0 if record was rewritten succesfully, or error code if any error.
245 _flrec_delete(fcb, recnum)
251 char recnobuf [RECNOSIZE];
254 * Check that recnum is within the range of existing record numbers.
256 if (recnum < 1 || recnum > fcb->lastrecno)
259 rec_position = _fl_getpos(fcb, recnum); /* Offset in .rec file */
262 * Check that the record is not marked as deleted.
264 _cp_fromfile(fcb, fcb->datfd, &delflag, rec_position, 1);
265 if (delflag == FL_RECDELETED) {
270 * Set the delete flag to FL_RECDELETED.
272 delflag = FL_RECDELETED;
273 _cp_tofile(fcb, fcb->datfd, &delflag, rec_position, 1);
276 * Insert record into chain of deleted records.
278 strecno(fcb->freerecno, recnobuf);
279 _cp_tofile(fcb, fcb->datfd, recnobuf, rec_position + 1, RECNOSIZE);
280 fcb->freerecno = recnum;
286 * _flrec_wrrec(fcb, record, recnum, reclen)
288 * Write a record by record number.
291 * FCB File Control Block
292 * recnum record number of the record
293 * record record buffer
294 * int reclen (NOT USED)
296 * Returns 0 if record was written succesfully, or error code if any error.
298 * Note that _flrec_wrrec() commits updates and syncs the FCB to avoid
299 * buffer pool overflow.
304 _flrec_wrrec(fcb, record, recnum, reclen)
313 char recnumbuf [RECNOSIZE];
316 * Check that recnum is not negative.
321 rec_position = _fl_getpos(fcb, recnum); /* Offset in .rec file */
323 if (recnum > fcb->lastrecno) {
326 * If the recnum is bigger than the highest record number in the .rec
327 * file, extend the .rec file.
329 while (_fl_getpos(fcb, recnum + 1) > fcb->datsize * ISPAGESIZE) {
330 fcb->datsize = _extend_file(fcb, fcb->datfd, fcb->datsize);
332 /* Sync the updates to avoid buffer pool overflow. */
335 (void)_isfcb_cntlpg_w2(fcb);
339 * Mark all records in the range <fcb->lastrecno+1, recnum> as
342 delflag = FL_RECDELETED;
343 for (recnum2 = fcb->lastrecno + 1; recnum2 <= recnum; recnum2++) {
344 _cp_tofile(fcb, fcb->datfd, &delflag, _fl_getpos(fcb, recnum2), 1);
345 strecno(fcb->freerecno, recnumbuf);
346 _cp_tofile(fcb, fcb->datfd, recnumbuf,
347 _fl_getpos(fcb, recnum2) + 1, RECNOSIZE);
348 fcb->freerecno = recnum2;
350 /* Sync the updates to avoid buffer pool overflow. */
353 fcb->lastrecno = recnum;
354 (void)_isfcb_cntlpg_w2(fcb);
358 * Note that the disk structures are in a consistent state now,
359 * the .rec was extended by a few records marked as 'deleted'.
360 * This is important for subsequent rollbacks.
365 * If recnum specifies a record that has existed, check whether it
366 * has been deleted. _flrec_wrrec() does not override existing record.
368 _cp_fromfile(fcb, fcb->datfd, &delflag, rec_position, 1);
369 if (delflag == FL_RECEXISTS) {
374 * Remove the record from the chain of deleted records.
376 remove_from_chain(fcb, recnum);
379 * Copy new record to the .rec file.
381 delflag = FL_RECEXISTS;
382 _cp_tofile(fcb, fcb->datfd, &delflag, rec_position, 1);
383 _cp_tofile(fcb, fcb->datfd, record, rec_position + 1, fcb->minreclen);
389 * remvoe_from_chain(fcb, recnum)
391 * Remove record from the chain of deleted records.
395 remove_from_chain(fcb, recnum)
399 char recnobuf1 [RECNOSIZE] , recnobuf2 [RECNOSIZE];
403 pos1 = _fl_getpos(fcb, recnum);
404 _cp_fromfile(fcb, fcb->datfd, recnobuf1, pos1 + 1, RECNOSIZE);
406 if (fcb->freerecno == recnum) {
407 fcb->freerecno = ldrecno(recnobuf1);
410 recnum2 = fcb->freerecno;
412 pos2 = _fl_getpos(fcb, recnum2);
413 _cp_fromfile(fcb, fcb->datfd, recnobuf2, pos2 + 1, RECNOSIZE);
414 recnum2 = ldrecno(recnobuf2);
415 } while (recnum2 != recnum && recnum2 != NULL_RECNO);
417 _cp_tofile(fcb, fcb->datfd, recnobuf1, pos2 + 1, RECNOSIZE);