aboutsummaryrefslogtreecommitdiff
path: root/paludis/tar_extras.cc
blob: 239504a5ffb8a389e5e2260ebd4eaafb1298a110 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2010, 2011 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/tar_extras.hh>
#include <paludis/merger.hh>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <archive.h>
#include <archive_entry.h>

#include "config.h"

using namespace paludis;

struct PaludisTarExtras
{
    struct archive * archive;
    struct archive_entry_linkresolver * linkresolver;
};

extern "C"
PaludisTarExtras *
paludis_tar_extras_init(const std::string & f, const std::string & compress)
{
    auto extras(new PaludisTarExtras);
    extras->archive = archive_write_new();

    if (! extras->archive)
        throw MergerError("archive_write_new returned null");

    if (compress == "bz2")
        archive_write_set_compression_bzip2(extras->archive);
    else
        archive_write_set_compression_none(extras->archive);

    archive_write_set_format_gnutar(extras->archive);

    if (ARCHIVE_OK != archive_write_open_filename(extras->archive, f.c_str()))
        throw MergerError("archive_write_open_filename failed");

    extras->linkresolver = archive_entry_linkresolver_new();

    if (extras->linkresolver == NULL)
        throw MergerError("archive_entry_linkresolver_new failed");

    archive_entry_linkresolver_set_strategy(extras->linkresolver, archive_format(extras->archive));

    return extras;
}

extern "C"
void
paludis_tar_extras_add_file(PaludisTarExtras * const extras, const std::string & from, const std::string & path)
{
    struct archive * disk_archive(archive_read_disk_new());
    if (! disk_archive)
        throw MergerError("archive_read_disk_new failed");

    archive_read_disk_set_standard_lookup(disk_archive);

    struct archive_entry * entry(archive_entry_new());
    struct archive_entry * sparse(archive_entry_new());

    int fd(open(from.c_str(), O_RDONLY));

    archive_entry_copy_pathname(entry, path.c_str());
    if (ARCHIVE_OK != archive_read_disk_entry_from_file(disk_archive, entry, fd, 0))
        throw MergerError("archive_read_disk_entry_from_file failed");

    archive_entry_linkify(extras->linkresolver, &entry, &sparse);

    if (ARCHIVE_OK != archive_write_header(extras->archive, entry))
        throw MergerError("archive_write_header failed");

    if (archive_entry_size(entry) > 0)
    {
        int bytes_read;
        char buf[4096];
        while ((bytes_read = read(fd, buf, sizeof(buf))) > 0)
            if (bytes_read != archive_write_data(extras->archive, buf, bytes_read))
                throw MergerError("archive_write_data failed");

        close(fd);

        if (ARCHIVE_OK != archive_write_finish_entry(extras->archive))
            throw MergerError("archive_write_finish_entry failed");

        if (ARCHIVE_OK != archive_read_finish(disk_archive))
            throw MergerError("archive_read_finish failed");
    }
    else
        close(fd);

    archive_entry_free(entry);
}

extern "C"
void
paludis_tar_extras_add_sym(PaludisTarExtras * const extras, const std::string & from, const std::string & path, const std::string & dest)
{
    struct archive_entry * entry(archive_entry_new());
    if (! entry)
        throw MergerError("archive_entry_new returned null");

    struct stat st;
    if (0 != lstat(from.c_str(), &st))
        throw MergerError("lstat failed");

    archive_entry_copy_pathname(entry, path.c_str());
    archive_entry_copy_stat(entry, &st);
    archive_entry_copy_symlink(entry, dest.c_str());
    archive_entry_set_size(entry, dest.length());
    if (ARCHIVE_OK != archive_write_header(extras->archive, entry))
        throw MergerError("archive_write_header failed");

    if (ARCHIVE_OK != archive_write_finish_entry(extras->archive))
        throw MergerError("archive_write_finish_entry failed");

    archive_entry_free(entry);
}

extern "C"
void
paludis_tar_extras_cleanup(PaludisTarExtras * const extras)
{
    if (ARCHIVE_OK != archive_write_close(extras->archive))
        throw MergerError("archive_write_close failed");
    if (ARCHIVE_OK != archive_write_finish(extras->archive))
        throw MergerError("archive_write_finish failed");
    archive_entry_linkresolver_free(extras->linkresolver);
    delete extras;
}