Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/pkg
Path: blob/main/libpkg/metalog.c
2645 views
1
/*-
2
* Copyright (c) 2016 Brad Davis <[email protected]>
3
* All rights reserved.
4
*
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
7
* are met:
8
* 1. Redistributions of source code must retain the above copyright
9
* notice, this list of conditions and the following disclaimer
10
* in this position and unchanged.
11
* 2. Redistributions in binary form must reproduce the above copyright
12
* notice, this list of conditions and the following disclaimer in the
13
* documentation and/or other materials provided with the distribution.
14
*
15
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
*/
26
27
#include <errno.h>
28
29
#include "pkg.h"
30
#include "private/pkg.h"
31
#include "private/event.h"
32
33
static FILE *metalogfp = NULL;
34
35
int
36
metalog_open(const char *metalog)
37
{
38
metalogfp = fopen(metalog, "ae");
39
if (metalogfp == NULL)
40
pkg_fatal_errno("Unable to open metalog '%s'", metalog);
41
/* Package install scripts may add entries, so avoid interleaving. */
42
setvbuf(metalogfp, NULL, _IOLBF, 0);
43
return (EPKG_OK);
44
}
45
46
int
47
metalog_add(int type, const char *path, const char *uname, const char *gname,
48
int mode, unsigned long fflags, const char *link)
49
{
50
char *fflags_buffer = NULL;
51
int ret = EPKG_FATAL;
52
53
if (metalogfp == NULL)
54
goto out;
55
56
#ifdef HAVE_FFLAGSTOSTR
57
if (fflags) {
58
fflags_buffer = fflagstostr(fflags);
59
}
60
#endif
61
62
// directory
63
switch (type) {
64
case PKG_METALOG_DIR:
65
if (fprintf(metalogfp,
66
"./%s type=dir uname=%s gname=%s mode=%3o%s%s\n",
67
path, uname, gname, mode,
68
fflags ? " flags=" : "",
69
fflags_buffer ? fflags_buffer : "") < 0) {
70
pkg_errno("%s", "Unable to write to the metalog");
71
goto out;
72
}
73
break;
74
case PKG_METALOG_FILE:
75
if (fprintf(metalogfp,
76
"./%s type=file uname=%s gname=%s mode=%3o%s%s\n",
77
path, uname, gname, mode,
78
fflags ? " flags=" : "",
79
fflags_buffer ? fflags_buffer : "") < 0) {
80
pkg_errno("%s", "Unable to write to the metalog");
81
goto out;
82
}
83
break;
84
case PKG_METALOG_LINK:
85
if (fprintf(metalogfp,
86
"./%s type=link uname=%s gname=%s mode=%3o link=%s%s%s\n",
87
path, uname, gname, mode, link,
88
fflags ? " flags=" : "",
89
fflags_buffer ? fflags_buffer : "") < 0) {
90
pkg_errno("%s", "Unable to write to the metalog");
91
goto out;
92
}
93
break;
94
}
95
ret = EPKG_OK;
96
97
out:
98
free(fflags_buffer);
99
return (ret);
100
}
101
102
void
103
metalog_close()
104
{
105
if (metalogfp != NULL) {
106
fclose(metalogfp);
107
}
108
}
109
110