Welcome to little lamb

Code » limb » release » tree

[release] / src / liblimb / posixplz.h / mkfiletempat.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 <errno.h>
#include <string.h>
#include <limb/posixplz.h>
#include <limb/random.h>

int
mkfiletempat(int bfd, char *name, createat_fn fn, mode_t mode, void *data)
{
    size_t len = strlen (name);
    size_t xlen;

    for (xlen = 0; xlen < len && name[len - 1 - xlen] == 'X'; ++xlen)
        ;
    if (xlen < 6) return (errno = EINVAL, -1);

    int r;
    do {
        random_name(name + len - xlen, xlen);
        r = fn(bfd, name, mode, data);
    } while (r < 0 && errno == EEXIST);
    return r;
}