1 /******************************************************************************
2 * example.c
3 * Example for using bkisofs
4 * Author: Andrew Smith
5 * Compile with: cc example.c bk.a -o example
6 * */
7
8 #include <stdio.h>
9 #include <time.h>
10
11 /* need to include bk.h for access to bkisofs functions and structures */
12 #include "bk.h"
13
14 void addProgressUpdaterCbk(VolInfo* volInfo);
15 void fatalError(const char* message);
16 void printNameAndContents(BkFileBase* item, int numSpaces);
17 void readProgressUpdaterCbk(VolInfo* volInfo);
18 void writeProgressUpdaterCbk(VolInfo* volInfo, double percentComplete);
19
main(int argv,char ** argc)20 int main( int argv, char** argc)
21 {
22 /* A variable of type VolInfo stores information about an image */
23 VolInfo volInfo;
24 /* bk functions return ints that need to be checked to see whether
25 * the functions were successful or not */
26 int rc;
27
28 if(argv != 2)
29 fatalError("Usage: example myfile.iso");
30
31 /* initialise volInfo, set it up to scan for duplicate files */
32 rc = bk_init_vol_info(&volInfo, true);
33 if(rc <= 0)
34 fatalError(bk_get_error_string(rc));
35
36 /* open the iso file (supplied as argument 1) */
37 rc = bk_open_image(&volInfo, argc[1]);
38 if(rc <= 0)
39 fatalError(bk_get_error_string(rc));
40
41 /* read information about the volume (required before reading directory tree) */
42 rc = bk_read_vol_info(&volInfo);
43 if(rc <= 0)
44 fatalError(bk_get_error_string(rc));
45
46 /* read the directory tree */
47 if(volInfo.filenameTypes & FNTYPE_ROCKRIDGE)
48 rc = bk_read_dir_tree(&volInfo, FNTYPE_ROCKRIDGE, true, readProgressUpdaterCbk);
49 else if(volInfo.filenameTypes & FNTYPE_JOLIET)
50 rc = bk_read_dir_tree(&volInfo, FNTYPE_JOLIET, false, readProgressUpdaterCbk);
51 else
52 rc = bk_read_dir_tree(&volInfo, FNTYPE_9660, false, readProgressUpdaterCbk);
53 if(rc <= 0)
54 fatalError(bk_get_error_string(rc));
55
56 /* add the file /etc/fstab to the root of the image */
57 rc = bk_add(&volInfo, "/etc/fstab", "/", addProgressUpdaterCbk);
58 if(rc <= 0)
59 fatalError(bk_get_error_string(rc));
60
61 /* print the entire directory tree */
62 printNameAndContents(BK_BASE_PTR( &(volInfo.dirTree) ), 0);
63
64 /* save the new ISO as /tmp/example.iso */
65 /* note that bkisofs will print some stuff to stdout when writing an ISO */
66 rc = bk_write_image("/tmp/example.iso", &volInfo, time(NULL),
67 FNTYPE_9660 | FNTYPE_ROCKRIDGE | FNTYPE_JOLIET,
68 writeProgressUpdaterCbk);
69
70 /* we're finished with this ISO, so clean up */
71 bk_destroy_vol_info(&volInfo);
72
73 return 0;
74 }
75
76 /* you can use this to update a progress bar or something */
addProgressUpdaterCbk(VolInfo * volInfo)77 void addProgressUpdaterCbk(VolInfo* volInfo)
78 {
79 printf("Add progress updater\n");
80 }
81
fatalError(const char * message)82 void fatalError(const char* message)
83 {
84 printf("Fatal error: %s\n", message);
85 exit(1);
86 }
87
printNameAndContents(BkFileBase * base,int numSpaces)88 void printNameAndContents(BkFileBase* base, int numSpaces)
89 {
90 int count;
91
92 /* print the spaces (indentation, for prettyness) */
93 for(count = 0; count < numSpaces; count++)
94 printf(" ");
95
96 if(IS_DIR(base->posixFileMode))
97 {
98 /* print name of the directory */
99 printf("%s (directory)\n", base->name);
100
101 /* print all the directory's children */
102 BkFileBase* child = BK_DIR_PTR(base)->children;
103 while(child != NULL)
104 {
105 printNameAndContents(child, numSpaces + 2);
106 child = child->next;
107 }
108 }
109 else if(IS_REG_FILE(base->posixFileMode))
110 {
111 /* print name and size of the file */
112 printf("%s (regular file), size %u\n", base->name, BK_FILE_PTR(base)->size);
113 }
114 else if(IS_SYMLINK(base->posixFileMode))
115 {
116 /* print name and target of the symbolic link */
117 printf("%s -> %s (symbolic link)\n", base->name, BK_SYMLINK_PTR(base)->target);
118 }
119 }
120
121 /* you can use this to update a progress bar or something */
readProgressUpdaterCbk(VolInfo * volInfo)122 void readProgressUpdaterCbk(VolInfo* volInfo)
123 {
124 printf("Read progress updater\n");
125 }
126
127 /* you can use this to update a progress bar or something */
writeProgressUpdaterCbk(VolInfo * volInfo,double percentComplete)128 void writeProgressUpdaterCbk(VolInfo* volInfo, double percentComplete)
129 {
130 printf("Write progress updater: ~%.2lf%% complete\n", percentComplete);
131 }
132