Re: [systemd-devel] sd-bus: get size of array container in D-Bus message

2023-08-07 Thread Lennart Poettering
On Mi, 02.08.23 06:39, Stanislav Angelovič (stanislav.angelo...@protonmail.com) 
wrote:

> Hi folks,
>
> I have a quick question: is there a way to get container size when
> deserializing an array from a D-Bus message (be it an array of
> trivial or non-trivial D-Bus types)? Say I enter a container with
> sd_bus_message_enter_container​, and then, before reading individual
> elements, I'd like to get the number of elements in that array in
> that message (so I can reserve storage on my side, etc.).

The D-Bus marshalling format does not allow us to determine this
efficiently: for arrays it only encodes the size in bytes in the
serialization, not the number of entries. Given that array element
sizes can have varying sizes (consider strings or variants) we thus
cannot determine the array's number of elements efficiently ahead of
time, except by iterating through the potentially huge array.

You can implement a helper for that, but we don't provide an API for
that, since it might make people write inefficient code, by assuming
this was a fast call.

Generally, consumers are better off using an exponential realloc()
loop for growing their data structures as they parse messages.

Yeah, this is not fun, but hey, this is C, so nothing is "fun".

Lennart

--
Lennart Poettering, Berlin


Re: [systemd-devel] sd-bus: get size of array container in D-Bus message

2023-08-02 Thread Luca Boccassi
On Wed, 2 Aug 2023 at 20:00, Giacinto Cifelli  wrote:
>
> On Wed, Aug 2, 2023 at 6:44 PM Stephen Hemminger
>  wrote:
> >
> > On Wed, 02 Aug 2023 06:39:47 +
> > Stanislav Angelovič  wrote:
> >
> > > Hi folks,
> > >
> > > I have a quick question: is there a way to get container size when 
> > > deserializing an array from a D-Bus message (be it an array of trivial or 
> > > non-trivial D-Bus types)? Say I enter a container with 
> > > sd_bus_message_enter_container, and then, before reading individual 
> > > elements, I'd like to get the number of elements in that array in that 
> > > message (so I can reserve storage on my side, etc.).
> > >
> > > If not, could such an API function be added?
> > >
> > > Thanks in advance.
> > >
> > > Stanislav.
> >
> > The dbus messsage just an encoding of a byte stream. There is nothing in 
> > the byte stream
> > that indicates the length.
>
> yes, but the whole message is available locally, it could be parsed on
> request, discarding the data, and keeping only the number of elements
> in an array.

For basic types there's
https://www.freedesktop.org/software/systemd/man/sd_bus_message_read_array.html


Re: [systemd-devel] sd-bus: get size of array container in D-Bus message

2023-08-02 Thread Giacinto Cifelli
On Wed, Aug 2, 2023 at 6:44 PM Stephen Hemminger
 wrote:
>
> On Wed, 02 Aug 2023 06:39:47 +
> Stanislav Angelovič  wrote:
>
> > Hi folks,
> >
> > I have a quick question: is there a way to get container size when 
> > deserializing an array from a D-Bus message (be it an array of trivial or 
> > non-trivial D-Bus types)? Say I enter a container with 
> > sd_bus_message_enter_container, and then, before reading individual 
> > elements, I'd like to get the number of elements in that array in that 
> > message (so I can reserve storage on my side, etc.).
> >
> > If not, could such an API function be added?
> >
> > Thanks in advance.
> >
> > Stanislav.
>
> The dbus messsage just an encoding of a byte stream. There is nothing in the 
> byte stream
> that indicates the length.

yes, but the whole message is available locally, it could be parsed on
request, discarding the data, and keeping only the number of elements
in an array.


Re: [systemd-devel] sd-bus: get size of array container in D-Bus message

2023-08-02 Thread Stephen Hemminger
On Wed, 02 Aug 2023 06:39:47 +
Stanislav Angelovič  wrote:

> Hi folks,
> 
> I have a quick question: is there a way to get container size when 
> deserializing an array from a D-Bus message (be it an array of trivial or 
> non-trivial D-Bus types)? Say I enter a container with 
> sd_bus_message_enter_container​, and then, before reading individual 
> elements, I'd like to get the number of elements in that array in that 
> message (so I can reserve storage on my side, etc.).
> 
> If not, could such an API function be added?
> 
> Thanks in advance.
> 
> Stanislav.

The dbus messsage just an encoding of a byte stream. There is nothing in the 
byte stream
that indicates the length.


[systemd-devel] sd-bus: get size of array container in D-Bus message

2023-08-02 Thread Stanislav Angelovič
Hi folks,

I have a quick question: is there a way to get container size when 
deserializing an array from a D-Bus message (be it an array of trivial or 
non-trivial D-Bus types)? Say I enter a container with 
sd_bus_message_enter_container​, and then, before reading individual elements, 
I'd like to get the number of elements in that array in that message (so I can 
reserve storage on my side, etc.).

If not, could such an API function be added?

Thanks in advance.

Stanislav.