2
0

spec_repair.C 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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: spec_repair.C /main/3 1995/10/20 16:26:51 rswiston $
  28. /*
  29. *
  30. * spec_repair.cc
  31. *
  32. * Copyright (c) 1990 by Sun Microsystems, Inc.
  33. */
  34. #include <stddef.h>
  35. #include <stdio.h>
  36. #include <errno.h>
  37. #if defined(_AIX)
  38. #include <time.h>
  39. #endif
  40. #include <sys/stat.h>
  41. #include "spec.h"
  42. #include "options_tt.h"
  43. #include "ttdbck.h"
  44. #include "db/tt_db_object.h"
  45. #include "db/tt_db_object_utils.h"
  46. #include "db/tt_db_key.h"
  47. #include "db/tt_db_object.h"
  48. #include "db/tt_db_results.h"
  49. #include "db/tt_db_access.h"
  50. #include "tt_db_server_consts.h"
  51. #define MKERR(msg, err) fprintf(stderr, \
  52. "ttdbck: error %s spec: %s\n", \
  53. msg, \
  54. tt_status_message(err))
  55. const char* MP_TYPE_PROP = "_NODE_TYPE";
  56. void Spec::
  57. repair_spec()
  58. {
  59. // by this point all the information about the spec is collected
  60. // in the Spec instance. We make any requested changes to
  61. // the Spec instance, then completely replace the
  62. // info on disk with the info from the Spec instance.
  63. if (opts->repair_type_p()) {
  64. type = opts->repair_type();
  65. }
  66. if (opts->repair_filename_p()) {
  67. filename = opts->repair_filename();
  68. }
  69. _Tt_db_object_ptr oldobj = new _Tt_db_object();
  70. switch(oldobj->getDBResults()) {
  71. case TT_DB_OK:
  72. break;
  73. default:
  74. MKERR("creating", TT_ERR_INTERNAL);
  75. return;
  76. }
  77. _Tt_string objid = oldobj->create(filename, key->key()->string());
  78. switch(oldobj->getDBResults()) {
  79. case TT_DB_OK:
  80. break;
  81. default:
  82. MKERR("creating", TT_ERR_INTERNAL);
  83. return;
  84. }
  85. _Tt_db_access_ptr access = oldobj->getAccess();
  86. switch(oldobj->getDBResults()) {
  87. case TT_DB_OK:
  88. break;
  89. default:
  90. MKERR("creating", TT_ERR_INTERNAL);
  91. return;
  92. }
  93. switch(oldobj->remove()) {
  94. case TT_DB_OK:
  95. case TT_DB_ERR_NO_SUCH_OBJECT:
  96. case TT_DB_ERR_NO_SUCH_PROPERTY:
  97. case TT_DB_WRN_FORWARD_POINTER:
  98. break;
  99. case TT_DB_ERR_ACCESS_DENIED:
  100. MKERR("destroying", TT_ERR_ACCESS);
  101. return;
  102. default:
  103. MKERR("destroying", TT_ERR_DBAVAIL);
  104. return;
  105. }
  106. if (opts->repair_delete_p()) {
  107. // don't recreate the spec.
  108. } else {
  109. // Should ensure we are running under a particular namespace
  110. // for this? Like an override namespace to force into the
  111. // db under repair?
  112. _Tt_db_object_ptr newobj = new _Tt_db_object();
  113. switch(newobj->getDBResults()) {
  114. case TT_DB_OK:
  115. break;
  116. default:
  117. MKERR("creating", TT_ERR_INTERNAL);
  118. return;
  119. }
  120. _Tt_string newobjid = newobj->create(filename,
  121. key->key()->string());
  122. switch(newobj->getDBResults()) {
  123. case TT_DB_OK:
  124. break;
  125. default:
  126. MKERR("creating", TT_ERR_INTERNAL);
  127. return;
  128. }
  129. newobj->setType(type);
  130. newobj->setAccess(access);
  131. // Re-create the properties
  132. _Tt_string_list_cursor c;
  133. Prop_ptr sp;
  134. _Tt_string_list_cursor v;
  135. _Tt_db_property_ptr dbprop = new _Tt_db_property();
  136. uid_t euid = 0;
  137. gid_t group = 0;
  138. mode_t mode = 0;
  139. int owner_written = 0;
  140. int group_written = 0;
  141. int mode_written = 0;
  142. c.reset(propnames);
  143. while(c.next()) {
  144. sp = props->lookup(*c);
  145. v.reset(sp->_values);
  146. if (!v.next()) {
  147. // No values for the property. This is
  148. // theoretically impossible -- delete the
  149. // property.
  150. } else if (sp->_name == TT_DB_OBJECT_TYPE_PROPERTY) {
  151. // this is the special prop that holds the
  152. // type name. Don't try to set it by that name.
  153. } else {
  154. if (sp->_name ==
  155. TT_OBJECT_OWNER_PROPERTY) {
  156. // This is the special user field
  157. // Note that the access info is
  158. // already written out above.
  159. // Re-write it here anyway, in
  160. // case it was corrupt in the
  161. // old DB.
  162. memcpy((char *)&euid,
  163. (char *)(*v), sizeof(uid_t));
  164. owner_written = 1;
  165. }
  166. else if (sp->_name ==
  167. TT_OBJECT_GROUP_PROPERTY) {
  168. // This is the special group field
  169. // See note for owner field above
  170. memcpy((char *)&group,
  171. (char *)(*v), sizeof(gid_t));
  172. group_written = 1;
  173. }
  174. else if (sp->_name ==
  175. TT_OBJECT_MODE_PROPERTY) {
  176. // This is the special mode field
  177. // See note for owner field above
  178. memcpy((char *)&mode,
  179. (char *)(*v), sizeof(mode_t));
  180. mode_written = 1;
  181. }
  182. else {
  183. // Ordinary property -- create
  184. // a new _Tt_db_property record
  185. dbprop->name = sp->_name;
  186. dbprop->values->push(*v);
  187. while(v.next()) {
  188. // Add any remaining values
  189. dbprop->values->push(*v);
  190. }
  191. }
  192. }
  193. newobj->addProperty(dbprop);
  194. }
  195. if (owner_written && group_written && mode_written) {
  196. // We have complete access info from the old prop
  197. // list, so we may as well use it.
  198. access->user = euid;
  199. access->group = group;
  200. access->mode = mode;
  201. newobj->setAccess(access);
  202. }
  203. // There\'s not much we can do if this call doesn\'t work
  204. newobj->write();
  205. }
  206. }