source: project/release/4/svn-client/trunk/svn-client-base.c @ 33974

Last change on this file since 33974 was 33974, checked in by sjamaan, 19 months ago

svn-client: Make svn-diff accept revisions to diff (API change!)

File size: 4.9 KB
Line 
1#include <apr.h>
2#include <apr_hash.h>
3#include <apr_file_io.h>
4#include <apr_pools.h>
5#include <svn_auth.h>
6#include <svn_client.h>
7#include <svn_config.h>
8#include <svn_path.h>
9#include <svn_pools.h>
10#include <svn_repos.h>
11#include <svn_time.h>
12#include <svn_utf.h>
13#include <sys/file.h>
14#include <sys/stat.h>
15#include <sys/types.h>
16#include <unistd.h>
17#include <errno.h>
18
19static svn_opt_revision_t revision_head;
20static svn_opt_revision_t revision_working;
21static svn_opt_revision_t revision_unspecified;
22
23char *svnwiki_user, *svnwiki_pass;
24apr_pool_t *chicken_svn_client_global_pool;
25apr_pool_t *chicken_svn_client_global_auth_pool = NULL;
26svn_client_ctx_t *svn_ctx;
27static apr_array_header_t *auth_providers;
28
29svn_error_t *
30svnwiki_simple_first_credentials (void **credentials, void **iter_baton, void *provider_baton, apr_hash_t *parameters, const char *realmstring, apr_pool_t *pool)
31{
32  svn_auth_cred_simple_t *cred;
33  if (svnwiki_user == NULL) {
34          *credentials = NULL;
35          return 0;
36  }
37  *credentials = cred = apr_pcalloc(pool, sizeof(svn_auth_cred_simple_t));
38  cred->username = apr_pstrdup (pool, svnwiki_user);
39  cred->password = apr_pstrdup (pool, svnwiki_pass);
40  cred->may_save = 0;
41  return 0;
42}
43
44svn_error_t *
45svnwiki_username_first_credentials (void **credentials, void **iter_baton, void *provider_baton, apr_hash_t *parameters, const char *realmstring, apr_pool_t *pool)
46{
47  svn_auth_cred_username_t *cred;
48  if (svnwiki_user == NULL) {
49          *credentials = NULL;
50          return 0;
51  }
52  *credentials = cred = apr_pcalloc(pool, sizeof(svn_auth_cred_username_t));
53  cred->username = apr_pstrdup (pool, svnwiki_user);
54  cred->may_save = 0;
55  return 0;
56}
57
58svn_error_t *
59svnwiki_log_callback (const char **log_msg, const char **tmp_file, apr_array_header_t *commit_items, void *baton, apr_pool_t *pool)
60{
61  *tmp_file = NULL;
62  *log_msg = baton;
63  return SVN_NO_ERROR;
64}
65
66/* Pick the highest revision created */
67svn_error_t *
68svnclient_commit_set_revision(const svn_commit_info_t *commit_info, void *baton, apr_pool_t *pool)
69{
70  C_word *rev = (C_word *)baton;
71  if (!(*rev & C_FIXNUM_BIT) || C_unfix(*rev) < commit_info->revision) {
72    *rev = C_fix(commit_info->revision);
73  }
74  return SVN_NO_ERROR;
75}
76
77svn_auth_provider_t svnwiki_auth_simple   = { SVN_AUTH_CRED_SIMPLE, svnwiki_simple_first_credentials, NULL, NULL };
78svn_auth_provider_object_t svnwiki_auth_simple_obj = { &svnwiki_auth_simple, NULL };
79
80svn_auth_provider_t svnwiki_auth_username = { SVN_AUTH_CRED_USERNAME, svnwiki_username_first_credentials, NULL, NULL };
81svn_auth_provider_object_t svnwiki_auth_username_obj = { &svnwiki_auth_username, NULL };
82
83void
84set_creds_for_next_command(char *user, char *pass)
85{
86  svn_auth_provider_object_t *provider;
87 
88  svnwiki_user = user;
89  svnwiki_pass = pass;
90
91  if (chicken_svn_client_global_auth_pool != NULL)
92    svn_pool_destroy(chicken_svn_client_global_auth_pool);
93 
94  chicken_svn_client_global_auth_pool = svn_pool_create(chicken_svn_client_global_pool);
95  auth_providers = apr_array_make(chicken_svn_client_global_auth_pool, 3, sizeof(svn_auth_provider_object_t *));
96
97  APR_ARRAY_PUSH(auth_providers, svn_auth_provider_object_t *) = &svnwiki_auth_simple_obj;
98  APR_ARRAY_PUSH(auth_providers, svn_auth_provider_object_t *) = &svnwiki_auth_username_obj;
99  /* Always allow default SSL server overrides from user's config */
100  svn_auth_get_ssl_server_trust_file_provider(&provider, chicken_svn_client_global_auth_pool);
101  APR_ARRAY_PUSH(auth_providers, svn_auth_provider_object_t *) = provider;
102
103  svn_auth_open(&svn_ctx->auth_baton, auth_providers, chicken_svn_client_global_auth_pool);
104}
105
106char *svnclient_canonicalize_path_or_url(const char *path_or_url)
107{
108  char *result;
109  const char *canonical;
110  apr_pool_t *tmp_pool;
111  int len;
112  tmp_pool = svn_pool_create(NULL);
113
114  if (svn_path_is_url(path_or_url)) {
115    canonical = svn_uri_canonicalize(path_or_url, tmp_pool);
116  } else {
117    canonical = svn_dirent_canonicalize(path_or_url, tmp_pool);
118  }
119
120  len = strlen(canonical)+1;
121  result = malloc(len);
122  if (result == NULL) {
123    svn_pool_destroy(tmp_pool);
124    return NULL;
125  }
126  memcpy(result, canonical, len);
127  svn_pool_destroy(tmp_pool);
128  return result;
129}
130
131void chicken_svn_client_lib_initialize() {
132  apr_status_t status;
133
134  status = apr_initialize();
135  if (status != APR_SUCCESS) {
136    exit(1);
137  }
138  chicken_svn_client_global_pool = svn_pool_create(NULL);
139  if (chicken_svn_client_global_pool == NULL) {
140    exit (2);
141  }
142  svn_utf_initialize2(FALSE, chicken_svn_client_global_pool);
143  svn_config_ensure(NULL, chicken_svn_client_global_pool);
144
145  svn_ra_initialize(chicken_svn_client_global_pool);
146  svn_client_create_context2(&svn_ctx, NULL, chicken_svn_client_global_pool);
147  svn_config_get_config(&svn_ctx->config, NULL, chicken_svn_client_global_pool);
148
149  /* Initialize authentication */
150  svnwiki_user = NULL;
151  svnwiki_pass = NULL;
152
153  revision_head.kind = svn_opt_revision_head;
154  revision_unspecified.kind = svn_opt_revision_unspecified;
155  revision_working.kind = svn_opt_revision_working;
156}
Note: See TracBrowser for help on using the repository browser.