isgarbage.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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: isgarbage.c /main/5 1998/04/10 08:04:25 mgreess $ */
  28. #include "isam_impl.h"
  29. #include <stdlib.h>
  30. #include <sys/types.h>
  31. #include <sys/stat.h>
  32. int
  33. isgarbage(char * isfname)
  34. {
  35. char isfname2[MAXPATHLEN];
  36. int isfd = -1, isfd2 = -1;
  37. char buffer[ISMAXRECLEN];
  38. char *recbuf = NULL;
  39. struct dictinfo info;
  40. struct keydesc keybuf;
  41. struct stat statbuf;
  42. int count = 0,i;
  43. snprintf(isfname2, sizeof(isfname2), "%s~", isfname);
  44. if ((isfd = isopen(isfname, ISEXCLLOCK + ISINPUT)) == ISERROR) {
  45. goto ERROR;
  46. }
  47. if (isindexinfo(isfd, (struct keydesc *)&info, 0) == ISERROR) {
  48. goto ERROR;
  49. }
  50. if (strlen(isfname) + 5 >= ISMAXRECLEN)
  51. recbuf = (char*) malloc(strlen(isfname) + 5);
  52. else
  53. recbuf = buffer;
  54. sprintf(recbuf, "%s.rec", isfname);
  55. if (stat(recbuf, &statbuf) < 0) {
  56. goto ERROR;
  57. }
  58. iserase(isfname2); /* Delete any old backup (~) files. */
  59. if ((isfd2 = isbuild(isfname2, info.di_recsize, nokey,
  60. (DICTVARLENBIT&info.di_nkeys?ISVARLEN: ISFIXLEN)
  61. + ISEXCLLOCK + ISINOUT)) == ISERROR) {
  62. goto ERROR;
  63. }
  64. /* Copy all records */
  65. while (isread(isfd, recbuf, ISNEXT) == ISOK) {
  66. iswrite(isfd2, recbuf);
  67. count++;
  68. }
  69. if (count != info.di_nrecords) {
  70. goto ERROR;
  71. }
  72. /* Build all indexes from index info */
  73. for (i = 1; i <= (info.di_nkeys & DICTNKEYSMASK); i++) {
  74. if (isindexinfo(isfd, &keybuf, i) == ISERROR) {
  75. goto ERROR;
  76. }
  77. if (i == 1 && keybuf.k_nparts != 0) {
  78. /* Add primary index */
  79. if (isaddprimary(isfd2, &keybuf) == ISERROR) {
  80. goto ERROR;
  81. }
  82. }
  83. else if (i > 1) {
  84. /* Add secondary index */
  85. if (isaddindex(isfd2, &keybuf) == ISERROR) {
  86. goto ERROR;
  87. }
  88. }
  89. }
  90. (void)isclose(isfd);
  91. (void)isclose(isfd2);
  92. iserase(isfname); /* cannot abort at this point ! */
  93. if (isrename(isfname2, isfname) == ISERROR) {
  94. return (ISERROR);
  95. }
  96. return (ISOK);
  97. ERROR:
  98. if (isfd != -1)
  99. (void)isclose(isfd);
  100. if (isfd2 != -1) {
  101. (void)isclose(isfd2);
  102. (void)iserase(isfname2);
  103. }
  104. if ((recbuf != buffer) && (recbuf != NULL)) {
  105. free(recbuf);
  106. }
  107. return (ISERROR);
  108. }