File: repos.c

package info (click to toggle)
subversion 1.8.10-6%2Bdeb8u6
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 62,080 kB
  • sloc: ansic: 795,684; python: 115,859; java: 17,742; sh: 13,590; ruby: 12,397; cpp: 11,206; lisp: 7,540; perl: 5,649; sql: 1,466; makefile: 1,110; xml: 577
file content (2132 lines) | stat: -rw-r--r-- 98,315 bytes parent folder | download | duplicates (4)
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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
/* repos.c : repository creation; shared and exclusive repository locking
 *
 * ====================================================================
 *    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.
 * ====================================================================
 */

#include <apr_pools.h>
#include <apr_file_io.h>

#include "svn_pools.h"
#include "svn_error.h"
#include "svn_dirent_uri.h"
#include "svn_path.h"
#include "svn_utf.h"
#include "svn_time.h"
#include "svn_fs.h"
#include "svn_ra.h"  /* for SVN_RA_CAPABILITY_* */
#include "svn_repos.h"
#include "svn_hash.h"
#include "svn_version.h"
#include "svn_config.h"

#include "private/svn_repos_private.h"
#include "private/svn_subr_private.h"
#include "svn_private_config.h" /* for SVN_TEMPLATE_ROOT_DIR */

#include "repos.h"

/* Used to terminate lines in large multi-line string literals. */
#define NL APR_EOL_STR


/* Path accessor functions. */


const char *
svn_repos_path(svn_repos_t *repos, apr_pool_t *pool)
{
  return apr_pstrdup(pool, repos->path);
}


const char *
svn_repos_db_env(svn_repos_t *repos, apr_pool_t *pool)
{
  return apr_pstrdup(pool, repos->db_path);
}


const char *
svn_repos_conf_dir(svn_repos_t *repos, apr_pool_t *pool)
{
  return apr_pstrdup(pool, repos->conf_path);
}


const char *
svn_repos_svnserve_conf(svn_repos_t *repos, apr_pool_t *pool)
{
  return svn_dirent_join(repos->conf_path, SVN_REPOS__CONF_SVNSERVE_CONF, pool);
}


const char *
svn_repos_lock_dir(svn_repos_t *repos, apr_pool_t *pool)
{
  return apr_pstrdup(pool, repos->lock_path);
}


const char *
svn_repos_db_lockfile(svn_repos_t *repos, apr_pool_t *pool)
{
  return svn_dirent_join(repos->lock_path, SVN_REPOS__DB_LOCKFILE, pool);
}


const char *
svn_repos_db_logs_lockfile(svn_repos_t *repos, apr_pool_t *pool)
{
  return svn_dirent_join(repos->lock_path, SVN_REPOS__DB_LOGS_LOCKFILE, pool);
}

const char *
svn_repos_hook_dir(svn_repos_t *repos, apr_pool_t *pool)
{
  return apr_pstrdup(pool, repos->hook_path);
}


const char *
svn_repos_start_commit_hook(svn_repos_t *repos, apr_pool_t *pool)
{
  return svn_dirent_join(repos->hook_path, SVN_REPOS__HOOK_START_COMMIT, pool);
}


const char *
svn_repos_pre_commit_hook(svn_repos_t *repos, apr_pool_t *pool)
{
  return svn_dirent_join(repos->hook_path, SVN_REPOS__HOOK_PRE_COMMIT, pool);
}


const char *
svn_repos_pre_lock_hook(svn_repos_t *repos, apr_pool_t *pool)
{
  return svn_dirent_join(repos->hook_path, SVN_REPOS__HOOK_PRE_LOCK, pool);
}


const char *
svn_repos_pre_unlock_hook(svn_repos_t *repos, apr_pool_t *pool)
{
  return svn_dirent_join(repos->hook_path, SVN_REPOS__HOOK_PRE_UNLOCK, pool);
}

const char *
svn_repos_post_lock_hook(svn_repos_t *repos, apr_pool_t *pool)
{
  return svn_dirent_join(repos->hook_path, SVN_REPOS__HOOK_POST_LOCK, pool);
}


const char *
svn_repos_post_unlock_hook(svn_repos_t *repos, apr_pool_t *pool)
{
  return svn_dirent_join(repos->hook_path, SVN_REPOS__HOOK_POST_UNLOCK, pool);
}


const char *
svn_repos_post_commit_hook(svn_repos_t *repos, apr_pool_t *pool)
{
  return svn_dirent_join(repos->hook_path, SVN_REPOS__HOOK_POST_COMMIT, pool);
}


const char *
svn_repos_pre_revprop_change_hook(svn_repos_t *repos, apr_pool_t *pool)
{
  return svn_dirent_join(repos->hook_path, SVN_REPOS__HOOK_PRE_REVPROP_CHANGE,
                       pool);
}


const char *
svn_repos_post_revprop_change_hook(svn_repos_t *repos, apr_pool_t *pool)
{
  return svn_dirent_join(repos->hook_path, SVN_REPOS__HOOK_POST_REVPROP_CHANGE,
                       pool);
}

static svn_error_t *
create_repos_dir(const char *path, apr_pool_t *pool)
{
  svn_error_t *err;

  err = svn_io_dir_make(path, APR_OS_DEFAULT, pool);
  if (err && (APR_STATUS_IS_EEXIST(err->apr_err)))
    {
      svn_boolean_t is_empty;

      svn_error_clear(err);

      SVN_ERR(svn_io_dir_empty(&is_empty, path, pool));

      if (is_empty)
        err = NULL;
      else
        err = svn_error_createf(SVN_ERR_DIR_NOT_EMPTY, 0,
                                _("'%s' exists and is non-empty"),
                                svn_dirent_local_style(path, pool));
    }

  return svn_error_trace(err);
}

static const char * bdb_lock_file_contents =
  "DB lock file, representing locks on the versioned filesystem."            NL
  ""                                                                         NL
  "All accessors -- both readers and writers -- of the repository's"         NL
  "Berkeley DB environment take out shared locks on this file, and"          NL
  "each accessor removes its lock when done.  If and when the DB"            NL
  "recovery procedure is run, the recovery code takes out an"                NL
  "exclusive lock on this file, so we can be sure no one else is"            NL
  "using the DB during the recovery."                                        NL
  ""                                                                         NL
  "You should never have to edit or remove this file."                       NL;

static const char * bdb_logs_lock_file_contents =
  "DB logs lock file, representing locks on the versioned filesystem logs."  NL
  ""                                                                         NL
  "All log manipulators of the repository's Berkeley DB environment"         NL
  "take out exclusive locks on this file to ensure that only one"            NL
  "accessor manipulates the logs at a time."                                 NL
  ""                                                                         NL
  "You should never have to edit or remove this file."                       NL;

static const char * pre12_compat_unneeded_file_contents =
  "This file is not used by Subversion 1.3.x or later."                      NL
  "However, its existence is required for compatibility with"                NL
  "Subversion 1.2.x or earlier."                                             NL;

/* Create the DB logs lockfile. */
static svn_error_t *
create_db_logs_lock(svn_repos_t *repos, apr_pool_t *pool) {
  const char *contents;
  const char *lockfile_path;

  lockfile_path = svn_repos_db_logs_lockfile(repos, pool);
  if (strcmp(repos->fs_type, SVN_FS_TYPE_BDB) == 0)
    contents = bdb_logs_lock_file_contents;
  else
    contents = pre12_compat_unneeded_file_contents;

  SVN_ERR_W(svn_io_file_create(lockfile_path, contents, pool),
            _("Creating db logs lock file"));

  return SVN_NO_ERROR;
}

/* Create the DB lockfile. */
static svn_error_t *
create_db_lock(svn_repos_t *repos, apr_pool_t *pool) {
  const char *contents;
  const char *lockfile_path;

  lockfile_path = svn_repos_db_lockfile(repos, pool);
  if (strcmp(repos->fs_type, SVN_FS_TYPE_BDB) == 0)
    contents = bdb_lock_file_contents;
  else
    contents = pre12_compat_unneeded_file_contents;

  SVN_ERR_W(svn_io_file_create(lockfile_path, contents, pool),
            _("Creating db lock file"));

  return SVN_NO_ERROR;
}

static svn_error_t *
create_locks(svn_repos_t *repos, apr_pool_t *pool)
{
  /* Create the locks directory. */
  SVN_ERR_W(create_repos_dir(repos->lock_path, pool),
            _("Creating lock dir"));

  SVN_ERR(create_db_lock(repos, pool));
  return create_db_logs_lock(repos, pool);
}


#define HOOKS_ENVIRONMENT_TEXT                                                \
  "# The hook program typically does not inherit the environment of"       NL \
  "# its parent process.  For example, a common problem is for the"        NL \
  "# PATH environment variable to not be set to its usual value, so"       NL \
  "# that subprograms fail to launch unless invoked via absolute path."    NL \
  "# If you're having unexpected problems with a hook program, the"        NL \
  "# culprit may be unusual (or missing) environment variables."           NL

#define PREWRITTEN_HOOKS_TEXT                                                 \
  "# For more examples and pre-written hooks, see those in"                NL \
  "# the Subversion repository at"                                         NL \
  "# http://svn.apache.org/repos/asf/subversion/trunk/tools/hook-scripts/ and"        NL \
  "# http://svn.apache.org/repos/asf/subversion/trunk/contrib/hook-scripts/"          NL


