Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rmdir_leading() improvements #1135

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

alejandro-colomar
Copy link
Collaborator

@alejandro-colomar alejandro-colomar commented Dec 2, 2024

Constify the input, and move code out of the loop.

This introduces strprefix(), a string-handling API that I'll use soon for other purposes.


Revisions:

v2
  • Add const correctness. Now strprefix() is a const-generic macro that will return a pointer that is const-qualified iff the input string is const-qualified.
$ git range-diff master gh/rmdir_leading rmdir_leading 
1:  6bc6e17b ! 1:  68279715 lib/string/strcmp/: strprefix(): Add function
    @@ Metadata
     Author: Alejandro Colomar <[email protected]>
     
      ## Commit message ##
    -    lib/string/strcmp/: strprefix(): Add function
    +    lib/string/strcmp/: strprefix(): Add API
     
         Signed-off-by: Alejandro Colomar <[email protected]>
     
    @@ lib/string/strcmp/strprefix.c (new)
     +#include "string/strcmp/strprefix.h"
     +
     +
    -+extern inline char *strprefix(const char *s, const char *prefix);
    ++extern inline const char *strprefix_(const char *s, const char *prefix);
     
      ## lib/string/strcmp/strprefix.h (new) ##
     @@
    @@ lib/string/strcmp/strprefix.h (new)
     +#include <string.h>
     +
     +#include "attr.h"
    ++#include "cast.h"
    ++
    ++
    ++#define strprefix(s, prefix)                                          \
    ++(                                                                     \
    ++  _Generic(s,                                                   \
    ++          const char *:               strprefix_(s, p),         \
    ++          const void *:               strprefix_(s, p),         \
    ++          char *:  const_cast(char *, strprefix_(s, p)),        \
    ++          void *:  const_cast(char *, strprefix_(s, p))         \
    ++  )                                                             \
    ++)
     +
     +
     +ATTR_STRING(1)
     +ATTR_STRING(2)
    -+inline char *strprefix(const char *s, const char *prefix);
    ++inline const char *strprefix_(const char *s, const char *prefix);
     +
     +
     +/*
     + * Return NULL if s does not start with prefix.
     + * Return `s + strlen(prefix)` if s starts with prefix.
     + */
    -+inline char *
    -+strprefix(const char *s, const char *prefix)
    ++inline const char *
    ++strprefix_(const char *s, const char *prefix)
     +{
     +  if (strncmp(s, prefix, strlen(prefix)) != 0)
     +          return NULL;
     +
    -+  return (char *) s + strlen(prefix);
    ++  return s + strlen(prefix);
     +}
     +
     +
2:  5c5b69fd = 2:  8ba0d74a lib/tcbfuncs.c: rmdir_leading(): Constify input
3:  293831c2 = 3:  4d073b01 lib/tcbfuncs.c: rmdir_leading(): Create string just once
v2b
  • Simplify intermediate diff.
  • wsfix
