aboutsummaryrefslogtreecommitdiff
path: root/paludis/util/safe_ifstream.cc
blob: 62704ba4c517146b5f485577db1ecaf8f4ace175 (plain)
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
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2009, 2010 Ciaran McCreesh
 *
 * This file is part of the Paludis package manager. Paludis is free software;
 * you can redistribute it and/or modify it under the terms of the GNU General
 * Public License version 2, as published by the Free Software Foundation.
 *
 * Paludis 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., 59 Temple
 * Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <paludis/util/safe_ifstream.hh>
#include <paludis/util/stringify.hh>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>
#include <cstring>
#include <errno.h>

using namespace paludis;

SafeIFStreamBuf::SafeIFStreamBuf(const int f) :
    fd(f)
{
    setg(buffer + lookbehind_size, buffer + lookbehind_size, buffer + lookbehind_size);
    underflow();
}

SafeIFStreamBuf::int_type
SafeIFStreamBuf::underflow()
{
    if (gptr() < egptr())
        return traits_type::to_int_type(*gptr());

    int n_putback = gptr() - eback();
    if (n_putback > lookbehind_size)
        n_putback = lookbehind_size;

    std::memmove(buffer + (lookbehind_size - n_putback), gptr() - n_putback, n_putback);

    int n_read(read(fd, buffer + lookbehind_size, buffer_size - lookbehind_size));
    if (-1 == n_read)
        throw SafeIFStreamError("Error reading from fd " + stringify(fd) + ": " + strerror(errno));
    else if (0 == n_read)
        return traits_type::eof();

    setg(buffer + (lookbehind_size - n_putback), buffer + lookbehind_size, buffer + lookbehind_size + n_read);
    return traits_type::to_int_type(*gptr());
}

SafeIFStreamBuf::pos_type
SafeIFStreamBuf::seekoff(off_type off, std::ios_base::seekdir dir, std::ios_base::openmode)
{
    off_t result;
    if (dir == std::ios_base::beg)
        result = lseek(fd, off, SEEK_SET);
    else if (dir == std::ios_base::cur)
        result = lseek(fd, off, SEEK_CUR);
    else if (dir == std::ios_base::end)
        result = lseek(fd, off, SEEK_END);
    else
        throw SafeIFStreamError("Got bad std::ios_base::seekdir");

    if (-1 == result)
        throw SafeIFStreamError("Error seeking to offset with fd " + stringify(fd) + ": " + strerror(errno));

    setg(buffer + lookbehind_size, buffer + lookbehind_size, buffer + lookbehind_size);
    return result;
}

SafeIFStreamBuf::pos_type
SafeIFStreamBuf::seekpos(pos_type p, std::ios_base::openmode)
{
    off_t result(lseek(fd, p, SEEK_SET));
    if (-1 == result)
        throw SafeIFStreamError("Error seeking with fd " + stringify(fd) + ": " + strerror(errno));

    setg(buffer + lookbehind_size, buffer + lookbehind_size, buffer + lookbehind_size);
    return p;
}

SafeIFStreamBase::SafeIFStreamBase(const int f) :
    buf(f)
{
}

namespace
{
    int open_fsentry(const FSEntry & e)
    {
        Context context("When opening '" + stringify(e) + "' for read:");

        int result(open(stringify(e).c_str(), O_RDONLY | O_CLOEXEC));
        if (-1 == result)
            throw SafeIFStreamError("Could not open '" + stringify(e) + "': " + strerror(errno));

        return result;
    }
}

SafeIFStream::SafeIFStream(const int f) :
    SafeIFStreamBase(f),
    std::istream(&buf),
    _close(false)
{
}

SafeIFStream::SafeIFStream(const FSEntry & e) :
    SafeIFStreamBase(open_fsentry(e)),
    std::istream(&buf),
    _close(true)
{
}

SafeIFStream::~SafeIFStream()
{
    if (_close)
        ::close(buf.fd);
}

SafeIFStreamError::SafeIFStreamError(const std::string & s) throw () :
    Exception(s)
{
}