Follow-up to r29036: Now that the "mergeinfo" transaction file is no
[svn.git] / tools / examples / getlocks_test.c
blob6c7117970d614303a12adf3e2fa2b354dd673275
1 /*
2 * getlocks_test.c : show all repository locks living below a URL
4 * ====================================================================
5 * Copyright (c) 2000-2005 CollabNet. All rights reserved.
7 * This software is licensed as described in the file COPYING, which
8 * you should have received as part of this distribution. The terms
9 * are also available at http://subversion.tigris.org/license-1.html.
10 * If newer versions of this license are posted there, you may use a
11 * newer version instead, at your option.
13 * This software consists of voluntary contributions made by many
14 * individuals. For exact contribution history, see the revision
15 * history and logs, available at http://subversion.tigris.org/.
16 * ====================================================================
18 * To compile on unix against Subversion and APR libraries, try
19 * something like:
21 * cc getlocks_test.c -o getlocks_test \
22 * -I/usr/local/include/subversion-1 -I/usr/local/apache2/include \
23 * -L/usr/local/apache2/lib -L/usr/local/lib \
24 * -lsvn_client-1 -lsvn_ra-1 -lsvn_subr-1 -lapr-0 -laprutil-0
28 #include "svn_client.h"
29 #include "svn_pools.h"
30 #include "svn_config.h"
31 #include "svn_cmdline.h"
32 #include "svn_time.h"
33 #include "svn_fs.h"
34 #include "svn_path.h"
36 /* Display a prompt and read a one-line response into the provided buffer,
37 removing a trailing newline if present. */
38 static svn_error_t *
39 prompt_and_read_line(const char *prompt,
40 char *buffer,
41 size_t max)
43 int len;
44 printf("%s: ", prompt);
45 if (fgets(buffer, max, stdin) == NULL)
46 return svn_error_create(0, NULL, "error reading stdin");
47 len = strlen(buffer);
48 if (len > 0 && buffer[len-1] == '\n')
49 buffer[len-1] = 0;
50 return SVN_NO_ERROR;
53 /* A tiny callback function of type 'svn_auth_simple_prompt_func_t'. For
54 a much better example, see svn_cl__auth_simple_prompt in the official
55 svn cmdline client. */
56 static svn_error_t *
57 my_simple_prompt_callback (svn_auth_cred_simple_t **cred,
58 void *baton,
59 const char *realm,
60 const char *username,
61 svn_boolean_t may_save,
62 apr_pool_t *pool)
64 svn_auth_cred_simple_t *ret = apr_pcalloc (pool, sizeof (*ret));
65 char answerbuf[100];
67 if (realm)
69 printf ("Authentication realm: %s\n", realm);
72 if (username)
73 ret->username = apr_pstrdup (pool, username);
74 else
76 SVN_ERR (prompt_and_read_line("Username", answerbuf, sizeof(answerbuf)));
77 ret->username = apr_pstrdup (pool, answerbuf);
80 SVN_ERR (prompt_and_read_line("Password", answerbuf, sizeof(answerbuf)));
81 ret->password = apr_pstrdup (pool, answerbuf);
83 *cred = ret;
84 return SVN_NO_ERROR;
88 /* A tiny callback function of type 'svn_auth_username_prompt_func_t'. For
89 a much better example, see svn_cl__auth_username_prompt in the official
90 svn cmdline client. */
91 static svn_error_t *
92 my_username_prompt_callback (svn_auth_cred_username_t **cred,
93 void *baton,
94 const char *realm,
95 svn_boolean_t may_save,
96 apr_pool_t *pool)
98 svn_auth_cred_username_t *ret = apr_pcalloc (pool, sizeof (*ret));
99 char answerbuf[100];
101 if (realm)
103 printf ("Authentication realm: %s\n", realm);
106 SVN_ERR (prompt_and_read_line("Username", answerbuf, sizeof(answerbuf)));
107 ret->username = apr_pstrdup (pool, answerbuf);
109 *cred = ret;
110 return SVN_NO_ERROR;
114 /* A callback function used when the RA layer needs a handle to a
115 temporary file. This is a reduced version of the callback used in
116 the official svn cmdline client. */
117 static svn_error_t *
118 open_tmp_file (apr_file_t **fp,
119 void *callback_baton,
120 apr_pool_t *pool)
122 const char *path;
123 const char *ignored_filename;
125 SVN_ERR (svn_io_temp_dir (&path, pool));
126 path = svn_path_join (path, "tempfile", pool);
128 /* Open a unique file, with delete-on-close set. */
129 SVN_ERR (svn_io_open_unique_file2 (fp, &ignored_filename,
130 path, ".tmp",
131 svn_io_file_del_on_close, pool));
133 return SVN_NO_ERROR;
139 main (int argc, const char **argv)
141 apr_pool_t *pool;
142 svn_error_t *err;
143 apr_hash_t *locks;
144 apr_hash_index_t *hi;
145 const char *URL;
146 svn_ra_session_t *session;
147 svn_ra_callbacks_t *cbtable;
148 apr_hash_t *cfg_hash;
149 svn_auth_baton_t *auth_baton;
151 if (argc <= 1)
153 printf ("Usage: %s URL\n", argv[0]);
154 printf (" Print all locks at or below URL.\n");
155 return EXIT_FAILURE;
157 URL = argv[1];
159 /* Initialize the app. Send all error messages to 'stderr'. */
160 if (svn_cmdline_init ("ra_test", stderr) != EXIT_SUCCESS)
161 return EXIT_FAILURE;
163 /* Create top-level memory pool. Be sure to read the HACKING file to
164 understand how to properly use/free subpools. */
165 pool = svn_pool_create (NULL);
167 /* Initialize the FS library. */
168 err = svn_fs_initialize (pool);
169 if (err) goto hit_error;
171 /* Make sure the ~/.subversion run-time config files exist, and load. */
172 err = svn_config_ensure (NULL, pool);
173 if (err) goto hit_error;
175 err = svn_config_get_config (&cfg_hash, NULL, pool);
176 if (err) goto hit_error;
178 /* Build an authentication baton. */
180 /* There are many different kinds of authentication back-end
181 "providers". See svn_auth.h for a full overview. */
182 svn_auth_provider_object_t *provider;
183 apr_array_header_t *providers
184 = apr_array_make (pool, 4, sizeof (svn_auth_provider_object_t *));
186 svn_client_get_simple_prompt_provider (&provider,
187 my_simple_prompt_callback,
188 NULL, /* baton */
189 2, /* retry limit */ pool);
190 APR_ARRAY_PUSH (providers, svn_auth_provider_object_t *) = provider;
192 svn_client_get_username_prompt_provider (&provider,
193 my_username_prompt_callback,
194 NULL, /* baton */
195 2, /* retry limit */ pool);
196 APR_ARRAY_PUSH (providers, svn_auth_provider_object_t *) = provider;
198 /* Register the auth-providers into the context's auth_baton. */
199 svn_auth_open (&auth_baton, providers, pool);
202 /* Create a table of callbacks for the RA session, mostly nonexistent. */
203 cbtable = apr_pcalloc (pool, sizeof(*cbtable));
204 cbtable->auth_baton = auth_baton;
205 cbtable->open_tmp_file = open_tmp_file;
207 /* Now do the real work. */
209 err = svn_ra_open (&session, URL, cbtable, NULL, cfg_hash, pool);
210 if (err) goto hit_error;
212 err = svn_ra_get_locks (session, &locks, "", pool);
213 if (err) goto hit_error;
215 err = svn_cmdline_printf (pool, "\n");
216 if (err) goto hit_error;
218 for (hi = apr_hash_first (pool, locks); hi; hi = apr_hash_next (hi))
220 const void *key;
221 void *val;
222 const char *path, *cr_date, *exp_date;
223 svn_lock_t *lock;
225 apr_hash_this (hi, &key, NULL, &val);
226 path = key;
227 lock = val;
229 cr_date = svn_time_to_human_cstring (lock->creation_date, pool);
231 if (lock->expiration_date)
232 exp_date = svn_time_to_human_cstring (lock->expiration_date, pool);
233 else
234 exp_date = "never";
236 err = svn_cmdline_printf (pool, "%s\n", path);
237 if (err) goto hit_error;
239 err = svn_cmdline_printf (pool,
240 " UUID Token: %s\n", lock->token);
241 if (err) goto hit_error;
243 err = svn_cmdline_printf (pool,
244 " Owner: %s\n", lock->owner);
245 if (err) goto hit_error;
247 err = svn_cmdline_printf (pool,
248 " Comment: %s\n",
249 lock->comment ? lock->comment : "none");
250 if (err) goto hit_error;
252 err = svn_cmdline_printf (pool,
253 " Created: %s\n", cr_date);
254 if (err) goto hit_error;
256 err = svn_cmdline_printf (pool,
257 " Expires: %s\n\n", exp_date);
258 if (err) goto hit_error;
261 return EXIT_SUCCESS;
263 hit_error:
264 svn_handle_error2 (err, stderr, FALSE, "getlocks_test: ");
265 return EXIT_FAILURE;