static svn_error_t *
create_hooks(svn_repos_t *repos, apr_pool_t *pool)
{
  const char *this_path, *contents;

  /* Create the hook directory. */
  SVN_ERR_W(create_repos_dir(repos->hook_path, pool),
            _("Creating hook directory"));

  /*** Write a default template for each standard hook file. */

  /* Start-commit hook. */
  {
    this_path = apr_psprintf(pool, "%s%s",
                             svn_repos_start_commit_hook(repos, pool),
                             SVN_REPOS__HOOK_DESC_EXT);

#define SCRIPT_NAME SVN_REPOS__HOOK_START_COMMIT

    contents =
"#!/bin/sh"                                                                  NL
""                                                                           NL
"# START-COMMIT HOOK"                                                        NL
"#"                                                                          NL
"# The start-commit hook is invoked immediately after a Subversion txn is"   NL
"# created and populated with initial revprops in the process of doing a"    NL
"# commit. Subversion runs this hook by invoking a program (script, "        NL
"# executable, binary, etc.) named '"SCRIPT_NAME"' (for which this file"     NL
"# is a template) with the following ordered arguments:"                     NL
"#"                                                                          NL
"#   [1] REPOS-PATH   (the path to this repository)"                         NL
"#   [2] USER         (the authenticated user attempting to commit)"         NL
"#   [3] CAPABILITIES (a colon-separated list of capabilities reported"      NL
"#                     by the client; see note below)"                       NL
"#   [4] TXN-NAME     (the name of the commit txn just created)"             NL
"#"                                                                          NL
"# Note: The CAPABILITIES parameter is new in Subversion 1.5, and 1.5"       NL
"# clients will typically report at least the \""                            \
   SVN_RA_CAPABILITY_MERGEINFO "\" capability."                              NL
"# If there are other capabilities, then the list is colon-separated,"       NL
"# e.g.: \"" SVN_RA_CAPABILITY_MERGEINFO ":some-other-capability\" "         \
  "(the order is undefined)."                                                NL
"#"                                                                          NL
"# Note: The TXN-NAME parameter is new in Subversion 1.8.  Prior to version" NL
"# 1.8, the start-commit hook was invoked before the commit txn was even"    NL
"# created, so the ability to inspect the commit txn and its metadata from"  NL
"# within the start-commit hook was not possible."                           NL
"# "                                                                         NL
"# The list is self-reported by the client.  Therefore, you should not"      NL
"# make security assumptions based on the capabilities list, nor should"     NL
"# you assume that clients reliably report every capability they have."      NL
"#"                                                                          NL
"# The working directory for this hook program's invocation is undefined,"   NL
"# so the program should set one explicitly if it cares."                    NL
"#"                                                                          NL
"# If the hook program exits with success, the commit continues; but"        NL
"# if it exits with failure (non-zero), the commit is stopped before"        NL
"# a Subversion txn is created, and STDERR is returned to the client."       NL
"#"                                                                          NL
"# On a Unix system, the normal procedure is to have '"SCRIPT_NAME"'"        NL
"# invoke other programs to do the real work, though it may do the"          NL
"# work itself too."                                                         NL
"#"                                                                          NL
"# Note that '"SCRIPT_NAME"' must be executable by the user(s) who will"     NL
"# invoke it (typically the user httpd runs as), and that user must"         NL
"# have filesystem-level permission to access the repository."               NL
"#"                                                                          NL
"# On a Windows system, you should name the hook program"                    NL
"# '"SCRIPT_NAME".bat' or '"SCRIPT_NAME".exe',"                              NL
"# but the basic idea is the same."                                          NL
"# "                                                                         NL
HOOKS_ENVIRONMENT_TEXT
"# "                                                                         NL
"# Here is an example hook script, for a Unix /bin/sh interpreter."          NL
PREWRITTEN_HOOKS_TEXT
""                                                                           NL
""                                                                           NL
"REPOS=\"$1\""                                                               NL
"USER=\"$2\""                                                                NL
""                                                                           NL
"commit-allower.pl --repository \"$REPOS\" --user \"$USER\" || exit 1"       NL
"special-auth-check.py --user \"$USER\" --auth-level 3 || exit 1"            NL
""                                                                           NL
"# All checks passed, so allow the commit."                                  NL
"exit 0"                                                                     NL;

#undef SCRIPT_NAME

    SVN_ERR_W(svn_io_file_create(this_path, contents, pool),
              _("Creating start-commit hook"));

    SVN_ERR(svn_io_set_file_executable(this_path, TRUE, FALSE, pool));
  }  /* end start-commit hook */

  /* Pre-commit hook. */
  {
    this_path = apr_psprintf(pool, "%s%s",
                             svn_repos_pre_commit_hook(repos, pool),
                             SVN_REPOS__HOOK_DESC_EXT);

#define SCRIPT_NAME SVN_REPOS__HOOK_PRE_COMMIT

    contents =
"#!/bin/sh"                                                                  NL
""                                                                           NL
"# PRE-COMMIT HOOK"                                                          NL
"#"                                                                          NL
"# The pre-commit hook is invoked before a Subversion txn is"                NL
"# committed.  Subversion runs this hook by invoking a program"              NL
"# (script, executable, binary, etc.) named '"SCRIPT_NAME"' (for which"      NL
"# this file is a template), with the following ordered arguments:"          NL
"#"                                                                          NL
"#   [1] REPOS-PATH   (the path to this repository)"                         NL
"#   [2] TXN-NAME     (the name of the txn about to be committed)"           NL
"#"                                                                          NL
"#   [STDIN] LOCK-TOKENS ** the lock tokens are passed via STDIN."           NL
"#"                                                                          NL
"#   If STDIN contains the line \"LOCK-TOKENS:\\n\" (the \"\\n\" denotes a"  NL
"#   single newline), the lines following it are the lock tokens for"        NL
"#   this commit.  The end of the list is marked by a line containing"       NL
"#   only a newline character."                                              NL
"#"                                                                          NL
"#   Each lock token line consists of a URI-escaped path, followed"          NL
"#   by the separator character '|', followed by the lock token string,"     NL
"#   followed by a newline."                                                 NL
"#"                                                                          NL
"# The default working directory for the invocation is undefined, so"        NL
"# the program should set one explicitly if it cares."                       NL
"#"                                                                          NL
"# If the hook program exits with success, the txn is committed; but"        NL
"# if it exits with failure (non-zero), the txn is aborted, no commit"       NL
"# takes place, and STDERR is returned to the client.   The hook"            NL
"# program can use the 'svnlook' utility to help it examine the txn."        NL
"#"                                                                          NL
"# On a Unix system, the normal procedure is to have '"SCRIPT_NAME"'"        NL
"# invoke other programs to do the real work, though it may do the"          NL
"# work itself too."                                                         NL
"#"                                                                          NL
"#   ***  NOTE: THE HOOK PROGRAM MUST NOT MODIFY THE TXN, EXCEPT  ***"       NL
"#   ***  FOR REVISION PROPERTIES (like svn:log or svn:author).   ***"       NL
"#"                                                                          NL
"#   This is why we recommend using the read-only 'svnlook' utility."        NL
"#   In the future, Subversion may enforce the rule that pre-commit"         NL
"#   hooks should not modify the versioned data in txns, or else come"       NL
"#   up with a mechanism to make it safe to do so (by informing the"         NL
"#   committing client of the changes).  However, right now neither"         NL
"#   mechanism is implemented, so hook writers just have to be careful."     NL
"#"                                                                          NL
"# Note that '"SCRIPT_NAME"' must be executable by the user(s) who will"     NL
"# invoke it (typically the user httpd runs as), and that user must"         NL
"# have filesystem-level permission to access the repository."               NL
"#"                                                                          NL
"# On a Windows system, you should name the hook program"                    NL
"# '"SCRIPT_NAME".bat' or '"SCRIPT_NAME".exe',"                              NL
"# but the basic idea is the same."                                          NL
"#"                                                                          NL
HOOKS_ENVIRONMENT_TEXT
"# "                                                                         NL
"# Here is an example hook script, for a Unix /bin/sh interpreter."          NL
PREWRITTEN_HOOKS_TEXT
""                                                                           NL
""                                                                           NL
"REPOS=\"$1\""                                                               NL
"TXN=\"$2\""                                                                 NL
""                                                                           NL
"# Make sure that the log message contains some text."                       NL
"SVNLOOK=" SVN_BINDIR "/svnlook"                                             NL
"$SVNLOOK log -t \"$TXN\" \"$REPOS\" | \\"                                   NL
"   grep \"[a-zA-Z0-9]\" > /dev/null || exit 1"                              NL
""                                                                           NL
"# Check that the author of this commit has the rights to perform"           NL
"# the commit on the files and directories being modified."                  NL
"commit-access-control.pl \"$REPOS\" \"$TXN\" commit-access-control.cfg || exit 1"
                                                                             NL
""                                                                           NL
"# All checks passed, so allow the commit."                                  NL
"exit 0"                                                                     NL;

#undef SCRIPT_NAME

    SVN_ERR_W(svn_io_file_create(this_path, contents, pool),
              _("Creating pre-commit hook"));

    SVN_ERR(svn_io_set_file_executable(this_path, TRUE, FALSE, pool));
  }  /* end pre-commit hook */


  /* Pre-revprop-change hook. */
  {
    this_path = apr_psprintf(pool, "%s%s",
                             svn_repos_pre_revprop_change_hook(repos, pool),
                             SVN_REPOS__HOOK_DESC_EXT);

#define SCRIPT_NAME SVN_REPOS__HOOK_PRE_REVPROP_CHANGE

    contents =
"#!/bin/sh"                                                                  NL
""                                                                           NL
"# PRE-REVPROP-CHANGE HOOK"                                                  NL
"#"                                                                          NL
"# The pre-revprop-change hook is invoked before a revision property"        NL
"# is added, modified or deleted.  Subversion runs this hook by invoking"    NL
"# a program (script, executable, binary, etc.) named '"SCRIPT_NAME"'"       NL
"# (for which this file is a template), with the following ordered"          NL
"# arguments:"                                                               NL
"#"                                                                          NL
"#   [1] REPOS-PATH   (the path to this repository)"                         NL
"#   [2] REV          (the revision being tweaked)"                          NL
"#   [3] USER         (the username of the person tweaking the property)"    NL
"#   [4] PROPNAME     (the property being set on the revision)"              NL
"#   [5] ACTION       (the property is being 'A'dded, 'M'odified, or 'D'eleted)"
                                                                             NL
"#"                                                                          NL
"#   [STDIN] PROPVAL  ** the new property value is passed via STDIN."        NL
"#"                                                                          NL
"# If the hook program exits with success, the propchange happens; but"      NL
"# if it exits with failure (non-zero), the propchange doesn't happen."      NL
"# The hook program can use the 'svnlook' utility to examine the "           NL
"# existing value of the revision property."                                 NL
"#"                                                                          NL
"# WARNING: unlike other hooks, this hook MUST exist for revision"           NL
"# properties to be changed.  If the hook does not exist, Subversion "       NL
"# will behave as if the hook were present, but failed.  The reason"         NL
"# for this is that revision properties are UNVERSIONED, meaning that"       NL
"# a successful propchange is destructive;  the old value is gone"           NL
"# forever.  We recommend the hook back up the old value somewhere."         NL
"#"                                                                          NL
"# On a Unix system, the normal procedure is to have '"SCRIPT_NAME"'"        NL
"# invoke other programs to do the real work, though it may do the"          NL
"# work itself too."                                                         NL
"#"                                                                          NL
"# Note that '"SCRIPT_NAME"' must be executable by the user(s) who will"     NL
"# invoke it (typically the user httpd runs as), and that user must"         NL
"# have filesystem-level permission to access the repository."               NL
"#"                                                                          NL
"# On a Windows system, you should name the hook program"                    NL
"# '"SCRIPT_NAME".bat' or '"SCRIPT_NAME".exe',"                              NL
"# but the basic idea is the same."                                          NL
"#"                                                                          NL
HOOKS_ENVIRONMENT_TEXT
"# "                                                                         NL
"# Here is an example hook script, for a Unix /bin/sh interpreter."          NL
PREWRITTEN_HOOKS_TEXT
""                                                                           NL
""                                                                           NL
"REPOS=\"$1\""                                                               NL
"REV=\"$2\""                                                                 NL
"USER=\"$3\""                                                                NL
"PROPNAME=\"$4\""                                                            NL
"ACTION=\"$5\""                                                              NL
""                                                                           NL
"if [ \"$ACTION\" = \"M\" -a \"$PROPNAME\" = \"svn:log\" ]; then exit 0; fi" NL
""                                                                           NL
"echo \"Changing revision properties other than svn:log is prohibited\" >&2" NL
"exit 1"                                                                     NL;

#undef SCRIPT_NAME

    SVN_ERR_W(svn_io_file_create(this_path, contents, pool),
              _("Creating pre-revprop-change hook"));

    SVN_ERR(svn_io_set_file_executable(this_path, TRUE, FALSE, pool));
  }  /* end pre-revprop-change hook */


  /* Pre-lock hook. */
  {
    this_path = apr_psprintf(pool, "%s%s",
                             svn_repos_pre_lock_hook(repos, pool),
                             SVN_REPOS__HOOK_DESC_EXT);

#define SCRIPT_NAME SVN_REPOS__HOOK_PRE_LOCK

    contents =
"#!/bin/sh"                                                                  NL
""                                                                           NL
"# PRE-LOCK HOOK"                                                            NL
"#"                                                                          NL
"# The pre-lock hook is invoked before an exclusive lock is"                 NL
"# created.  Subversion runs this hook by invoking a program "               NL
"# (script, executable, binary, etc.) named '"SCRIPT_NAME"' (for which"      NL
"# this file is a template), with the following ordered arguments:"          NL
"#"                                                                          NL
"#   [1] REPOS-PATH   (the path to this repository)"                         NL
"#   [2] PATH         (the path in the repository about to be locked)"       NL
"#   [3] USER         (the user creating the lock)"                          NL
"#   [4] COMMENT      (the comment of the lock)"                             NL
"#   [5] STEAL-LOCK   (1 if the user is trying to steal the lock, else 0)"   NL
"#"                                                                          NL
"# If the hook program outputs anything on stdout, the output string will"   NL
"# be used as the lock token for this lock operation.  If you choose to use" NL
"# this feature, you must guarantee the tokens generated are unique across"  NL
"# the repository each time."                                                NL
"#"                                                                          NL
"# The default working directory for the invocation is undefined, so"        NL
"# the program should set one explicitly if it cares."                       NL
"#"                                                                          NL
"# If the hook program exits with success, the lock is created; but"         NL
"# if it exits with failure (non-zero), the lock action is aborted"          NL
"# and STDERR is returned to the client."                                    NL
""                                                                           NL
"# On a Unix system, the normal procedure is to have '"SCRIPT_NAME"'"        NL
"# invoke other programs to do the real work, though it may do the"          NL
"# work itself too."                                                         NL
"#"                                                                          NL
"# Note that '"SCRIPT_NAME"' must be executable by the user(s) who will"     NL
"# invoke it (typically the user httpd runs as), and that user must"         NL
"# have filesystem-level permission to access the repository."               NL
"#"                                                                          NL
"# On a Windows system, you should name the hook program"                    NL
"# '"SCRIPT_NAME".bat' or '"SCRIPT_NAME".exe',"                              NL
"# but the basic idea is the same."                                          NL
"#"                                                                          NL
"# Here is an example hook script, for a Unix /bin/sh interpreter:"          NL
""                                                                           NL
"REPOS=\"$1\""                                                               NL
"PATH=\"$2\""                                                                NL
"USER=\"$3\""                                                                NL
"COMMENT=\"$4\""                                                             NL
"STEAL=\"$5\""                                                               NL
""                                                                           NL
"# If a lock exists and is owned by a different person, don't allow it"      NL
"# to be stolen (e.g., with 'svn lock --force ...')."                        NL
""                                                                           NL
"# (Maybe this script could send email to the lock owner?)"                  NL
"SVNLOOK=" SVN_BINDIR "/svnlook"                                             NL
"GREP=/bin/grep"                                                             NL
"SED=/bin/sed"                                                               NL
""                                                                           NL
"LOCK_OWNER=`$SVNLOOK lock \"$REPOS\" \"$PATH\" | \\"                        NL
"            $GREP '^Owner: ' | $SED 's/Owner: //'`"                         NL
""                                                                           NL
"# If we get no result from svnlook, there's no lock, allow the lock to"     NL
"# happen:"                                                                  NL
"if [ \"$LOCK_OWNER\" = \"\" ]; then"                                        NL
"  exit 0"                                                                   NL
"fi"                                                                         NL
""                                                                           NL
"# If the person locking matches the lock's owner, allow the lock to"        NL
"# happen:"                                                                  NL
"if [ \"$LOCK_OWNER\" = \"$USER\" ]; then"                                   NL
"  exit 0"                                                                   NL
"fi"                                                                         NL
""                                                                           NL
"# Otherwise, we've got an owner mismatch, so return failure:"               NL
"echo \"Error: $PATH already locked by ${LOCK_OWNER}.\" 1>&2"                NL
"exit 1"                                                                     NL;

#undef SCRIPT_NAME

    SVN_ERR_W(svn_io_file_create(this_path, contents, pool),
              "Creating pre-lock hook");

    SVN_ERR(svn_io_set_file_executable(this_path, TRUE, FALSE, pool));
  }  /* end pre-lock hook */


  /* Pre-unlock hook. */
  {
    this_path = apr_psprintf(pool, "%s%s",
                             svn_repos_pre_unlock_hook(repos, pool),
                             SVN_REPOS__HOOK_DESC_EXT);

#define SCRIPT_NAME SVN_REPOS__HOOK_PRE_UNLOCK

    contents =
"#!/bin/sh"                                                                  NL
""                                                                           NL
"# PRE-UNLOCK HOOK"                                                          NL
"#"                                                                          NL
"# The pre-unlock hook is invoked before an exclusive lock is"               NL
"# destroyed.  Subversion runs this hook by invoking a program "             NL
"# (script, executable, binary, etc.) named '"SCRIPT_NAME"' (for which"      NL
"# this file is a template), with the following ordered arguments:"          NL
"#"                                                                          NL
"#   [1] REPOS-PATH   (the path to this repository)"                         NL
"#   [2] PATH         (the path in the repository about to be unlocked)"     NL
"#   [3] USER         (the user destroying the lock)"                        NL
"#   [4] TOKEN        (the lock token to be destroyed)"                      NL
"#   [5] BREAK-UNLOCK (1 if the user is breaking the lock, else 0)"          NL
"#"                                                                          NL
"# The default working directory for the invocation is undefined, so"        NL
"# the program should set one explicitly if it cares."                       NL
"#"                                                                          NL
"# If the hook program exits with success, the lock is destroyed; but"       NL
"# if it exits with failure (non-zero), the unlock action is aborted"        NL
"# and STDERR is returned to the client."                                    NL
""                                                                           NL
"# On a Unix system, the normal procedure is to have '"SCRIPT_NAME"'"        NL
"# invoke other programs to do the real work, though it may do the"          NL
"# work itself too."                                                         NL
"#"                                                                          NL
"# Note that '"SCRIPT_NAME"' must be executable by the user(s) who will"     NL
"# invoke it (typically the user httpd runs as), and that user must"         NL
"# have filesystem-level permission to access the repository."               NL
"#"                                                                          NL
"# On a Windows system, you should name the hook program"                    NL
"# '"SCRIPT_NAME".bat' or '"SCRIPT_NAME".exe',"                              NL
"# but the basic idea is the same."                                          NL
"#"                                                                          NL
"# Here is an example hook script, for a Unix /bin/sh interpreter:"          NL
""                                                                           NL
"REPOS=\"$1\""                                                               NL
"PATH=\"$2\""                                                                NL
"USER=\"$3\""                                                                NL
"TOKEN=\"$4\""                                                               NL
"BREAK=\"$5\""                                                               NL
""                                                                           NL
"# If a lock is owned by a different person, don't allow it be broken."      NL
"# (Maybe this script could send email to the lock owner?)"                  NL
""                                                                           NL
"SVNLOOK=" SVN_BINDIR "/svnlook"                                             NL
"GREP=/bin/grep"                                                             NL
"SED=/bin/sed"                                                               NL
""                                                                           NL
"LOCK_OWNER=`$SVNLOOK lock \"$REPOS\" \"$PATH\" | \\"                        NL
"            $GREP '^Owner: ' | $SED 's/Owner: //'`"                         NL
""                                                                           NL
"# If we get no result from svnlook, there's no lock, return success:"       NL
"if [ \"$LOCK_OWNER\" = \"\" ]; then"                                        NL
"  exit 0"                                                                   NL
"fi"                                                                         NL
""                                                                           NL
"# If the person unlocking matches the lock's owner, return success:"        NL
"if [ \"$LOCK_OWNER\" = \"$USER\" ]; then"                                   NL
"  exit 0"                                                                   NL
"fi"                                                                         NL
""                                                                           NL
"# Otherwise, we've got an owner mismatch, so return failure:"               NL
"echo \"Error: $PATH locked by ${LOCK_OWNER}.\" 1>&2"                        NL
"exit 1"                                                                     NL;

#undef SCRIPT_NAME

    SVN_ERR_W(svn_io_file_create(this_path, contents, pool),
              "Creating pre-unlock hook");

    SVN_ERR(svn_io_set_file_executable(this_path, TRUE, FALSE, pool));
  }  /* end pre-unlock hook */



  /* Post-commit hook. */
  {
    this_path = apr_psprintf(pool, "%s%s",
                             svn_repos_post_commit_hook(repos, pool),
                             SVN_REPOS__HOOK_DESC_EXT);

#define SCRIPT_NAME SVN_REPOS__HOOK_POST_COMMIT

    contents =
"#!/bin/sh"                                                                  NL
""                                                                           NL
"# POST-COMMIT HOOK"                                                         NL
"#"                                                                          NL
"# The post-commit hook is invoked after a commit.  Subversion runs"         NL
"# this hook by invoking a program (script, executable, binary, etc.)"       NL
"# named '"SCRIPT_NAME"' (for which this file is a template) with the "      NL
"# following ordered arguments:"                                             NL
"#"                                                                          NL
"#   [1] REPOS-PATH   (the path to this repository)"                         NL
"#   [2] REV          (the number of the revision just committed)"           NL
"#   [3] TXN-NAME     (the name of the transaction that has become REV)"     NL
"#"                                                                          NL
"# The default working directory for the invocation is undefined, so"        NL
"# the program should set one explicitly if it cares."                       NL
"#"                                                                          NL
"# Because the commit has already completed and cannot be undone,"           NL
"# the exit code of the hook program is ignored.  The hook program"          NL
"# can use the 'svnlook' utility to help it examine the"                     NL
"# newly-committed tree."                                                    NL
"#"                                                                          NL
"# On a Unix system, the normal procedure is to have '"SCRIPT_NAME"'"        NL
"# invoke other programs to do the real work, though it may do the"          NL
"# work itself too."                                                         NL
"#"                                                                          NL
"# Note that '"SCRIPT_NAME"' must be executable by the user(s) who will"     NL
"# invoke it (typically the user httpd runs as), and that user must"         NL
"# have filesystem-level permission to access the repository."               NL
"#"                                                                          NL
"# On a Windows system, you should name the hook program"                    NL
"# '"SCRIPT_NAME".bat' or '"SCRIPT_NAME".exe',"                              NL
"# but the basic idea is the same."                                          NL
"# "                                                                         NL
HOOKS_ENVIRONMENT_TEXT
"# "                                                                         NL
"# Here is an example hook script, for a Unix /bin/sh interpreter."          NL
PREWRITTEN_HOOKS_TEXT
""                                                                           NL
""                                                                           NL
"REPOS=\"$1\""                                                               NL
"REV=\"$2\""                                                                 NL
"TXN_NAME=\"$3\""                                                            NL
                                                                             NL
"mailer.py commit \"$REPOS\" \"$REV\" /path/to/mailer.conf"                  NL;

#undef SCRIPT_NAME

    SVN_ERR_W(svn_io_file_create(this_path, contents, pool),
              _("Creating post-commit hook"));

    SVN_ERR(svn_io_set_file_executable(this_path, TRUE, FALSE, pool));
  } /* end post-commit hook */


  /* Post-lock hook. */
  {
    this_path = apr_psprintf(pool, "%s%s",
                             svn_repos_post_lock_hook(repos, pool),
                             SVN_REPOS__HOOK_DESC_EXT);

#define SCRIPT_NAME SVN_REPOS__HOOK_POST_LOCK

    contents =
"#!/bin/sh"                                                                  NL
""                                                                           NL
"# POST-LOCK HOOK"                                                           NL
"#"                                                                          NL
"# The post-lock hook is run after a path is locked.  Subversion runs"       NL
"# this hook by invoking a program (script, executable, binary, etc.)"       NL
"# named '"SCRIPT_NAME"' (for which this file is a template) with the "      NL
"# following ordered arguments:"                                             NL
"#"                                                                          NL
"#   [1] REPOS-PATH   (the path to this repository)"                         NL
"#   [2] USER         (the user who created the lock)"                       NL
"#"                                                                          NL
"# The paths that were just locked are passed to the hook via STDIN (as"     NL
"# of Subversion 1.2, only one path is passed per invocation, but the"       NL
"# plan is to pass all locked paths at once, so the hook program"            NL
"# should be written accordingly)."                                          NL
"#"                                                                          NL
"# The default working directory for the invocation is undefined, so"        NL
"# the program should set one explicitly if it cares."                       NL
"#"                                                                          NL
"# Because the lock has already been created and cannot be undone,"          NL
"# the exit code of the hook program is ignored.  The hook program"          NL
"# can use the 'svnlook' utility to help it examine the"                     NL
"# newly-created lock."                                                      NL
"#"                                                                          NL
"# On a Unix system, the normal procedure is to have '"SCRIPT_NAME"'"        NL
"# invoke other programs to do the real work, though it may do the"          NL
"# work itself too."                                                         NL
"#"                                                                          NL
"# Note that '"SCRIPT_NAME"' must be executable by the user(s) who will"     NL
"# invoke it (typically the user httpd runs as), and that user must"         NL
"# have filesystem-level permission to access the repository."               NL
"#"                                                                          NL
"# On a Windows system, you should name the hook program"                    NL
"# '"SCRIPT_NAME".bat' or '"SCRIPT_NAME".exe',"                              NL
"# but the basic idea is the same."                                          NL
"# "                                                                         NL
"# Here is an example hook script, for a Unix /bin/sh interpreter:"          NL
""                                                                           NL
"REPOS=\"$1\""                                                               NL
"USER=\"$2\""                                                                NL
""                                                                           NL
"# Send email to interested parties, let them know a lock was created:"      NL
"mailer.py lock \"$REPOS\" \"$USER\" /path/to/mailer.conf"                   NL;

#undef SCRIPT_NAME

    SVN_ERR_W(svn_io_file_create(this_path, contents, pool),
              "Creating post-lock hook");

    SVN_ERR(svn_io_set_file_executable(this_path, TRUE, FALSE, pool));
  } /* end post-lock hook */


  /* Post-unlock hook. */
  {
    this_path = apr_psprintf(pool, "%s%s",
                             svn_repos_post_unlock_hook(repos, pool),
                             SVN_REPOS__HOOK_DESC_EXT);

#define SCRIPT_NAME SVN_REPOS__HOOK_POST_UNLOCK

    contents =
"#!/bin/sh"                                                                  NL
""                                                                           NL
"# POST-UNLOCK HOOK"                                                         NL
"#"                                                                          NL
"# The post-unlock hook runs after a path is unlocked.  Subversion runs"     NL
"# this hook by invoking a program (script, executable, binary, etc.)"       NL
"# named '"SCRIPT_NAME"' (for which this file is a template) with the "      NL
"# following ordered arguments:"                                             NL
"#"                                                                          NL
"#   [1] REPOS-PATH   (the path to this repository)"                         NL
"#   [2] USER         (the user who destroyed the lock)"                     NL
"#"                                                                          NL
"# The paths that were just unlocked are passed to the hook via STDIN"       NL
"# (as of Subversion 1.2, only one path is passed per invocation, but"       NL
"# the plan is to pass all unlocked paths at once, so the hook program"      NL
"# should be written accordingly)."                                          NL
"#"                                                                          NL
"# The default working directory for the invocation is undefined, so"        NL
"# the program should set one explicitly if it cares."                       NL
"#"                                                                          NL
"# Because the lock has already been destroyed and cannot be undone,"        NL
"# the exit code of the hook program is ignored."                            NL
"#"                                                                          NL
"# On a Unix system, the normal procedure is to have '"SCRIPT_NAME"'"        NL
"# invoke other programs to do the real work, though it may do the"          NL
"# work itself too."                                                         NL
"#"                                                                          NL
"# Note that '"SCRIPT_NAME"' must be executable by the user(s) who will"     NL
"# invoke it (typically the user httpd runs as), and that user must"         NL
"# have filesystem-level permission to access the repository."               NL
"#"                                                                          NL
"# On a Windows system, you should name the hook program"                    NL
"# '"SCRIPT_NAME".bat' or '"SCRIPT_NAME".exe',"                              NL
"# but the basic idea is the same."                                          NL
"# "                                                                         NL
"# Here is an example hook script, for a Unix /bin/sh interpreter:"          NL
""                                                                           NL
"REPOS=\"$1\""                                                               NL
"USER=\"$2\""                                                                NL
""                                                                           NL
"# Send email to interested parties, let them know a lock was removed:"      NL
"mailer.py unlock \"$REPOS\" \"$USER\" /path/to/mailer.conf"                 NL;

#undef SCRIPT_NAME

    SVN_ERR_W(svn_io_file_create(this_path, contents, pool),
              "Creating post-unlock hook");

    SVN_ERR(svn_io_set_file_executable(this_path, TRUE, FALSE, pool));
  } /* end post-unlock hook */


  /* Post-revprop-change hook. */
  {
    this_path = apr_psprintf(pool, "%s%s",
                             svn_repos_post_revprop_change_hook(repos, pool),
                             SVN_REPOS__HOOK_DESC_EXT);

#define SCRIPT_NAME SVN_REPOS__HOOK_POST_REVPROP_CHANGE

    contents =
"#!/bin/sh"                                                                  NL
""                                                                           NL
"# POST-REVPROP-CHANGE HOOK"                                                 NL
"#"                                                                          NL
"# The post-revprop-change hook is invoked after a revision property"        NL
"# has been added, modified or deleted.  Subversion runs this hook by"       NL
"# invoking a program (script, executable, binary, etc.) named"              NL
"# '"SCRIPT_NAME"' (for which this file is a template), with the"            NL
"# following ordered arguments:"                                             NL
"#"                                                                          NL
"#   [1] REPOS-PATH   (the path to this repository)"                         NL
"#   [2] REV          (the revision that was tweaked)"                       NL
"#   [3] USER         (the username of the person tweaking the property)"    NL
"#   [4] PROPNAME     (the property that was changed)"                       NL
"#   [5] ACTION       (the property was 'A'dded, 'M'odified, or 'D'eleted)"  NL
"#"                                                                          NL
"#   [STDIN] PROPVAL  ** the old property value is passed via STDIN."        NL
"#"                                                                          NL
"# Because the propchange has already completed and cannot be undone,"       NL
"# the exit code of the hook program is ignored.  The hook program"          NL
"# can use the 'svnlook' utility to help it examine the"                     NL
"# new property value."                                                      NL
"#"                                                                          NL
"# On a Unix system, the normal procedure is to have '"SCRIPT_NAME"'"        NL
"# invoke other programs to do the real work, though it may do the"          NL
"# work itself too."                                                         NL
"#"                                                                          NL
"# Note that '"SCRIPT_NAME"' must be executable by the user(s) who will"     NL
"# invoke it (typically the user httpd runs as), and that user must"         NL
"# have filesystem-level permission to access the repository."               NL
"#"                                                                          NL
"# On a Windows system, you should name the hook program"                    NL
"# '"SCRIPT_NAME".bat' or '"SCRIPT_NAME".exe',"                              NL
"# but the basic idea is the same."                                          NL
"# "                                                                         NL
HOOKS_ENVIRONMENT_TEXT
"# "                                                                         NL
"# Here is an example hook script, for a Unix /bin/sh interpreter."          NL
PREWRITTEN_HOOKS_TEXT
""                                                                           NL
""                                                                           NL
"REPOS=\"$1\""                                                               NL
"REV=\"$2\""                                                                 NL
"USER=\"$3\""                                                                NL
"PROPNAME=\"$4\""                                                            NL
"ACTION=\"$5\""                                                              NL
""                                                                           NL
"mailer.py propchange2 \"$REPOS\" \"$REV\" \"$USER\" \"$PROPNAME\" "
"\"$ACTION\" /path/to/mailer.conf"                                           NL;

#undef SCRIPT_NAME

    SVN_ERR_W(svn_io_file_create(this_path, contents, pool),
              _("Creating post-revprop-change hook"));

    SVN_ERR(svn_io_set_file_executable(this_path, TRUE, FALSE, pool));
  } /* end post-revprop-change hook */

  return SVN_NO_ERROR;
}