$ git range-diff master gh/rmdir_leading rmdir_leading 
1:  68279715 = 1:  68279715 lib/string/strcmp/: strprefix(): Add API
2:  8ba0d74a ! 2:  e3d3332c lib/tcbfuncs.c: rmdir_leading(): Constify input
    @@ lib/tcbfuncs.c: static shadowtcb_status unlink_suffs (const char *user)
     +
        while ((ind = strrchr (path, '/'))) {
                stpcpy(ind, "");
    --          if (asprintf (&dir, TCB_DIR "/%s", path) == -1) {
    +           if (asprintf (&dir, TCB_DIR "/%s", path) == -1) {
     -                  OUT_OF_MEMORY;
     -                  return SHADOWTCB_FAILURE;
    --          }
    -+          if (asprintf(&dir, TCB_DIR "/%s", path) == -1)
     +                  goto free_path;
    +           }
     +
                if (rmdir (dir) != 0) {
                        if (errno != ENOTEMPTY) {
3:  4d073b01 ! 3:  d6259969 lib/tcbfuncs.c: rmdir_leading(): Create string just once
    @@ lib/tcbfuncs.c: static shadowtcb_status unlink_suffs (const char *user)
     -  while ((ind = strrchr (path, '/'))) {
     +  while ((ind = strrchr(p, '/'))) {
                stpcpy(ind, "");
    --          if (asprintf(&dir, TCB_DIR "/%s", path) == -1)
    +-          if (asprintf (&dir, TCB_DIR "/%s", path) == -1) {
     -                  goto free_path;
    +-          }
      
     -          if (rmdir (dir) != 0) {
     +          if (rmdir(path) != 0) {
    @@ lib/tcbfuncs.c: static shadowtcb_status unlink_suffs (const char *user)
                                fprintf (shadow_logfd,
                                         _("%s: Cannot remove directory %s: %s\n"),
     -                                   shadow_progname, dir, strerror (errno));
    -+                                   shadow_progname, path, strerror (errno));
    ++                                   shadow_progname, path, strerror(errno));
                                ret = SHADOWTCB_FAILURE;
                        }
     -                  free (dir);
v3
  • tfix
$ git range-diff master gh/rmdir_leading rmdir_leading 
1:  68279715 ! 1:  f8deaed9 lib/string/strcmp/: strprefix(): Add API
    @@ lib/string/strcmp/strprefix.h (new)
     +#define strprefix(s, prefix)                                          \
     +(                                                                     \
     +  _Generic(s,                                                   \
    -+          const char *:               strprefix_(s, p),         \
    -+          const void *:               strprefix_(s, p),         \
    -+          char *:  const_cast(char *, strprefix_(s, p)),        \
    -+          void *:  const_cast(char *, strprefix_(s, p))         \
    ++          const char *:               strprefix_(s, prefix),    \
    ++          const void *:               strprefix_(s, prefix),    \
    ++          char *:  const_cast(char *, strprefix_(s, prefix)),   \
    ++          void *:  const_cast(char *, strprefix_(s, prefix))    \
     +  )                                                             \
     +)
     +
2:  e3d3332c = 2:  3db55209 lib/tcbfuncs.c: rmdir_leading(): Constify input
3:  d6259969 = 3:  b1f60988 lib/tcbfuncs.c: rmdir_leading(): Create string just once
v4
  • Reduce repetition
$ git range-diff master gh/rmdir_leading rmdir_leading 
1:  f8deaed9 ! 1:  ec7ba07c lib/string/strcmp/: strprefix(): Add API
    @@ lib/string/strcmp/strprefix.h (new)
     +
     +
     +#define strprefix(s, prefix)                                          \
    -+(                                                                     \
    ++({                                                                    \
    ++  const char  *p_;                                              \
    ++                                                                      \
    ++  p_ = strprefix_(s, prefix);                                   \
    ++                                                                      \
     +  _Generic(s,                                                   \
    -+          const char *:               strprefix_(s, prefix),    \
    -+          const void *:               strprefix_(s, prefix),    \
    -+          char *:  const_cast(char *, strprefix_(s, prefix)),   \
    -+          void *:  const_cast(char *, strprefix_(s, prefix))    \
    -+  )                                                             \
    -+)
    ++          const char *:                     p_,                 \
    ++          const void *:                     p_,                 \
    ++          char *:        const_cast(char *, p_),                \
    ++          void *:        const_cast(char *, p_)                 \
    ++  );
    ++})
     +
     +
     +ATTR_STRING(1)
2:  3db55209 = 2:  d889874b lib/tcbfuncs.c: rmdir_leading(): Constify input
3:  b1f60988 = 3:  7cb4a91b lib/tcbfuncs.c: rmdir_leading(): Create string just once
v4b
  • Add missing \
$ git range-diff master gh/rmdir_leading rmdir_leading 
1:  ec7ba07c ! 1:  a165d0c7 lib/string/strcmp/: strprefix(): Add API
    @@ lib/string/strcmp/strprefix.h (new)
     +          const void *:                     p_,                 \
     +          char *:        const_cast(char *, p_),                \
     +          void *:        const_cast(char *, p_)                 \
    -+  );
    ++  );                                                            \
     +})
     +
     +
