Skip to content

Commit 225a337

Browse files
MarcusJGStreetsathoelke
authored andcommitted
Added usage description and code snippet
Signed-off-by: Marcus Streets <[email protected]>
1 parent 3d80c0c commit 225a337

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

doc/storage/api/api.rst

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,55 @@ These definitions must be defined in the header file :file:`psa/protected_storag
805805
A caller may delete a `uid` with ``psa_ps_remove(uid)`` without invalidating the iteration context, provided the `uid` does *NOT* match the filter. However, if the `uid` matches the filter then any later call to `psa_ps_iterator_next()` fails with `PSA_ERROR_DATA_CORRUPT`.
806806

807807
A caller may call `psa_ps_rename(uid, uid_new)` without invalidating the iteration context, provided the `uid` does *NOT* match the filter, as this is equivalent to calling `psa_ps_remove(uid)`. However, if the `uid` matches the filter then any later call to `psa_ps_iterator_next()` fails with `PSA_ERROR_DATA_CORRUPT`.
808+
809+
The following code snippet uses a linked list to store the matching files before iterating over that list and removing them.
810+
811+
.. code-block:: c
808812
813+
my_context = NULL
814+
my_filter = 0x1111 0000 0000 0000
815+
my_length = 0x0020
816+
my_result = NULL
817+
// define a linked list
818+
typedef struct node {
819+
int val;
820+
struct node * next;
821+
} node_t;
822+
// instantiate the head
823+
node_t * head = NULL;
824+
head = (node_t *) malloc(sizeof(node_t));
825+
// and a current item
826+
node_t * current = head;
827+
if psa_ps_iterator_start(my_context, my_filter, my-length, my_result) == PSA_SUCCESS
828+
{
829+
do
830+
{
831+
current->next = (node_t *) malloc(sizeof(node_t));
832+
current->next->val = my_result;
833+
current->next->next = NULL;
834+
psa_ps_iterator_next(my_context, my_result)
835+
// we will get an does not exist error when we reach the last item, any other error is a storage fialure
836+
if my_reult <> PSA_ERROR_DOES_NOT_EXIST
837+
{
838+
/* deal with storage failure */
839+
}
840+
}
841+
while my_result == PSA_SUCCES ;
842+
}
843+
else { /* error handling */}
844+
// we got here so there cannot have been an error and we have the full list.
845+
// now we can iterate over the list and delete the file and throw away the list item
846+
node_t * current = head;
847+
while (current != NULL)
848+
{
849+
psa_ps_remove(current-> val)
850+
head = current
851+
current = current->next;
852+
free(head)
853+
}
854+
};
855+
856+
809857
810858
.. function:: psa_ps_iterator_next
811859

@@ -824,6 +872,7 @@ These definitions must be defined in the header file :file:`psa/protected_storag
824872

825873
.. retval:: PSA_SUCCESS
826874
The operation completed successfully.
875+
827876
.. retval:: PSA_ERROR_DOES_NOT_EXIST
828877
The iterator has returned all the uids that match this iteration.
829878

0 commit comments

Comments
 (0)