summaryrefslogtreecommitdiff
path: root/subversion/bindings/javahl/native/org_apache_subversion_javahl_util_PropLib.cpp
blob: d133f82a29188b4751e70715046ca1ecdd46d88f (plain)
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/**
 * @copyright
 * ====================================================================
 *    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.
 * ====================================================================
 * @endcopyright
 *
 * @file org_apache_subversion_javahl_util_PropLib.cpp
 * @brief Implementation of the native methods in the Java class PropLib
 */

#include <iostream>
#include <sstream>

#include "../include/org_apache_subversion_javahl_util_PropLib.h"

#include "jniwrapper/jni_stack.hpp"
#include "jniwrapper/jni_array.hpp"
#include "jniwrapper/jni_list.hpp"
#include "jniwrapper/jni_string.hpp"
#include "jniwrapper/jni_io_stream.hpp"
#include "ExternalItem.hpp"
#include "SubversionException.hpp"

#include "EnumMapper.h"
#include "Pool.h"

#include "svn_props.h"
#include "svn_time.h"
#include "svn_wc.h"

#include "private/svn_wc_private.h"
#include "svn_private_config.h"


namespace {
class PropGetter
{
public:
  PropGetter(const char* mime_type, svn_stream_t* contents)
    : m_mime_type(mime_type),
      m_contents(contents)
    {}

  static svn_error_t* callback(const svn_string_t** mime_type,
                               svn_stream_t* stream, void* baton,
                               apr_pool_t* pool)
    {
      PropGetter* self = static_cast<PropGetter*>(baton);
      if (mime_type)
        {
          if (self->m_mime_type)
            *mime_type = svn_string_create(self->m_mime_type, pool);
          else
            *mime_type = svn_string_create_empty(pool);
        }

      if (stream && self->m_contents)
        {
          SVN_ERR(svn_stream_copy3(self->m_contents,
                                   svn_stream_disown(stream, pool),
                                   NULL, NULL, pool));
        }

      return SVN_NO_ERROR;
    }

private:
  const char* m_mime_type;
  svn_stream_t* m_contents;
};

struct FormatRevision
{
  explicit FormatRevision(const svn_opt_revision_t* const& revarg,
                          const SVN::Pool& poolarg)
    : rev(revarg), pool(poolarg)
    {}

  const svn_opt_revision_t* const& rev;
  const SVN::Pool& pool;
};

std::ostream& operator<<(std::ostream& os, const FormatRevision& pr)
{
  switch (pr.rev->kind)
    {
    case svn_opt_revision_number:
      os << pr.rev->value.number;
      break;
    case svn_opt_revision_date:
      os << '{'
         << svn_time_to_cstring(pr.rev->value.date, pr.pool.getPool())
         << '}';
      break;
    default:
      throw std::logic_error(
          _("Invalid revision tag; must be a number or a date"));
    }
  return os;
}

bool operator==(const svn_opt_revision_t& a,
                const svn_opt_revision_t& b)
{
  if (a.kind != b.kind)
    return false;
  if (a.kind == svn_opt_revision_number
      && a.value.number != b.value.number)
    return false;
  if (a.kind == svn_opt_revision_date
      && a.value.date != b.value.date)
    return false;
  return true;
}

inline bool operator!=(const svn_opt_revision_t& a,
                       const svn_opt_revision_t& b)
{
  return !(a == b);
}

class UnparseFunctor
{
public:
  explicit UnparseFunctor(std::ostringstream& buffer, bool old_format,
                          SVN::Pool& iterpool)
    : m_buffer(buffer),
      m_old_format(old_format),
      m_iterpool(iterpool)
    {}

