vfs: check userland buffers before reading them.
[haiku.git] / src / bin / pkgman / command_refresh.cpp
blob20fbb13639e5657865cf5f6f67015df23c835e1a
1 /*
2 * Copyright 2011, Oliver Tappe <zooey@hirschkaefere.de>
3 * Distributed under the terms of the MIT License.
4 */
7 #include <getopt.h>
8 #include <stdio.h>
9 #include <stdlib.h>
11 #include <Errors.h>
12 #include <StringList.h>
14 #include <package/Context.h>
15 #include <package/RefreshRepositoryRequest.h>
16 #include <package/PackageRoster.h>
18 #include "Command.h"
19 #include "DecisionProvider.h"
20 #include "JobStateListener.h"
21 #include "pkgman.h"
24 using namespace BPackageKit;
27 // TODO: internationalization!
30 static const char* const kShortUsage =
31 " %command% [<repo-name> ...]\n"
32 " Refreshes all or just the given repositories.\n";
34 static const char* const kLongUsage =
35 "Usage: %program% %command% [<repo-name> ...]\n"
36 "Refreshes all or just the given repositories.\n"
37 "\n";
40 DEFINE_COMMAND(RefreshCommand, "refresh", kShortUsage, kLongUsage,
41 kCommandCategoryRepositories)
44 int
45 RefreshCommand::Execute(int argc, const char* const* argv)
47 while (true) {
48 static struct option sLongOptions[] = {
49 { "help", no_argument, 0, 'h' },
50 { 0, 0, 0, 0 }
53 opterr = 0; // don't print errors
54 int c = getopt_long(argc, (char**)argv, "hu", sLongOptions, NULL);
55 if (c == -1)
56 break;
58 switch (c) {
59 case 'h':
60 PrintUsageAndExit(false);
61 break;
63 default:
64 PrintUsageAndExit(true);
65 break;
69 // The remaining arguments are repo names.
70 const char* const* repoArgs = argv + optind;
71 int nameCount = argc - optind;
73 BStringList repositoryNames(20);
75 BPackageRoster roster;
76 if (nameCount == 0) {
77 status_t result = roster.GetRepositoryNames(repositoryNames);
78 if (result != B_OK)
79 DIE(result, "can't collect repository names");
80 } else {
81 for (int i = 0; i < nameCount; ++i) {
82 if (!repositoryNames.Add(repoArgs[i]))
83 DIE(B_NO_MEMORY, "can't allocate repository name");
87 DecisionProvider decisionProvider;
88 JobStateListener listener;
89 BContext context(decisionProvider, listener);
91 status_t result;
92 for (int i = 0; i < repositoryNames.CountStrings(); ++i) {
93 const BString& repoName = repositoryNames.StringAt(i);
94 BRepositoryConfig repoConfig;
95 result = roster.GetRepositoryConfig(repoName, &repoConfig);
96 if (result != B_OK) {
97 BPath path;
98 repoConfig.Entry().GetPath(&path);
99 WARN(result, "skipping repository-config '%s'", path.Path());
100 continue;
103 BRefreshRepositoryRequest refreshRequest(context, repoConfig);
104 result = refreshRequest.Process();
105 if (result != B_OK) {
106 DIE(result, "request for refreshing repository \"%s\" failed",
107 repoName.String());
111 return 0;