2
0

isfileio.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  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)
  10. * any later version.
  11. *
  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
  16. * details.
  17. *
  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
  22. */
  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: isfileio.c /main/3 1995/10/23 11:39:26 rswiston $ */
  28. /*
  29. * Copyright (c) 1988 by Sun Microsystems, Inc.
  30. */
  31. /*
  32. * isfileio.c
  33. *
  34. * Description:
  35. * I/O operations on UNIX files associated with FCB
  36. */
  37. #include "isam_impl.h"
  38. static int _getoffset();
  39. static Blkno _getblkno();
  40. /*
  41. * _cp_tofile(fcb, unixfd, data, pos, len)
  42. *
  43. * Copy data to a file associated with FCB (unixfd distinguishes among
  44. * .rec, .ind., and .var file). The data are copied from buffer data to
  45. * the file at offset pos. The number of bytes to copy is in len.
  46. */
  47. void
  48. _cp_tofile(Fcb *fcb, int unixfd, char *data, long pos, int len)
  49. {
  50. int offset; /* Offset within a page */
  51. Blkno blkno;
  52. Bufhdr *bufhdr;
  53. int nbytes;
  54. /*
  55. * Data may span multiple blocks.
  56. */
  57. while (len > 0) {
  58. blkno = _getblkno(pos);
  59. offset = _getoffset(pos);
  60. nbytes = (len < ISPAGESIZE - offset) ? len : ISPAGESIZE - offset;
  61. bufhdr = _isdisk_fix(fcb, unixfd, blkno, ISFIXWRITE);
  62. memcpy( bufhdr->isb_buffer + offset,data, nbytes);
  63. _isdisk_unfix(bufhdr);
  64. pos += nbytes;
  65. data += nbytes;
  66. len -= nbytes;
  67. }
  68. }
  69. /*
  70. * _cp_fromfile(fcb, unixfd, data, pos, len)
  71. *
  72. * Copy data from a file associated with FCB (unixfd distinguishes among
  73. * .rec, .ind., and .var file). The data are copied to buffer data from
  74. * the file at offset pos. The number of bytes to copy is in len.
  75. */
  76. void
  77. _cp_fromfile(Fcb *fcb, int unixfd, char *data, long pos, int len)
  78. {
  79. int offset; /* Offset within a page */
  80. Blkno blkno;
  81. Bufhdr *bufhdr;
  82. int nbytes;
  83. /*
  84. * Data may span multiple blocks.
  85. */
  86. while (len > 0) {
  87. blkno = _getblkno(pos);
  88. offset = _getoffset(pos);
  89. nbytes = (len < ISPAGESIZE - offset) ? len : ISPAGESIZE - offset;
  90. bufhdr = _isdisk_fix(fcb, unixfd, blkno, ISFIXREAD);
  91. memcpy( data,bufhdr->isb_buffer + offset, nbytes);
  92. _isdisk_unfix(bufhdr);
  93. pos += nbytes;
  94. data += nbytes;
  95. len -= nbytes;
  96. }
  97. }
  98. /*
  99. * extend_file(fcb, unixfd, oldsize)
  100. *
  101. * Extend UNIX file by one block.
  102. */
  103. Blkno
  104. _extend_file(Fcb *fcb, int unixfd, Blkno oldsize)
  105. {
  106. Bufhdr *bufhdr;
  107. char buf[ISPAGESIZE];
  108. /*
  109. * We must write something to the buffer, or we will get
  110. * segmentation fault when using mapped I/O.
  111. */
  112. _isseekpg(unixfd, oldsize);
  113. _iswritepg(unixfd, buf);
  114. bufhdr = _isdisk_fix(fcb, unixfd, oldsize, ISFIXNOREAD);
  115. _isdisk_unfix(bufhdr);
  116. return (oldsize + 1);
  117. }
  118. /*
  119. * off = _getoffset(pos)
  120. *
  121. * Calculate offset relative to the beginning of page
  122. */
  123. Static int
  124. _getoffset(long pos)
  125. {
  126. return ((int)(pos % ISPAGESIZE));
  127. }
  128. /*
  129. * blkno = _getblkno(pos)
  130. *
  131. * Calculate block number of the block containing position pos.
  132. */
  133. Static Blkno
  134. _getblkno(long pos)
  135. {
  136. return ((int)(pos / ISPAGESIZE));
  137. }