From: Jarkko Sakkinen Date: Sat, 27 Nov 2010 15:31:13 +0000 (-0800) Subject: smack_label_set_add() returns now const pointer to the short name. X-Git-Tag: accepted/trunk/20130107.204027~176 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=e779d71c1a0bed3df2b0d64cc26267eef4dc3244;p=platform%2Fupstream%2Fsmack.git smack_label_set_add() returns now const pointer to the short name. --- diff --git a/src/smack.h b/src/smack.h index f0b8c13..97630d4 100644 --- a/src/smack.h +++ b/src/smack.h @@ -44,8 +44,9 @@ typedef struct _SmackRuleSet *SmackRuleSet; /*! - * Handle to a in-memory representation for long label to - * short label mapping. + * Handle to a in-memory representation for label repository that contains + * mapping between long and short names for labels. Short names are essentially + * standard Smack labels. */ typedef struct _SmackLabelSet *SmackLabelSet; @@ -239,10 +240,12 @@ extern int smack_label_set_save_to_file(SmackLabelSet handle, const char *path); * Add new label to a label set. * * @param handle handle to a label set - * @param long_name long label - * @return 0 on success + * @param long_name long name for the label as input + * + * @return pointer to the short name is returned when the operation is + * succesful. Otherwise, NULL is returned. */ -extern int smack_label_set_add(SmackLabelSet handle, const char *long_name); +extern const char *smack_label_set_add(SmackLabelSet handle, const char *long_name); /*! * Remove a label from a label set. @@ -254,14 +257,6 @@ extern int smack_label_set_add(SmackLabelSet handle, const char *long_name); extern int smack_label_set_remove(SmackLabelSet handle, const char *long_name); /*! - * Get pointer to the instance of a given long label inside label set. - * - * @param handle handle to a label set - * @param long_name long name of the label - */ -extern const char *smack_label_set_get(SmackLabelSet handle, const char *long_name); - -/*! * Get short label. * * @param handle handle to a label set diff --git a/src/smack_labels.c b/src/smack_labels.c index 55048d0..fe5e981 100644 --- a/src/smack_labels.c +++ b/src/smack_labels.c @@ -18,10 +18,10 @@ struct _SmackLabelSet { struct smack_label *label_by_short_name; }; -static int add_label(struct smack_label **label_by_long_name, - struct smack_label **label_by_short_name, - const char *long_name, - const char *short_name); +static struct smack_label *add_label(struct smack_label **label_by_long_name, + struct smack_label **label_by_short_name, + const char *long_name, + const char *short_name); SmackLabelSet smack_label_set_new(void) { @@ -37,6 +37,7 @@ extern SmackLabelSet smack_label_set_new_from_file(const char *path) char *buf = NULL; const char *ll, *sl; size_t size; + struct smack_label *l; int ret = 0; file = fopen(path, "r"); @@ -53,11 +54,19 @@ extern SmackLabelSet smack_label_set_new_from_file(const char *path) ll = strtok(buf, " \t\n"); sl = strtok(NULL, " \t\n"); - if (ll == NULL || sl == NULL || strtok(NULL, " \t\n") != NULL) { + if (ll == NULL || sl == NULL || + strtok(NULL, " \t\n") != NULL || + strlen(sl) > SMACK64_LEN) { + ret = -1; + break; + } + + l = add_label(&labels->label_by_long_name, + &labels->label_by_short_name, + ll, sl); + if (l == NULL) { ret = -1; - } else { - ret = add_label(&labels->label_by_long_name, &labels->label_by_short_name, - ll, sl); + break; } free(buf); @@ -114,24 +123,25 @@ int smack_label_set_save_to_file(SmackLabelSet handle, const char *path) return 0; } -int smack_label_set_add(SmackLabelSet handle, const char *long_name) +const char *smack_label_set_add(SmackLabelSet handle, const char *long_name) { char sl[SMACK64_LEN + 1]; int pos, len ,ret; + struct smack_label *l; if (strlen(long_name) == 0 || get_known_label(long_name) != NULL) - return -EINVAL; + return NULL; len = strlen(long_name); pos = (len > SMACK64_LEN) ? len - SMACK64_LEN : 0; strcpy(sl, &long_name[pos]); - ret = add_label(&handle->label_by_long_name, &handle->label_by_short_name, - long_name, sl); + l = add_label(&handle->label_by_long_name, &handle->label_by_short_name, + long_name, sl); - return ret; + return l->short_name; } int smack_label_set_remove(SmackLabelSet handle, const char *long_name) @@ -152,23 +162,6 @@ int smack_label_set_remove(SmackLabelSet handle, const char *long_name) return 0; } -const char *smack_label_set_get(SmackLabelSet handle, const char *long_name) -{ - struct smack_label *l; - const char *res; - - res = get_known_label(long_name); - if (res != NULL) - return res; - - HASH_FIND(long_name_hh, handle->label_by_long_name, long_name, strlen(long_name), l); - - if (l == NULL) - return NULL; - - return l->long_name; -} - const char *smack_label_set_to_short_name(SmackLabelSet handle, const char *long_name) { @@ -205,29 +198,26 @@ const char *smack_label_set_to_long_name(SmackLabelSet handle, return l->long_name; } -static int add_label(struct smack_label **label_by_long_name, - struct smack_label **label_by_short_name, - const char *long_name, - const char *short_name) +static struct smack_label *add_label(struct smack_label **label_by_long_name, + struct smack_label **label_by_short_name, + const char *long_name, + const char *short_name) { struct smack_label *l; - if (strlen(short_name) > SMACK64_LEN) - return -ERANGE; - HASH_FIND(long_name_hh, *label_by_long_name, long_name, strlen(long_name), l); if (l != NULL) - return -EEXIST; + return NULL; HASH_FIND(short_name_hh, *label_by_short_name, short_name, strlen(short_name), l); if (l != NULL) - return -EEXIST; + return NULL; l = calloc(1, sizeof(struct smack_label)); if (l == NULL) - return -ENOMEM; + return NULL; l->long_name = strdup(long_name); l->short_name = strdup(short_name); @@ -236,12 +226,12 @@ static int add_label(struct smack_label **label_by_long_name, free(l->long_name); free(l->short_name); free(l); - return -ENOMEM; + return NULL; } HASH_ADD_KEYPTR(long_name_hh, *label_by_long_name, l->long_name, strlen(l->long_name), l); HASH_ADD_KEYPTR(short_name_hh, *label_by_short_name, l->short_name, strlen(l->short_name), l); - return 0; + return l; } diff --git a/tests/check_labels.c b/tests/check_labels.c index 87d0bbf..277a349 100644 --- a/tests/check_labels.c +++ b/tests/check_labels.c @@ -36,7 +36,7 @@ START_TEST(test_to_short_and_long_name) SmackLabelSet labels = smack_label_set_new(); fail_unless(labels != NULL, "Creating labels failed"); rc = smack_label_set_add(labels, "ThisIsReallyReallyReallyLongLabelName"); - fail_unless(rc == 0, "Adding label failed"); + fail_unless(rc != 0, "Adding label failed"); short_name = smack_label_set_to_short_name(labels, "ThisIsReallyReallyReallyLongLabelName"); fail_unless(short_name != NULL, "No short name"); long_name = smack_label_set_to_long_name(labels, short_name); @@ -53,7 +53,7 @@ START_TEST(test_save_label) SmackLabelSet labels = smack_label_set_new(); fail_unless(labels != NULL, "Creating labels failed"); rc = smack_label_set_add(labels, "ThisIsReallyReallyReallyLongLabelName"); - fail_unless(rc == 0, "Adding label failed"); + fail_unless(rc != 0, "Adding label failed"); rc = smack_label_set_save_to_file(labels, "add_label-result.txt"); fail_unless(rc == 0, "Failed to write labelset"); rc = files_equal("add_label-result.txt", "data/add_label-excepted.txt"); diff --git a/tests/check_rules.c b/tests/check_rules.c index 3c56347..b71b24b 100644 --- a/tests/check_rules.c +++ b/tests/check_rules.c @@ -167,10 +167,10 @@ START_TEST(test_rule_set_attach_label_set_add_remove_rule) smack_rule_set_attach_label_set(rules, labels); rc = smack_label_set_add(labels, LONG_LABEL_1); - fail_unless(rc == 0, "Adding label was not succesful"); + fail_unless(rc != 0, "Adding label was not succesful"); rc = smack_label_set_add(labels, LONG_LABEL_2); - fail_unless(rc == 0, "Adding label was not succesful"); + fail_unless(rc != 0, "Adding label was not succesful"); rc = smack_rule_set_add(rules, LONG_LABEL_1, LONG_LABEL_2, "rx"); fail_unless(rc == 0, "Adding rule was not succesful"); @@ -216,10 +216,10 @@ START_TEST(test_rule_set_attach_label_set_save_rules) smack_rule_set_attach_label_set(rules, labels); rc = smack_label_set_add(labels, LONG_LABEL_1); - fail_unless(rc == 0, "Adding label was not succesful"); + fail_unless(rc != 0, "Adding label was not succesful"); rc = smack_label_set_add(labels, LONG_LABEL_2); - fail_unless(rc == 0, "Adding label was not succesful"); + fail_unless(rc != 0, "Adding label was not succesful"); rc = smack_rule_set_add(rules, LONG_LABEL_1, LONG_LABEL_2, "rx"); fail_unless(rc == 0, "Adding rule was not succesful");