1 /* 2 * Copyright 2013, Haiku, Inc. All Rights Reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Ingo Weinhold <ingo_weinhold@gmx.de> 7 */ 8 9 10 #include <stdio.h> 11 #include <stdlib.h> 12 #include <string.h> 13 14 #include <package/RepositoryConfig.h> 15 #include <package/RepositoryInfo.h> 16 17 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) 24 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) 31 32 33 static const char* sProgramName = "create_repository_config"; 34 35 36 void 37 print_usage_and_exit(bool error) 38 { 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); 47 } 48 49 50 int 51 main(int argc, const char* const* argv) 52 { 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); 57 } 58 print_usage_and_exit(true); 59 } 60 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++]; 65 66 // read the info 67 BPackageKit::BRepositoryInfo repoInfo; 68 DIE_ON_ERROR(repoInfo.SetTo(infoPath), 69 "failed to read repository info file \"%s\"", infoPath); 70 71 if (url == NULL) 72 url = repoInfo.OriginalBaseURL(); 73 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); 81 82 return 0; 83 } 84