Skip to content

Commit 4f64a80

Browse files
committed
Refactor: libcib: Drop working_cib variable from cib_perform_op()
We were setting *cib = working_cib in the "done" section. There are no early returns, so that always happens before we return. The only real change required here, is that we assign *cib to old_versions before setting *cib to a copy of itself. (Otherwise, old_versions would point to the copy.) We now have only three CIB pointers to deal with in cib_perform_op, not including saved_cib, which is used only in a small scope in order to free an object. We also have some easy opportunities to reduce duplication in an upcoming commit. Signed-off-by: Reid Wahl <nrwahl@protonmail.com>
1 parent f68d60e commit 4f64a80

1 file changed

Lines changed: 24 additions & 30 deletions

File tree

lib/cib/cib_utils.c

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,6 @@ cib_perform_op(enum cib_variant variant, cib__op_fn_t fn, xmlNode *req,
485485
xmlNode *old_versions = NULL;
486486

487487
xmlNode *top = NULL;
488-
xmlNode *working_cib = NULL;
489488

490489
pcmk__assert((fn != NULL) && (req != NULL)
491490
&& (config_changed != NULL) && (!*config_changed)
@@ -519,36 +518,31 @@ cib_perform_op(enum cib_variant variant, cib__op_fn_t fn, xmlNode *req,
519518

520519
rc = fn(op, call_options, section, req, input, cib, output);
521520

522-
/* Set working_cib to *cib after fn(), in case *cib points somewhere
523-
* else now (for example, after a erase or full-CIB replace op).
524-
*/
525-
working_cib = *cib;
526-
527521
} else {
528-
working_cib = pcmk__xml_copy(NULL, *cib);
529522
old_versions = *cib;
523+
*cib = pcmk__xml_copy(NULL, *cib);
530524

531-
pcmk__xml_doc_set_flags(working_cib->doc, pcmk__xf_tracking);
525+
pcmk__xml_doc_set_flags((*cib)->doc, pcmk__xf_tracking);
532526
if (enable_acl) {
533-
pcmk__enable_acl(*cib, working_cib, user);
527+
pcmk__enable_acl(*cib, *cib, user);
534528
}
535529

536-
rc = fn(op, call_options, section, req, input, &working_cib, output);
530+
rc = fn(op, call_options, section, req, input, cib, output);
537531
}
538532

539533
// Allow ourselves to make any additional necessary changes
540-
xml_acl_disable(working_cib);
534+
xml_acl_disable(*cib);
541535

542536
if (rc != pcmk_rc_ok) {
543537
goto done;
544538
}
545539

546-
if (working_cib == NULL) {
540+
if (*cib == NULL) {
547541
rc = EINVAL;
548542
goto done;
549543
}
550544

551-
if (xml_acl_denied(working_cib)) {
545+
if (xml_acl_denied(*cib)) {
552546
pcmk__trace("ACL rejected part or all of the proposed changes");
553547
rc = EACCES;
554548
goto done;
@@ -559,15 +553,15 @@ cib_perform_op(enum cib_variant variant, cib__op_fn_t fn, xmlNode *req,
559553
* is checked elsewhere.
560554
*/
561555
if (variant != cib_file) {
562-
rc = check_new_feature_set(working_cib);
556+
rc = check_new_feature_set(*cib);
563557
if (rc != pcmk_rc_ok) {
564558
goto done;
565559
}
566560
}
567561

568-
rc = check_cib_versions(old_versions, working_cib, req, input);
562+
rc = check_cib_versions(old_versions, *cib, req, input);
569563

570-
pcmk__strip_xml_text(working_cib);
564+
pcmk__strip_xml_text(*cib);
571565

572566
if (pcmk__xe_attr_is_true(req, PCMK__XA_CIB_UPDATE)) {
573567
/* This is a replace operation as a reply to a sync request. Keep
@@ -579,26 +573,26 @@ cib_perform_op(enum cib_variant variant, cib__op_fn_t fn, xmlNode *req,
579573
/* If we didn't make a copy, the diff will only be accurate for the
580574
* top-level PCMK_XE_CIB element
581575
*/
582-
*diff = xml_create_patchset(0, old_versions, working_cib, config_changed,
576+
*diff = xml_create_patchset(0, old_versions, *cib, config_changed,
583577
manage_version);
584578

585579
/* pcmk__xml_commit_changes() resets document private data, so call it even
586580
* if there were no changes.
587581
*/
588-
pcmk__xml_commit_changes(working_cib->doc);
582+
pcmk__xml_commit_changes((*cib)->doc);
589583

590584
if (*diff == NULL) {
591585
goto done;
592586
}
593587

594588
pcmk__log_xml_patchset(LOG_INFO, *diff);
595589

596-
/* working_cib must not be modified after this point, except for the
597-
* attributes for which pcmk__xa_filterable() returns true
590+
/* *cib must not be modified after this point, except for the attributes for
591+
* which pcmk__xa_filterable() returns true
598592
*/
599593

600594
if (*config_changed && !pcmk__is_set(call_options, cib_no_mtime)) {
601-
rc = set_update_origin(working_cib, req);
595+
rc = set_update_origin(*cib, req);
602596
if (rc != pcmk_rc_ok) {
603597
goto done;
604598
}
@@ -607,24 +601,24 @@ cib_perform_op(enum cib_variant variant, cib__op_fn_t fn, xmlNode *req,
607601
// Skip validation for status-only updates, since we allow anything there
608602
if ((rc == pcmk_rc_ok)
609603
&& !pcmk__str_eq(section, PCMK_XE_STATUS, pcmk__str_casei)
610-
&& !pcmk__configured_schema_validates(working_cib)) {
604+
&& !pcmk__configured_schema_validates(*cib)) {
611605

612606
rc = pcmk_rc_schema_validation;
613607
}
614608

615609
done:
616-
*cib = working_cib;
617-
618610
/* @TODO This may not work correctly when !should_copy_cib(), since we don't
619611
* keep the original CIB.
620612
*/
621-
if ((rc != pcmk_rc_ok) && cib_acl_enabled(old_versions, user)
622-
&& xml_acl_filtered_copy(user, old_versions, working_cib, cib)) {
623-
624-
if (*cib == NULL) {
625-
pcmk__debug("Pre-filtered the entire cib result");
613+
if ((rc != pcmk_rc_ok) && cib_acl_enabled(old_versions, user)) {
614+
xmlNode *saved_cib = *cib;
615+
616+
if (xml_acl_filtered_copy(user, old_versions, *cib, cib)) {
617+
if (*cib == NULL) {
618+
pcmk__debug("Pre-filtered the entire cib result");
619+
}
620+
pcmk__xml_free(saved_cib);
626621
}
627-
pcmk__xml_free(working_cib);
628622
}
629623

630624
pcmk__xml_free(top);

0 commit comments

Comments
 (0)