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
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"
36 /* Display a prompt and read a one-line response into the provided buffer,
37 removing a trailing newline if present. */
39 prompt_and_read_line(const char *prompt
,
44 printf("%s: ", prompt
);
45 if (fgets(buffer
, max
, stdin
) == NULL
)
46 return svn_error_create(0, NULL
, "error reading stdin");
48 if (len
> 0 && buffer
[len
-1] == '\n')
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. */
57 my_simple_prompt_callback (svn_auth_cred_simple_t
**cred
,
61 svn_boolean_t may_save
,
64 svn_auth_cred_simple_t
*ret
= apr_pcalloc (pool
, sizeof (*ret
));
69 printf ("Authentication realm: %s\n", realm
);
73 ret
->username
= apr_pstrdup (pool
, username
);
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
);
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. */
92 my_username_prompt_callback (svn_auth_cred_username_t
**cred
,
95 svn_boolean_t may_save
,
98 svn_auth_cred_username_t
*ret
= apr_pcalloc (pool
, sizeof (*ret
));
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
);
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. */
118 open_tmp_file (apr_file_t
**fp
,
119 void *callback_baton
,
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
,
131 svn_io_file_del_on_close
, pool
));
139 main (int argc
, const char **argv
)
144 apr_hash_index_t
*hi
;
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
;
153 printf ("Usage: %s URL\n", argv
[0]);
154 printf (" Print all locks at or below URL.\n");
159 /* Initialize the app. Send all error messages to 'stderr'. */
160 if (svn_cmdline_init ("ra_test", stderr
) != EXIT_SUCCESS
)
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
,
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
,
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
))
222 const char *path
, *cr_date
, *exp_date
;
225 apr_hash_this (hi
, &key
, NULL
, &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
);
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
,
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
;
264 svn_handle_error2 (err
, stderr
, FALSE
, "getlocks_test: ");