summaryrefslogtreecommitdiff
blob: 47676646f4bc8fd570aa32e0fe2057fa5e729076 (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
/******************************************************************************
* example.c
* Example for using bkisofs
* Author: Andrew Smith
* Compile with: cc example.c bk.a -o example
* */

#include <stdio.h>
#include <time.h>

/* need to include bk.h for access to bkisofs functions and structures */
#include "bk.h"

void addProgressUpdaterCbk(VolInfo* volInfo);
void fatalError(const char* message);
void printNameAndContents(BkFileBase* item, int numSpaces);
void readProgressUpdaterCbk(VolInfo* volInfo);
void writeProgressUpdaterCbk(VolInfo* volInfo, double percentComplete);

int main( int argv, char** argc)
{
    /* A variable of type VolInfo stores information about an image */
    VolInfo volInfo;
    /* bk functions return ints that need to be checked to see whether
    * the functions were successful or not */
    int rc;
    
    if(argv != 2)
        fatalError("Usage: example myfile.iso");
    
    /* initialise volInfo, set it up to scan for duplicate files */
    rc = bk_init_vol_info(&volInfo, true);
    if(rc <= 0)
        fatalError(bk_get_error_string(rc));
    
    /* open the iso file (supplied as argument 1) */
    rc = bk_open_image(&volInfo, argc[1]);
    if(rc <= 0)
        fatalError(bk_get_error_string(rc));
    
    /* read information about the volume (required before reading directory tree) */
    rc = bk_read_vol_info(&volInfo);
    if(rc <= 0)
        fatalError(bk_get_error_string(rc));
    
    /* read the directory tree */
    if(volInfo.filenameTypes & FNTYPE_ROCKRIDGE)
        rc = bk_read_dir_tree(&volInfo, FNTYPE_ROCKRIDGE, true, readProgressUpdaterCbk);
    else if(volInfo.filenameTypes & FNTYPE_JOLIET)
        rc = bk_read_dir_tree(&volInfo, FNTYPE_JOLIET, false, readProgressUpdaterCbk);
    else
        rc = bk_read_dir_tree(&volInfo, FNTYPE_9660, false, readProgressUpdaterCbk);
    if(rc <= 0)
        fatalError(bk_get_error_string(rc));
    
    /* add the file /etc/fstab to the root of the image */
    rc = bk_add(&volInfo, "/etc/fstab", "/", addProgressUpdaterCbk);
    if(rc <= 0)
        fatalError(bk_get_error_string(rc));
    
    /* print the entire directory tree */
    printNameAndContents(BK_BASE_PTR( &(volInfo.dirTree) ), 0);
    
    /* save the new ISO as /tmp/example.iso */
    /* note that bkisofs will print some stuff to stdout when writing an ISO */
    rc = bk_write_image("/tmp/example.iso", &volInfo, time(NULL),
                        FNTYPE_9660 | FNTYPE_ROCKRIDGE | FNTYPE_JOLIET,
                        writeProgressUpdaterCbk);
    
    /* we're finished with this ISO, so clean up */
    bk_destroy_vol_info(&volInfo);
    
    return 0;
}

/* you can use this to update a progress bar or something */
void addProgressUpdaterCbk(VolInfo* volInfo)
{
    printf("Add progress updater\n");
}

void fatalError(const char* message)
{
    printf("Fatal error: %s\n", message);
    exit(1);
}

void printNameAndContents(BkFileBase* base, int numSpaces)
{
    int count;
    
    /* print the spaces (indentation, for prettyness) */
    for(count = 0; count < numSpaces; count++)
        printf(" ");
    
    if(IS_DIR(base->posixFileMode))
    {
        /* print name of the directory */
        printf("%s (directory)\n", base->name);
        
        /* print all the directory's children */
        BkFileBase* child = BK_DIR_PTR(base)->children;
        while(child != NULL)
        {
            printNameAndContents(child, numSpaces + 2);
            child = child->next;
        }
    }
    else if(IS_REG_FILE(base->posixFileMode))
    {
        /* print name and size of the file */
        printf("%s (regular file), size %u\n", base->name, BK_FILE_PTR(base)->size);
    }
    else if(IS_SYMLINK(base->posixFileMode))
    {
        /* print name and target of the symbolic link */
        printf("%s -> %s (symbolic link)\n", base->name, BK_SYMLINK_PTR(base)->target);
    }
}

/* you can use this to update a progress bar or something */
void readProgressUpdaterCbk(VolInfo* volInfo)
{
    printf("Read progress updater\n");
}

/* you can use this to update a progress bar or something */
void writeProgressUpdaterCbk(VolInfo* volInfo, double percentComplete)
{
    printf("Write progress updater: ~%.2lf%% complete\n", percentComplete);
}