2:  d889874b = 2:  d7ce4268 lib/tcbfuncs.c: rmdir_leading(): Constify input
3:  7cb4a91b = 3:  05c4ffb4 lib/tcbfuncs.c: rmdir_leading(): Create string just once
v4c
  • Rebase
$ git range-diff master..gh/rmdir_leading shadow/master..rmdir_leading 
1:  a165d0c7 = 1:  064e7a50 lib/string/strcmp/: strprefix(): Add API
2:  d7ce4268 = 2:  e134d63a lib/tcbfuncs.c: rmdir_leading(): Constify input
3:  05c4ffb4 = 3:  4cd5dedc lib/tcbfuncs.c: rmdir_leading(): Create string just once
v4d
  • Rebase
$ git range-diff master..gh/rmdir_leading shadow/master..rmdir_leading 
1:  064e7a50 = 1:  024ee8e0 lib/string/strcmp/: strprefix(): Add API
2:  e134d63a = 2:  caf89f8e lib/tcbfuncs.c: rmdir_leading(): Constify input
3:  4cd5dedc = 3:  04d59a59 lib/tcbfuncs.c: rmdir_leading(): Create string just once
v4e
  • Rebase
$ git range-diff master..gh/rmdir_leading shadow/master..rmdir_leading 
1:  024ee8e0 = 1:  1f0a9146 lib/string/strcmp/: strprefix(): Add API
2:  caf89f8e = 2:  0bfe0cfa lib/tcbfuncs.c: rmdir_leading(): Constify input
3:  04d59a59 = 3:  bd50dbcc lib/tcbfuncs.c: rmdir_leading(): Create string just once
v4f
  • Rebase
$ git range-diff master..gh/rmdir_leading shadow/master..rmdir_leading 
1:  1f0a9146 = 1:  962d9a6e lib/string/strcmp/: strprefix(): Add API
2:  0bfe0cfa = 2:  52770ba2 lib/tcbfuncs.c: rmdir_leading(): Constify input
3:  bd50dbcc = 3:  4c491145 lib/tcbfuncs.c: rmdir_leading(): Create string just once
v4g
  • Rebase
$ git range-diff alx/master..gh/rmdir_leading master..rmdir_leading 
1:  962d9a6e = 1:  5e5ec22c lib/string/strcmp/: strprefix(): Add API
2:  52770ba2 = 2:  88bf3c7a lib/tcbfuncs.c: rmdir_leading(): Constify input
3:  4c491145 = 3:  52b40efd lib/tcbfuncs.c: rmdir_leading(): Create string just once
v4h
  • Rebase
$ git range-diff alx/master..gh/rmdir_leading master..rmdir_leading 
1:  5e5ec22c = 1:  51b7da6b lib/string/strcmp/: strprefix(): Add API
2:  88bf3c7a = 2:  b351c7b4 lib/tcbfuncs.c: rmdir_leading(): Constify input
3:  52b40efd = 3:  200c2deb lib/tcbfuncs.c: rmdir_leading(): Create string just once
v4i
  • Rebase
$ git range-diff master..gh/rmdir_leading shadow/master..rmdir_leading 
1:  51b7da6b = 1:  ca1eded7 lib/string/strcmp/: strprefix(): Add API
2:  b351c7b4 = 2:  ecc493a6 lib/tcbfuncs.c: rmdir_leading(): Constify input
3:  200c2deb = 3:  ee0f6c2e lib/tcbfuncs.c: rmdir_leading(): Create string just once

@alejandro-colomar alejandro-colomar force-pushed the rmdir_leading branch 8 times, most recently from 04d59a5 to bd50dbc Compare December 6, 2024 11:49
@alejandro-colomar
Copy link
Collaborator Author

Queued after the release of 4.17.0.

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.

1 participant