iserase.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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: iserase.c /main/3 1995/10/23 11:38:08 rswiston $ */
  28. /*
  29. * Copyright (c) 1988 by Sun Microsystems, Inc.
  30. */
  31. /*
  32. * iserase.c
  33. *
  34. * Description:
  35. * Erase an ISAM file.
  36. */
  37. #include "isam_impl.h"
  38. #include <unistd.h>
  39. #include <sys/time.h>
  40. static void _unlink_datfile(), _unlink_indfile(), _unlink_varfile();
  41. static int _amerase();
  42. /*
  43. * isfd = iserase(isfname, mode)
  44. *
  45. *
  46. * Errors:
  47. * EBADFILE ISAM file is corrupted or it is not an NetISAM file
  48. * EFLOCKED The file is exclusively locked by other process.
  49. * EFNAME Invalid ISAM file name
  50. * EFNAME ISAM file does not exist
  51. * ETOOMANY Too many ISAM file descriptors are in use (128 is the limit)
  52. *
  53. * The following error code is "borrowed" from UNIX:
  54. * EACCES UNIX file system protection denies access to the file:
  55. * - mode is INOUT or OUTPUT and ISAM file is on
  56. * a Read-Only mounted file system
  57. * - UNIX file permissions don't allow access to the file
  58. */
  59. int
  60. iserase(char *isfname)
  61. {
  62. Isfd isfd;
  63. Fab *fab;
  64. /*
  65. * Open the file
  66. */
  67. if ((isfd = isopen(isfname, ISINOUT)) == -1)
  68. return (ISERROR); /* iserrno is set */
  69. /*
  70. * Get File Access Block.
  71. */
  72. if ((fab = _isfd_find(isfd)) == NULL) {
  73. _isfatal_error("iserase() cannot find FAB");
  74. _setiserrno2(EFATAL, '9', '0');
  75. return ISERROR;
  76. }
  77. if (_amerase(&fab->isfhandle, &fab->errcode)
  78. && fab->errcode.iserrno != ENOENT) {
  79. _seterr_errcode(&fab->errcode);
  80. (void)isclose(isfd);
  81. return (ISERROR);
  82. }
  83. _fab_destroy(fab); /* Deallocate Fab object */
  84. _isfd_delete(isfd);
  85. return (ISOK); /* Successful iserase() */
  86. }
  87. /*
  88. * _amerase(isfhandle)
  89. *
  90. * _amerase() erases ISAM file
  91. *
  92. * Input params:
  93. * isfhandle Handle of ISAM file
  94. *
  95. * Output params:
  96. * errcode Error code
  97. *
  98. */
  99. static int
  100. _amerase(Bytearray *isfhandle, struct errcode *errcode)
  101. {
  102. Fcb *fcb;
  103. char *isfname = _getisfname(isfhandle);
  104. _isam_entryhook();
  105. /*
  106. * Get FCB corresponding to the isfhandle handle.
  107. */
  108. if ((fcb = _openfcb(isfhandle, errcode)) == NULL) {
  109. goto ERROR;
  110. }
  111. /*
  112. * Delete FCB and remove it from FCB cache.
  113. */
  114. (void) _watchfd_decr(_isfcb_nfds(fcb));
  115. _isfcb_close(fcb);
  116. _mngfcb_delete(isfhandle);
  117. /*
  118. * Unlink all UNIX files.
  119. */
  120. _unlink_datfile(isfname);
  121. _unlink_indfile(isfname);
  122. _unlink_varfile(isfname);
  123. _isam_exithook();
  124. return (ISOK);
  125. ERROR:
  126. _isam_exithook();
  127. return (ISERROR);
  128. }
  129. Static void
  130. _unlink_datfile(char *isfname)
  131. {
  132. char namebuf[MAXPATHLEN];
  133. snprintf(namebuf, sizeof(namebuf), "%s", isfname);
  134. _makedat_isfname(namebuf);
  135. (void)unlink(namebuf);
  136. }
  137. Static void
  138. _unlink_indfile(char *isfname)
  139. {
  140. char namebuf[MAXPATHLEN];
  141. snprintf(namebuf, sizeof(namebuf), "%s", isfname);
  142. _makeind_isfname(namebuf);
  143. (void)unlink(namebuf);
  144. }
  145. Static void
  146. _unlink_varfile(char *isfname)
  147. {
  148. char namebuf[MAXPATHLEN];
  149. snprintf(namebuf, sizeof(namebuf), "%s", isfname);
  150. _makevar_isfname(namebuf);
  151. (void)unlink(namebuf);
  152. }