isbuild.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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: isbuild.c /main/3 1995/10/23 11:36:21 rswiston $ */
  28. /*
  29. * Copyright (c) 1988 by Sun Microsystems, Inc.
  30. */
  31. /*
  32. * isbuild.c
  33. *
  34. * Description:
  35. * Create an ISAM file.
  36. */
  37. #include "isam_impl.h"
  38. #include <unistd.h>
  39. #include <netdb.h>
  40. #include <sys/file.h>
  41. #include <sys/time.h>
  42. #include <sys/types.h>
  43. #include <sys/stat.h>
  44. static int
  45. _ambuild(char *isfname, enum openmode openmode, Bool varflag,
  46. int minlen, int maxlen, struct keydesc *primkey,
  47. int owner, int group, int umask,
  48. Bytearray *isfhandle, Bytearray *curpos,
  49. struct errcode *errcode);
  50. /*
  51. * isfd = isbuild(isfname, recordlength, primkey, mode)
  52. *
  53. * Isbuild() determines on which machine the ISAM file is to be built,
  54. * checks the permissions for creating a file by this user running on
  55. * this client machine by using the access(2) UNIX call.
  56. * If the file is remote, it is created by the netisamd daemon running
  57. * on the machine hosting the ISAM file. The chown(2) is then used to
  58. * change the ownership of the file to the client.
  59. * All UNIX files created will have their permissions set to 0666, allowing
  60. * thus both read and write access to anybody.
  61. *
  62. * Isbuild() returns an ISAM file descriptor (isfd) is the call was successful,
  63. * or a value of -1 if the call failed.
  64. *
  65. * Errors:
  66. * EBADARG Improper mode parameter
  67. * EBADARG isreclen >= recordlength and ISVARLEN specified
  68. * E2BIG recordlength greater than system imposed limit (8196)
  69. * EBADKEY Invalid key descriptor
  70. * EBADFILE ISAM file is corrupted or it is not an NetISAM file
  71. * EFNAME Invalid ISAM file name
  72. * ETOOMANY Too many ISAM file descriptors are in use (128 is the limit)
  73. *
  74. * The following error numbers are "borrowed" from UNIX.
  75. * EACCES UNIX file system protection denies creation of the file
  76. * EEXIST - ISAM file already exists
  77. * EEXIST - A UNIX file with the same name exists
  78. */
  79. int
  80. isbuild(char *isfname, int recordlength, struct keydesc *primkey, int mode)
  81. {
  82. Fab *fab;
  83. Isfd isfd;
  84. enum openmode openmode;
  85. int minreclen; /* Minimum record length */
  86. int origumask;
  87. /* Get file open mode part of the mode parameter. */
  88. if ((openmode = _getopenmode(mode)) == OM_BADMODE) {
  89. _setiserrno2(EBADARG, '9', '0');
  90. return (NOISFD);
  91. }
  92. /*
  93. * Minimum record length.
  94. */
  95. minreclen = ((mode & ISLENMODE) == ISVARLEN) ? isreclen : recordlength;
  96. /* Check recordlength against system imposed limit. */
  97. if (recordlength > ISMAXRECLEN) {
  98. _setiserrno2(E2BIG, '9', '0');
  99. return (NOISFD);
  100. }
  101. /* Check that ssminreclen >= ISMINRECLEN. */
  102. if (minreclen < ISMINRECLEN) {
  103. _setiserrno2(EBADARG, '9', '0');
  104. return (NOISFD);
  105. }
  106. /* Check that minreclen <= recordlength */
  107. if (minreclen > recordlength) {
  108. _setiserrno2(EBADARG, '9', '0');
  109. return (NOISFD);
  110. }
  111. /* Create a Fab object. */
  112. fab = _fab_new(isfname,
  113. openmode,
  114. (Bool)((mode & ISLENMODE) == ISVARLEN),
  115. minreclen,
  116. recordlength);
  117. if (fab == NULL) {
  118. return (NOISFD); /* iserrno is set by fab_new */
  119. }
  120. /* Get an ISAM file descriptor for this fab */
  121. if ((isfd = _isfd_insert(fab)) == NOISFD) {
  122. /* Table of ISAM file descriptors would overflow. */
  123. _fab_destroy(fab);
  124. _setiserrno2(ETOOMANY, '9', '0');
  125. return (NOISFD);
  126. }
  127. FAB_ISFDSET(fab, isfd);
  128. /*
  129. * Extract umask. It is send to the Acces Layer (which may reside
  130. * on a remote machine).
  131. */
  132. origumask = umask(0);
  133. (void)umask(origumask);
  134. /*
  135. * Call lower layers.
  136. */
  137. if (_ambuild(fab->isfname, fab->openmode, fab->varlength,
  138. fab->minreclen, fab->maxreclen, primkey, getuid(),
  139. getgid(), origumask, &fab->isfhandle, &fab->curpos,
  140. &fab->errcode)) {
  141. _seterr_errcode(&fab->errcode);
  142. _fab_destroy(fab);
  143. return (NOISFD);
  144. }
  145. return ((int)isfd); /* Successful isopen() */
  146. }
  147. /*
  148. * _ambuild(isfname, openmode, varflag, minlen, maxlen,
  149. * primkey, owner, group, umask, isfhandle, curpos, errcode)
  150. *
  151. * _ambuild() creates a new ISAM file with the name isfname.
  152. *
  153. * Input params:
  154. * isfname ISAM file name
  155. * varflag is 0/1 flag set to 1 if the file is for variable lengths records
  156. * minlen minimum length of record in bytes
  157. * maxlen maximum length of record in bytes
  158. * primkey definition of the primary key
  159. * owner, group set the ownership of the file to this user and group
  160. * umask application's value of umask
  161. *
  162. * Output params:
  163. * isfhandle a file handle to be used in subsequent operations on the file
  164. * curpos initial current record position
  165. * errcode {iserrno, isstat1-4}
  166. *
  167. * _ambuild() returns 0 if successful, or -1 to indicate an error.
  168. */
  169. #define FDNEEDED 3 /* Needs at most 3 UNIX fds to open ISAM file */
  170. /* ARGSUSED */
  171. static int
  172. _ambuild(char *isfname, enum openmode openmode, Bool varflag,
  173. int minlen, int maxlen, struct keydesc *primkey,
  174. int owner, int group, int umask,
  175. Bytearray *isfhandle, Bytearray *curpos,
  176. struct errcode *errcode)
  177. {
  178. Fcb *fcb = NULL;
  179. Bytearray *isfhandle2;
  180. Bytearray isfhandle0 = _bytearr_getempty();
  181. Keydesc2 keydesc2;
  182. int err;
  183. Crp *crp;
  184. _isam_entryhook();
  185. /*
  186. * Validate the primary key descriptor.
  187. */
  188. if (!USE_PHYS_ORDER(primkey) &&
  189. _validate_keydesc(primkey, minlen) == ISERROR) {
  190. _amseterrcode(errcode, EBADKEY);
  191. goto ERROR;
  192. }
  193. /*
  194. * Make isfhandle0.
  195. */
  196. isfhandle0 = _makeisfhandle(isfname);
  197. /*
  198. * Check that there is not entry with the same name in FCB cache.
  199. */
  200. if ((fcb = _mngfcb_find(&isfhandle0)) != NULL) {
  201. fcb = _mngfcb_find(&isfhandle0);
  202. (void) _watchfd_decr(_isfcb_nfds(fcb));
  203. _isfcb_close(fcb);
  204. _mngfcb_delete(&isfhandle0);
  205. }
  206. /*
  207. * Check that there are UNIX file descriptors available.
  208. */
  209. while (_watchfd_check() < FDNEEDED) {
  210. /*
  211. * Find victim (LRU FCB) and close it.
  212. */
  213. if((isfhandle2 = _mngfcb_victim()) == NULL)
  214. _isfatal_error ("_openfcb() cannot find LRU victim");
  215. fcb = _mngfcb_find(isfhandle2);
  216. (void) _watchfd_decr(_isfcb_nfds(fcb));
  217. _isfcb_close(fcb);
  218. _mngfcb_delete(isfhandle2);
  219. }
  220. /*
  221. * Create UNIX files, return isfhandle and File Control Block (fcb).
  222. */
  223. if ((fcb = _isfcb_create(isfname, 1, (primkey->k_nparts != 0), (int)varflag,
  224. owner, group, umask, errcode)) == NULL) {
  225. goto ERROR;
  226. }
  227. /*
  228. * Add length info to the FCB.
  229. */
  230. _isfcb_setreclength(fcb, varflag, minlen, maxlen);
  231. if (!USE_PHYS_ORDER(primkey)) {
  232. /*
  233. * Convert key descriptor to internal form.
  234. */
  235. _iskey_xtoi (&keydesc2, primkey);
  236. /*
  237. * Create index structure.
  238. */
  239. if ((err = _create_index(fcb , &keydesc2)) != ISOK) {
  240. _amseterrcode(errcode, err);
  241. goto ERROR;
  242. }
  243. /*
  244. * Add primary key descriptor to FCB.
  245. */
  246. if (_isfcb_primkeyadd(fcb, &keydesc2) == ISERROR) {
  247. _amseterrcode(errcode, ETOOMANY);
  248. goto ERROR;
  249. }
  250. }
  251. /*
  252. * Initial current record position.
  253. */
  254. if (FCB_NOPRIMARY_KEY(fcb)) {
  255. /* Use physical order. */
  256. crp = (Crp *) _ismalloc(sizeof(*crp));
  257. memset ((char *) crp, 0, sizeof(*crp));
  258. crp->keyid = PHYS_ORDER;
  259. crp->flag = CRP_BEFOREANY;
  260. curpos->length = sizeof(*crp);
  261. curpos->data = (char *) crp;
  262. }
  263. else {
  264. /*
  265. * Use primary key order.
  266. */
  267. crp = (Crp *) _ismalloc((unsigned)(sizeof(*crp) + fcb->keys[0].k2_len));
  268. memset((char *) crp, 0, (sizeof(*crp) + fcb->keys[0].k2_len));
  269. crp->keyid = fcb->keys[0].k2_keyid;
  270. crp->flag = CRP_BEFOREANY;
  271. _iskey_fillmin(&fcb->keys[0], crp->key);
  272. curpos->length = sizeof(*crp) + fcb->keys[0].k2_len;
  273. curpos->data = (char *) crp;
  274. /*
  275. * Set full key length as the number of bytes to match in key comparison
  276. */
  277. crp->matchkeylen = fcb->keys[0].k2_len - RECNOSIZE;
  278. if (ALLOWS_DUPS2(&fcb->keys[0]))
  279. crp->matchkeylen -= DUPIDSIZE;
  280. }
  281. _amseterrcode(errcode, ISOK);
  282. /*
  283. * Register the number of UNIX fd consumed.
  284. */
  285. (void) _watchfd_incr(_isfcb_nfds(fcb));
  286. /*
  287. * Insert new entry into FCB cache.
  288. */
  289. _mngfcb_insert(fcb, &isfhandle0);
  290. *isfhandle = isfhandle0;
  291. /* Commit all work in disk cache. */
  292. _issignals_mask();
  293. _isdisk_commit();
  294. _isdisk_sync();
  295. _isdisk_inval();
  296. /*
  297. * Create Control Page (CNTLPAGE).
  298. */
  299. if (_isfcb_cntlpg_w(fcb) == ISERROR) {
  300. _issignals_unmask();
  301. goto ERROR;
  302. }
  303. _issignals_unmask();
  304. _isam_exithook();
  305. return (ISOK);
  306. ERROR:
  307. if (fcb != NULL) {
  308. _isfcb_remove(fcb);
  309. _isfcb_close(fcb);
  310. }
  311. _bytearr_free(&isfhandle0);
  312. _isam_exithook();
  313. return (ISERROR);
  314. }