static svn_error_t *
create_conf(svn_repos_t *repos, apr_pool_t *pool)
{
  SVN_ERR_W(create_repos_dir(repos->conf_path, pool),
            _("Creating conf directory"));

  /* Write a default template for svnserve.conf. */
  {
    static const char * const svnserve_conf_contents =
"### This file controls the configuration of the svnserve daemon, if you"    NL
"### use it to allow access to this repository.  (If you only allow"         NL
"### access through http: and/or file: URLs, then this file is"              NL
"### irrelevant.)"                                                           NL
""                                                                           NL
"### Visit http://subversion.apache.org/ for more information."              NL
""                                                                           NL
"[general]"                                                                  NL
"### The anon-access and auth-access options control access to the"          NL
"### repository for unauthenticated (a.k.a. anonymous) users and"            NL
"### authenticated users, respectively."                                     NL
"### Valid values are \"write\", \"read\", and \"none\"."                    NL
"### Setting the value to \"none\" prohibits both reading and writing;"      NL
"### \"read\" allows read-only access, and \"write\" allows complete "       NL
"### read/write access to the repository."                                   NL
"### The sample settings below are the defaults and specify that anonymous"  NL
"### users have read-only access to the repository, while authenticated"     NL
"### users have read and write access to the repository."                    NL
"# anon-access = read"                                                       NL
"# auth-access = write"                                                      NL
"### The password-db option controls the location of the password"           NL
"### database file.  Unless you specify a path starting with a /,"           NL
"### the file's location is relative to the directory containing"            NL
"### this configuration file."                                               NL
"### If SASL is enabled (see below), this file will NOT be used."            NL
"### Uncomment the line below to use the default password file."             NL
"# password-db = passwd"                                                     NL
"### The authz-db option controls the location of the authorization"         NL
"### rules for path-based access control.  Unless you specify a path"        NL
"### starting with a /, the file's location is relative to the"              NL
"### directory containing this file.  The specified path may be a"           NL
"### repository relative URL (^/) or an absolute file:// URL to a text"      NL
"### file in a Subversion repository.  If you don't specify an authz-db,"    NL
"### no path-based access control is done."                                  NL
"### Uncomment the line below to use the default authorization file."        NL
"# authz-db = " SVN_REPOS__CONF_AUTHZ                                        NL
"### The groups-db option controls the location of the groups file."         NL
"### Unless you specify a path starting with a /, the file's location is"    NL
"### relative to the directory containing this file.  The specified path"    NL
"### may be a repository relative URL (^/) or an absolute file:// URL to a"  NL
"### text file in a Subversion repository."                                  NL
"# groups-db = " SVN_REPOS__CONF_GROUPS                                      NL
"### This option specifies the authentication realm of the repository."      NL
"### If two repositories have the same authentication realm, they should"    NL
"### have the same password database, and vice versa.  The default realm"    NL
"### is repository's uuid."                                                  NL
"# realm = My First Repository"                                              NL
"### The force-username-case option causes svnserve to case-normalize"       NL
"### usernames before comparing them against the authorization rules in the" NL
"### authz-db file configured above.  Valid values are \"upper\" (to upper-" NL
"### case the usernames), \"lower\" (to lowercase the usernames), and"       NL
"### \"none\" (to compare usernames as-is without case conversion, which"    NL
"### is the default behavior)."                                              NL
"# force-username-case = none"                                               NL
"### The hooks-env options specifies a path to the hook script environment " NL
"### configuration file. This option overrides the per-repository default"   NL
"### and can be used to configure the hook script environment for multiple " NL
"### repositories in a single file, if an absolute path is specified."       NL
"### Unless you specify an absolute path, the file's location is relative"   NL
"### to the directory containing this file."                                 NL
"# hooks-env = " SVN_REPOS__CONF_HOOKS_ENV                                   NL
""                                                                           NL
"[sasl]"                                                                     NL
"### This option specifies whether you want to use the Cyrus SASL"           NL
"### library for authentication. Default is false."                          NL
"### This section will be ignored if svnserve is not built with Cyrus"       NL
"### SASL support; to check, run 'svnserve --version' and look for a line"   NL
"### reading 'Cyrus SASL authentication is available.'"                      NL
"# use-sasl = true"                                                          NL
"### These options specify the desired strength of the security layer"       NL
"### that you want SASL to provide. 0 means no encryption, 1 means"          NL
"### integrity-checking only, values larger than 1 are correlated"           NL
"### to the effective key length for encryption (e.g. 128 means 128-bit"     NL
"### encryption). The values below are the defaults."                        NL
"# min-encryption = 0"                                                       NL
"# max-encryption = 256"                                                     NL;

    SVN_ERR_W(svn_io_file_create(svn_repos_svnserve_conf(repos, pool),
                                 svnserve_conf_contents, pool),
              _("Creating svnserve.conf file"));
  }

  {
    static const char * const passwd_contents =
"### This file is an example password file for svnserve."                    NL
"### Its format is similar to that of svnserve.conf. As shown in the"        NL
"### example below it contains one section labelled [users]."                NL
"### The name and password for each user follow, one account per line."      NL
""                                                                           NL
"[users]"                                                                    NL
"# harry = harryssecret"                                                     NL
"# sally = sallyssecret"                                                     NL;

    SVN_ERR_W(svn_io_file_create(svn_dirent_join(repos->conf_path,
                                                 SVN_REPOS__CONF_PASSWD,
                                                 pool),
                                 passwd_contents, pool),
              _("Creating passwd file"));
  }

  {
    static const char * const authz_contents =
"### This file is an example authorization file for svnserve."               NL
"### Its format is identical to that of mod_authz_svn authorization"         NL
"### files."                                                                 NL
"### As shown below each section defines authorizations for the path and"    NL
"### (optional) repository specified by the section name."                   NL
"### The authorizations follow. An authorization line can refer to:"         NL
"###  - a single user,"                                                      NL
"###  - a group of users defined in a special [groups] section,"             NL
"###  - an alias defined in a special [aliases] section,"                    NL
"###  - all authenticated users, using the '$authenticated' token,"          NL
"###  - only anonymous users, using the '$anonymous' token,"                 NL
"###  - anyone, using the '*' wildcard."                                     NL
"###"                                                                        NL
"### A match can be inverted by prefixing the rule with '~'. Rules can"      NL
"### grant read ('r') access, read-write ('rw') access, or no access"        NL
"### ('')."                                                                  NL
""                                                                           NL
"[aliases]"                                                                  NL
"# joe = /C=XZ/ST=Dessert/L=Snake City/O=Snake Oil, Ltd./OU=Research Institute/CN=Joe Average"  NL
""                                                                           NL
"[groups]"                                                                   NL
"# harry_and_sally = harry,sally"                                            NL
"# harry_sally_and_joe = harry,sally,&joe"                                   NL
""                                                                           NL
"# [/foo/bar]"                                                               NL
"# harry = rw"                                                               NL
"# &joe = r"                                                                 NL
"# * ="                                                                      NL
""                                                                           NL
"# [repository:/baz/fuz]"                                                    NL
"# @harry_and_sally = rw"                                                    NL
"# * = r"                                                                    NL;

    SVN_ERR_W(svn_io_file_create(svn_dirent_join(repos->conf_path,
                                                 SVN_REPOS__CONF_AUTHZ,
                                                 pool),
                                 authz_contents, pool),
              _("Creating authz file"));
  }

  {
    static const char * const hooks_env_contents =
"### This file is an example hook script environment configuration file."    NL
"### Hook scripts run in an empty environment by default."                   NL
"### As shown below each section defines environment variables for a"        NL
"### particular hook script. The [default] section defines environment"      NL
"### variables for all hook scripts, unless overridden by a hook-specific"   NL
"### section."                                                               NL
""                                                                           NL
"### This example configures a UTF-8 locale for all hook scripts, so that "  NL
"### special characters, such as umlauts, may be printed to stderr."         NL
"### If UTF-8 is used with a mod_dav_svn server, the SVNUseUTF8 option must" NL
"### also be set to 'yes' in httpd.conf."                                    NL
"### With svnserve, the LANG environment variable of the svnserve process"   NL
"### must be set to the same value as given here."                           NL
"[default]"                                                                  NL
"LANG = en_US.UTF-8"                                                         NL
""                                                                           NL
"### This sets the PATH environment variable for the pre-commit hook."       NL
"[pre-commit]"                                                               NL
"PATH = /usr/local/bin:/usr/bin:/usr/sbin"                                   NL;

    SVN_ERR_W(svn_io_file_create(svn_dirent_join(repos->conf_path,
                                                 SVN_REPOS__CONF_HOOKS_ENV \
                                                 SVN_REPOS__HOOK_DESC_EXT,
                                                 pool),
                                 hooks_env_contents, pool),
              _("Creating hooks-env file"));
  }

  return SVN_NO_ERROR;
}

