isamopen.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. /*%% $TOG: isamopen.c /main/5 1998/04/10 08:03:57 mgreess $ */
  28. /*
  29. * Copyright (c) 1988 by Sun Microsystems, Inc.
  30. */
  31. /*
  32. * isamopen.c
  33. *
  34. * Description: _amopen()
  35. * Open an ISAM file.
  36. *
  37. *
  38. */
  39. #include <stdlib.h>
  40. #include "isam_impl.h"
  41. /*
  42. * _amopen(isfname, varflag, minlen, maxlen, primkey,
  43. * isfhandle, curpos, errcode)
  44. *
  45. * _amopen() opens an ISAM file.
  46. *
  47. * Input params:
  48. * isfname ISAM file name
  49. *
  50. * Output params:
  51. * isfhandle a file handle to be used in subsequent operations on the file
  52. * varflag TRUE if file is for variable length records
  53. * minlen, maxlen minimum and maximum record length
  54. * curpos initial current record position
  55. * errcode {iserrno, isstat1-4}
  56. *
  57. * _amopen() returns 0 if successful, or -1 to indicate an error.
  58. */
  59. /* ARGSUSED */
  60. int
  61. _amopen(char *isfname, enum openmode openmode, Bool *varflag,
  62. int *minlen, int *maxlen, Bytearray *isfhandle,
  63. Bytearray *curpos, struct errcode *errcode)
  64. {
  65. Fcb *fcb;
  66. Bytearray isfhandle0;
  67. Crp *crp;
  68. _isam_entryhook();
  69. /*
  70. * Make isfhandle0.
  71. */
  72. isfhandle0 = _makeisfhandle(isfname);
  73. /*
  74. * Invalidate the FCB cache entry to avoid a problem found when using
  75. * multiple servers.
  76. */
  77. if ((fcb = _mngfcb_find(&isfhandle0)) != NULL) {
  78. (void) _watchfd_decr(_isfcb_nfds(fcb));
  79. _isfcb_close(fcb);
  80. _mngfcb_delete(&isfhandle0);
  81. }
  82. /*
  83. * Get the FCB that corresponds to the isfhandle handle.
  84. */
  85. if ((fcb = _openfcb(&isfhandle0, errcode)) == NULL) {
  86. goto ERROR;
  87. }
  88. /*
  89. * Check if the FCB allows writes if INOUT or OUTPUT is needed.
  90. */
  91. if (fcb->rdonly==TRUE && (openmode==OM_INOUT || openmode==OM_OUTPUT)) {
  92. _amseterrcode(errcode, EACCES);
  93. goto ERROR;
  94. }
  95. /*
  96. * Fill output parameters.
  97. */
  98. *minlen = fcb->minreclen;
  99. *maxlen = fcb->maxreclen;
  100. *varflag = fcb->varflag;
  101. *isfhandle = isfhandle0;
  102. /*
  103. * Initial current record position.
  104. */
  105. if (FCB_NOPRIMARY_KEY(fcb)) {
  106. /* Use physical order. */
  107. crp = (Crp *) _ismalloc(sizeof(*crp));
  108. memset ((char *) crp, 0, sizeof(*crp));
  109. crp->keyid = PHYS_ORDER;
  110. crp->flag = CRP_BEFOREANY;
  111. curpos->length = sizeof(*crp);
  112. curpos->data = (char *) crp;
  113. }
  114. else {
  115. /*
  116. * Use primary key order.
  117. */
  118. crp = (Crp *) _ismalloc((unsigned)(sizeof(*crp) + fcb->keys[0].k2_len));
  119. memset((char *) crp, 0, (sizeof(*crp) + fcb->keys[0].k2_len));
  120. crp->keyid = fcb->keys[0].k2_keyid;
  121. crp->flag = CRP_BEFOREANY;
  122. _iskey_fillmin(&fcb->keys[0], crp->key);
  123. curpos->length = sizeof(*crp) + fcb->keys[0].k2_len;
  124. curpos->data = (char *) crp;
  125. /*
  126. * Set full key length as the number of bytes to match in key comparison
  127. */
  128. crp->matchkeylen = fcb->keys[0].k2_len - RECNOSIZE;
  129. if (ALLOWS_DUPS2(&fcb->keys[0]))
  130. crp->matchkeylen -= DUPIDSIZE;
  131. }
  132. _isam_exithook();
  133. return (ISOK);
  134. ERROR:
  135. _bytearr_free(&isfhandle0);
  136. _isam_exithook();
  137. return (ISERROR);
  138. }
  139. /*
  140. * fcb = _openfcb(isfhandle, errcode)
  141. *
  142. * Try to locate FCB in the FCB cache. If not found, open the FCB and
  143. * insert it into the cache.
  144. *
  145. * Return a pointer to the FCB, or NULL in the case of any error.
  146. */
  147. #define FDNEEDED 3 /* Needs 3 UNIX fd to open a file*/
  148. Fcb *
  149. _openfcb(Bytearray *isfhandle, struct errcode *errcode)
  150. {
  151. Fcb *fcb;
  152. Bytearray *isfhandle2;
  153. char errbuf[BUFSIZ];
  154. char *errmsg0;
  155. if ((fcb = _mngfcb_find(isfhandle)) != NULL)
  156. goto out;
  157. /*
  158. * Check that there are UNIX file descriptors available.
  159. */
  160. while (_watchfd_check() < FDNEEDED) {
  161. /*
  162. * Find victim (LRU FCB) and close it.
  163. */
  164. if((isfhandle2 = _mngfcb_victim()) == NULL)
  165. _isfatal_error ("_openfcb() cannot find LRU victim");
  166. fcb = _mngfcb_find(isfhandle2);
  167. (void) _watchfd_decr(_isfcb_nfds(fcb));
  168. _isfcb_close(fcb);
  169. _mngfcb_delete(isfhandle2);
  170. }
  171. /*
  172. * Open files, create FCB block.
  173. */
  174. if ((fcb = _isfcb_open(_getisfname(isfhandle), errcode)) == NULL) {
  175. return (NULL);
  176. }
  177. /*
  178. * Check that ISAM file is not corrupted (only check magic number).
  179. */
  180. if (_check_isam_magic(fcb) != ISOK) {
  181. _amseterrcode(errcode, EBADFILE);
  182. _isfcb_close(fcb);
  183. return (NULL);
  184. }
  185. /*
  186. * Read information from CNTL PAGE and store it in the FCB.
  187. */
  188. if (_isfcb_cntlpg_r(fcb) == ISERROR)
  189. _isfatal_error("_openfcb() cannot read CNTL PAGE");
  190. /*
  191. * Register UNIX file descriptors consumed.
  192. */
  193. (void) _watchfd_incr(_isfcb_nfds(fcb));
  194. /*
  195. * Insert new entry into the FCB cache.
  196. */
  197. _mngfcb_insert(fcb, isfhandle);
  198. out:
  199. /*
  200. * Check that all file descriptors are open. This is needed to handle
  201. * user errors such as removing .ind or .var files.
  202. */
  203. if (fcb->varflag==TRUE && fcb->varfd == -1) {
  204. char *fmt = "%s.var has been removed";
  205. if (strlen(fmt) + strlen(fcb->isfname) + 1 >= BUFSIZ)
  206. errmsg0 = (char*) malloc(strlen(fmt) + strlen(fcb->isfname) + 1);
  207. else
  208. errmsg0 = errbuf;
  209. (void)sprintf(errmsg0, fmt, fcb->isfname);
  210. _isam_warning(errmsg0);
  211. _amseterrcode(errcode, EBADFILE);
  212. if (errmsg0 != errbuf) free(errmsg0);
  213. goto err;
  214. }
  215. if ((fcb->nkeys > 1 || !FCB_NOPRIMARY_KEY(fcb)) && fcb->indfd == -1) {
  216. char *fmt = "%s.ind has been removed";
  217. if (strlen(fmt) + strlen(fcb->isfname) + 1 >= BUFSIZ)
  218. errmsg0 = (char*) malloc(strlen(fmt) + strlen(fcb->isfname) + 1);
  219. else
  220. errmsg0 = errbuf;
  221. (void)sprintf(errmsg0, fmt, fcb->isfname);
  222. _isam_warning(errmsg0);
  223. _amseterrcode(errcode, EBADFILE);
  224. if (errmsg0 != errbuf) free(errmsg0);
  225. goto err;
  226. }
  227. return (fcb);
  228. err:
  229. /*
  230. * Delete FCB and remove it from FCB cache. Close UNIX fds.
  231. *
  232. * This is needed to recover netisamd server if users remove .ind or
  233. * or .var files.
  234. */
  235. (void) _watchfd_decr(_isfcb_nfds(fcb));
  236. _isfcb_close(fcb);
  237. _mngfcb_delete(isfhandle);
  238. return (NULL);
  239. }
  240. /*
  241. * isfname = _getisfname(isfhandle)
  242. *
  243. * Get ISAM file name from ISAM file handle.
  244. */
  245. char *
  246. _getisfname(Bytearray *isfhandle)
  247. {
  248. return (isfhandle->data);
  249. }
  250. /*
  251. * isfhandle = _makeisfhandle(isfname)
  252. *
  253. * Make ISAM file handle.
  254. */
  255. Bytearray
  256. _makeisfhandle(char *isfname)
  257. {
  258. return (_bytearr_new((u_short)(strlen(isfname) + 1), isfname));
  259. }