/*-1* Copyright (c) 2016 Brad Davis <[email protected]>2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer9* in this position and unchanged.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR15* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES16* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.17* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,18* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT19* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,20* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY21* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT22* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF23* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.24*/2526#include <errno.h>2728#include "pkg.h"29#include "private/pkg.h"30#include "private/event.h"3132static FILE *metalogfp = NULL;3334int35metalog_open(const char *metalog)36{37metalogfp = fopen(metalog, "ae");38if (metalogfp == NULL)39pkg_fatal_errno("Unable to open metalog '%s'", metalog);40/* Package install scripts may add entries, so avoid interleaving. */41setvbuf(metalogfp, NULL, _IOLBF, 0);42return (EPKG_OK);43}4445int46metalog_add(int type, const char *path, const char *uname, const char *gname,47int mode, unsigned long fflags, const char *link)48{49char *fflags_buffer = NULL;50int ret = EPKG_FATAL;5152if (metalogfp == NULL)53goto out;5455#ifdef HAVE_FFLAGSTOSTR56if (fflags) {57fflags_buffer = fflagstostr(fflags);58}59#endif6061// directory62switch (type) {63case PKG_METALOG_DIR:64if (fprintf(metalogfp,65"./%s type=dir uname=%s gname=%s mode=%3o%s%s\n",66path, uname, gname, mode,67fflags ? " flags=" : "",68fflags_buffer ? fflags_buffer : "") < 0) {69pkg_errno("%s", "Unable to write to the metalog");70goto out;71}72break;73case PKG_METALOG_FILE:74if (fprintf(metalogfp,75"./%s type=file uname=%s gname=%s mode=%3o%s%s\n",76path, uname, gname, mode,77fflags ? " flags=" : "",78fflags_buffer ? fflags_buffer : "") < 0) {79pkg_errno("%s", "Unable to write to the metalog");80goto out;81}82break;83case PKG_METALOG_LINK:84if (fprintf(metalogfp,85"./%s type=link uname=%s gname=%s mode=%3o link=%s%s%s\n",86path, uname, gname, mode, link,87fflags ? " flags=" : "",88fflags_buffer ? fflags_buffer : "") < 0) {89pkg_errno("%s", "Unable to write to the metalog");90goto out;91}92break;93}94ret = EPKG_OK;9596out:97free(fflags_buffer);98return (ret);99}100101void102metalog_close()103{104if (metalogfp != NULL) {105fclose(metalogfp);106}107}108109110