Skip to content

libnetplan: String helpers for the new API design + filepath transition#275

Merged
schopin-pro merged 3 commits into
canonical:mainfrom
schopin-pro:lib-api-string
May 5, 2022
Merged

libnetplan: String helpers for the new API design + filepath transition#275
schopin-pro merged 3 commits into
canonical:mainfrom
schopin-pro:lib-api-string

Conversation

@schopin-pro
Copy link
Copy Markdown
Contributor

Description

This PR implements some infrastructure to easily implement new APIs according to our guidelines, especially regarding strings.
To demonstrate it, the last commit transition netdef->filename into netdef->filepath, implementing the new getter accordingly.

Checklist

  • Runs make check successfully.
  • Retains 100% code coverage (make check-coverage).

@schopin-pro schopin-pro requested a review from slyon May 3, 2022 11:06
Copy link
Copy Markdown
Contributor

@slyon slyon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much! LGTM.

I left some minor inline comments that we should fix while rebasing the branch, before we merge it. Very nice handling of the deprecation warning and the python buffer allocation that is automatically increasing in size!

Comment thread src/util.c
Comment thread include/netplan.h
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 5, 2022

Codecov Report

Merging #275 (a0ccbb4) into main (90311f1) will decrease coverage by 0.05%.
The diff coverage is 91.83%.

❗ Current head a0ccbb4 differs from pull request most recent head b03ce94. Consider uploading reports for the commit b03ce94 to get more accurate results

@@            Coverage Diff             @@
##             main     #275      +/-   ##
==========================================
- Coverage   99.10%   99.04%   -0.06%     
==========================================
  Files          61       61              
  Lines       11129    10893     -236     
==========================================
- Hits        11029    10789     -240     
- Misses        100      104       +4     
Impacted Files Coverage Δ
src/abi_compat.c 79.36% <50.00%> (-3.69%) ⬇️
netplan/libnetplan.py 99.46% <94.44%> (-0.54%) ⬇️
src/dbus.c 100.00% <100.00%> (ø)
src/error.c 100.00% <100.00%> (ø)
src/parse.c 99.82% <100.00%> (-0.02%) ⬇️
src/types.c 100.00% <100.00%> (ø)
src/util.c 100.00% <100.00%> (ø)
tests/test_libnetplan.py 100.00% <100.00%> (ø)
... and 9 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 90311f1...b03ce94. Read the comment docs.

This allows us to access safer string manipulation functions such as
stpncpy (no that's not a type), which are available in the POSIX 2008
standard. This specific function will be useful in implementing the new
API pattern of user-provided string buffers.

In the newer versions of the POSIX standard, usleep has been deprecated
then removed entirely, to be replaced by nanosleep, hence the change in
dbus.c.
Comment thread src/util.c Outdated
@slyon
Copy link
Copy Markdown
Contributor

slyon commented May 5, 2022

Thank you very much. LGTM.

Feel free to merge it!

This function copies a NUL-terminated string into a sized buffer using
the semantics decided in our API design document, with two small
differences:

* Except for the NULL case, the output buffer is systematically
  overwritten.
* The return value isn't the last written byte, but the length of the
  NUL-terminated string *including the first NUL* (i.e. what can be
  safely memcpy-ed). That's because stpncpy writes NULs up 'til the end
  of the buffer.

Those differences seemed minor enough not to be bothered with
reimplementing a string manipulation function from scratch because of
them.

V2: added documentation on the netplan_copy_string function.
V3: fix typo
As discussed, we rename the ambiguous filename field into filepath, as
it denotes a full file patch, not the file base name. We introduce a new
API for it that conforms to the API design guidelines (that should
really be spelled out in the repo :-) ).

The older function is kept around for ABI compatibility but moved out of the
API, hence the API break warning.
@schopin-pro schopin-pro merged commit 6b9a3b7 into canonical:main May 5, 2022
@schopin-pro schopin-pro deleted the lib-api-string branch May 5, 2022 14:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants