xref: /haiku/src/tests/kits/package/make_repo.cpp (revision 3b17d8dd7f1f25ad91f64ea12770200043d56af1)
1 #include <stdio.h>
2 #include <stdlib.h>
3 
4 #include <File.h>
5 #include <Message.h>
6 #include <String.h>
7 
8 #include <package/RepositoryInfo.h>
9 
10 
11 using namespace BPackageKit;
12 
13 
14 int
main(int argc,const char ** argv)15 main(int argc, const char** argv)
16 {
17 	if (argc < 5) {
18 		fprintf(stderr, "usage: %s <name> <url> <priority> <pkg-count>\n",
19 			argv[0]);
20 		return 1;
21 	}
22 
23 	BRepositoryInfo repoInfo;
24 	repoInfo.SetName(argv[1]);
25 	repoInfo.SetURL(argv[2]);
26 	repoInfo.SetPriority(atoi(argv[3]));
27 
28 	BMessage repoInfoArchive;
29 	status_t result = repoInfo.Archive(&repoInfoArchive);
30 	if (result != B_OK) {
31 		fprintf(stderr, "couldn't archive repository-header\n");
32 		return 1;
33 	}
34 
35 	BFile output(argv[1], B_READ_WRITE | B_CREATE_FILE | B_ERASE_FILE);
36 	if ((result = repoInfoArchive.Flatten(&output)) != B_OK) {
37 		fprintf(stderr, "couldn't flatten repository-header archive\n");
38 		return 1;
39 	}
40 
41 	int pkgCount = atoi(argv[4]);
42 	for (int i = 0; i < pkgCount; ++i) {
43 		BMessage pkg('PKGp');
44 
45 		BString name = BString("pkg") << i + 1;
46 		pkg.AddString("name", name.String());
47 
48 		BString majorVersion
49 			= BString() << 1 + i % 5 << "." << i % 10;
50 		BString minorVersion
51 			= BString() << i % 100;
52 		pkg.AddString("version",
53 			(BString() << majorVersion << "." << minorVersion).String());
54 
55 		pkg.AddString("provides",
56 			(BString() << name << "-" << majorVersion).String());
57 		if (i % 2 == 1)
58 			pkg.AddString("provides", (BString("lib") << name).String());
59 		if (i % 3 != 1)
60 			pkg.AddString("provides", (BString("cmd:") << name).String());
61 
62 		if (i > 1) {
63 			int requiresCount = rand() % 10 % i;
64 			for (int r = 0; r < requiresCount; ++r) {
65 				int reqIndex = rand() % i;
66 				pkg.AddString("requires",
67 					(BString("pkg") << reqIndex).String());
68 			}
69 		}
70 
71 		result = pkg.Flatten(&output);
72 		if (result != B_OK) {
73 			fprintf(stderr, "couldn't flatten pkg message #%d\n", i + 1);
74 			return 1;
75 		}
76 	}
77 
78 	return 0;
79 }
80