vfs: check userland buffers before reading them.
[haiku.git] / src / tools / create_repository_config / create_repository_config.cpp
blob9837945b31a50c37a384a9c6f4d83603624cb7f7
1 /*
2 * Copyright 2013, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Ingo Weinhold <ingo_weinhold@gmx.de>
7 */
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
14 #include <package/RepositoryConfig.h>
15 #include <package/RepositoryInfo.h>
18 #define DIE(error, ...) \
19 do { \
20 fprintf(stderr, "Error: " __VA_ARGS__); \
21 fprintf(stderr, ": %s\n", strerror(error)); \
22 exit(1); \
23 } while (false)
25 #define DIE_ON_ERROR(error, ...) \
26 do { \
27 status_t _error = error; \
28 if (error != B_OK) \
29 DIE(_error, __VA_ARGS__); \
30 } while (false)
33 static const char* sProgramName = "create_repository_config";
36 void
37 print_usage_and_exit(bool error)
39 fprintf(error ? stderr : stdout,
40 "Usage: %s [ <URL> ] <repository info> <repository config>\n"
41 "Creates a repository config file from a given repository info and\n"
42 "the base URL (the directory in which the \"repo\", \"repo.info\',\n"
43 "and \"repo.sha256 files can be found). If the URL is not specified,\n"
44 "the one from the repository info is used.",
45 sProgramName);
46 exit(error ? 1 : 0);
50 int
51 main(int argc, const char* const* argv)
53 if (argc < 3 || argc > 4) {
54 if (argc == 2
55 && (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0)) {
56 print_usage_and_exit(false);
58 print_usage_and_exit(true);
61 int argi = 1;
62 const char* url = argc == 4 ? argv[argi++] : NULL;
63 const char* infoPath = argv[argi++];
64 const char* configPath = argv[argi++];
66 // read the info
67 BPackageKit::BRepositoryInfo repoInfo;
68 DIE_ON_ERROR(repoInfo.SetTo(infoPath),
69 "failed to read repository info file \"%s\"", infoPath);
71 if (url == NULL)
72 url = repoInfo.OriginalBaseURL();
74 // init and write the config
75 BPackageKit::BRepositoryConfig repoConfig;
76 repoConfig.SetName(repoInfo.Name());
77 repoConfig.SetBaseURL(url);
78 repoConfig.SetPriority(repoInfo.Priority());
79 DIE_ON_ERROR(repoConfig.Store(configPath),
80 "failed to write repository config file \"%s\"", configPath);
82 return 0;