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: isfixrec.c /main/3 1995/10/23 11:39:42 rswiston $ */
29 * Copyright (c) 1988 by Sun Microsystems, Inc.
36 * Fixed length record access (FLRA) module.
39 #include "isam_impl.h"
42 long _fl_getpos(); /* Get offset in .rec file */
43 int _fl_deleted(); /* 0/1 returns 1 if record is deleted */
44 static void remove_from_chain(); /* used by _flrec_wrrec() */
47 * _flrec_write(fcb, record, recnum, reclen)
52 * FCB File Control Block
53 * record record buffer
54 * reclen record length (NOT USED)
57 * recnum record number of the new record
59 * Returns 0 if record was written successfully, or -1 if any error.
64 _flrec_write(Fcb *fcb, char *record, Recno *recnum, int reclen)
68 char delflag = FL_RECEXISTS;
69 char recnobuf [RECNOSIZE];
72 * Reuse a deleted record if one exits.
73 * Otherwise, extend .rec file by a record.
75 if (fcb->freerecno != NULL_RECNO) {
76 recnum2 = fcb->freerecno;
79 * Remove record from the chain of deleted records.
81 rec_position = _fl_getpos(fcb, recnum2); /* Offset in .rec file */
82 _cp_fromfile(fcb, fcb->datfd, recnobuf, rec_position + 1, RECNOSIZE);
83 fcb->freerecno = ldrecno(recnobuf);
86 recnum2 = ++(fcb->lastrecno);
89 * Extend .rec file size if necessary.
91 while (_fl_getpos(fcb, recnum2 + 1) > fcb->datsize * ISPAGESIZE) {
92 fcb->datsize = _extend_file(fcb, fcb->datfd, fcb->datsize);
94 rec_position = _fl_getpos(fcb, recnum2); /* Offset in .rec file */
99 * Copy record to the .at file. Mark record as undeleted.
101 _cp_tofile(fcb, fcb->datfd, &delflag, rec_position, 1);
102 _cp_tofile(fcb, fcb->datfd, record, rec_position + 1, fcb->minreclen);
110 * _flrec_read(fcb, record, recnum, reclen)
115 * FCB File Control Block
116 * recnum record number of the record
117 * reclen filled with the record size for compatibilty with
118 * variable length records
121 * record record buffer is filled with data
123 * Returns 0 if record was read successfully, or error code if any error.
127 _flrec_read(Fcb *fcb, char *record, Recno recnum, int *reclen)
133 * Check that recnum is within the range of existing record numbers.
135 if (recnum < 1 || recnum > fcb->lastrecno)
138 rec_position = _fl_getpos(fcb, recnum); /* Offset in .rec file */
141 * Check that the record is not marked as deleted.
143 _cp_fromfile(fcb, fcb->datfd, &delflag, rec_position, 1);
144 if (delflag == FL_RECDELETED) {
149 * Copy record from the .at file.
151 _cp_fromfile(fcb, fcb->datfd, record, rec_position + 1, fcb->minreclen);
153 *reclen = fcb->minreclen;
159 * pos = _fl_getpos(fcb, recnum)
161 * Calculate the position of record in .rec file.
165 _fl_getpos(Fcb *fcb, Recno recnum)
167 return ((long)(ISCNTLSIZE + (fcb->minreclen + 1) * (recnum -1)));
171 * _flrec_rewrite(fcb, record, recnum, reclen)
176 * FCB File Control Block
177 * recnum record number of the record
179 * int reclen (NOT USED)
181 * Returns 0 if record was rewritten successfully, or error code if any error.
186 _flrec_rewrite(Fcb *fcb, char *record, Recno recnum, int reclen)
192 * Check that recnum is within the range of existing record numbers.
194 if (recnum < 1 || recnum > fcb->lastrecno)
197 rec_position = _fl_getpos(fcb, recnum); /* Offset in .rec file */
200 * Check that the record is not marked as deleted.
202 _cp_fromfile(fcb, fcb->datfd, &delflag, rec_position, 1);
203 if (delflag == FL_RECDELETED) {
208 * Copy new record to the .rec file.
210 _cp_tofile(fcb, fcb->datfd, record, rec_position + 1, fcb->minreclen);
216 * _flrec_delete(fcb, recnum)
221 * FCB File Control Block
222 * recnum record number of the record
224 * Returns 0 if record was rewritten successfully, or error code if any error.
228 _flrec_delete(Fcb *fcb, Recno recnum)
232 char recnobuf [RECNOSIZE];
235 * Check that recnum is within the range of existing record numbers.
237 if (recnum < 1 || recnum > fcb->lastrecno)
240 rec_position = _fl_getpos(fcb, recnum); /* Offset in .rec file */
243 * Check that the record is not marked as deleted.
245 _cp_fromfile(fcb, fcb->datfd, &delflag, rec_position, 1);
246 if (delflag == FL_RECDELETED) {
251 * Set the delete flag to FL_RECDELETED.
253 delflag = FL_RECDELETED;
254 _cp_tofile(fcb, fcb->datfd, &delflag, rec_position, 1);
257 * Insert record into chain of deleted records.
259 strecno(fcb->freerecno, recnobuf);
260 _cp_tofile(fcb, fcb->datfd, recnobuf, rec_position + 1, RECNOSIZE);
261 fcb->freerecno = recnum;
267 * _flrec_wrrec(fcb, record, recnum, reclen)
269 * Write a record by record number.
272 * FCB File Control Block
273 * recnum record number of the record
274 * record record buffer
275 * int reclen (NOT USED)
277 * Returns 0 if record was written successfully, or error code if any error.
279 * Note that _flrec_wrrec() commits updates and syncs the FCB to avoid
280 * buffer pool overflow.
285 _flrec_wrrec(Fcb *fcb, char *record, Recno recnum, int reclen)
290 char recnumbuf [RECNOSIZE];
293 * Check that recnum is not negative.
298 rec_position = _fl_getpos(fcb, recnum); /* Offset in .rec file */
300 if (recnum > fcb->lastrecno) {
303 * If the recnum is bigger than the highest record number in the .rec
304 * file, extend the .rec file.
306 while (_fl_getpos(fcb, recnum + 1) > fcb->datsize * ISPAGESIZE) {
307 fcb->datsize = _extend_file(fcb, fcb->datfd, fcb->datsize);
309 /* Sync the updates to avoid buffer pool overflow. */
312 (void)_isfcb_cntlpg_w2(fcb);
316 * Mark all records in the range <fcb->lastrecno+1, recnum> as
319 delflag = FL_RECDELETED;
320 for (recnum2 = fcb->lastrecno + 1; recnum2 <= recnum; recnum2++) {
321 _cp_tofile(fcb, fcb->datfd, &delflag, _fl_getpos(fcb, recnum2), 1);
322 strecno(fcb->freerecno, recnumbuf);
323 _cp_tofile(fcb, fcb->datfd, recnumbuf,
324 _fl_getpos(fcb, recnum2) + 1, RECNOSIZE);
325 fcb->freerecno = recnum2;
327 /* Sync the updates to avoid buffer pool overflow. */
330 fcb->lastrecno = recnum;
331 (void)_isfcb_cntlpg_w2(fcb);
335 * Note that the disk structures are in a consistent state now,
336 * the .rec was extended by a few records marked as 'deleted'.
337 * This is important for subsequent rollbacks.
342 * If recnum specifies a record that has existed, check whether it
343 * has been deleted. _flrec_wrrec() does not override existing record.
345 _cp_fromfile(fcb, fcb->datfd, &delflag, rec_position, 1);
346 if (delflag == FL_RECEXISTS) {
351 * Remove the record from the chain of deleted records.
353 remove_from_chain(fcb, recnum);
356 * Copy new record to the .rec file.
358 delflag = FL_RECEXISTS;
359 _cp_tofile(fcb, fcb->datfd, &delflag, rec_position, 1);
360 _cp_tofile(fcb, fcb->datfd, record, rec_position + 1, fcb->minreclen);
366 * remvoe_from_chain(fcb, recnum)
368 * Remove record from the chain of deleted records.
372 remove_from_chain(Fcb *fcb, Recno recnum)
374 char recnobuf1 [RECNOSIZE] , recnobuf2 [RECNOSIZE];
378 pos1 = _fl_getpos(fcb, recnum);
379 _cp_fromfile(fcb, fcb->datfd, recnobuf1, pos1 + 1, RECNOSIZE);
381 if (fcb->freerecno == recnum) {
382 fcb->freerecno = ldrecno(recnobuf1);
385 recnum2 = fcb->freerecno;
387 pos2 = _fl_getpos(fcb, recnum2);
388 _cp_fromfile(fcb, fcb->datfd, recnobuf2, pos2 + 1, RECNOSIZE);
389 recnum2 = ldrecno(recnobuf2);
390 } while (recnum2 != recnum && recnum2 != NULL_RECNO);
392 _cp_tofile(fcb, fcb->datfd, recnobuf1, pos2 + 1, RECNOSIZE);