  void operator()(const JavaHL::ExternalItem& item)
    {
      m_iterpool.clear();

      const Java::Env env(item.get_env());
      const Java::LocalFrame frame(env);

      if (!m_old_format)
        {
          if (item.revision()->kind != svn_opt_revision_head
              && *item.revision() != *item.peg_revision())
            {
              m_buffer << "-r"
                       << FormatRevision(item.revision(), m_iterpool)
                       << ' ';
            }
          if (item.peg_revision()->kind == svn_opt_revision_head)
            m_buffer << item.url() << ' ';
          else
            {
              m_buffer << item.url() << '@'
                       << FormatRevision(item.peg_revision(), m_iterpool)
                       << ' ';
            }
          m_buffer << item.target_dir() << '\n';
        }
      else
        {
          // Sanity check: old format does not support peg revisions
          if (item.peg_revision()->kind != svn_opt_revision_head
              && *item.revision() != *item.peg_revision())
            {
              JavaHL::SubversionException(env)
                .raise(_("Clients older than Subversion 1.5"
                         " do not support peg revision syntax"
                         " in the svn:externals property"));
            }

          // Sanity check: old format does not support relative URLs
          const std::string url = item.url();
          if (   (url.size() >= 1 && (url[0] == '.' || url[0] == '/'))
                 || (url.size() >= 2 && (url[0] == '^' && url[1] == '/')))
            {
              JavaHL::SubversionException(env)
                .raise(_("Clients older than Subversion 1.5"
                         " do not support relative URLs"
                         " in the svn:externals property"));
            }

          m_buffer << item.target_dir() << ' ';
          if (item.revision()->kind != svn_opt_revision_head)
            {
              m_buffer << "-r"
                       << FormatRevision(item.revision(), m_iterpool)
                       << ' ';
            }
          m_buffer << url << '\n';
        }
    }

private:
  std::ostringstream& m_buffer;
  const bool m_old_format;
  SVN::Pool& m_iterpool;
};
} // anoymous namespace


JNIEXPORT jbyteArray JNICALL
Java_org_apache_subversion_javahl_util_PropLib_checkNodeProp(
    JNIEnv* jenv, jobject jthis,
    jstring jname, jbyteArray jpropval, jstring jpath, jobject jkind,
    jstring jmime_type, jobject jfile_contents,
    jboolean jskip_some_checks)
{
  SVN_JAVAHL_JNI_TRY(PropLib, checkLocalProp)
    {
      const Java::Env env(jenv);

      const svn_node_kind_t kind = EnumMapper::toNodeKind(jkind);
      SVN_JAVAHL_OLDSTYLE_EXCEPTION_CHECK(env);

      const Java::String name_str(env, jname);
      const Java::ByteArray value(env, jpropval);
      const Java::String path_str(env, jpath);
      const Java::String mime_type_str(env, jmime_type);
      Java::InputStream file_contents(env, jfile_contents);

      // Using a "global" request pool since we don't keep a context
      // with its own pool around for these functions.
      SVN::Pool pool;

      const Java::String::Contents name(name_str);
      const Java::String::Contents path(path_str);
      const Java::String::Contents mime_type(mime_type_str);
      PropGetter getter(mime_type.c_str(), file_contents.get_stream(pool));

      const svn_string_t* canonval;
      SVN_JAVAHL_CHECK(env,
                       svn_wc_canonicalize_svn_prop(
                           &canonval, name.c_str(),
                           Java::ByteArray::Contents(value).get_string(pool),
                           path.c_str(), kind,
                           svn_boolean_t(jskip_some_checks),
                           PropGetter::callback, &getter,
                           pool.getPool()));
      return Java::ByteArray(env, canonval->data, jint(canonval->len)).get();
    }
  SVN_JAVAHL_JNI_CATCH;
  return NULL;
}


