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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
/*
* minimal_client.c - a minimal Subversion client application ("hello world")
*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*
* This app demonstrates how to use the svn_client.h API.
*
* It reads a directory URL from the commandline, runs
* svn_client_list() and prints the list of directory-entries. It
* also knows how to deal with basic username/password authentication
* challenges.
*
* For a much more complex example, the svn cmdline client might be
* considered the 'reference implementation'.
*
* To compile on Debian:
*
* cc minimal_client.c -o minimal_client \
* -I/usr/include/subversion-1 $(pkg-config --cflags apr-1) -lsvn_client-1
*
*/
#include "svn_client.h"
#include "svn_cmdline.h"
#include "svn_pools.h"
#include "svn_config.h"
#include "svn_fs.h"
/* Display a prompt and read a one-line response into the provided buffer,
removing a trailing newline if present. */
static svn_error_t *
prompt_and_read_line(const char *prompt,
char *buffer,
size_t max)
{
int len;
printf("%s: ", prompt);
if (fgets(buffer, max, stdin) == NULL)
return svn_error_create(0, NULL, "error reading stdin");
len = strlen(buffer);
if (len > 0 && buffer[len-1] == '\n')
buffer[len-1] = 0;
return SVN_NO_ERROR;
}
/* A tiny callback function of type 'svn_auth_simple_prompt_func_t'. For
a much better example, see svn_cl__auth_simple_prompt in the official
svn cmdline client. */
static svn_error_t *
my_simple_prompt_callback (svn_auth_cred_simple_t **cred,
void *baton,
const char *realm,
const char *username,
svn_boolean_t may_save,
apr_pool_t *pool)
{
svn_auth_cred_simple_t *ret = apr_pcalloc (pool, sizeof (*ret));
char answerbuf[100];
if (realm)
{
printf ("Authentication realm: %s\n", realm);
}
if (username)
ret->username = apr_pstrdup (pool, username);
else
{
SVN_ERR (prompt_and_read_line("Username", answerbuf, sizeof(answerbuf)));
ret->username = apr_pstrdup (pool, answerbuf);
}
SVN_ERR (prompt_and_read_line("Password", answerbuf, sizeof(answerbuf)));
ret->password = apr_pstrdup (pool, answerbuf);
*cred = ret;
return SVN_NO_ERROR;
}
/* A tiny callback function of type 'svn_auth_username_prompt_func_t'. For
a much better example, see svn_cl__auth_username_prompt in the official
svn cmdline client. */
static svn_error_t *
my_username_prompt_callback (svn_auth_cred_username_t **cred,
void *baton,
const char *realm,
svn_boolean_t may_save,
apr_pool_t *pool)
{
svn_auth_cred_username_t *ret = apr_pcalloc (pool, sizeof (*ret));
char answerbuf[100];
if (realm)
{
printf ("Authentication realm: %s\n", realm);
}
SVN_ERR (prompt_and_read_line("Username", answerbuf, sizeof(answerbuf)));
ret->username = apr_pstrdup (pool, answerbuf);
*cred = ret;
return SVN_NO_ERROR;
}
int
main (int argc, const char **argv)
{
apr_pool_t *pool;
svn_error_t *err;
svn_opt_revision_t revision;
apr_hash_t *dirents;
apr_hash_index_t *hi;
svn_client_ctx_t *ctx;
const char *URL;
if (argc <= 1)
{
printf ("Usage: %s URL\n", argv[0]);
return EXIT_FAILURE;
}
else
URL = argv[1];
/* Initialize the app. Send all error messages to 'stderr'. */
if (svn_cmdline_init ("minimal_client", stderr) != EXIT_SUCCESS)
return EXIT_FAILURE;
/* Create top-level memory pool. Be sure to read the HACKING file to
understand how to properly use/free subpools. */
pool = svn_pool_create (NULL);
/* Initialize the FS library. */
err = svn_fs_initialize (pool);
if (err)
{
/* For functions deeper in the stack, we usually use the
SVN_ERR() exception-throwing macro (see svn_error.h). At the
top level, we catch & print the error with svn_handle_error2(). */
svn_handle_error2 (err, stderr, FALSE, "minimal_client: ");
return EXIT_FAILURE;
}
/* Make sure the ~/.subversion run-time config files exist */
err = svn_config_ensure (NULL, pool);
if (err)
{
svn_handle_error2 (err, stderr, FALSE, "minimal_client: ");
return EXIT_FAILURE;
}
/* All clients need to fill out a client_ctx object. */
{
/* Initialize and allocate the client_ctx object. */
if ((err = svn_client_create_context (&ctx, pool)))
{
svn_handle_error2 (err, stderr, FALSE, "minimal_client: ");
return EXIT_FAILURE;
}
/* Load the run-time config file into a hash */
if ((err = svn_config_get_config (&(ctx->config), NULL, pool)))
{
svn_handle_error2 (err, stderr, FALSE, "minimal_client: ");
return EXIT_FAILURE;
}
#ifdef WIN32
/* Set the working copy administrative directory name. */
if (getenv ("SVN_ASP_DOT_NET_HACK"))
{
err = svn_wc_set_adm_dir ("_svn", pool);
if (err)
{
svn_handle_error2 (err, stderr, FALSE, "minimal_client: ");
return EXIT_FAILURE;
}
}
#endif
/* Depending on what your client does, you'll want to read about
(and implement) the various callback function types below. */
/* A func (& context) which receives event signals during
checkouts, updates, commits, etc. */
/* ctx->notify_func = my_notification_func;
ctx->notify_baton = NULL; */
/* A func (& context) which can receive log messages */
/* ctx->log_msg_func = my_log_msg_receiver_func;
ctx->log_msg_baton = NULL; */
/* A func (& context) which checks whether the user cancelled */
/* ctx->cancel_func = my_cancel_checking_func;
ctx->cancel_baton = NULL; */
/* Make the client_ctx capable of authenticating users */
{
/* There are many different kinds of authentication back-end
"providers". See svn_auth.h for a full overview.
If you want to get the auth behavior of the 'svn' program,
you can use svn_cmdline_setup_auth_baton, which will give
you the exact set of auth providers it uses. This program
doesn't use it because it's only appropriate for a command
line program, and this is supposed to be a general purpose
example. */
svn_auth_provider_object_t *provider;
apr_array_header_t *providers
= apr_array_make (pool, 4, sizeof (svn_auth_provider_object_t *));
svn_auth_get_simple_prompt_provider (&provider,
my_simple_prompt_callback,
NULL, /* baton */
2, /* retry limit */ pool);
APR_ARRAY_PUSH (providers, svn_auth_provider_object_t *) = provider;
svn_auth_get_username_prompt_provider (&provider,
my_username_prompt_callback,
NULL, /* baton */
2, /* retry limit */ pool);
APR_ARRAY_PUSH (providers, svn_auth_provider_object_t *) = provider;
/* Register the auth-providers into the context's auth_baton. */
svn_auth_open (&ctx->auth_baton, providers, pool);
}
} /* end of client_ctx setup */
/* Now do the real work. */
/* Set revision to always be the HEAD revision. It could, however,
be set to a specific revision number, date, or other values. */
revision.kind = svn_opt_revision_head;
/* Main call into libsvn_client does all the work. */
err = svn_client_ls (&dirents,
URL, &revision,
FALSE, /* no recursion */
ctx, pool);
if (err)
{
svn_handle_error2 (err, stderr, FALSE, "minimal_client: ");
return EXIT_FAILURE;
}
/* Print the dir entries in the hash. */
for (hi = apr_hash_first (pool, dirents); hi; hi = apr_hash_next (hi))
{
const char *entryname;
svn_dirent_t *val;
apr_hash_this (hi, (void *) &entryname, NULL, (void *) &val);
printf (" %s\n", entryname);
/* 'val' is actually an svn_dirent_t structure; a more complex
program would mine it for extra printable information. */
}
return EXIT_SUCCESS;
}
|