svn_error_t *
svn_repos_hooks_setenv(svn_repos_t *repos,
                       const char *hooks_env_path,
                       apr_pool_t *scratch_pool)
{
  if (hooks_env_path == NULL)
    repos->hooks_env_path = svn_dirent_join(repos->conf_path,
                                            SVN_REPOS__CONF_HOOKS_ENV,
                                            repos->pool);
  else if (!svn_dirent_is_absolute(hooks_env_path))
    repos->hooks_env_path = svn_dirent_join(repos->conf_path,
                                            hooks_env_path,
                                            repos->pool);
  else
    repos->hooks_env_path = apr_pstrdup(repos->pool, hooks_env_path);

  return SVN_NO_ERROR;
}

/* Allocate and return a new svn_repos_t * object, initializing the
   directory pathname members based on PATH, and initializing the
   REPOSITORY_CAPABILITIES member.
   The members FS, FORMAT, and FS_TYPE are *not* initialized (they are null),
   and it is the caller's responsibility to fill them in if needed.  */
static svn_repos_t *
create_svn_repos_t(const char *path, apr_pool_t *pool)
{
  svn_repos_t *repos = apr_pcalloc(pool, sizeof(*repos));

  repos->path = apr_pstrdup(pool, path);
  repos->db_path = svn_dirent_join(path, SVN_REPOS__DB_DIR, pool);
  repos->conf_path = svn_dirent_join(path, SVN_REPOS__CONF_DIR, pool);
  repos->hook_path = svn_dirent_join(path, SVN_REPOS__HOOK_DIR, pool);
  repos->lock_path = svn_dirent_join(path, SVN_REPOS__LOCK_DIR, pool);
  repos->hooks_env_path = NULL;
  repos->repository_capabilities = apr_hash_make(pool);
  repos->pool = pool;

  return repos;
}