JNIEXPORT jobject JNICALL
Java_org_apache_subversion_javahl_util_PropLib_parseExternals(
    JNIEnv* jenv, jobject jthis,
    jbyteArray jdescription, jstring jparent_dir, jboolean jcanonicalize_url)
{
  SVN_JAVAHL_JNI_TRY(PropLib, parseExternals)
    {
      const Java::Env env(jenv);

      const Java::ByteArray description(env, jdescription);
      const Java::String parent_dir(env, jparent_dir);

      // Using a "global" request pool since we don't keep a context
      // with its own pool around for these functions.
      SVN::Pool pool;

      apr_array_header_t* externals;
      {
        // There is no guarantee that the description contents are
        // null-terminated. Copy them to an svn_string_t to make sure
        // that they are.
        svn_string_t* const description_contents =
          Java::ByteArray::Contents(description).get_string(pool);

        SVN_JAVAHL_CHECK(env,
                         svn_wc_parse_externals_description3(
                             &externals,
                             Java::String::Contents(parent_dir).c_str(),
                             description_contents->data,
                             svn_boolean_t(jcanonicalize_url),
                             pool.getPool()));
      }

      Java::List<JavaHL::ExternalItem> items(env, externals->nelts);
      for (jint i = 0; i < externals->nelts; ++i)
        {
          // References to the newly created external items are stored
          // in the list, so make sure the local reference in this
          // frame get cleared on each iteration.
          Java::LocalFrame frame;

          const svn_wc_external_item2_t* const item =
            APR_ARRAY_IDX(externals, i, svn_wc_external_item2_t*);
          items.add(JavaHL::ExternalItem(env,
                                         item->target_dir,
                                         item->url,
                                         &item->revision,
                                         &item->peg_revision));
        }
      return items.get();
    }
  SVN_JAVAHL_JNI_CATCH;
  return NULL;
}


JNIEXPORT jbyteArray JNICALL
Java_org_apache_subversion_javahl_util_PropLib_unparseExternals(
    JNIEnv* jenv, jobject jthis,
    jobject jitems, jstring jparent_dir, jboolean jold_format)
{
  SVN_JAVAHL_JNI_TRY(PropLib, unparseExternals)
    {
      const Java::Env env(jenv);

      const Java::ImmutableList<JavaHL::ExternalItem> items(env, jitems);
      const Java::String parent_dir(env, jparent_dir);

      // Using a "global" iteration pool since we don't keep a context
      // with its own pool around for these functions.
      SVN::Pool iterpool;

      std::ostringstream buffer;
      items.for_each(UnparseFunctor(buffer, jold_format, iterpool));
      const std::string description(buffer.str());

      // Validate the result. Even though we generated the string
      // ourselves, we did not validate the input paths and URLs.
      SVN_JAVAHL_CHECK(env,
                       svn_wc_parse_externals_description3(
                           NULL,
                           Java::String::Contents(parent_dir).c_str(),
                           description.c_str(),
                           false, iterpool.getPool()));
      return Java::ByteArray(env, description).get();
    }
  SVN_JAVAHL_JNI_CATCH;
  return NULL;
}


JNIEXPORT jstring JNICALL
Java_org_apache_subversion_javahl_util_PropLib_resolveExternalsUrl(
    JNIEnv* jenv, jobject jthis,
    jobject jitem, jstring jrepos_root_url, jstring jparent_dir_url)
{
  SVN_JAVAHL_JNI_TRY(PropLib, unparseExternals)
    {
      const Java::Env env(jenv);

      const Java::String repos_root_url(env, jrepos_root_url);
      const Java::String parent_dir_url(env, jparent_dir_url);
      const JavaHL::ExternalItem item(env, jitem);

      // Using a "global" request pool since we don't keep a context
      // with its own pool around for these functions.
      SVN::Pool pool;

      const char* resolved_url;
      SVN_JAVAHL_CHECK(env,
                       svn_wc__resolve_relative_external_url(
                           &resolved_url,
                           item.get_external_item(pool),
                           Java::String::Contents(repos_root_url).c_str(),
                           Java::String::Contents(parent_dir_url).c_str(),
                           pool.getPool(), pool.getPool()));
      return Java::String(env, resolved_url).get();
    }
  SVN_JAVAHL_JNI_CATCH;
  return NULL;
}