Libarchive - problem when creating archives

Hello,

while trying to create an tar.gz-archive using libarchive I have big problem: The archives where created, but inside the archives I found the compressed folders only, but no files.
I searched the internet and checked the man-pages but didn´t get any clue, what I did wrong. Maybe somebody could give me a hint, what I did wrong:

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
#include <stdlib.h>

#include <sys/stat.h>
#include <fcntl.h>

#include <string.h>

#include <archive.h>
#include <archive_entry.h>

static void tar_create(const char *outname, const char **filename)
{
    struct archive* a;
    struct archive_entry* entry;
    struct stat st;
    char buff[8192];
    int fd(0);
    int len(0);
   
    a = archive_write_new();

    archive_write_set_compression_gzip( a );
    archive_write_set_format_ustar( a );
    archive_write_set_bytes_per_block( a, 4096 );
    archive_write_open_filename( a, outname );
   
    while( *filename ) {
        stat( *filename, &st );
        entry = archive_entry_new();
        archive_entry_copy_stat(entry, &st );
        archive_entry_set_pathname( entry, *filename );
        archive_write_header( a, entry );
        fd = open( *filename, O_RDONLY );
        if( fd != -1 ) {
            len = read( fd, buff, sizeof( buff ));
            while( len > 0 ) {
                //buf contains the correct data and the len the correct length at this point, but check is 0.
                size_t check = archive_write_data( a, buff, len );
                
                len = read(fd, buff, sizeof(buff));
            }
            close(fd);
        }
        archive_entry_free(entry);
        ++filename;
    }
    archive_write_close(a);
    archive_write_finish(a);
} 


What I found out: after
size_t check = archive_write_data( a, buff, len );
check is 0 instead of returning the correct size. Bit I really don´t know why this happens.

Thanks in advance for any help you could give.
I've never used libarchive, but man pages seems to indicate:

archive_write_data() returns a count of the number of bytes actually
     written.   On error, -1 is returned and the archive_errno() and
     archive_error_string() functions will return appropriate values. 


Have you checked archive_errno() or archive_error_string()? Perhaps after Line 48?

If you are getting 0 returned from archive_write_data() and nothing from those functions, you are, indeed, stuck in some weird limbo...
Last edited on
Thanks for your help!

I checked those error strings already, but there was no error.

Meanwhile I fixed my mistake. I thought that copying the stat data will set all needed data about filesize and permissions, but I was wrong. After setting these informations manually everything works. :-)

Topic archived. No new replies allowed.