static svn_error_t *
create_repos_structure(svn_repos_t *repos,
                       const char *path,
                       apr_hash_t *fs_config,
                       apr_pool_t *pool)
{
  /* Create the top-level repository directory. */
  SVN_ERR_W(create_repos_dir(path, pool),
            _("Could not create top-level directory"));

  /* Create the DAV sandbox directory if pre-1.4 or pre-1.5-compatible. */
  if (fs_config
      && (svn_hash_gets(fs_config, SVN_FS_CONFIG_PRE_1_4_COMPATIBLE)
          || svn_hash_gets(fs_config, SVN_FS_CONFIG_PRE_1_5_COMPATIBLE)))
    {
      const char *dav_path = svn_dirent_join(repos->path,
                                             SVN_REPOS__DAV_DIR, pool);
      SVN_ERR_W(create_repos_dir(dav_path, pool),
                _("Creating DAV sandbox dir"));
    }

  /* Create the lock directory.  */
  SVN_ERR(create_locks(repos, pool));

  /* Create the hooks directory.  */
  SVN_ERR(create_hooks(repos, pool));

  /* Create the conf directory.  */
  SVN_ERR(create_conf(repos, pool));

  /* Write the top-level README file. */
  {
    const char * const readme_header =
      "This is a Subversion repository; use the 'svnadmin' and 'svnlook' "   NL
      "tools to examine it.  Do not add, delete, or modify files here "      NL
      "unless you know how to avoid corrupting the repository."              NL
      ""                                                                     NL;
    const char * const readme_bdb_insert =
      "The directory \"" SVN_REPOS__DB_DIR "\" contains a Berkeley DB environment."  NL
      "you may need to tweak the values in \"" SVN_REPOS__DB_DIR "/DB_CONFIG\" to match the"  NL
      "requirements of your site."                                           NL
      ""                                                                     NL;
    const char * const readme_footer =
      "Visit http://subversion.apache.org/ for more information."            NL;
    apr_file_t *f;
    apr_size_t written;

    SVN_ERR(svn_io_file_open(&f,
                             svn_dirent_join(path, SVN_REPOS__README, pool),
                             (APR_WRITE | APR_CREATE | APR_EXCL),
                             APR_OS_DEFAULT, pool));

    SVN_ERR(svn_io_file_write_full(f, readme_header, strlen(readme_header),
                                   &written, pool));
    if (strcmp(repos->fs_type, SVN_FS_TYPE_BDB) == 0)
      SVN_ERR(svn_io_file_write_full(f, readme_bdb_insert,
                                     strlen(readme_bdb_insert),
                                     &written, pool));
    SVN_ERR(svn_io_file_write_full(f, readme_footer, strlen(readme_footer),
                                   &written, pool));

    return svn_io_file_close(f, pool);
  }
}


