aboutsummaryrefslogtreecommitdiff
path: root/paludis/tar_extras.cc
blob: 4e7199b4e932f445547d407f1d23dd429d3992c5 (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
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

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

struct PaludisTarExtras
{
    struct archive * archive;
};

extern "C"
PaludisTarExtras *
paludis_tar_extras_init(const std::string & f)
{
    auto extras(new PaludisTarExtras);
    extras->archive = archive_write_new();
    archive_write_set_compression_none(extras->archive);
    archive_write_set_format_pax(extras->archive);
    archive_write_open_filename(extras->archive, f.c_str());
    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());
    archive_read_disk_set_standard_lookup(disk_archive);

    struct archive_entry * entry(archive_entry_new());

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

    archive_entry_copy_pathname(entry, path.c_str());
    archive_read_disk_entry_from_file(disk_archive, entry, fd, 0);
    archive_write_header(extras->archive, entry);

    int bytes_read;
    char buf[4096];
    while ((bytes_read = read(fd, buf, sizeof(buf))) > 0)
        archive_write_data(extras->archive, buf, bytes_read);

    close(fd);

    archive_write_finish_entry(extras->archive);
    archive_read_finish(disk_archive);
    archive_entry_free(entry);
}

extern "C"
void
paludis_tar_extras_cleanup(PaludisTarExtras * const extras)
{
    archive_write_close(extras->archive);
    archive_write_finish(extras->archive);
    delete extras;
}