tttar_spec.C 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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: tttar_spec.C /main/3 1995/10/20 17:00:42 rswiston $
  28. /*
  29. * tttar_spec.cc - Implementation of specs for Link Service/ToolTalk archiving
  30. *
  31. * Copyright (c) 1990 by Sun Microsystems, Inc.
  32. *
  33. */
  34. #if defined(__linux__) || defined(CSRG_BASED) || defined(sun)
  35. #include <unistd.h>
  36. #else
  37. #include <osfcn.h>
  38. #endif
  39. #include "Tt/tt_c.h"
  40. #include "util/tt_iostream.h"
  41. #include "tttar_utils.h"
  42. #include "tttar_spec.h"
  43. /*
  44. * Lstar_spec::Lstar_spec()
  45. */
  46. Lstar_spec::
  47. Lstar_spec()
  48. {
  49. _props = new Lstar_spec_prop_list();
  50. }
  51. /*
  52. * Lstar_spec::Lstar_spec() -
  53. */
  54. Lstar_spec::
  55. Lstar_spec( _Tt_string id, _Tt_string path )
  56. {
  57. _id = id;
  58. _path = path;
  59. _props = new Lstar_spec_prop_list();
  60. }
  61. /*
  62. * Lstar_spec::~Lstar_spec()
  63. */
  64. Lstar_spec::
  65. ~Lstar_spec()
  66. {
  67. }
  68. /*
  69. * Lstar_spec::xdr()
  70. */
  71. bool_t Lstar_spec::
  72. xdr( XDR *xdrs )
  73. {
  74. if (! this->_id.xdr(xdrs)) {
  75. return FALSE;
  76. }
  77. if (! this->_path.xdr(xdrs)) {
  78. return FALSE;
  79. }
  80. if (! this->_type.xdr(xdrs)) {
  81. return FALSE;
  82. }
  83. if (! this->_props.xdr(xdrs)) {
  84. return FALSE;
  85. }
  86. return TRUE;
  87. }
  88. /*
  89. * Lstar_spec::read_self() - Read from ToolTalk everything we need to know
  90. * about ourselves in order to archive ourself.
  91. */
  92. Tt_status Lstar_spec::
  93. read_self()
  94. {
  95. /*
  96. * Get the spec's type.
  97. */
  98. note_ptr_err( tt_spec_type( (char *)_id ));
  99. if (IS_TT_ERR(err_noted)) {
  100. return err_noted;
  101. }
  102. _type = ptr_returned;
  103. /*
  104. * Get how many properties are on the spec.
  105. */
  106. note_int_err( tt_spec_propnames_count( (char *)_id ));
  107. if (IS_TT_ERR(err_noted)) {
  108. return err_noted;
  109. }
  110. int num_props = int_returned;
  111. /*
  112. * Push the spec's properties onto our list in reverse order,
  113. * to preserve the admittedly meaningless order they had.
  114. */
  115. for (int n = num_props - 1; n >= 0; n--) {
  116. note_ptr_err( tt_spec_propname( (char *)_id, n ));
  117. switch (err_noted) {
  118. case TT_ERR_OBJID:
  119. case TT_ERR_DBAVAIL:
  120. return err_noted;
  121. case TT_ERR_NUM:
  122. continue;
  123. case TT_ERR_DBEXIST:
  124. default:
  125. if (IS_TT_ERR(err_noted)) {
  126. return err_noted;
  127. }
  128. }
  129. _Tt_string propname = ptr_returned;
  130. Lstar_spec_prop_ptr prop_ptr;
  131. prop_ptr = new Lstar_spec_prop( _id, propname );
  132. this->_props->push( prop_ptr );
  133. }
  134. return err_noted;
  135. }
  136. /*
  137. * Lstar_spec::write_self() - Recreate a spec like the one we are, returning
  138. * the id of the spec created. The string returned must be freed
  139. * using tt_free().
  140. */
  141. char * Lstar_spec::
  142. write_self( _Tt_string where, bool_t preserve__props, Tt_status *err )
  143. {
  144. char *spec_created = NULL;
  145. _Tt_string path;
  146. if (this->_path.len() <= 0) {
  147. *err = TT_ERR_PATH;
  148. return NULL;
  149. }
  150. /*
  151. * TO_DO: tt_spec_create() won't convert /./ to / in a path
  152. * if the path doesn't exist.
  153. */
  154. if (this->_path.left(2) == "./") {
  155. this->_path = this->_path.right( _path.len() - 2 );
  156. }
  157. /*
  158. * If the archived path is absolute, ignore <where>.
  159. */
  160. if (this->_path[0] == '/') {
  161. path = this->_path;
  162. } else {
  163. path = where.cat( "/" ).cat( this->_path );
  164. }
  165. note_ptr_err( tt_spec_create( (char *)path ));
  166. *err = err_noted;
  167. spec_created = ptr_returned;
  168. if (IS_TT_ERR(err_noted)) {
  169. return spec_created;
  170. }
  171. note_err( tt_spec_type_set( spec_created, (char *)this->_type ));
  172. *err = err_noted;
  173. if (IS_TT_ERR(err_noted)) {
  174. return spec_created;
  175. }
  176. Lstar_spec_prop_list_cursor prop_cursor( this->_props );
  177. while (prop_cursor.next()) {
  178. *err = prop_cursor->write_self( spec_created, preserve__props );
  179. }
  180. note_err( tt_spec_write( spec_created ));
  181. *err = err_noted;
  182. return spec_created;
  183. }
  184. /*
  185. * Lstar_spec::print()
  186. */
  187. void Lstar_spec::
  188. print( FILE *fs ) const
  189. {
  190. fprintf( fs, "spec id: " );
  191. this->_id.print( fs );
  192. fprintf( fs, "\nspec type: " );
  193. this->_type.print( fs );
  194. fprintf( fs, "\nspec path: " );
  195. this->_path.print( fs );
  196. fprintf( fs, "\n" );
  197. this->_props->print(Lstar_spec::do_print, fs );
  198. fprintf( fs, "\n" );
  199. }
  200. implement_list_of(Lstar_spec_prop)
  201. /*
  202. * Lstar_spec_prop::Lstar_spec_prop()
  203. */
  204. Lstar_spec_prop::
  205. Lstar_spec_prop()
  206. {
  207. }
  208. /*
  209. * Lstar_spec_prop::Lstar_spec_prop()
  210. */
  211. Lstar_spec_prop::
  212. Lstar_spec_prop( _Tt_string id, _Tt_string propname )
  213. {
  214. _propname = propname;
  215. _values = new _Tt_string_list();
  216. /*
  217. * Get how many values are in the spec's property.
  218. */
  219. note_int_err( tt_spec_prop_count( (char *)id, (char *)propname ));
  220. switch (err_noted) {
  221. case TT_ERR_PROPNAME:
  222. case TT_ERR_OBJID:
  223. case TT_ERR_DBAVAIL:
  224. return;
  225. case TT_ERR_DBEXIST:
  226. default:
  227. if (IS_TT_ERR(err_noted)) {
  228. return;
  229. }
  230. }
  231. int num_values = int_returned;
  232. /*
  233. * Push the property's values onto our list in reverse order,
  234. * to preserve the order they had.
  235. */
  236. for (int n = num_values - 1; n >= 0; n--) {
  237. int len;
  238. unsigned char *value;
  239. note_err( tt_spec_bprop( (char *)id, (char *)propname, n, &value, &len ));
  240. switch (err_noted) {
  241. case TT_ERR_PROPNAME:
  242. case TT_ERR_OBJID:
  243. case TT_ERR_DBAVAIL:
  244. return;
  245. case TT_ERR_NUM:
  246. continue;
  247. case TT_ERR_DBEXIST:
  248. default:
  249. if (IS_TT_ERR(err_noted)) {
  250. return;
  251. }
  252. }
  253. _Tt_string val( value, len );
  254. this->_values->push( val );
  255. }
  256. }
  257. /*
  258. * Lstar_spec_prop::~Lstar_spec_prop()
  259. */
  260. Lstar_spec_prop::
  261. ~Lstar_spec_prop()
  262. {
  263. }
  264. /*
  265. * Lstar_spec_prop::xdr()
  266. */
  267. bool_t Lstar_spec_prop::
  268. xdr( XDR *xdrs )
  269. {
  270. if (! this->_propname.xdr(xdrs)) {
  271. return FALSE;
  272. }
  273. if (! this->_values.xdr(xdrs)) {
  274. return FALSE;
  275. }
  276. return TRUE;
  277. }
  278. /*
  279. * Lstar_spec_prop::write_self() - Write this prop onto the given spec.
  280. */
  281. Tt_status Lstar_spec_prop::
  282. write_self( char *spec_id, bool_t preserve__props )
  283. {
  284. /*
  285. * If we're not root and this is a blessed property,
  286. * do not attempt to write it.
  287. * Insert link here to policy statement about prop names in tt_c.h.
  288. */
  289. if ( (_propname[0] == '_')
  290. && ( (! preserve__props)
  291. || (getuid() != 0)))
  292. {
  293. return TT_OK;
  294. }
  295. _Tt_string_list_cursor value_cursor( this->_values );
  296. char *val;
  297. int len;
  298. while (value_cursor.next()) {
  299. val = (char *)(*value_cursor);
  300. len = (*value_cursor).len();
  301. note_err( tt_spec_bprop_add( spec_id, (char *)_propname,
  302. (unsigned char *)val, len));
  303. if (IS_TT_ERR(err_noted)) {
  304. return err_noted;
  305. }
  306. }
  307. return err_noted;
  308. }
  309. /*
  310. * Lstar_spec_prop::print()
  311. */
  312. void Lstar_spec_prop::
  313. print( FILE *fs ) const
  314. {
  315. this->_propname.print( fs );
  316. fprintf( fs, ": " );
  317. this->_values->print(_tt_string_print,fs);
  318. fprintf( fs, "\n" );
  319. }
  320. void Lstar_spec::
  321. do_print(const _Tt_ostream &os, const _Tt_object *obj)
  322. {
  323. ((Lstar_spec *)obj)->print(os.theFILE());
  324. }
  325. void Lstar_spec_prop::
  326. do_print(FILE *fs, const _Tt_object *obj)
  327. {
  328. ((Lstar_spec_prop *)obj)->print(fs);
  329. }