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 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
|
/*
* svnput.c : upload a single file to a repository, overwriting
* any existing file by the same name.
*
* ***************************************************************
* WARNING!! Despite the warnings it gives, this program allows
* you to potentially overwrite a file you've never seen.
* USE AT YOUR OWN RISK!
*
* (While the repository won't 'lose' overwritten data, the
* overwriting may happen without your knowledge, and has the
* potential to cause much grief with your collaborators!)
*
* ***************************************************************
*
* ====================================================================
* Copyright (c) 2000-2004 CollabNet. All rights reserved.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at http://subversion.tigris.org/license-1.html.
* If newer versions of this license are posted there, you may use a
* newer version instead, at your option.
*
* This software consists of voluntary contributions made by many
* individuals. For exact contribution history, see the revision
* history and logs, available at http://subversion.tigris.org/.
* ====================================================================
*
* To compile on unix against Subversion and APR libraries, try
* something like:
*
* cc svnput.c -o svnput \
* -I/usr/local/include/subversion-1 -I/usr/local/apache2/include \
* -L/usr/local/apache2/lib -L/usr/local/lib \
* -lsvn_client-1 -lapr-0 -laprutil-0
*
*/
#include "svn_client.h"
#include "svn_pools.h"
#include "svn_config.h"
#include "svn_fs.h"
#include "svn_cmdline.h"
#include "svn_path.h"
#include "svn_time.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;
}
/* A callback function used when the RA layer needs a handle to a
temporary file. This is a reduced version of the callback used in
the official svn cmdline client. */
static svn_error_t *
open_tmp_file (apr_file_t **fp,
void *callback_baton,
apr_pool_t *pool)
{
const char *path;
const char *ignored_filename;
SVN_ERR (svn_io_temp_dir (&path, pool));
path = svn_path_join (path, "tempfile", pool);
/* Open a unique file, with delete-on-close set. */
SVN_ERR (svn_io_open_unique_file2 (fp, &ignored_filename,
path, ".tmp",
svn_io_file_del_on_close, pool));
return SVN_NO_ERROR;
}
/* Called when a commit is successful. */
static svn_error_t *
my_commit_callback (svn_revnum_t new_revision,
const char *date,
const char *author,
void *baton)
{
printf ("Upload complete. Committed revision %ld.\n", new_revision);
return SVN_NO_ERROR;
}
int
main (int argc, const char **argv)
{
apr_pool_t *pool;
svn_error_t *err;
apr_hash_t *dirents;
const char *upload_file, *URL;
const char *parent_URL, *basename;
svn_ra_plugin_t *ra_lib;
void *session, *ra_baton;
svn_revnum_t rev;
const svn_delta_editor_t *editor;
void *edit_baton;
svn_dirent_t *dirent;
svn_ra_callbacks_t *cbtable;
apr_hash_t *cfg_hash;
svn_auth_baton_t *auth_baton;
if (argc <= 2)
{
printf ("Usage: %s PATH URL\n", argv[0]);
printf (" Uploads file at PATH to Subversion repository URL.\n");
return EXIT_FAILURE;
}
upload_file = argv[1];
URL = argv[2];
/* 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) goto hit_error;
/* Make sure the ~/.subversion run-time config files exist, and load. */
err = svn_config_ensure (NULL, pool);
if (err) goto hit_error;
err = svn_config_get_config (&cfg_hash, NULL, pool);
if (err) goto hit_error;
/* Build an authentication baton. */
{
/* There are many different kinds of authentication back-end
"providers". See svn_auth.h for a full overview. */
svn_auth_provider_object_t *provider;
apr_array_header_t *providers
= apr_array_make (pool, 4, sizeof (svn_auth_provider_object_t *));
svn_client_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_client_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 (&auth_baton, providers, pool);
}
/* Create a table of callbacks for the RA session, mostly nonexistent. */
cbtable = apr_pcalloc (pool, sizeof(*cbtable));
cbtable->auth_baton = auth_baton;
cbtable->open_tmp_file = open_tmp_file;
/* Now do the real work. */
/* Open an RA session to the parent URL, fetch current HEAD rev and
"lock" onto that revnum for the remainder of the session. */
svn_path_split (URL, &parent_URL, &basename, pool);
err = svn_ra_init_ra_libs (&ra_baton, pool);
if (err) goto hit_error;
err = svn_ra_get_ra_library (&ra_lib, ra_baton, parent_URL, pool);
if (err) goto hit_error;
err = ra_lib->open (&session, parent_URL, cbtable, NULL, cfg_hash, pool);
if (err) goto hit_error;
err = ra_lib->get_latest_revnum (session, &rev, pool);
if (err) goto hit_error;
/* Examine contents of parent dir in the rev. */
err = ra_lib->get_dir (session, "", rev, &dirents, NULL, NULL, pool);
if (err) goto hit_error;
/* Sanity checks. Don't let the user shoot himself *too* much. */
dirent = apr_hash_get (dirents, basename, APR_HASH_KEY_STRING);
if (dirent && dirent->kind == svn_node_dir)
{
printf ("Sorry, a directory already exists at that URL.\n");
return EXIT_FAILURE;
}
if (dirent && dirent->kind == svn_node_file)
{
char answer[5];
printf ("\n*** WARNING ***\n\n");
printf ("You're about to overwrite r%ld of this file.\n", rev);
printf ("It was last changed by user '%s',\n",
dirent->last_author ? dirent->last_author : "?");
printf ("on %s.\n", svn_time_to_human_cstring (dirent->time, pool));
printf ("\nSomebody *might* have just changed the file seconds ago,\n"
"and your upload would be overwriting their changes!\n\n");
err = prompt_and_read_line("Are you SURE you want to upload? [y/n]",
answer, sizeof(answer));
if (err) goto hit_error;
if (apr_strnatcasecmp (answer, "y"))
{
printf ("Operation aborted.\n");
return EXIT_SUCCESS;
}
}
/* Fetch a commit editor (it's anchored on the parent URL, because
the session is too.) */
/* ### someday add an option for a user-written commit message? */
err = ra_lib->get_commit_editor (session, &editor, &edit_baton,
"File upload from 'svnput' program.",
my_commit_callback, NULL, pool);
if (err) goto hit_error;
/* Drive the editor */
{
void *root_baton, *file_baton, *handler_baton;
svn_txdelta_window_handler_t handler;
svn_stream_t *contents;
apr_file_t *f = NULL;
err = editor->open_root (edit_baton, rev, pool, &root_baton);
if (err) goto hit_error;
if (! dirent)
{
err = editor->add_file (basename, root_baton, NULL, SVN_INVALID_REVNUM,
pool, &file_baton);
}
else
{
err = editor->open_file (basename, root_baton, rev, pool,
&file_baton);
}
if (err) goto hit_error;
err = editor->apply_textdelta (file_baton, NULL, pool,
&handler, &handler_baton);
if (err) goto hit_error;
err = svn_io_file_open (&f, upload_file, APR_READ, APR_OS_DEFAULT, pool);
if (err) goto hit_error;
contents = svn_stream_from_aprfile (f, pool);
err = svn_txdelta_send_stream (contents, handler, handler_baton,
NULL, pool);
if (err) goto hit_error;
err = svn_io_file_close (f, pool);
if (err) goto hit_error;
err = editor->close_file (file_baton, NULL, pool);
if (err) goto hit_error;
err = editor->close_edit (edit_baton, pool);
if (err) goto hit_error;
}
return EXIT_SUCCESS;
hit_error:
svn_handle_error2 (err, stderr, FALSE, "svnput: ");
return EXIT_FAILURE;
}
|