Welcome to little lamb

Code » limb » master » tree

[master] / src / liblimb / djbunix.h / rmstar_in_tmpat.c

/* This file is part of limb                           https://lila.oss/limb
 * Copyright (C) 2023 Olivier Brunel                          jjk@jjacky.com */
/* SPDX-License-Identifier: GPL-2.0-only */
#include <limb/djbunix.h>

int
rmstar_in_tmpat(int fd, stralloc *sa, size_t offset)
{
    size_t salen = sa->len;
    size_t maxlen;
    int ret = 0;

    /* get all names from within */
    if (sa_lsat(sa, fd, sa->s + offset, &maxlen) < 0)
        return -1;

    size_t l = strlen(sa->s + offset);
    size_t pathoff = sa->len;

    /* make sure we can store dir/longestfile */
    if (!stralloc_readyplus(sa, l + 1 + maxlen + 1)) {
        ret = -1;
        goto err;
    }

    /* add the common (relative to fd) path */
    stralloc_catb(sa, sa->s + offset, l);
    stralloc_catb(sa, "/", 1);
    size_t fileoff = sa->len;

    /* and remove them all */
    for (size_t i = salen; i < pathoff; ) {
        l = strlen(sa->s + i) + 1;
        sa->len = fileoff;
        stralloc_catb(sa, sa->s + i, l);
        if (rm_rf_in_tmpat(fd, sa, pathoff) < 0) {
            ret = -1;
            break;
        }
        i += l;
    }

err:
    sa->len = salen;
    return ret;
}