/* There is, at present, nothing within the direct responsibility
   of libsvn_repos which requires locking.  For historical compatibility
   reasons, the BDB libsvn_fs backend does not do its own locking, expecting
   libsvn_repos to do the locking for it.  Here we take care of that
   backend-specific requirement.
   The kind of lock is controlled by EXCLUSIVE and NONBLOCKING.
   The lock is scoped to POOL.  */
static svn_error_t *
lock_repos(svn_repos_t *repos,
           svn_boolean_t exclusive,
           svn_boolean_t nonblocking,
           apr_pool_t *pool)
{
  if (strcmp(repos->fs_type, SVN_FS_TYPE_BDB) == 0)
    {
      svn_error_t *err;
      const char *lockfile_path = svn_repos_db_lockfile(repos, pool);

      err = svn_io_file_lock2(lockfile_path, exclusive, nonblocking, pool);
      if (err != NULL && APR_STATUS_IS_EAGAIN(err->apr_err))
        return svn_error_trace(err);
      SVN_ERR_W(err, _("Error opening db lockfile"));
    }
  return SVN_NO_ERROR;
}


svn_error_t *
svn_repos_create(svn_repos_t **repos_p,
                 const char *path,
                 const char *unused_1,
                 const char *unused_2,
                 apr_hash_t *config,
                 apr_hash_t *fs_config,
                 apr_pool_t *pool)
{
  svn_repos_t *repos;
  svn_error_t *err;
  const char *root_path;
  const char *local_abspath;

  /* Allocate a repository object, filling in the format we will create. */
  repos = create_svn_repos_t(path, pool);
  repos->format = SVN_REPOS__FORMAT_NUMBER;

  /* Discover the type of the filesystem we are about to create. */
  repos->fs_type = svn_hash__get_cstring(fs_config, SVN_FS_CONFIG_FS_TYPE,
                                         DEFAULT_FS_TYPE);
  if (svn_hash__get_bool(fs_config, SVN_FS_CONFIG_PRE_1_4_COMPATIBLE, FALSE))
    repos->format = SVN_REPOS__FORMAT_NUMBER_LEGACY;

  /* Don't create a repository inside another repository. */
  SVN_ERR(svn_dirent_get_absolute(&local_abspath, path, pool));
  root_path = svn_repos_find_root_path(local_abspath, pool);
  if (root_path != NULL)
    {
      if (strcmp(root_path, local_abspath) == 0)
        return svn_error_createf(SVN_ERR_REPOS_BAD_ARGS, NULL,
                                 _("'%s' is an existing repository"),
                                 svn_dirent_local_style(root_path, pool));
      else
        return svn_error_createf(SVN_ERR_REPOS_BAD_ARGS, NULL,
                                 _("'%s' is a subdirectory of an existing "
                                   "repository " "rooted at '%s'"),
                                 svn_dirent_local_style(local_abspath, pool),
                                 svn_dirent_local_style(root_path, pool));
    }

  /* Create the various files and subdirectories for the repository. */
  SVN_ERR_W(create_repos_structure(repos, path, fs_config, pool),
            _("Repository creation failed"));

  /* Lock if needed. */
  SVN_ERR(lock_repos(repos, FALSE, FALSE, pool));

  /* Create an environment for the filesystem. */
  if ((err = svn_fs_create(&repos->fs, repos->db_path, fs_config, pool)))
    {
      /* If there was an error making the filesytem, e.g. unknown/supported
       * filesystem type.  Clean up after ourselves.  Yes this is safe because
       * create_repos_structure will fail if the path existed before we started
       * so we can't accidentally remove a directory that previously existed.
       */

      return svn_error_trace(
                   svn_error_compose_create(
                        err,
                        svn_io_remove_dir2(path, FALSE, NULL, NULL, pool)));
    }

  /* This repository is ready.  Stamp it with a format number. */
  SVN_ERR(svn_io_write_version_file
          (svn_dirent_join(path, SVN_REPOS__FORMAT, pool),
           repos->format, pool));

  *repos_p = repos;
  return SVN_NO_ERROR;
}


/* Check if @a path is the root of a repository by checking if the
 * path contains the expected files and directories.  Return TRUE
 * on errors (which would be permission errors, probably) so that
 * we the user will see them after we try to open the repository
 * for real.  */
static svn_boolean_t
check_repos_path(const char *path,
                 apr_pool_t *pool)
{
  svn_node_kind_t kind;
  svn_error_t *err;

  err = svn_io_check_path(svn_dirent_join(path, SVN_REPOS__FORMAT, pool),
                          &kind, pool);
  if (err)
    {
      svn_error_clear(err);
      return TRUE;
    }
  if (kind != svn_node_file)
    return FALSE;

  /* Check the db/ subdir, but allow it to be a symlink (Subversion
     works just fine if it's a symlink). */
  err = svn_io_check_resolved_path
    (svn_dirent_join(path, SVN_REPOS__DB_DIR, pool), &kind, pool);
  if (err)
    {
      svn_error_clear(err);
      return TRUE;
    }
  if (kind != svn_node_dir)
    return FALSE;

  return TRUE;
}


/* Verify that REPOS's format is suitable.
   Use POOL for temporary allocation. */
static svn_error_t *
check_repos_format(svn_repos_t *repos,
                   apr_pool_t *pool)
{
  int format;
  const char *format_path;

  format_path = svn_dirent_join(repos->path, SVN_REPOS__FORMAT, pool);
  SVN_ERR(svn_io_read_version_file(&format, format_path, pool));

  if (format != SVN_REPOS__FORMAT_NUMBER &&
      format != SVN_REPOS__FORMAT_NUMBER_LEGACY)
    {
      return svn_error_createf
        (SVN_ERR_REPOS_UNSUPPORTED_VERSION, NULL,
         _("Expected repository format '%d' or '%d'; found format '%d'"),
         SVN_REPOS__FORMAT_NUMBER_LEGACY, SVN_REPOS__FORMAT_NUMBER,
         format);
    }

  repos->format = format;

  return SVN_NO_ERROR;
}


/* Set *REPOS_P to a repository at PATH which has been opened.
   See lock_repos() above regarding EXCLUSIVE and NONBLOCKING.
   OPEN_FS indicates whether the Subversion filesystem should be opened,
   the handle being placed into repos->fs.
   Do all allocation in POOL.  */
static svn_error_t *
get_repos(svn_repos_t **repos_p,
          const char *path,
          svn_boolean_t exclusive,
          svn_boolean_t nonblocking,
          svn_boolean_t open_fs,
          apr_hash_t *fs_config,
          apr_pool_t *pool)
{
  svn_repos_t *repos;

  /* Allocate a repository object. */
  repos = create_svn_repos_t(path, pool);

  /* Verify the validity of our repository format. */
  SVN_ERR(check_repos_format(repos, pool));

  /* Discover the FS type. */
  SVN_ERR(svn_fs_type(&repos->fs_type, repos->db_path, pool));

  /* Lock if needed. */
  SVN_ERR(lock_repos(repos, exclusive, nonblocking, pool));

  /* Open up the filesystem only after obtaining the lock. */
  if (open_fs)
    SVN_ERR(svn_fs_open(&repos->fs, repos->db_path, fs_config, pool));

#ifdef SVN_DEBUG_CRASH_AT_REPOS_OPEN
  /* If $PATH/config/debug-abort exists, crash the server here.
     This debugging feature can be used to test client recovery
     when the server crashes.

     See: Issue #4274 */
  {
    svn_node_kind_t kind;
    svn_error_t *err = svn_io_check_path(
        svn_dirent_join(repos->conf_path, "debug-abort", pool),
        &kind, pool);
    svn_error_clear(err);
    if (!err && kind == svn_node_file)
      SVN_ERR_MALFUNCTION_NO_RETURN();
  }
#endif /* SVN_DEBUG_CRASH_AT_REPOS_OPEN */

  *repos_p = repos;
  return SVN_NO_ERROR;
}



