summaryrefslogtreecommitdiff
blob: 201d57d227b4d1c1daa9e6850ead4b14415fcb53 (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/******************************************************************************
 * example.c
 * Example for using bkisofs
 * Author: Andrew Smith
 * Compile with: cc example.c bk.a -o example
 * */

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

/* need to include bk.h for access to bkisofs functions and structures */
#include "bkisofs/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 < 3) {
		puts("Usage: ingenue-bkisofs <in.iso> <action1> <arg1> [<arg2>] [<action2> <arg2> [etc.]]\nValid actions:\n\tadd <path> <source>\n\treplace <path> <source>\n\tdelete <path>\n\tvolname <name>\n\tpublisher <name>\n\textract <path> <destination>\n\tprint\n\twrite <filename>");
		exit(1);
	}

	/* 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));

	unsigned short i=3;
	char* slash;
	for (i=2; i<argv; i++) {
		if (strcmp(argc[i], "replace") == 0 || strcmp(argc[i], "delete") == 0) {
			printf("delete %s\n", argc[i+1]);
			rc = bk_delete(&volInfo, argc[i+1]);
			if (strcmp(argc[i], "delete") == 0)
				i++;
		}
		if (strcmp(argc[i], "add") == 0 || strcmp(argc[i], "replace") == 0) {
			if (argc[i+1][strlen(argc[i+1])-1] == '/') {
				printf("add %s -> %s\n", argc[i+2], argc[i+1]);
				rc = bk_add(&volInfo, argc[i+2], argc[i+1], addProgressUpdaterCbk);
			} else {
				slash=strrchr(argc[i+1], '/');
				*slash='\0';
				printf("add %s -> %s[/]%s\n", argc[i+2], argc[i+1][0]?argc[i+1]:"/", slash+1);
				rc = bk_add_as(&volInfo, argc[i+2], argc[i+1][0]?argc[i+1]:"/", slash+1, addProgressUpdaterCbk);
			}
			i+=2;
		} else if (strcmp(argc[i], "rename") == 0) {
			printf("rename %s -> %s\n", argc[i+1], argc[i+2]);
			rc = bk_rename(&volInfo, argc[i++], argc[i++]);
		} else if (strcmp(argc[i], "write") == 0) {
			printf("write %s\n", argc[i+1]);
			rc = bk_write_image(argc[i+1], &volInfo, time(NULL), volInfo.filenameTypes, writeProgressUpdaterCbk);
		} else if (strcmp(argc[i], "print") == 0) {
			printNameAndContents(BK_BASE_PTR( &(volInfo.dirTree) ), 0);
		} else if (strcmp(argc[i], "extract") == 0) {
			if (argc[i+2][strlen(argc[i+2])-1] == '/') {
				printf("extract %s -> %s\n", argc[i+1], argc[i+2]);
				rc = bk_extract(&volInfo, argc[i+1], argc[i+2], true, NULL);
			} else {
				slash=strrchr(argc[i+2], '/');
				*slash='\0';
				printf("extract %s -> %s[/]%s\n", argc[i+1], argc[i+2][0]?argc[i+2]:"/", slash+1);
				rc = bk_extract_as(&volInfo, argc[i+1], argc[i+2][0]?argc[i+2]:"/", slash+1, true, NULL);
			}
			i+=2;
		} else if (strcmp(argc[i], "volname") == 0) {
			rc=bk_set_vol_name(&volInfo, argc[i++]);
		} else if (strcmp(argc[i], "publisher") == 0) {
			rc=bk_set_publisher(&volInfo, argc[i++]);
		}
		if (rc <= 0)
			fatalError(bk_get_error_string(rc));
	}

	/* 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"); // We don't like progress
}

/* 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);
}