summaryrefslogtreecommitdiff
path: root/subversion/bindings/javahl/src/org/tigris/subversion/javahl/SVNAdmin.java
blob: 337fee06485d4b71a547c59de8a56e4719f9b9b1 (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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
/**
 * @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
 */

package org.tigris.subversion.javahl;

import java.util.Set;
import java.io.OutputStream;
import java.io.InputStream;
import java.io.File;
import java.io.IOException;

/**
 * This class offers the same commands as the svnadmin commandline
 * client.
 */
public class SVNAdmin
{
    private org.apache.subversion.javahl.SVNRepos aSVNAdmin;

    /**
     * Standard empty constructor, builds just the native peer.
     */
    public SVNAdmin()
    {
        aSVNAdmin = new org.apache.subversion.javahl.SVNRepos();
        cppAddr = aSVNAdmin.getCppAddr();
    }

    /**
     * release the native peer (should not depend on finalize)
     */
    public void dispose()
    {
        aSVNAdmin.dispose();
    }

    /**
     * release the native peer (should use dispose instead)
     */
    protected void finalize()
    {
    }

    /**
     * slot for the adress of the native peer. The JNI code is the only user
     * of this member
     */
    protected long cppAddr;

    /**
     * Filesystem in a Berkeley DB
     */
    public static final String BDB = "bdb";

    /**
     * Filesystem in the filesystem
     */
    public static final String FSFS = "fsfs";

    /**
     * @return Version information about the underlying native libraries.
     */
    public Version getVersion()
    {
        return new Version(
                    org.apache.subversion.javahl.NativeResources.getVersion());
    }

    /**
     * create a subversion repository.
     * @param path                  the path where the repository will been
     *                              created.
     * @param disableFsyncCommit    disable to fsync at the commit (BDB).
     * @param keepLog               keep the log files (BDB).
     * @param configPath            optional path for user configuration files.
     * @param fstype                the type of the filesystem (BDB or FSFS)
     * @throws ClientException  throw in case of problem
     */
    public void create(String path, boolean disableFsyncCommit, boolean keepLog,
                       String configPath, String fstype)
            throws ClientException
    {
        try {
            aSVNAdmin.create(new File(path), disableFsyncCommit, keepLog,
                             configPath == null ? null : new File(configPath),
                             fstype);
        } catch (org.apache.subversion.javahl.ClientException ex) {
            throw new ClientException(ex);
        }
    }

    /**
     * deltify the revisions in the repository
     * @param path              the path to the repository
     * @param start             start revision
     * @param end               end revision
     * @throws ClientException  throw in case of problem
     */
    public void deltify(String path, Revision start, Revision end)
            throws ClientException
    {
        try
        {
            aSVNAdmin.deltify(new File(path),
                              start == null ? null : start.toApache(),
                              end == null ? null : end.toApache());
        }
        catch (org.apache.subversion.javahl.ClientException ex)
        {
            throw new ClientException(ex);
        }
    }

    /**
     * dump the data in a repository
     * @param path              the path to the repository
     * @param dataOut           the data will be outputed here
     * @param errorOut          the messages will be outputed here
     * @param start             the first revision to be dumped
     * @param end               the last revision to be dumped
     * @param incremental       the dump will be incremantal
     * @throws ClientException  throw in case of problem
     */
    public void dump(String path, OutputInterface dataOut,
                     OutputInterface errorOut, Revision start,
                     Revision end, boolean incremental)
            throws ClientException
    {
        dump(path, dataOut, errorOut, start, end, incremental, false);
    }

    /**
     * dump the data in a repository
     * @param path              the path to the repository
     * @param dataOut           the data will be outputed here
     * @param errorOut          the messages will be outputed here
     * @param start             the first revision to be dumped
     * @param end               the last revision to be dumped
     * @param incremental       the dump will be incremantal
     * @param useDeltas         the dump will contain deltas between nodes
     * @throws ClientException  throw in case of problem
     * @since 1.5
     */
    public void dump(String path, OutputInterface dataOut,
                     OutputInterface errorOut, Revision start, Revision end,
                     boolean incremental, boolean useDeltas)
            throws ClientException
    {
        try
        {
            aSVNAdmin.dump(new File(path), new OutputWrapper(dataOut),
                           start == null ? null : start.toApache(),
                           end == null ? null : end.toApache(),
                           incremental, useDeltas,
                           new ReposNotifyHandler(errorOut));
        }
        catch (org.apache.subversion.javahl.ClientException ex)
        {
            throw new ClientException(ex);
        }
    }

    /**
     * make a hot copy of the repository
     * @param path              the path to the source repository
     * @param targetPath        the path to the target repository
     * @param cleanLogs         clean the unused log files in the source
     *                          repository
     * @throws ClientException  throw in case of problem
     */
    public void hotcopy(String path, String targetPath, boolean cleanLogs)
            throws ClientException
    {
        try
        {
            aSVNAdmin.hotcopy(new File(path), new File(targetPath), cleanLogs);
        }
        catch (org.apache.subversion.javahl.ClientException ex)
        {
            throw new ClientException(ex);
        }
    }

    /**
     * list all logfiles (BDB) in use or not)
     * @param path              the path to the repository
     * @param receiver          interface to receive the logfile names
     * @throws ClientException  throw in case of problem
     */
    public void listDBLogs(String path, MessageReceiver receiver)
            throws ClientException
    {
        try
        {
            aSVNAdmin.listDBLogs(new File(path), receiver);
        }
        catch (org.apache.subversion.javahl.ClientException ex)
        {
            throw new ClientException(ex);
        }
    }

    /**
     * list unused logfiles
     * @param path              the path to the repository
     * @param receiver          interface to receive the logfile names
     * @throws ClientException  throw in case of problem
     */
    public void listUnusedDBLogs(String path, MessageReceiver receiver)
            throws ClientException
    {
        try
        {
            aSVNAdmin.listUnusedDBLogs(new File(path), receiver);
        }
        catch (org.apache.subversion.javahl.ClientException ex)
        {
            throw new ClientException(ex);
        }
    }

    /**
     * interface to receive the messages
     */
    public static interface MessageReceiver
        extends org.apache.subversion.javahl.ISVNRepos.MessageReceiver
    {
    }

    /**
     * load the data of a dump into a repository,
     * @param path              the path to the repository
     * @param dataInput         the data input source
     * @param messageOutput     the target for processing messages
     * @param ignoreUUID        ignore any UUID found in the input stream
     * @param forceUUID         set the repository UUID to any found in the
     *                          stream
     * @param relativePath      the directory in the repository, where the data
     *                          in put optional.
     * @throws ClientException  throw in case of problem
     */
    public void load(String path, InputInterface dataInput,
                     OutputInterface messageOutput, boolean ignoreUUID,
                     boolean forceUUID, String relativePath)
            throws ClientException
    {
        load(path, dataInput, messageOutput, ignoreUUID, forceUUID,
             false, false, relativePath);
    }

    /**
     * load the data of a dump into a repository,
     * @param path              the path to the repository
     * @param dataInput         the data input source
     * @param messageOutput     the target for processing messages
     * @param ignoreUUID        ignore any UUID found in the input stream
     * @param forceUUID         set the repository UUID to any found in the
     *                          stream
     * @param usePreCommitHook  use the pre-commit hook when processing commits
     * @param usePostCommitHook use the post-commit hook when processing commits
     * @param relativePath      the directory in the repository, where the data
     *                          in put optional.
     * @throws ClientException  throw in case of problem
     * @since 1.5
     */
    public void load(String path, InputInterface dataInput,
                     OutputInterface messageOutput, boolean ignoreUUID,
                     boolean forceUUID, boolean usePreCommitHook,
                     boolean usePostCommitHook, String relativePath)
            throws ClientException
    {
        try
        {
            aSVNAdmin.load(new File(path), new InputWrapper(dataInput),
                           ignoreUUID, forceUUID, usePreCommitHook,
                           usePostCommitHook, relativePath,
                           new ReposNotifyHandler(messageOutput));
        }
        catch (org.apache.subversion.javahl.ClientException ex)
        {
            throw new ClientException(ex);
        }
    }

    /**
     * list all open transactions in a repository
     * @param path              the path to the repository
     * @param receiver          receives one transaction name per call
     * @throws ClientException  throw in case of problem
     */
    public void lstxns(String path, MessageReceiver receiver)
            throws ClientException
    {
        try
        {
            aSVNAdmin.lstxns(new File(path), receiver);
        }
        catch (org.apache.subversion.javahl.ClientException ex)
        {
            throw new ClientException(ex);
        }
    }

    /**
     * recover the berkeley db of a repository, returns youngest revision
     * @param path              the path to the repository
     * @throws ClientException  throw in case of problem
     */
    public long recover(String path)
            throws ClientException
    {
        try
        {
            return aSVNAdmin.recover(new File(path), null);
        }
        catch (org.apache.subversion.javahl.ClientException ex)
        {
            throw new ClientException(ex);
        }
    }

    /**
     * remove open transaction in a repository
     * @param path              the path to the repository
     * @param transactions      the transactions to be removed
     * @throws ClientException  throw in case of problem
     */
    public void rmtxns(String path, String [] transactions)
            throws ClientException
    {
        try
        {
            aSVNAdmin.rmtxns(new File(path), transactions);
        }
        catch (org.apache.subversion.javahl.ClientException ex)
        {
            throw new ClientException(ex);
        }
    }

    /**
     * set the log message of a revision
     * @param path              the path to the repository
     * @param rev               the revision to be changed
     * @param message           the message to be set
     * @param bypassHooks       if to bypass all repository hooks
     * @throws ClientException  throw in case of problem
     * @deprecated Use setRevProp() instead.
     */
    public void setLog(String path, Revision rev, String message,
                       boolean bypassHooks)
            throws ClientException
    {
        try
        {
            aSVNAdmin.setRevProp(new File(path),
                                 rev == null ? null : rev.toApache(),
                                 "svn:log", message,
                                 !bypassHooks, !bypassHooks);
        }
        catch (org.apache.subversion.javahl.SubversionException ex)
        {
            throw ClientException.fromException(ex);
        }
    }

    /**
     * Change the value of the revision property <code>propName</code>
     * to <code>propValue</code>.  By default, does not run
     * pre-/post-revprop-change hook scripts.
     *
     * @param path The path to the repository.
     * @param rev The revision for which to change a property value.
     * @param propName The name of the property to change.
     * @param propValue The new value to set for the property.
     * @param usePreRevPropChangeHook Whether to run the
     * <i>pre-revprop-change</i> hook script.
     * @param usePostRevPropChangeHook Whether to run the
     * <i>post-revprop-change</i> hook script.
     * @throws SubversionException If a problem occurs.
     * @since 1.5.0
     */
    public void setRevProp(String path, Revision rev, String propName,
                           String propValue, boolean usePreRevPropChangeHook,
                           boolean usePostRevPropChangeHook)
            throws SubversionException
    {
        try
        {
            aSVNAdmin.setRevProp(new File(path),
                                 rev == null ? null : rev.toApache(),
                                 propName, propValue,
                                 usePreRevPropChangeHook,
                                 usePostRevPropChangeHook);
        }
        catch (org.apache.subversion.javahl.SubversionException ex)
        {
            throw new SubversionException(ex);
        }
    }

    /**
     * Verify the repository at <code>path</code> between revisions
     * <code>start</code> and <code>end</code>.
     *
     * @param path              the path to the repository
     * @param messageOut        the receiver of all messages
     * @param start             the first revision
     * @param end               the last revision
     * @throws ClientException If an error occurred.
     */
    public void verify(String path, OutputInterface messageOut,
                       Revision start, Revision end)
            throws ClientException
    {
        try
        {
            aSVNAdmin.verify(new File(path),
                             start == null ? null : start.toApache(),
                             end == null ? null : end.toApache(),
                             new ReposNotifyHandler(messageOut));
        }
        catch (org.apache.subversion.javahl.ClientException ex)
        {
            throw new ClientException(ex);
        }
    }

    /**
     * list all locks in the repository
     * @param path              the path to the repository
     * @throws ClientException  throw in case of problem
     * @since 1.2
     */
    public Lock[] lslocks(String path)
            throws ClientException
    {
        try
        {
            Set<org.apache.subversion.javahl.types.Lock> aLocks =
                                                    aSVNAdmin.lslocks(
                                                        new File(path),
                                                        Depth.toADepth(
                                                              Depth.infinity));
            Lock[] locks = new Lock[aLocks.size()];

            int i = 0;
            for (org.apache.subversion.javahl.types.Lock lock : aLocks)
            {
                locks[i] = new Lock(lock);
                i++;
            }

            return locks;
        }
        catch (org.apache.subversion.javahl.ClientException ex)
        {
            throw new ClientException(ex);
        }
    }

    /**
     * remove multiple locks from the repository
     * @param path              the path to the repository
     * @param locks             the name of the locked items
     * @throws ClientException  throw in case of problem
     * @since 1.2
     */
    public void rmlocks(String path, String [] locks)
            throws ClientException
    {
        try
        {
            aSVNAdmin.rmlocks(new File(path), locks);
        }
        catch (org.apache.subversion.javahl.ClientException ex)
        {
            throw new ClientException(ex);
        }
    }

    private class OutputWrapper extends OutputStream
    {
        private OutputInterface outputer;

        OutputWrapper(OutputInterface outputer)
        {
            this.outputer = outputer;
        }

        public void write(int b) throws IOException
        {
            outputer.write(new byte[]{ (byte) ( b & 0xFF) });
        }

        public void write(byte[] b) throws IOException
        {
            outputer.write(b);
        }

        public void close() throws IOException
        {
            outputer.close();
        }
    }

    private class InputWrapper extends InputStream
    {
        private InputInterface inputer;

        InputWrapper(InputInterface inputer)
        {
            this.inputer = inputer;
        }

        public int read() throws IOException
        {
            byte[] b = new byte[1];
            if (inputer.read(b) > 0)
                return b[0];
            else
                return -1;
        }

        public int read(byte[] b) throws IOException
        {
            return inputer.read(b);
        }

        public void close() throws IOException
        {
            inputer.close();
        }
    }

    private class ReposNotifyHandler
        implements org.apache.subversion.javahl.callback.ReposNotifyCallback
    {
        private OutputInterface outputer;

        public ReposNotifyHandler(OutputInterface outputer)
        {
            this.outputer = outputer;
        }

        public void onNotify(org.apache.subversion.javahl.ReposNotifyInformation
                                                                           info)
        {
            String val;

            switch (info.getAction())
            {
                case warning:
                    val = info.getWarning();
                    break;

                case dump_rev_end:
                    val = "* Dumped revision " + info.getRevision() + ".\n";
                    break;

                case verify_rev_end:
                    val = "* Verified revision " + info.getRevision() + ".\n";
                    break;

                case load_txn_committed:
                    if (info.getOldRevision() == Revision.SVN_INVALID_REVNUM)
                        val = "\n------- Committed revision " +
                              info.getNewRevision() + " >>>\n\n";
                    else
                        val = "\n------- Committed new rev " +
                               info.getNewRevision() +
                              " (loaded from original rev " +
                              info.getOldRevision() +
                              ") >>>\n\n";
                    break;

                case load_node_start:
                    switch (info.getNodeAction())
                    {
                        case change:
                            val = "     * editing path : " + info.getPath() +
                                  " ...";
                            break;

                        case deleted:
                            val = "     * deleting path : " + info.getPath() +
                                  " ...";
                            break;

                        case add:
                            val = "     * adding path : " + info.getPath() +
                                  " ...";
                            break;

                        case replace:
                            val = "     * replacing path : " + info.getPath() +
                                  " ...";
                            break;

                        default:
                            val = null;
                    }
                    break;

                case load_node_done:
                    val = " done.\n";
                    break;

                case load_copied_node:
                    val = "COPIED...";
                    break;

                case load_txn_start:
                    val = "<<< Started new transaction, based on " +
                          "original revision " + info.getOldRevision() + "\n";
                    break;

                case load_normalized_mergeinfo:
                    val = " removing '\\r' from svn:mergeinfo ...";
                    break;

                default:
                    val = null;
            }

            if (val != null)
                try
                {
                    outputer.write(val.getBytes());
                }
                catch (IOException ex)
                {
                    ; // ignore
                }
        }
    }
}