const char *
svn_repos_find_root_path(const char *path,
                         apr_pool_t *pool)
{
  const char *candidate = path;
  const char *decoded;
  svn_error_t *err;

  while (1)
    {
      /* Try to decode the path, so we don't fail if it contains characters
         that aren't supported by the OS filesystem.  The subversion fs
         isn't restricted by the OS filesystem character set. */
      err = svn_path_cstring_from_utf8(&decoded, candidate, pool);
      if (!err && check_repos_path(candidate, pool))
        break;
      svn_error_clear(err);

      if (svn_path_is_empty(candidate) ||
          svn_dirent_is_root(candidate, strlen(candidate)))
        return NULL;

      candidate = svn_dirent_dirname(candidate, pool);
    }

  return candidate;
}


svn_error_t *
svn_repos_open2(svn_repos_t **repos_p,
                const char *path,
                apr_hash_t *fs_config,
                apr_pool_t *pool)
{
  /* Fetch a repository object initialized with a shared read/write
     lock on the database. */

  return get_repos(repos_p, path, FALSE, FALSE, TRUE, fs_config, pool);
}


svn_error_t *
svn_repos_upgrade2(const char *path,
                   svn_boolean_t nonblocking,
                   svn_repos_notify_func_t notify_func,
                   void *notify_baton,
                   apr_pool_t *pool)
{
  svn_repos_t *repos;
  const char *format_path;
  int format;
  apr_pool_t *subpool = svn_pool_create(pool);

  /* Fetch a repository object; for the Berkeley DB backend, it is
     initialized with an EXCLUSIVE lock on the database.  This will at
     least prevent others from trying to read or write to it while we
     run recovery. (Other backends should do their own locking; see
     lock_repos.) */
  SVN_ERR(get_repos(&repos, path, TRUE, nonblocking, FALSE, NULL, subpool));

  if (notify_func)
    {
      /* We notify *twice* here, because there are two different logistical
         actions occuring. */
      svn_repos_notify_t *notify = svn_repos_notify_create(
                                    svn_repos_notify_mutex_acquired, subpool);
      notify_func(notify_baton, notify, subpool);

      notify->action = svn_repos_notify_upgrade_start;
      notify_func(notify_baton, notify, subpool);
    }

  /* Try to overwrite with its own contents.  We do this only to
     verify that we can, because we don't want to actually bump the
     format of the repository until our underlying filesystem claims
     to have been upgraded correctly. */
  format_path = svn_dirent_join(repos->path, SVN_REPOS__FORMAT, subpool);
  SVN_ERR(svn_io_read_version_file(&format, format_path, subpool));
  SVN_ERR(svn_io_write_version_file(format_path, format, subpool));

  /* Try to upgrade the filesystem. */
  SVN_ERR(svn_fs_upgrade(repos->db_path, subpool));

  /* Now overwrite our format file with the latest version. */
  SVN_ERR(svn_io_write_version_file(format_path, SVN_REPOS__FORMAT_NUMBER,
                                    subpool));

  /* Close shop and free the subpool, to release the exclusive lock. */
  svn_pool_destroy(subpool);

  return SVN_NO_ERROR;
}


svn_error_t *
svn_repos_delete(const char *path,
                 apr_pool_t *pool)
{
  const char *db_path = svn_dirent_join(path, SVN_REPOS__DB_DIR, pool);

  /* Delete the filesystem environment... */
  SVN_ERR(svn_fs_delete_fs(db_path, pool));

  /* ...then blow away everything else.  */
  return svn_io_remove_dir2(path, FALSE, NULL, NULL, pool);
}


/* Repository supports the capability. */
static const char *capability_yes = "yes";
/* Repository does not support the capability. */
static const char *capability_no = "no";

svn_error_t *
svn_repos_has_capability(svn_repos_t *repos,
                         svn_boolean_t *has,
                         const char *capability,
                         apr_pool_t *pool)
{
  const char *val = svn_hash_gets(repos->repository_capabilities, capability);

  if (val == capability_yes)
    {
      *has = TRUE;
    }
  else if (val == capability_no)
    {
      *has = FALSE;
    }
  /* Else don't know, so investigate. */
  else if (strcmp(capability, SVN_REPOS_CAPABILITY_MERGEINFO) == 0)
    {
      svn_error_t *err;
      svn_fs_root_t *root;
      svn_mergeinfo_catalog_t ignored;
      apr_array_header_t *paths = apr_array_make(pool, 1,
                                                 sizeof(char *));

      SVN_ERR(svn_fs_revision_root(&root, repos->fs, 0, pool));
      APR_ARRAY_PUSH(paths, const char *) = "";
      err = svn_fs_get_mergeinfo2(&ignored, root, paths, FALSE, FALSE,
                                  TRUE, pool, pool);

      if (err)
        {
          if (err->apr_err == SVN_ERR_UNSUPPORTED_FEATURE)
            {
              svn_error_clear(err);
              svn_hash_sets(repos->repository_capabilities,
                            SVN_REPOS_CAPABILITY_MERGEINFO, capability_no);
              *has = FALSE;
            }
          else if (err->apr_err == SVN_ERR_FS_NOT_FOUND)
            {
              /* Mergeinfo requests use relative paths, and anyway we're
                 in r0, so we're likely to get this error -- but it
                 means the repository supports mergeinfo! */
              svn_error_clear(err);
              svn_hash_sets(repos->repository_capabilities,
                            SVN_REPOS_CAPABILITY_MERGEINFO, capability_yes);
              *has = TRUE;
            }
          else
            {
              return svn_error_trace(err);
            }
        }
      else
        {
          svn_hash_sets(repos->repository_capabilities,
                        SVN_REPOS_CAPABILITY_MERGEINFO, capability_yes);
          *has = TRUE;
        }
    }
  else
    {
      return svn_error_createf(SVN_ERR_UNKNOWN_CAPABILITY, 0,
                               _("unknown capability '%s'"), capability);
    }

  return SVN_NO_ERROR;
}

svn_fs_t *
svn_repos_fs(svn_repos_t *repos)
{
  if (! repos)
    return NULL;
  return repos->fs;
}


/* For historical reasons, for the Berkeley DB backend, this code uses
 * repository locking, which is motivated by the need to support the
 * Berkeley DB error DB_RUN_RECOVERY.  (FSFS takes care of locking
 * itself, inside its implementation of svn_fs_recover.)  Here's how
 * it works:
 *
 * Every accessor of a repository's database takes out a shared lock
 * on the repository -- both readers and writers get shared locks, and
 * there can be an unlimited number of shared locks simultaneously.
 *
 * Sometimes, a db access returns the error DB_RUN_RECOVERY.  When
 * this happens, we need to run svn_fs_recover() on the db
 * with no other accessors present.  So we take out an exclusive lock
 * on the repository.  From the moment we request the exclusive lock,
 * no more shared locks are granted, and when the last shared lock
 * disappears, the exclusive lock is granted.  As soon as we get it,
 * we can run recovery.
 *
 * We assume that once any berkeley call returns DB_RUN_RECOVERY, they
 * all do, until recovery is run.
 */

svn_error_t *
svn_repos_recover4(const char *path,
                   svn_boolean_t nonblocking,
                   svn_repos_notify_func_t notify_func,
                   void *notify_baton,
                   svn_cancel_func_t cancel_func,
                   void * cancel_baton,
                   apr_pool_t *pool)
{
  svn_repos_t *repos;
  apr_pool_t *subpool = svn_pool_create(pool);

  /* Fetch a repository object; for the Berkeley DB backend, it is
     initialized with an EXCLUSIVE lock on the database.  This will at
     least prevent others from trying to read or write to it while we
     run recovery. (Other backends should do their own locking; see
     lock_repos.) */
  SVN_ERR(get_repos(&repos, path, TRUE, nonblocking,
                    FALSE,    /* don't try to open the db yet. */
                    NULL,
                    subpool));

  if (notify_func)
    {
      /* We notify *twice* here, because there are two different logistical
         actions occuring. */
      svn_repos_notify_t *notify = svn_repos_notify_create(
                                    svn_repos_notify_mutex_acquired, subpool);
      notify_func(notify_baton, notify, subpool);

      notify->action = svn_repos_notify_recover_start;
      notify_func(notify_baton, notify, subpool);
    }

  /* Recover the database to a consistent state. */
  SVN_ERR(svn_fs_recover(repos->db_path, cancel_func, cancel_baton, subpool));

  /* Close shop and free the subpool, to release the exclusive lock. */
  svn_pool_destroy(subpool);

  return SVN_NO_ERROR;
}

struct freeze_baton_t {
  apr_array_header_t *paths;
  int counter;
  svn_repos_freeze_func_t freeze_func;
  void *freeze_baton;
};

static svn_error_t *
multi_freeze(void *baton,
             apr_pool_t *pool)
{
  struct freeze_baton_t *fb = baton;

  if (fb->counter == fb->paths->nelts)
    {
      SVN_ERR(fb->freeze_func(fb->freeze_baton, pool));
      return SVN_NO_ERROR;
    }
  else
    {
      /* Using a subpool as the only way to unlock the repos lock used
         by BDB is to clear the pool used to take the lock. */
      apr_pool_t *subpool = svn_pool_create(pool);
      const char *path = APR_ARRAY_IDX(fb->paths, fb->counter, const char *);
      svn_repos_t *repos;

      ++fb->counter;

      SVN_ERR(get_repos(&repos, path,
                        TRUE  /* exclusive (only applies to BDB) */,
                        FALSE /* non-blocking */,
                        FALSE /* open-fs */,
                        NULL, subpool));


      if (strcmp(repos->fs_type, SVN_FS_TYPE_BDB) == 0)
        {
          svn_error_t *err = multi_freeze(fb, subpool);

          svn_pool_destroy(subpool);

          return err;
        }
      else
        {
          SVN_ERR(svn_fs_open(&repos->fs, repos->db_path, NULL, subpool));
          SVN_ERR(svn_fs_freeze(svn_repos_fs(repos), multi_freeze, fb,
                                subpool));
        }

      svn_pool_destroy(subpool);
    }

  return SVN_NO_ERROR;
}

