/* * Copyright (C) 2023 Patrick McDermott * * This file is part of opkg-opk. * * opkg-opk is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * opkg-opk is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with opkg-opk. If not, see . */ #include #include #include #include "defs.h" #include "dirent.h" static int _opkg_opk_dirent_name_prefix(struct opkg_opk_dirent *dirent, int is_dir, char **name_buf, size_t *name_buf_len, char **pref_buf, size_t *pref_buf_len, size_t *name_len, size_t *pref_len) { size_t len; char **buf; int sep; int ret; len = strlen(dirent->name); if ((*name_len > 0 || is_dir) && *name_len + len + 1 <= *name_buf_len) { /* Space available in name buffer. Not last node, or is a * directory, so append separator. */ buf = name_buf; *name_len += len + 1; sep = 1; } else if (*name_len == 0 && *name_len + len <= *name_buf_len) { /* Space available in name buffer. Last node (first in * recursion), so don't append separator. */ buf = name_buf; *name_len += len; sep = 0; } else if (*pref_len > 0 && *pref_len + len + 1 <= *pref_buf_len) { /* Space available in prefix buffer. Not last node, so append * separator. */ buf = pref_buf; *pref_len += len + 1; sep = 1; } else if (*pref_len == 0 && *pref_len + len <= *pref_buf_len) { /* Space available in prefix buffer. Last node (first in * recursion), so don't append separator. */ buf = pref_buf; *pref_len += len; sep = 0; } else { /* Both buffers full. */ return OPKG_OPK_END; } if (*buf == NULL) { /* Caller provided a NULL buffer pointer. */ return OPKG_OPK_ERROR; } /* Recurse. */ if (dirent->parent != NULL && (ret = _opkg_opk_dirent_name_prefix(dirent->parent, is_dir, name_buf, name_buf_len, pref_buf, pref_buf_len, name_len, pref_len)) != OPKG_OPK_OK) { return ret; } /* Copy node name and possibly separator suffix. */ memcpy(*buf, dirent->name, len); *buf += len; if (sep == 1) { **buf = '/'; *buf += 1; } return OPKG_OPK_OK; } int opkg_opk_dirent_name_prefix(struct opkg_opk_dirent *dirent, int is_dir, char *name_buf, size_t name_buf_len, char *pref_buf, size_t pref_buf_len) { size_t name_len = 0; size_t pref_len = 0; char *name = name_buf; char *pref = pref_buf; int ret; /* NUL bytes */ --name_buf_len; --pref_buf_len; if ((ret = _opkg_opk_dirent_name_prefix(dirent, is_dir, &name_buf, &name_buf_len, &pref_buf, &pref_buf_len, &name_len, &pref_len)) != OPKG_OPK_OK) { return ret; } name[name_len] = '\0'; pref[pref_len] = '\0'; return OPKG_OPK_OK; } static char * _opkg_opk_dirent_name_alloc(struct opkg_opk_dirent *dirent, int is_dir, char **cur, size_t *buf_len) { size_t len; int sep; char *buf; len = strlen(dirent->name); if (*buf_len > 1 /* 1 for NUL */ || is_dir) { /* Not last node, or is a directory, so append separator. */ sep = 1; /* Used in math below, so must be 1, not just != 0. */ ++len; } else { /* Last node (first in recursion), so don't append separator. */ sep = 0; } *buf_len += len; if (dirent->parent == NULL) { buf = malloc(*buf_len); if (buf == NULL) { return NULL; } *cur = buf; } else if ((buf = _opkg_opk_dirent_name_alloc(dirent->parent, is_dir, cur, buf_len)) == NULL) { return NULL; } /* Copy node name and possibly separator suffix. */ memcpy(*cur, dirent->name, len - sep); *cur += len - sep; if (sep == 1) { **cur = '/'; *cur += 1; } return buf; } char * opkg_opk_dirent_name_alloc(struct opkg_opk_dirent *dirent, int is_dir) { char *buf; char *cur = NULL; size_t buf_len = 1; /* 1 for NUL */ if ((buf = _opkg_opk_dirent_name_alloc(dirent, is_dir, &cur, &buf_len)) == NULL) { return NULL; } *cur = '\0'; return buf; }