Code Coverage: vinyl-cache/vmod/vmod_std_fileread.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
0
/*-
1
 * Copyright (c) 2010-2011 Varnish Software AS
2
 * All rights reserved.
3
 *
4
 * Author: Sanjoy Das <sanjoy@playingwithpointers.com>
5
 *
6
 * SPDX-License-Identifier: BSD-2-Clause
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 *
29
 * XXX: It might make sense to use just a single global list of all files
30
 * XXX: and use the call-private pointer to point to the file instance on
31
 * XXX: that list.
32
 * XXX: Duplicates in the global list can be avoided by examining the
33
 * XXX: dev+inode fields of the stat structure.
34
 * XXX: Individual files would need to be refcounted, so they can be
35
 * XXX: deleted when no VCL's reference them.
36
 *
37
 * XXX: We should periodically stat(2) the filename and check if the
38
 * XXX: underlying file has been updated.
39
 */
40
41
#include "config.h"
42
43
#include <stdlib.h>
44
#include <string.h>
45
46
#include "cache/cache.h"
47
48
#include "vfil.h"
49
50
#include "vcc_std_if.h"
51
52
struct frfile {
53
        unsigned                        magic;
54
#define CACHED_FILE_MAGIC 0xa8e9d87a
55
        char                            *file_name;
56
        void                            *contents;
57
        struct vrt_blob                 blob[1];
58
        int                             refcount;
59
        VTAILQ_ENTRY(frfile)            list;
60
};
61
62
static VTAILQ_HEAD(, frfile)    frlist = VTAILQ_HEAD_INITIALIZER(frlist);
63
static pthread_mutex_t          frmtx = PTHREAD_MUTEX_INITIALIZER;
64
65
static void
66
fini_frfile(VRT_CTX, void *ptr)
67 5
{
68 5
        struct frfile *frf;
69
70 5
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
71 5
        CAST_OBJ_NOTNULL(frf, ptr, CACHED_FILE_MAGIC);
72
73 5
        PTOK(pthread_mutex_lock(&frmtx));
74 5
        if (--frf->refcount > 0)
75 5
                frf = NULL;
76 0
        else
77 0
                VTAILQ_REMOVE(&frlist, frf, list);
78 5
        PTOK(pthread_mutex_unlock(&frmtx));
79 5
        if (frf != NULL) {
80 0
                free(frf->contents);
81 0
                free(frf->file_name);
82 0
                FREE_OBJ(frf);
83 0
        }
84 5
}
85
86
static const struct vmod_priv_methods frfile_methods[1] = {{
87
        .magic = VMOD_PRIV_METHODS_MAGIC,
88
        .type = "vmod_std_fileread",
89
        .fini = fini_frfile
90
}};
91
92
static struct frfile *
93
find_frfile(struct vmod_priv *priv, VCL_STRING file_name)
94 36
{
95 36
        struct frfile *frf = NULL;
96 36
        char *s;
97 36
        ssize_t sz = -1;
98
99 36
        AN(priv);
100
101 36
        if (file_name == NULL)
102 0
                return (NULL);
103
104 36
        if (priv->priv != NULL) {
105 20
                CAST_OBJ_NOTNULL(frf, priv->priv, CACHED_FILE_MAGIC);
106 20
                if (!vstrcmp(file_name, frf->file_name))
107 4
                        return (frf);
108 20
        }
109
110 32
        PTOK(pthread_mutex_lock(&frmtx));
111 32
        if (frf != NULL)
112 16
                frf->refcount--;
113 54
        VTAILQ_FOREACH(frf, &frlist, list) {
114 54
                if (!vstrcmp(file_name, frf->file_name)) {
115 20
                        frf->refcount++;
116 20
                        break;
117 20
                }
118 54
        }
119 32
        PTOK(pthread_mutex_unlock(&frmtx));
120 32
        if (frf != NULL) {
121 20
                priv->methods = frfile_methods;
122 20
                priv->priv = frf;
123 20
                return (frf);
124 20
        }
125
126 12
        s = VFIL_readfile(NULL, file_name, &sz);
127 12
        if (s != NULL) {
128 8
                assert(sz >= 0);
129 8
                ALLOC_OBJ(frf, CACHED_FILE_MAGIC);
130 8
                AN(frf);
131 8
                REPLACE(frf->file_name, file_name);
132 8
                frf->refcount = 1;
133 8
                frf->contents = s;
134 8
                frf->blob->magic = VRT_BLOB_MAGIC;
135 8
                frf->blob->blob = s;
136 8
                frf->blob->len = (size_t)sz;
137 8
                priv->methods = frfile_methods;
138 8
                priv->priv = frf;
139 8
                PTOK(pthread_mutex_lock(&frmtx));
140 8
                VTAILQ_INSERT_HEAD(&frlist, frf, list);
141 8
                PTOK(pthread_mutex_unlock(&frmtx));
142 8
        }
143 12
        return (frf);
144 12
}
145
146
VCL_STRING v_matchproto_(td_std_fileread)
147
vmod_fileread(VRT_CTX, struct vmod_priv *priv, VCL_STRING file_name)
148 27
{
149 27
        struct frfile *frf;
150
151 27
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
152 27
        AN(priv);
153
154 27
        frf = find_frfile(priv, file_name);
155 27
        if (frf == NULL)
156 4
                return (NULL);
157 23
        return (frf->contents);
158 27
}
159
160
VCL_BLOB v_matchproto_(td_std_blobread)
161
vmod_blobread(VRT_CTX, struct vmod_priv *priv, VCL_STRING file_name)
162 9
{
163 9
        struct frfile *frf;
164
165 9
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
166 9
        AN(priv);
167
168 9
        frf = find_frfile(priv, file_name);
169 9
        if (frf == NULL)
170 0
                return (NULL);
171 9
        return (frf->blob);
172 9
}