/* For BDB we fall back on BDB's repos layer lock which means that the
   repository is unreadable while frozen.

   For FSFS we delegate to the FS layer which uses the FSFS write-lock
   and an SQLite reserved lock which means the repository is readable
   while frozen. */
svn_error_t *
svn_repos_freeze(apr_array_header_t *paths,
                 svn_repos_freeze_func_t freeze_func,
                 void *freeze_baton,
                 apr_pool_t *pool)
{
  struct freeze_baton_t fb;

  fb.paths = paths;
  fb.counter = 0;
  fb.freeze_func = freeze_func;
  fb.freeze_baton = freeze_baton;

  SVN_ERR(multi_freeze(&fb, pool));

  return SVN_NO_ERROR;
}

svn_error_t *svn_repos_db_logfiles(apr_array_header_t **logfiles,
                                   const char *path,
                                   svn_boolean_t only_unused,
                                   apr_pool_t *pool)
{
  svn_repos_t *repos;
  int i;

  SVN_ERR(get_repos(&repos, path,
                    FALSE, FALSE,
                    FALSE,     /* Do not open fs. */
                    NULL,
                    pool));

  SVN_ERR(svn_fs_berkeley_logfiles(logfiles,
                                   svn_repos_db_env(repos, pool),
                                   only_unused,
                                   pool));

  /* Loop, printing log files. */
  for (i = 0; i < (*logfiles)->nelts; i++)
    {
      const char ** log_file = &(APR_ARRAY_IDX(*logfiles, i, const char *));
      *log_file = svn_dirent_join(SVN_REPOS__DB_DIR, *log_file, pool);
    }

  return SVN_NO_ERROR;
}

/* Baton for hotcopy_structure(). */
struct hotcopy_ctx_t {
  const char *dest;     /* target location to construct */
  size_t src_len; /* len of the source path*/

  /* As in svn_repos_hotcopy2() */
  svn_boolean_t incremental;
  svn_cancel_func_t cancel_func;
  void *cancel_baton;
};

/* Copy the repository structure of PATH to BATON->DEST, with exception of
 * @c SVN_REPOS__DB_DIR, @c SVN_REPOS__LOCK_DIR and @c SVN_REPOS__FORMAT;
 * those directories and files are handled separately.
 *
 * BATON is a (struct hotcopy_ctx_t *).  BATON->SRC_LEN is the length
 * of PATH.
 *
 * Implements svn_io_walk_func_t.
 */
static svn_error_t *hotcopy_structure(void *baton,
                                      const char *path,
                                      const apr_finfo_t *finfo,
                                      apr_pool_t *pool)
{
  const struct hotcopy_ctx_t *ctx = baton;
  const char *sub_path;
  const char *target;

  if (ctx->cancel_func)
    SVN_ERR(ctx->cancel_func(ctx->cancel_baton));

  if (strlen(path) == ctx->src_len)
    {
      sub_path = "";
    }
  else
    {
      sub_path = &path[ctx->src_len+1];

      /* Check if we are inside db directory and if so skip it */
      if (svn_path_compare_paths
          (svn_dirent_get_longest_ancestor(SVN_REPOS__DB_DIR, sub_path, pool),
           SVN_REPOS__DB_DIR) == 0)
        return SVN_NO_ERROR;

      if (svn_path_compare_paths
          (svn_dirent_get_longest_ancestor(SVN_REPOS__LOCK_DIR, sub_path,
                                           pool),
           SVN_REPOS__LOCK_DIR) == 0)
        return SVN_NO_ERROR;

      if (svn_path_compare_paths
          (svn_dirent_get_longest_ancestor(SVN_REPOS__FORMAT, sub_path, pool),
           SVN_REPOS__FORMAT) == 0)
        return SVN_NO_ERROR;
    }

  target = svn_dirent_join(ctx->dest, sub_path, pool);

  if (finfo->filetype == APR_DIR)
    {
      svn_error_t *err;

      err = create_repos_dir(target, pool);
      if (ctx->incremental && err && err->apr_err == SVN_ERR_DIR_NOT_EMPTY)
        {
          svn_error_clear(err);
          err = SVN_NO_ERROR;
        }
      return svn_error_trace(err);
    }
  else if (finfo->filetype == APR_REG)
    return svn_io_copy_file(path, target, TRUE, pool);
  else if (finfo->filetype == APR_LNK)
    return svn_io_copy_link(path, target, pool);
  else
    return SVN_NO_ERROR;
}


/** Obtain a lock on db logs lock file. Create one if it does not exist.
 */
static svn_error_t *
lock_db_logs_file(svn_repos_t *repos,
                  svn_boolean_t exclusive,
                  apr_pool_t *pool)
{
  const char * lock_file = svn_repos_db_logs_lockfile(repos, pool);

  /* Try to create a lock file, in case if it is missing. As in case of the
     repositories created before hotcopy functionality.  */
  svn_error_clear(create_db_logs_lock(repos, pool));

  return svn_io_file_lock2(lock_file, exclusive, FALSE, pool);
}


/* Make a copy of a repository with hot backup of fs. */
svn_error_t *
svn_repos_hotcopy2(const char *src_path,
                   const char *dst_path,
                   svn_boolean_t clean_logs,
                   svn_boolean_t incremental,
                   svn_cancel_func_t cancel_func,
                   void *cancel_baton,
                   apr_pool_t *pool)
{
  svn_repos_t *src_repos;
  svn_repos_t *dst_repos;
  struct hotcopy_ctx_t hotcopy_context;
  svn_error_t *err;
  const char *src_abspath;
  const char *dst_abspath;

  SVN_ERR(svn_dirent_get_absolute(&src_abspath, src_path, pool));
  SVN_ERR(svn_dirent_get_absolute(&dst_abspath, dst_path, pool));
  if (strcmp(src_abspath, dst_abspath) == 0)
    return svn_error_create(SVN_ERR_INCORRECT_PARAMS, NULL,
                             _("Hotcopy source and destination are equal"));

  /* Try to open original repository */
  SVN_ERR(get_repos(&src_repos, src_abspath,
                    FALSE, FALSE,
                    FALSE,    /* don't try to open the db yet. */
                    NULL,
                    pool));

  /* If we are going to clean logs, then get an exclusive lock on
     db-logs.lock, to ensure that no one else will work with logs.

     If we are just copying, then get a shared lock to ensure that
     no one else will clean logs while we copying them */

  SVN_ERR(lock_db_logs_file(src_repos, clean_logs, pool));

  /* Copy the repository to a new path, with exception of
     specially handled directories */

  hotcopy_context.dest = dst_abspath;
  hotcopy_context.src_len = strlen(src_abspath);
  hotcopy_context.incremental = incremental;
  hotcopy_context.cancel_func = cancel_func;
  hotcopy_context.cancel_baton = cancel_baton;
  SVN_ERR(svn_io_dir_walk2(src_abspath,
                           0,
                           hotcopy_structure,
                           &hotcopy_context,
                           pool));

  /* Prepare dst_repos object so that we may create locks,
     so that we may open repository */

  dst_repos = create_svn_repos_t(dst_abspath, pool);
  dst_repos->fs_type = src_repos->fs_type;
  dst_repos->format = src_repos->format;

  err = create_locks(dst_repos, pool);
  if (err)
    {
      if (incremental && err->apr_err == SVN_ERR_DIR_NOT_EMPTY)
        svn_error_clear(err);
      else
        return svn_error_trace(err);
    }

  err = svn_io_dir_make_sgid(dst_repos->db_path, APR_OS_DEFAULT, pool);
  if (err)
    {
      if (incremental && APR_STATUS_IS_EEXIST(err->apr_err))
        svn_error_clear(err);
      else
        return svn_error_trace(err);
    }

  /* Exclusively lock the new repository.
     No one should be accessing it at the moment */
  SVN_ERR(lock_repos(dst_repos, TRUE, FALSE, pool));

  SVN_ERR(svn_fs_hotcopy2(src_repos->db_path, dst_repos->db_path,
                          clean_logs, incremental,
                          cancel_func, cancel_baton, pool));

  /* Destination repository is ready.  Stamp it with a format number. */
  return svn_io_write_version_file
          (svn_dirent_join(dst_repos->path, SVN_REPOS__FORMAT, pool),
           dst_repos->format, pool);
}

svn_error_t *
svn_repos_hotcopy(const char *src_path,
                  const char *dst_path,
                  svn_boolean_t clean_logs,
                  apr_pool_t *pool)
{
  return svn_error_trace(svn_repos_hotcopy2(src_path, dst_path, clean_logs,
                                            FALSE, NULL, NULL, pool));
}

/* Return the library version number. */
const svn_version_t *
svn_repos_version(void)
{
  SVN_VERSION_BODY;
}



svn_error_t *
svn_repos_stat(svn_dirent_t **dirent,
               svn_fs_root_t *root,
               const char *path,
               apr_pool_t *pool)
{
  svn_node_kind_t kind;
  svn_dirent_t *ent;
  const char *datestring;
  apr_hash_t *prophash;

  SVN_ERR(svn_fs_check_path(&kind, root, path, pool));

  if (kind == svn_node_none)
    {
      *dirent = NULL;
      return SVN_NO_ERROR;
    }

  ent = svn_dirent_create(pool);
  ent->kind = kind;

  if (kind == svn_node_file)
    SVN_ERR(svn_fs_file_length(&(ent->size), root, path, pool));

  SVN_ERR(svn_fs_node_proplist(&prophash, root, path, pool));
  if (apr_hash_count(prophash) > 0)
    ent->has_props = TRUE;

  SVN_ERR(svn_repos_get_committed_info(&(ent->created_rev),
                                       &datestring,
                                       &(ent->last_author),
                                       root, path, pool));
  if (datestring)
    SVN_ERR(svn_time_from_cstring(&(ent->time), datestring, pool));

  *dirent = ent;
  return SVN_NO_ERROR;
}

svn_error_t *
svn_repos_remember_client_capabilities(svn_repos_t *repos,
                                       const apr_array_header_t *capabilities)
{
  repos->client_capabilities = capabilities;
  return SVN_NO_ERROR;
}

svn_error_t *
svn_repos__fs_type(const char **fs_type,
                   const char *repos_path,
                   apr_pool_t *pool)
{
  svn_repos_t repos;
  repos.path = (char*)repos_path;

  SVN_ERR(check_repos_format(&repos, pool));

  return svn_fs_type(fs_type,
                     svn_dirent_join(repos_path, SVN_REPOS__DB_DIR, pool),
                     pool);
}