This file is indexed.

/usr/include/dballe/core/structbuf.h is in libdballe-dev 7.7-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 * core/structbuf - memory or file-backed storage of structures
 *
 * Copyright (C) 2014  ARPA-SIM <urpsim@smr.arpa.emr.it>
 *
 * This program 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 2 of the License.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 *
 * Author: Enrico Zini <enrico@enricozini.com>
 */

#ifndef DBALLE_CORE_STRUCTBUF_H
#define DBALLE_CORE_STRUCTBUF_H

#include <wreport/error.h>
#include <cstdlib>
#include <unistd.h>
#include <sys/mman.h>

namespace dballe {

namespace structbuf {
int make_anonymous_tmpfile();
void write_buffer(int fd, void* buf, size_t size);
}

/**
 * Buffer of simple structures that becomes file backed if it grows beyond a
 * certain size.
 *
 * bufsize is the number of T items that we keep in memory before becoming
 * file-backed.
 */
template<typename T, int bufsize=1024>
class Structbuf
{
protected:
    /**
     * In-memory buffer using during appending. When it becomes full, it is
     * flushed out to a temporary file.
     */
    T* membuf = nullptr;

    /// Number of items in membuf
    unsigned membuf_last = 0;

    /**
     * Memory area used for reading. It points to membuf if we are
     * memory-backed, or it is the mmap view of the file if we are file-backed
     */
    const T* readbuf = (const T*)MAP_FAILED;

    /// Number of items appended so far
    size_t m_count = 0;

    /// Unix file descriptor to the temporary file, or -1 if we are memory
    /// backed
    int tmpfile_fd = -1;

public:
    Structbuf()
        : membuf(new T[bufsize])
    {
    }
    ~Structbuf()
    {
        delete[] membuf;
        if (tmpfile_fd != -1)
        {
            if (readbuf != MAP_FAILED)
                munmap((void*)readbuf, m_count * sizeof(T));
            ::close(tmpfile_fd);
        }
    }

    /// Get the number of structures that have been added to the buffer so far
    size_t size() const { return m_count; }

    /// Return true if the buffer has become file-backed
    bool is_file_backed() const { return tmpfile_fd != -1; }

    /// Append an item to the buffer
    void append(const T& val)
    {
        if (readbuf != MAP_FAILED)
            throw wreport::error_consistency("writing to a Structbuf that is already being read");
        if (membuf_last == bufsize)
            write_to_file();
        membuf[membuf_last++] = val;
        ++m_count;
    }

    /// Stop appending and get ready to read back the data
    void ready_to_read()
    {
        if (tmpfile_fd == -1)
            readbuf = membuf;
        else
        {
            // Flush the remaining memory data to file
            if (membuf_last) write_to_file();

            // mmap the file for reading
            readbuf = (const T*)mmap(nullptr, m_count * sizeof(T), PROT_READ, MAP_SHARED, tmpfile_fd, 0);
            if (readbuf == MAP_FAILED)
                throw wreport::error_system("cannot map temporary file contents to memory");
        }
    }

    /// Read back an item
    const T& operator[](size_t idx) const
    {
        return readbuf[idx];
    }

protected:
    void write_to_file()
    {
        if (tmpfile_fd == -1)
            tmpfile_fd = structbuf::make_anonymous_tmpfile();
        structbuf::write_buffer(tmpfile_fd, membuf, sizeof(T) * membuf_last);
        membuf_last = 0;
    }
};

}

#endif