Change the format of the revprops block sent in svnserve for
[svn.git] / subversion / libsvn_repos / repos.c
blobbb41b46d2f1fdeb1278499585ef7390d3d3b2efb
1 /* repos.c : repository creation; shared and exclusive repository locking
3 * ====================================================================
4 * Copyright (c) 2000-2007 CollabNet. All rights reserved.
6 * This software is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution. The terms
8 * are also available at http://subversion.tigris.org/license-1.html.
9 * If newer versions of this license are posted there, you may use a
10 * newer version instead, at your option.
12 * This software consists of voluntary contributions made by many
13 * individuals. For exact contribution history, see the revision
14 * history and logs, available at http://subversion.tigris.org/.
15 * ====================================================================
18 #include <assert.h>
20 #include <apr_pools.h>
21 #include <apr_file_io.h>
23 #include "svn_pools.h"
24 #include "svn_error.h"
25 #include "svn_path.h"
26 #include "svn_utf.h"
27 #include "svn_time.h"
28 #include "svn_fs.h"
29 #include "svn_ra.h" /* for SVN_RA_CAPABILITY_* */
30 #include "svn_repos.h"
31 #include "svn_private_config.h" /* for SVN_TEMPLATE_ROOT_DIR */
33 #include "repos.h"
35 /* Used to terminate lines in large multi-line string literals. */
36 #define NL APR_EOL_STR
39 /* Path accessor functions. */
42 const char *
43 svn_repos_path(svn_repos_t *repos, apr_pool_t *pool)
45 return apr_pstrdup(pool, repos->path);
49 const char *
50 svn_repos_db_env(svn_repos_t *repos, apr_pool_t *pool)
52 return apr_pstrdup(pool, repos->db_path);
56 const char *
57 svn_repos_conf_dir(svn_repos_t *repos, apr_pool_t *pool)
59 return apr_pstrdup(pool, repos->conf_path);
63 const char *
64 svn_repos_svnserve_conf(svn_repos_t *repos, apr_pool_t *pool)
66 return svn_path_join(repos->conf_path, SVN_REPOS__CONF_SVNSERVE_CONF, pool);
70 const char *
71 svn_repos_lock_dir(svn_repos_t *repos, apr_pool_t *pool)
73 return apr_pstrdup(pool, repos->lock_path);
77 const char *
78 svn_repos_db_lockfile(svn_repos_t *repos, apr_pool_t *pool)
80 return svn_path_join(repos->lock_path, SVN_REPOS__DB_LOCKFILE, pool);
84 const char *
85 svn_repos_db_logs_lockfile(svn_repos_t *repos, apr_pool_t *pool)
87 return svn_path_join(repos->lock_path, SVN_REPOS__DB_LOGS_LOCKFILE, pool);
90 const char *
91 svn_repos_hook_dir(svn_repos_t *repos, apr_pool_t *pool)
93 return apr_pstrdup(pool, repos->hook_path);
97 const char *
98 svn_repos_start_commit_hook(svn_repos_t *repos, apr_pool_t *pool)
100 return svn_path_join(repos->hook_path, SVN_REPOS__HOOK_START_COMMIT, pool);
104 const char *
105 svn_repos_pre_commit_hook(svn_repos_t *repos, apr_pool_t *pool)
107 return svn_path_join(repos->hook_path, SVN_REPOS__HOOK_PRE_COMMIT, pool);
111 const char *
112 svn_repos_pre_lock_hook(svn_repos_t *repos, apr_pool_t *pool)
114 return svn_path_join(repos->hook_path, SVN_REPOS__HOOK_PRE_LOCK, pool);
118 const char *
119 svn_repos_pre_unlock_hook(svn_repos_t *repos, apr_pool_t *pool)
121 return svn_path_join(repos->hook_path, SVN_REPOS__HOOK_PRE_UNLOCK, pool);
124 const char *
125 svn_repos_post_lock_hook(svn_repos_t *repos, apr_pool_t *pool)
127 return svn_path_join(repos->hook_path, SVN_REPOS__HOOK_POST_LOCK, pool);
131 const char *
132 svn_repos_post_unlock_hook(svn_repos_t *repos, apr_pool_t *pool)
134 return svn_path_join(repos->hook_path, SVN_REPOS__HOOK_POST_UNLOCK, pool);
138 const char *
139 svn_repos_post_commit_hook(svn_repos_t *repos, apr_pool_t *pool)
141 return svn_path_join(repos->hook_path, SVN_REPOS__HOOK_POST_COMMIT, pool);
145 const char *
146 svn_repos_pre_revprop_change_hook(svn_repos_t *repos, apr_pool_t *pool)
148 return svn_path_join(repos->hook_path, SVN_REPOS__HOOK_PRE_REVPROP_CHANGE,
149 pool);
153 const char *
154 svn_repos_post_revprop_change_hook(svn_repos_t *repos, apr_pool_t *pool)
156 return svn_path_join(repos->hook_path, SVN_REPOS__HOOK_POST_REVPROP_CHANGE,
157 pool);
161 static svn_error_t *
162 create_repos_dir(const char *path, apr_pool_t *pool)
164 svn_error_t *err;
166 err = svn_io_dir_make(path, APR_OS_DEFAULT, pool);
167 if (err && (APR_STATUS_IS_EEXIST(err->apr_err)))
169 svn_boolean_t is_empty;
171 svn_error_clear(err);
173 SVN_ERR(svn_io_dir_empty(&is_empty, path, pool));
175 if (is_empty)
176 err = NULL;
177 else
178 err = svn_error_createf(SVN_ERR_DIR_NOT_EMPTY, 0,
179 _("'%s' exists and is non-empty"),
180 svn_path_local_style(path, pool));
183 return err;
186 static const char * bdb_lock_file_contents =
187 "DB lock file, representing locks on the versioned filesystem." NL
188 "" NL
189 "All accessors -- both readers and writers -- of the repository's" NL
190 "Berkeley DB environment take out shared locks on this file, and" NL
191 "each accessor removes its lock when done. If and when the DB" NL
192 "recovery procedure is run, the recovery code takes out an" NL
193 "exclusive lock on this file, so we can be sure no one else is" NL
194 "using the DB during the recovery." NL
195 "" NL
196 "You should never have to edit or remove this file." NL;
198 static const char * bdb_logs_lock_file_contents =
199 "DB logs lock file, representing locks on the versioned filesystem logs." NL
200 "" NL
201 "All log manipulators of the repository's Berkeley DB environment" NL
202 "take out exclusive locks on this file to ensure that only one" NL
203 "accessor manipulates the logs at a time." NL
204 "" NL
205 "You should never have to edit or remove this file." NL;
207 static const char * pre12_compat_unneeded_file_contents =
208 "This file is not used by Subversion 1.3.x or later." NL
209 "However, its existence is required for compatibility with" NL
210 "Subversion 1.2.x or earlier." NL;
212 /* Create the DB logs lockfile. */
213 static svn_error_t *
214 create_db_logs_lock(svn_repos_t *repos, apr_pool_t *pool) {
215 const char *contents;
216 const char *lockfile_path;
218 lockfile_path = svn_repos_db_logs_lockfile(repos, pool);
219 if (strcmp(repos->fs_type, SVN_FS_TYPE_BDB) == 0)
220 contents = bdb_logs_lock_file_contents;
221 else
222 contents = pre12_compat_unneeded_file_contents;
224 SVN_ERR_W(svn_io_file_create(lockfile_path, contents, pool),
225 _("Creating db logs lock file"));
227 return SVN_NO_ERROR;
230 /* Create the DB lockfile. */
231 static svn_error_t *
232 create_db_lock(svn_repos_t *repos, apr_pool_t *pool) {
233 const char *contents;
234 const char *lockfile_path;
236 lockfile_path = svn_repos_db_lockfile(repos, pool);
237 if (strcmp(repos->fs_type, SVN_FS_TYPE_BDB) == 0)
238 contents = bdb_lock_file_contents;
239 else
240 contents = pre12_compat_unneeded_file_contents;
242 SVN_ERR_W(svn_io_file_create(lockfile_path, contents, pool),
243 _("Creating db lock file"));
245 return SVN_NO_ERROR;
248 static svn_error_t *
249 create_locks(svn_repos_t *repos, apr_pool_t *pool)
251 /* Create the locks directory. */
252 SVN_ERR_W(create_repos_dir(repos->lock_path, pool),
253 _("Creating lock dir"));
255 SVN_ERR(create_db_lock(repos, pool));
256 SVN_ERR(create_db_logs_lock(repos, pool));
258 return SVN_NO_ERROR;
262 #define HOOKS_ENVIRONMENT_TEXT \
263 "# The hook program typically does not inherit the environment of" NL \
264 "# its parent process. For example, a common problem is for the" NL \
265 "# PATH environment variable to not be set to its usual value, so" NL \
266 "# that subprograms fail to launch unless invoked via absolute path." NL \
267 "# If you're having unexpected problems with a hook program, the" NL \
268 "# culprit may be unusual (or missing) environment variables." NL
270 #define PREWRITTEN_HOOKS_TEXT \
271 "# For more examples and pre-written hooks, see those in" NL \
272 "# the Subversion repository at" NL \
273 "# http://svn.collab.net/repos/svn/trunk/tools/hook-scripts/ and" NL \
274 "# http://svn.collab.net/repos/svn/trunk/contrib/hook-scripts/" NL
277 static svn_error_t *
278 create_hooks(svn_repos_t *repos, apr_pool_t *pool)
280 const char *this_path, *contents;
282 /* Create the hook directory. */
283 SVN_ERR_W(create_repos_dir(repos->hook_path, pool),
284 _("Creating hook directory"));
286 /*** Write a default template for each standard hook file. */
288 /* Start-commit hook. */
290 this_path = apr_psprintf(pool, "%s%s",
291 svn_repos_start_commit_hook(repos, pool),
292 SVN_REPOS__HOOK_DESC_EXT);
294 #define SCRIPT_NAME SVN_REPOS__HOOK_START_COMMIT
296 contents =
297 "#!/bin/sh" NL
298 "" NL
299 "# START-COMMIT HOOK" NL
300 "#" NL
301 "# The start-commit hook is invoked before a Subversion txn is created" NL
302 "# in the process of doing a commit. Subversion runs this hook" NL
303 "# by invoking a program (script, executable, binary, etc.) named" NL
304 "# '"SCRIPT_NAME"' (for which this file is a template)" NL
305 "# with the following ordered arguments:" NL
306 "#" NL
307 "# [1] REPOS-PATH (the path to this repository)" NL
308 "# [2] USER (the authenticated user attempting to commit)" NL
309 "# [3] CAPABILITIES (a colon-separated list of capabilities reported" NL
310 "# by the client; see note below)" NL
311 "#" NL
312 "# Note: The CAPABILITIES parameter is new in Subversion 1.5, and 1.5" NL
313 "# clients will typically report at least the \"" \
314 SVN_RA_CAPABILITY_MERGEINFO "\" capability." NL
315 "# If there are other capabilities, then the list is colon-separated," NL
316 "# e.g.: \"" SVN_RA_CAPABILITY_MERGEINFO ":some-other-capability\" " \
317 "(the order is undefined)." NL
318 "#" NL
319 "# The list is self-reported by the client. Therefore, you should not" NL
320 "# make security assumptions based on the capabilities list, nor should" NL
321 "# you assume that clients reliably report every capability they have." NL
322 "#" NL
323 "# The working directory for this hook program's invocation is undefined," NL
324 "# so the program should set one explicitly if it cares." NL
325 "#" NL
326 "# If the hook program exits with success, the commit continues; but" NL
327 "# if it exits with failure (non-zero), the commit is stopped before" NL
328 "# a Subversion txn is created, and STDERR is returned to the client." NL
329 "#" NL
330 "# On a Unix system, the normal procedure is to have '"SCRIPT_NAME"'" NL
331 "# invoke other programs to do the real work, though it may do the" NL
332 "# work itself too." NL
333 "#" NL
334 "# Note that '"SCRIPT_NAME"' must be executable by the user(s) who will" NL
335 "# invoke it (typically the user httpd runs as), and that user must" NL
336 "# have filesystem-level permission to access the repository." NL
337 "#" NL
338 "# On a Windows system, you should name the hook program" NL
339 "# '"SCRIPT_NAME".bat' or '"SCRIPT_NAME".exe'," NL
340 "# but the basic idea is the same." NL
341 "# " NL
342 HOOKS_ENVIRONMENT_TEXT
343 "# " NL
344 "# Here is an example hook script, for a Unix /bin/sh interpreter." NL
345 PREWRITTEN_HOOKS_TEXT
346 "" NL
347 "" NL
348 "REPOS=\"$1\"" NL
349 "USER=\"$2\"" NL
350 "" NL
351 "commit-allower.pl --repository \"$REPOS\" --user \"$USER\" || exit 1" NL
352 "special-auth-check.py --user \"$USER\" --auth-level 3 || exit 1" NL
353 "" NL
354 "# All checks passed, so allow the commit." NL
355 "exit 0" NL;
357 #undef SCRIPT_NAME
359 SVN_ERR_W(svn_io_file_create(this_path, contents, pool),
360 _("Creating start-commit hook"));
361 } /* end start-commit hook */
363 /* Pre-commit hook. */
365 this_path = apr_psprintf(pool, "%s%s",
366 svn_repos_pre_commit_hook(repos, pool),
367 SVN_REPOS__HOOK_DESC_EXT);
369 #define SCRIPT_NAME SVN_REPOS__HOOK_PRE_COMMIT
371 contents =
372 "#!/bin/sh" NL
373 "" NL
374 "# PRE-COMMIT HOOK" NL
375 "#" NL
376 "# The pre-commit hook is invoked before a Subversion txn is" NL
377 "# committed. Subversion runs this hook by invoking a program" NL
378 "# (script, executable, binary, etc.) named '"SCRIPT_NAME"' (for which" NL
379 "# this file is a template), with the following ordered arguments:" NL
380 "#" NL
381 "# [1] REPOS-PATH (the path to this repository)" NL
382 "# [2] TXN-NAME (the name of the txn about to be committed)" NL
383 "#" NL
384 "# The default working directory for the invocation is undefined, so" NL
385 "# the program should set one explicitly if it cares." NL
386 "#" NL
387 "# If the hook program exits with success, the txn is committed; but" NL
388 "# if it exits with failure (non-zero), the txn is aborted, no commit" NL
389 "# takes place, and STDERR is returned to the client. The hook" NL
390 "# program can use the 'svnlook' utility to help it examine the txn." NL
391 "#" NL
392 "# On a Unix system, the normal procedure is to have '"SCRIPT_NAME"'" NL
393 "# invoke other programs to do the real work, though it may do the" NL
394 "# work itself too." NL
395 "#" NL
396 "# *** NOTE: THE HOOK PROGRAM MUST NOT MODIFY THE TXN, EXCEPT ***" NL
397 "# *** FOR REVISION PROPERTIES (like svn:log or svn:author). ***" NL
398 "#" NL
399 "# This is why we recommend using the read-only 'svnlook' utility." NL
400 "# In the future, Subversion may enforce the rule that pre-commit" NL
401 "# hooks should not modify the versioned data in txns, or else come" NL
402 "# up with a mechanism to make it safe to do so (by informing the" NL
403 "# committing client of the changes). However, right now neither" NL
404 "# mechanism is implemented, so hook writers just have to be careful." NL
405 "#" NL
406 "# Note that '"SCRIPT_NAME"' must be executable by the user(s) who will" NL
407 "# invoke it (typically the user httpd runs as), and that user must" NL
408 "# have filesystem-level permission to access the repository." NL
409 "#" NL
410 "# On a Windows system, you should name the hook program" NL
411 "# '"SCRIPT_NAME".bat' or '"SCRIPT_NAME".exe'," NL
412 "# but the basic idea is the same." NL
413 "#" NL
414 HOOKS_ENVIRONMENT_TEXT
415 "# " NL
416 "# Here is an example hook script, for a Unix /bin/sh interpreter." NL
417 PREWRITTEN_HOOKS_TEXT
418 "" NL
419 "" NL
420 "REPOS=\"$1\"" NL
421 "TXN=\"$2\"" NL
422 "" NL
423 "# Make sure that the log message contains some text." NL
424 "SVNLOOK=" SVN_BINDIR "/svnlook" NL
425 "$SVNLOOK log -t \"$TXN\" \"$REPOS\" | \\" NL
426 " grep \"[a-zA-Z0-9]\" > /dev/null || exit 1" NL
427 "" NL
428 "# Check that the author of this commit has the rights to perform" NL
429 "# the commit on the files and directories being modified." NL
430 "commit-access-control.pl \"$REPOS\" \"$TXN\" commit-access-control.cfg || exit 1"
432 "" NL
433 "# All checks passed, so allow the commit." NL
434 "exit 0" NL;
436 #undef SCRIPT_NAME
438 SVN_ERR_W(svn_io_file_create(this_path, contents, pool),
439 _("Creating pre-commit hook"));
440 } /* end pre-commit hook */
443 /* Pre-revprop-change hook. */
445 this_path = apr_psprintf(pool, "%s%s",
446 svn_repos_pre_revprop_change_hook(repos, pool),
447 SVN_REPOS__HOOK_DESC_EXT);
449 #define SCRIPT_NAME SVN_REPOS__HOOK_PRE_REVPROP_CHANGE
451 contents =
452 "#!/bin/sh" NL
453 "" NL
454 "# PRE-REVPROP-CHANGE HOOK" NL
455 "#" NL
456 "# The pre-revprop-change hook is invoked before a revision property" NL
457 "# is added, modified or deleted. Subversion runs this hook by invoking" NL
458 "# a program (script, executable, binary, etc.) named '"SCRIPT_NAME"'" NL
459 "# (for which this file is a template), with the following ordered" NL
460 "# arguments:" NL
461 "#" NL
462 "# [1] REPOS-PATH (the path to this repository)" NL
463 "# [2] REVISION (the revision being tweaked)" NL
464 "# [3] USER (the username of the person tweaking the property)" NL
465 "# [4] PROPNAME (the property being set on the revision)" NL
466 "# [5] ACTION (the property is being 'A'dded, 'M'odified, or 'D'eleted)"
468 "#" NL
469 "# [STDIN] PROPVAL ** the new property value is passed via STDIN." NL
470 "#" NL
471 "# If the hook program exits with success, the propchange happens; but" NL
472 "# if it exits with failure (non-zero), the propchange doesn't happen." NL
473 "# The hook program can use the 'svnlook' utility to examine the " NL
474 "# existing value of the revision property." NL
475 "#" NL
476 "# WARNING: unlike other hooks, this hook MUST exist for revision" NL
477 "# properties to be changed. If the hook does not exist, Subversion " NL
478 "# will behave as if the hook were present, but failed. The reason" NL
479 "# for this is that revision properties are UNVERSIONED, meaning that" NL
480 "# a successful propchange is destructive; the old value is gone" NL
481 "# forever. We recommend the hook back up the old value somewhere." NL
482 "#" NL
483 "# On a Unix system, the normal procedure is to have '"SCRIPT_NAME"'" NL
484 "# invoke other programs to do the real work, though it may do the" NL
485 "# work itself too." NL
486 "#" NL
487 "# Note that '"SCRIPT_NAME"' must be executable by the user(s) who will" NL
488 "# invoke it (typically the user httpd runs as), and that user must" NL
489 "# have filesystem-level permission to access the repository." NL
490 "#" NL
491 "# On a Windows system, you should name the hook program" NL
492 "# '"SCRIPT_NAME".bat' or '"SCRIPT_NAME".exe'," NL
493 "# but the basic idea is the same." NL
494 "#" NL
495 HOOKS_ENVIRONMENT_TEXT
496 "# " NL
497 "# Here is an example hook script, for a Unix /bin/sh interpreter." NL
498 PREWRITTEN_HOOKS_TEXT
499 "" NL
500 "" NL
501 "REPOS=\"$1\"" NL
502 "REV=\"$2\"" NL
503 "USER=\"$3\"" NL
504 "PROPNAME=\"$4\"" NL
505 "ACTION=\"$5\"" NL
506 "" NL
507 "if [ \"$ACTION\" = \"M\" -a \"$PROPNAME\" = \"svn:log\" ]; then exit 0; fi" NL
508 "" NL
509 "echo \"Changing revision properties other than svn:log is prohibited\" >&2" NL
510 "exit 1" NL;
512 #undef SCRIPT_NAME
514 SVN_ERR_W(svn_io_file_create(this_path, contents, pool),
515 _("Creating pre-revprop-change hook"));
516 } /* end pre-revprop-change hook */
519 /* Pre-lock hook. */
521 this_path = apr_psprintf(pool, "%s%s",
522 svn_repos_pre_lock_hook(repos, pool),
523 SVN_REPOS__HOOK_DESC_EXT);
525 #define SCRIPT_NAME SVN_REPOS__HOOK_PRE_LOCK
527 contents =
528 "#!/bin/sh" NL
529 "" NL
530 "# PRE-LOCK HOOK" NL
531 "#" NL
532 "# The pre-lock hook is invoked before an exclusive lock is" NL
533 "# created. Subversion runs this hook by invoking a program " NL
534 "# (script, executable, binary, etc.) named '"SCRIPT_NAME"' (for which" NL
535 "# this file is a template), with the following ordered arguments:" NL
536 "#" NL
537 "# [1] REPOS-PATH (the path to this repository)" NL
538 "# [2] PATH (the path in the repository about to be locked)" NL
539 "# [3] USER (the user creating the lock)" NL
540 "#" NL
541 "# The default working directory for the invocation is undefined, so" NL
542 "# the program should set one explicitly if it cares." NL
543 "#" NL
544 "# If the hook program exits with success, the lock is created; but" NL
545 "# if it exits with failure (non-zero), the lock action is aborted" NL
546 "# and STDERR is returned to the client." NL
547 "" NL
548 "# On a Unix system, the normal procedure is to have '"SCRIPT_NAME"'" NL
549 "# invoke other programs to do the real work, though it may do the" NL
550 "# work itself too." NL
551 "#" NL
552 "# Note that '"SCRIPT_NAME"' must be executable by the user(s) who will" NL
553 "# invoke it (typically the user httpd runs as), and that user must" NL
554 "# have filesystem-level permission to access the repository." NL
555 "#" NL
556 "# On a Windows system, you should name the hook program" NL
557 "# '"SCRIPT_NAME".bat' or '"SCRIPT_NAME".exe'," NL
558 "# but the basic idea is the same." NL
559 "#" NL
560 "# Here is an example hook script, for a Unix /bin/sh interpreter:" NL
561 "" NL
562 "REPOS=\"$1\"" NL
563 "PATH=\"$2\"" NL
564 "USER=\"$3\"" NL
565 "" NL
566 "# If a lock exists and is owned by a different person, don't allow it" NL
567 "# to be stolen (e.g., with 'svn lock --force ...')." NL
568 "" NL
569 "# (Maybe this script could send email to the lock owner?)" NL
570 "SVNLOOK=" SVN_BINDIR "/svnlook" NL
571 "GREP=/bin/grep" NL
572 "SED=/bin/sed" NL
573 "" NL
574 "LOCK_OWNER=`$SVNLOOK lock \"$REPOS\" \"$PATH\" | \\" NL
575 " $GREP '^Owner: ' | $SED 's/Owner: //'`" NL
576 "" NL
577 "# If we get no result from svnlook, there's no lock, allow the lock to" NL
578 "# happen:" NL
579 "if [ \"$LOCK_OWNER\" = \"\" ]; then" NL
580 " exit 0" NL
581 "fi" NL
582 "" NL
583 "# If the person locking matches the lock's owner, allow the lock to" NL
584 "# happen:" NL
585 "if [ \"$LOCK_OWNER\" = \"$USER\" ]; then" NL
586 " exit 0" NL
587 "fi" NL
588 "" NL
589 "# Otherwise, we've got an owner mismatch, so return failure:" NL
590 "echo \"Error: $PATH already locked by ${LOCK_OWNER}.\" 1>&2" NL
591 "exit 1" NL;
593 #undef SCRIPT_NAME
595 SVN_ERR_W(svn_io_file_create(this_path, contents, pool),
596 "Creating pre-lock hook");
597 } /* end pre-lock hook */
600 /* Pre-unlock hook. */
602 this_path = apr_psprintf(pool, "%s%s",
603 svn_repos_pre_unlock_hook(repos, pool),
604 SVN_REPOS__HOOK_DESC_EXT);
606 #define SCRIPT_NAME SVN_REPOS__HOOK_PRE_UNLOCK
608 contents =
609 "#!/bin/sh" NL
610 "" NL
611 "# PRE-UNLOCK HOOK" NL
612 "#" NL
613 "# The pre-unlock hook is invoked before an exclusive lock is" NL
614 "# destroyed. Subversion runs this hook by invoking a program " NL
615 "# (script, executable, binary, etc.) named '"SCRIPT_NAME"' (for which" NL
616 "# this file is a template), with the following ordered arguments:" NL
617 "#" NL
618 "# [1] REPOS-PATH (the path to this repository)" NL
619 "# [2] PATH (the path in the repository about to be unlocked)" NL
620 "# [3] USER (the user destroying the lock)" NL
621 "#" NL
622 "# The default working directory for the invocation is undefined, so" NL
623 "# the program should set one explicitly if it cares." NL
624 "#" NL
625 "# If the hook program exits with success, the lock is destroyed; but" NL
626 "# if it exits with failure (non-zero), the unlock action is aborted" NL
627 "# and STDERR is returned to the client." NL
628 "" NL
629 "# On a Unix system, the normal procedure is to have '"SCRIPT_NAME"'" NL
630 "# invoke other programs to do the real work, though it may do the" NL
631 "# work itself too." NL
632 "#" NL
633 "# Note that '"SCRIPT_NAME"' must be executable by the user(s) who will" NL
634 "# invoke it (typically the user httpd runs as), and that user must" NL
635 "# have filesystem-level permission to access the repository." NL
636 "#" NL
637 "# On a Windows system, you should name the hook program" NL
638 "# '"SCRIPT_NAME".bat' or '"SCRIPT_NAME".exe'," NL
639 "# but the basic idea is the same." NL
640 "#" NL
641 "# Here is an example hook script, for a Unix /bin/sh interpreter:" NL
642 "" NL
643 "REPOS=\"$1\"" NL
644 "PATH=\"$2\"" NL
645 "USER=\"$3\"" NL
646 "" NL
647 "# If a lock is owned by a different person, don't allow it be broken." NL
648 "# (Maybe this script could send email to the lock owner?)" NL
649 "" NL
650 "SVNLOOK=" SVN_BINDIR "/svnlook" NL
651 "GREP=/bin/grep" NL
652 "SED=/bin/sed" NL
653 "" NL
654 "LOCK_OWNER=`$SVNLOOK lock \"$REPOS\" \"$PATH\" | \\" NL
655 " $GREP '^Owner: ' | $SED 's/Owner: //'`" NL
656 "" NL
657 "# If we get no result from svnlook, there's no lock, return success:" NL
658 "if [ \"$LOCK_OWNER\" = \"\" ]; then" NL
659 " exit 0" NL
660 "fi" NL
661 "" NL
662 "# If the person unlocking matches the lock's owner, return success:" NL
663 "if [ \"$LOCK_OWNER\" = \"$USER\" ]; then" NL
664 " exit 0" NL
665 "fi" NL
666 "" NL
667 "# Otherwise, we've got an owner mismatch, so return failure:" NL
668 "echo \"Error: $PATH locked by ${LOCK_OWNER}.\" 1>&2" NL
669 "exit 1" NL;
671 #undef SCRIPT_NAME
673 SVN_ERR_W(svn_io_file_create(this_path, contents, pool),
674 "Creating pre-unlock hook");
675 } /* end pre-unlock hook */
679 /* Post-commit hook. */
681 this_path = apr_psprintf(pool, "%s%s",
682 svn_repos_post_commit_hook(repos, pool),
683 SVN_REPOS__HOOK_DESC_EXT);
685 #define SCRIPT_NAME SVN_REPOS__HOOK_POST_COMMIT
687 contents =
688 "#!/bin/sh" NL
689 "" NL
690 "# POST-COMMIT HOOK" NL
691 "#" NL
692 "# The post-commit hook is invoked after a commit. Subversion runs" NL
693 "# this hook by invoking a program (script, executable, binary, etc.)" NL
694 "# named '"SCRIPT_NAME"' (for which this file is a template) with the " NL
695 "# following ordered arguments:" NL
696 "#" NL
697 "# [1] REPOS-PATH (the path to this repository)" NL
698 "# [2] REV (the number of the revision just committed)" NL
699 "#" NL
700 "# The default working directory for the invocation is undefined, so" NL
701 "# the program should set one explicitly if it cares." NL
702 "#" NL
703 "# Because the commit has already completed and cannot be undone," NL
704 "# the exit code of the hook program is ignored. The hook program" NL
705 "# can use the 'svnlook' utility to help it examine the" NL
706 "# newly-committed tree." NL
707 "#" NL
708 "# On a Unix system, the normal procedure is to have '"SCRIPT_NAME"'" NL
709 "# invoke other programs to do the real work, though it may do the" NL
710 "# work itself too." NL
711 "#" NL
712 "# Note that '"SCRIPT_NAME"' must be executable by the user(s) who will" NL
713 "# invoke it (typically the user httpd runs as), and that user must" NL
714 "# have filesystem-level permission to access the repository." NL
715 "#" NL
716 "# On a Windows system, you should name the hook program" NL
717 "# '"SCRIPT_NAME".bat' or '"SCRIPT_NAME".exe'," NL
718 "# but the basic idea is the same." NL
719 "# " NL
720 HOOKS_ENVIRONMENT_TEXT
721 "# " NL
722 "# Here is an example hook script, for a Unix /bin/sh interpreter." NL
723 PREWRITTEN_HOOKS_TEXT
724 "" NL
725 "" NL
726 "REPOS=\"$1\"" NL
727 "REV=\"$2\"" NL
729 "commit-email.pl \"$REPOS\" \"$REV\" commit-watchers@example.org" NL
730 "log-commit.py --repository \"$REPOS\" --revision \"$REV\"" NL;
732 #undef SCRIPT_NAME
734 SVN_ERR_W(svn_io_file_create(this_path, contents, pool),
735 _("Creating post-commit hook"));
736 } /* end post-commit hook */
739 /* Post-lock hook. */
741 this_path = apr_psprintf(pool, "%s%s",
742 svn_repos_post_lock_hook(repos, pool),
743 SVN_REPOS__HOOK_DESC_EXT);
745 #define SCRIPT_NAME SVN_REPOS__HOOK_POST_LOCK
747 contents =
748 "#!/bin/sh" NL
749 "" NL
750 "# POST-LOCK HOOK" NL
751 "#" NL
752 "# The post-lock hook is run after a path is locked. Subversion runs" NL
753 "# this hook by invoking a program (script, executable, binary, etc.)" NL
754 "# named '"SCRIPT_NAME"' (for which this file is a template) with the " NL
755 "# following ordered arguments:" NL
756 "#" NL
757 "# [1] REPOS-PATH (the path to this repository)" NL
758 "# [2] USER (the user who created the lock)" NL
759 "#" NL
760 "# The paths that were just locked are passed to the hook via STDIN (as" NL
761 "# of Subversion 1.2, only one path is passed per invocation, but the" NL
762 "# plan is to pass all locked paths at once, so the hook program" NL
763 "# should be written accordingly)." NL
764 "#" NL
765 "# The default working directory for the invocation is undefined, so" NL
766 "# the program should set one explicitly if it cares." NL
767 "#" NL
768 "# Because the lock has already been created and cannot be undone," NL
769 "# the exit code of the hook program is ignored. The hook program" NL
770 "# can use the 'svnlook' utility to help it examine the" NL
771 "# newly-created lock." NL
772 "#" NL
773 "# On a Unix system, the normal procedure is to have '"SCRIPT_NAME"'" NL
774 "# invoke other programs to do the real work, though it may do the" NL
775 "# work itself too." NL
776 "#" NL
777 "# Note that '"SCRIPT_NAME"' must be executable by the user(s) who will" NL
778 "# invoke it (typically the user httpd runs as), and that user must" NL
779 "# have filesystem-level permission to access the repository." NL
780 "#" NL
781 "# On a Windows system, you should name the hook program" NL
782 "# '"SCRIPT_NAME".bat' or '"SCRIPT_NAME".exe'," NL
783 "# but the basic idea is the same." NL
784 "# " NL
785 "# Here is an example hook script, for a Unix /bin/sh interpreter:" NL
786 "" NL
787 "REPOS=\"$1\"" NL
788 "USER=\"$2\"" NL
789 "" NL
790 "# Send email to interested parties, let them know a lock was created:" NL
791 "mailer.py lock \"$REPOS\" \"$USER\" /path/to/mailer.conf" NL;
793 #undef SCRIPT_NAME
795 SVN_ERR_W(svn_io_file_create(this_path, contents, pool),
796 "Creating post-lock hook");
797 } /* end post-lock hook */
800 /* Post-unlock hook. */
802 this_path = apr_psprintf(pool, "%s%s",
803 svn_repos_post_unlock_hook(repos, pool),
804 SVN_REPOS__HOOK_DESC_EXT);
806 #define SCRIPT_NAME SVN_REPOS__HOOK_POST_UNLOCK
808 contents =
809 "#!/bin/sh" NL
810 "" NL
811 "# POST-UNLOCK HOOK" NL
812 "#" NL
813 "# The post-unlock hook runs after a path is unlocked. Subversion runs" NL
814 "# this hook by invoking a program (script, executable, binary, etc.)" NL
815 "# named '"SCRIPT_NAME"' (for which this file is a template) with the " NL
816 "# following ordered arguments:" NL
817 "#" NL
818 "# [1] REPOS-PATH (the path to this repository)" NL
819 "# [2] USER (the user who destroyed the lock)" NL
820 "#" NL
821 "# The paths that were just unlocked are passed to the hook via STDIN" NL
822 "# (as of Subversion 1.2, only one path is passed per invocation, but" NL
823 "# the plan is to pass all unlocked paths at once, so the hook program" NL
824 "# should be written accordingly)." NL
825 "#" NL
826 "# The default working directory for the invocation is undefined, so" NL
827 "# the program should set one explicitly if it cares." NL
828 "#" NL
829 "# Because the lock has already been destroyed and cannot be undone," NL
830 "# the exit code of the hook program is ignored." NL
831 "#" NL
832 "# On a Unix system, the normal procedure is to have '"SCRIPT_NAME"'" NL
833 "# invoke other programs to do the real work, though it may do the" NL
834 "# work itself too." NL
835 "#" NL
836 "# Note that '"SCRIPT_NAME"' must be executable by the user(s) who will" NL
837 "# invoke it (typically the user httpd runs as), and that user must" NL
838 "# have filesystem-level permission to access the repository." NL
839 "#" NL
840 "# On a Windows system, you should name the hook program" NL
841 "# '"SCRIPT_NAME".bat' or '"SCRIPT_NAME".exe'," NL
842 "# but the basic idea is the same." NL
843 "# " NL
844 "# Here is an example hook script, for a Unix /bin/sh interpreter:" NL
845 "" NL
846 "REPOS=\"$1\"" NL
847 "USER=\"$2\"" NL
848 "" NL
849 "# Send email to interested parties, let them know a lock was removed:" NL
850 "mailer.py unlock \"$REPOS\" \"$USER\" /path/to/mailer.conf" NL;
852 #undef SCRIPT_NAME
854 SVN_ERR_W(svn_io_file_create(this_path, contents, pool),
855 "Creating post-unlock hook");
856 } /* end post-unlock hook */
859 /* Post-revprop-change hook. */
861 this_path = apr_psprintf(pool, "%s%s",
862 svn_repos_post_revprop_change_hook(repos, pool),
863 SVN_REPOS__HOOK_DESC_EXT);
865 #define SCRIPT_NAME SVN_REPOS__HOOK_POST_REVPROP_CHANGE
867 contents =
868 "#!/bin/sh" NL
869 "" NL
870 "# POST-REVPROP-CHANGE HOOK" NL
871 "#" NL
872 "# The post-revprop-change hook is invoked after a revision property" NL
873 "# has been added, modified or deleted. Subversion runs this hook by" NL
874 "# invoking a program (script, executable, binary, etc.) named" NL
875 "# '"SCRIPT_NAME"' (for which this file is a template), with the" NL
876 "# following ordered arguments:" NL
877 "#" NL
878 "# [1] REPOS-PATH (the path to this repository)" NL
879 "# [2] REV (the revision that was tweaked)" NL
880 "# [3] USER (the username of the person tweaking the property)" NL
881 "# [4] PROPNAME (the property that was changed)" NL
882 "# [5] ACTION (the property was 'A'dded, 'M'odified, or 'D'eleted)" NL
883 "#" NL
884 "# [STDIN] PROPVAL ** the old property value is passed via STDIN." NL
885 "#" NL
886 "# Because the propchange has already completed and cannot be undone," NL
887 "# the exit code of the hook program is ignored. The hook program" NL
888 "# can use the 'svnlook' utility to help it examine the" NL
889 "# new property value." NL
890 "#" NL
891 "# On a Unix system, the normal procedure is to have '"SCRIPT_NAME"'" NL
892 "# invoke other programs to do the real work, though it may do the" NL
893 "# work itself too." NL
894 "#" NL
895 "# Note that '"SCRIPT_NAME"' must be executable by the user(s) who will" NL
896 "# invoke it (typically the user httpd runs as), and that user must" NL
897 "# have filesystem-level permission to access the repository." NL
898 "#" NL
899 "# On a Windows system, you should name the hook program" NL
900 "# '"SCRIPT_NAME".bat' or '"SCRIPT_NAME".exe'," NL
901 "# but the basic idea is the same." NL
902 "# " NL
903 HOOKS_ENVIRONMENT_TEXT
904 "# " NL
905 "# Here is an example hook script, for a Unix /bin/sh interpreter." NL
906 PREWRITTEN_HOOKS_TEXT
907 "" NL
908 "" NL
909 "REPOS=\"$1\"" NL
910 "REV=\"$2\"" NL
911 "USER=\"$3\"" NL
912 "PROPNAME=\"$4\"" NL
913 "ACTION=\"$5\"" NL
914 "" NL
915 "commit-email.pl --revprop-change \"$REPOS\" \"$REV\" \"$USER\" \"$PROPNAME\" "
916 "watchers@example.org" NL;
918 #undef SCRIPT_NAME
920 SVN_ERR_W(svn_io_file_create(this_path, contents, pool),
921 _("Creating post-revprop-change hook"));
922 } /* end post-revprop-change hook */
924 return SVN_NO_ERROR;
927 static svn_error_t *
928 create_conf(svn_repos_t *repos, apr_pool_t *pool)
930 SVN_ERR_W(create_repos_dir(repos->conf_path, pool),
931 _("Creating conf directory"));
933 /* Write a default template for svnserve.conf. */
935 static const char * const svnserve_conf_contents =
936 "### This file controls the configuration of the svnserve daemon, if you" NL
937 "### use it to allow access to this repository. (If you only allow" NL
938 "### access through http: and/or file: URLs, then this file is" NL
939 "### irrelevant.)" NL
940 "" NL
941 "### Visit http://subversion.tigris.org/ for more information." NL
942 "" NL
943 "[general]" NL
944 "### These options control access to the repository for unauthenticated" NL
945 "### and authenticated users. Valid values are \"write\", \"read\"," NL
946 "### and \"none\". The sample settings below are the defaults." NL
947 "# anon-access = read" NL
948 "# auth-access = write" NL
949 "### The password-db option controls the location of the password" NL
950 "### database file. Unless you specify a path starting with a /," NL
951 "### the file's location is relative to the directory containing" NL
952 "### this configuration file." NL
953 #ifdef SVN_HAVE_SASL
954 "### If use-sasl is set to \"true\" below, this file will NOT be used." NL
955 #endif
956 "### Uncomment the line below to use the default password file." NL
957 "# password-db = passwd" NL
958 "### The authz-db option controls the location of the authorization" NL
959 "### rules for path-based access control. Unless you specify a path" NL
960 "### starting with a /, the file's location is relative to the the" NL
961 "### directory containing this file. If you don't specify an" NL
962 "### authz-db, no path-based access control is done." NL
963 "### Uncomment the line below to use the default authorization file." NL
964 "# authz-db = " SVN_REPOS__CONF_AUTHZ NL
965 "### This option specifies the authentication realm of the repository." NL
966 "### If two repositories have the same authentication realm, they should" NL
967 "### have the same password database, and vice versa. The default realm" NL
968 "### is repository's uuid." NL
969 #ifndef SVN_HAVE_SASL
970 "# realm = My First Repository" NL;
971 #else
972 "# realm = My First Repository" NL
973 "" NL
974 "[sasl]" NL
975 "### This option specifies whether you want to use the Cyrus SASL" NL
976 "### library for authentication. Default is false." NL
977 "# use-sasl = true" NL
978 "### These options specify the desired strength of the security layer" NL
979 "### that you want SASL to provide. 0 means no encryption, 1 means" NL
980 "### integrity-checking only, values larger than 1 are correlated" NL
981 "### to the effective key length for encryption (e.g. 128 means 128-bit" NL
982 "### encryption). The values below are the defaults." NL
983 "# min-encryption = 0" NL
984 "# max-encryption = 256" NL;
985 #endif
987 SVN_ERR_W(svn_io_file_create(svn_repos_svnserve_conf(repos, pool),
988 svnserve_conf_contents, pool),
989 _("Creating svnserve.conf file"));
993 static const char * const passwd_contents =
994 "### This file is an example password file for svnserve." NL
995 "### Its format is similar to that of svnserve.conf. As shown in the" NL
996 "### example below it contains one section labelled [users]." NL
997 "### The name and password for each user follow, one account per line." NL
998 "" NL
999 "[users]" NL
1000 "# harry = harryssecret" NL
1001 "# sally = sallyssecret" NL;
1003 SVN_ERR_W(svn_io_file_create(svn_path_join(repos->conf_path,
1004 SVN_REPOS__CONF_PASSWD,
1005 pool),
1006 passwd_contents, pool),
1007 _("Creating passwd file"));
1011 static const char * const authz_contents =
1012 "### This file is an example authorization file for svnserve." NL
1013 "### Its format is identical to that of mod_authz_svn authorization" NL
1014 "### files." NL
1015 "### As shown below each section defines authorizations for the path and" NL
1016 "### (optional) repository specified by the section name." NL
1017 "### The authorizations follow. An authorization line can refer to:" NL
1018 "### - a single user," NL
1019 "### - a group of users defined in a special [groups] section," NL
1020 "### - an alias defined in a special [aliases] section," NL
1021 "### - all authenticated users, using the '$authenticated' token," NL
1022 "### - only anonymous users, using the '$anonymous' token," NL
1023 "### - anyone, using the '*' wildcard." NL
1024 "###" NL
1025 "### A match can be inverted by prefixing the rule with '~'. Rules can" NL
1026 "### grant read ('r') access, read-write ('rw') access, or no access" NL
1027 "### ('')." NL
1028 "" NL
1029 "[aliases]" NL
1030 "# joe = /C=XZ/ST=Dessert/L=Snake City/O=Snake Oil, Ltd./OU=Research Institute/CN=Joe Average" NL
1031 "" NL
1032 "[groups]" NL
1033 "# harry_and_sally = harry,sally" NL
1034 "# harry_sally_and_joe = harry,sally,&joe" NL
1035 "" NL
1036 "# [/foo/bar]" NL
1037 "# harry = rw" NL
1038 "# &joe = r" NL
1039 "# * =" NL
1040 "" NL
1041 "# [repository:/baz/fuz]" NL
1042 "# @harry_and_sally = rw" NL
1043 "# * = r" NL;
1045 SVN_ERR_W(svn_io_file_create(svn_path_join(repos->conf_path,
1046 SVN_REPOS__CONF_AUTHZ,
1047 pool),
1048 authz_contents, pool),
1049 _("Creating authz file"));
1052 return SVN_NO_ERROR;
1055 /* Allocate and return a new svn_repos_t * object, initializing the
1056 directory pathname members based on PATH, and initializing the
1057 REPOSITORY_CAPABILITIES member.
1058 The members FS, FORMAT, and FS_TYPE are *not* initialized (they are null),
1059 and it is the caller's responsibility to fill them in if needed. */
1060 static svn_repos_t *
1061 create_svn_repos_t(const char *path, apr_pool_t *pool)
1063 svn_repos_t *repos = apr_pcalloc(pool, sizeof(*repos));
1065 repos->path = apr_pstrdup(pool, path);
1066 repos->db_path = svn_path_join(path, SVN_REPOS__DB_DIR, pool);
1067 repos->conf_path = svn_path_join(path, SVN_REPOS__CONF_DIR, pool);
1068 repos->hook_path = svn_path_join(path, SVN_REPOS__HOOK_DIR, pool);
1069 repos->lock_path = svn_path_join(path, SVN_REPOS__LOCK_DIR, pool);
1070 repos->repository_capabilities = apr_hash_make(pool);
1072 return repos;
1076 static svn_error_t *
1077 create_repos_structure(svn_repos_t *repos,
1078 const char *path,
1079 apr_hash_t *fs_config,
1080 apr_pool_t *pool)
1082 /* Create the top-level repository directory. */
1083 SVN_ERR_W(create_repos_dir(path, pool),
1084 _("Could not create top-level directory"));
1086 /* Create the DAV sandbox directory if pre-1.4 or pre-1.5-compatible. */
1087 if (fs_config
1088 && (apr_hash_get(fs_config, SVN_FS_CONFIG_PRE_1_4_COMPATIBLE,
1089 APR_HASH_KEY_STRING)
1090 || apr_hash_get(fs_config, SVN_FS_CONFIG_PRE_1_5_COMPATIBLE,
1091 APR_HASH_KEY_STRING)))
1093 const char *dav_path = svn_path_join(repos->path,
1094 SVN_REPOS__DAV_DIR, pool);
1095 SVN_ERR_W(create_repos_dir(dav_path, pool),
1096 _("Creating DAV sandbox dir"));
1099 /* Create the lock directory. */
1100 SVN_ERR(create_locks(repos, pool));
1102 /* Create the hooks directory. */
1103 SVN_ERR(create_hooks(repos, pool));
1105 /* Create the conf directory. */
1106 SVN_ERR(create_conf(repos, pool));
1108 /* Write the top-level README file. */
1110 const char * const readme_header =
1111 "This is a Subversion repository; use the 'svnadmin' tool to examine" NL
1112 "it. Do not add, delete, or modify files here unless you know how" NL
1113 "to avoid corrupting the repository." NL
1114 "" NL;
1115 const char * const readme_bdb_insert =
1116 "The directory \"" SVN_REPOS__DB_DIR "\" contains a Berkeley DB environment." NL
1117 "you may need to tweak the values in \"" SVN_REPOS__DB_DIR "/DB_CONFIG\" to match the" NL
1118 "requirements of your site." NL
1119 "" NL;
1120 const char * const readme_footer =
1121 "Visit http://subversion.tigris.org/ for more information." NL;
1122 apr_file_t *f;
1123 apr_size_t written;
1125 SVN_ERR(svn_io_file_open(&f,
1126 svn_path_join(path, SVN_REPOS__README, pool),
1127 (APR_WRITE | APR_CREATE | APR_EXCL),
1128 APR_OS_DEFAULT, pool));
1130 SVN_ERR(svn_io_file_write_full(f, readme_header, strlen(readme_header),
1131 &written, pool));
1132 if (strcmp(repos->fs_type, SVN_FS_TYPE_BDB) == 0)
1133 SVN_ERR(svn_io_file_write_full(f, readme_bdb_insert,
1134 strlen(readme_bdb_insert),
1135 &written, pool));
1136 SVN_ERR(svn_io_file_write_full(f, readme_footer, strlen(readme_footer),
1137 &written, pool));
1139 SVN_ERR(svn_io_file_close(f, pool));
1142 return SVN_NO_ERROR;
1146 /* There is, at present, nothing within the direct responsibility
1147 of libsvn_repos which requires locking. For historical compatibility
1148 reasons, the BDB libsvn_fs backend does not do its own locking, expecting
1149 libsvn_repos to do the locking for it. Here we take care of that
1150 backend-specific requirement.
1151 The kind of lock is controlled by EXCLUSIVE and NONBLOCKING.
1152 The lock is scoped to POOL. */
1153 static svn_error_t *
1154 lock_repos(svn_repos_t *repos,
1155 svn_boolean_t exclusive,
1156 svn_boolean_t nonblocking,
1157 apr_pool_t *pool)
1159 if (strcmp(repos->fs_type, SVN_FS_TYPE_BDB) == 0)
1161 svn_error_t *err;
1162 const char *lockfile_path = svn_repos_db_lockfile(repos, pool);
1164 err = svn_io_file_lock2(lockfile_path, exclusive, nonblocking, pool);
1165 if (err != NULL && APR_STATUS_IS_EAGAIN(err->apr_err))
1166 return err;
1167 SVN_ERR_W(err, _("Error opening db lockfile"));
1169 return SVN_NO_ERROR;
1173 svn_error_t *
1174 svn_repos_create(svn_repos_t **repos_p,
1175 const char *path,
1176 const char *unused_1,
1177 const char *unused_2,
1178 apr_hash_t *config,
1179 apr_hash_t *fs_config,
1180 apr_pool_t *pool)
1182 svn_repos_t *repos;
1183 svn_error_t *err;
1185 /* Allocate a repository object, filling in the format we will create. */
1186 repos = create_svn_repos_t(path, pool);
1187 repos->format = SVN_REPOS__FORMAT_NUMBER;
1189 /* Discover the type of the filesystem we are about to create. */
1190 if (fs_config)
1192 repos->fs_type = apr_hash_get(fs_config, SVN_FS_CONFIG_FS_TYPE,
1193 APR_HASH_KEY_STRING);
1194 if (apr_hash_get(fs_config, SVN_FS_CONFIG_PRE_1_4_COMPATIBLE,
1195 APR_HASH_KEY_STRING))
1196 repos->format = SVN_REPOS__FORMAT_NUMBER_LEGACY;
1199 if (! repos->fs_type)
1200 repos->fs_type = DEFAULT_FS_TYPE;
1202 /* Create the various files and subdirectories for the repository. */
1203 SVN_ERR_W(create_repos_structure(repos, path, fs_config, pool),
1204 _("Repository creation failed"));
1206 /* Lock if needed. */
1207 SVN_ERR(lock_repos(repos, FALSE, FALSE, pool));
1209 /* Create an environment for the filesystem. */
1210 if ((err = svn_fs_create(&repos->fs, repos->db_path, fs_config, pool)))
1212 /* If there was an error making the filesytem, e.g. unknown/supported
1213 * filesystem type. Clean up after ourselves. Yes this is safe because
1214 * create_repos_structure will fail if the path existed before we started
1215 * so we can't accidentally remove a directory that previously existed. */
1216 svn_error_clear(svn_io_remove_dir2(path, FALSE, NULL, NULL, pool));
1217 return err;
1220 /* This repository is ready. Stamp it with a format number. */
1221 SVN_ERR(svn_io_write_version_file
1222 (svn_path_join(path, SVN_REPOS__FORMAT, pool),
1223 repos->format, pool));
1225 *repos_p = repos;
1226 return SVN_NO_ERROR;
1230 /* Check if @a path is the root of a repository by checking if the
1231 * path contains the expected files and directories. Return TRUE
1232 * on errors (which would be permission errors, probably) so that
1233 * we the user will see them after we try to open the repository
1234 * for real. */
1235 static svn_boolean_t
1236 check_repos_path(const char *path,
1237 apr_pool_t *pool)
1239 svn_node_kind_t kind;
1240 svn_error_t *err;
1242 err = svn_io_check_path(svn_path_join(path, SVN_REPOS__FORMAT, pool),
1243 &kind, pool);
1244 if (err)
1246 svn_error_clear(err);
1247 return TRUE;
1249 if (kind != svn_node_file)
1250 return FALSE;
1252 /* Check the db/ subdir, but allow it to be a symlink (Subversion
1253 works just fine if it's a symlink). */
1254 err = svn_io_check_resolved_path
1255 (svn_path_join(path, SVN_REPOS__DB_DIR, pool), &kind, pool);
1256 if (err)
1258 svn_error_clear(err);
1259 return TRUE;
1261 if (kind != svn_node_dir)
1262 return FALSE;
1264 return TRUE;
1268 /* Verify that REPOS's format is suitable.
1269 Use POOL for temporary allocation. */
1270 static svn_error_t *
1271 check_repos_format(svn_repos_t *repos,
1272 apr_pool_t *pool)
1274 int format;
1275 const char *format_path;
1277 format_path = svn_path_join(repos->path, SVN_REPOS__FORMAT, pool);
1278 SVN_ERR(svn_io_read_version_file(&format, format_path, pool));
1280 if (format != SVN_REPOS__FORMAT_NUMBER &&
1281 format != SVN_REPOS__FORMAT_NUMBER_LEGACY)
1283 return svn_error_createf
1284 (SVN_ERR_REPOS_UNSUPPORTED_VERSION, NULL,
1285 _("Expected repository format '%d' or '%d'; found format '%d'"),
1286 SVN_REPOS__FORMAT_NUMBER_LEGACY, SVN_REPOS__FORMAT_NUMBER,
1287 format);
1290 repos->format = format;
1292 return SVN_NO_ERROR;
1296 /* Set *REPOS_P to a repository at PATH which has been opened.
1297 See lock_repos() above regarding EXCLUSIVE and NONBLOCKING.
1298 OPEN_FS indicates whether the Subversion filesystem should be opened,
1299 the handle being placed into repos->fs.
1300 Do all allocation in POOL. */
1301 static svn_error_t *
1302 get_repos(svn_repos_t **repos_p,
1303 const char *path,
1304 svn_boolean_t exclusive,
1305 svn_boolean_t nonblocking,
1306 svn_boolean_t open_fs,
1307 apr_pool_t *pool)
1309 svn_repos_t *repos;
1311 /* Allocate a repository object. */
1312 repos = create_svn_repos_t(path, pool);
1314 /* Verify the validity of our repository format. */
1315 SVN_ERR(check_repos_format(repos, pool));
1317 /* Discover the FS type. */
1318 SVN_ERR(svn_fs_type(&repos->fs_type, repos->db_path, pool));
1320 /* Lock if needed. */
1321 SVN_ERR(lock_repos(repos, exclusive, nonblocking, pool));
1323 /* Open up the filesystem only after obtaining the lock. */
1324 if (open_fs)
1325 SVN_ERR(svn_fs_open(&repos->fs, repos->db_path, NULL, pool));
1327 *repos_p = repos;
1328 return SVN_NO_ERROR;
1333 const char *
1334 svn_repos_find_root_path(const char *path,
1335 apr_pool_t *pool)
1337 const char *candidate = path;
1338 const char *decoded;
1339 svn_error_t *err;
1341 while (1)
1343 /* Try to decode the path, so we don't fail if it contains characters
1344 that aren't supported by the OS filesystem. The subversion fs
1345 isn't restricted by the OS filesystem character set. */
1346 err = svn_utf_cstring_from_utf8(&decoded, candidate, pool);
1347 if (!err && check_repos_path(candidate, pool))
1348 break;
1349 svn_error_clear(err);
1351 if (candidate[0] == '\0' ||
1352 svn_dirent_is_root(candidate, strlen(candidate)))
1353 return NULL;
1355 candidate = svn_path_dirname(candidate, pool);
1358 return candidate;
1362 svn_error_t *
1363 svn_repos_open(svn_repos_t **repos_p,
1364 const char *path,
1365 apr_pool_t *pool)
1367 /* Fetch a repository object initialized with a shared read/write
1368 lock on the database. */
1370 SVN_ERR(get_repos(repos_p, path, FALSE, FALSE, TRUE, pool));
1372 return SVN_NO_ERROR;
1376 svn_error_t *
1377 svn_repos_upgrade(const char *path,
1378 svn_boolean_t nonblocking,
1379 svn_error_t *(*start_callback)(void *baton),
1380 void *start_callback_baton,
1381 apr_pool_t *pool)
1383 svn_repos_t *repos;
1384 const char *format_path;
1385 int format;
1386 apr_pool_t *subpool = svn_pool_create(pool);
1388 /* Fetch a repository object; for the Berkeley DB backend, it is
1389 initialized with an EXCLUSIVE lock on the database. This will at
1390 least prevent others from trying to read or write to it while we
1391 run recovery. (Other backends should do their own locking; see
1392 lock_repos.) */
1393 SVN_ERR(get_repos(&repos, path, TRUE, nonblocking, FALSE, subpool));
1395 if (start_callback)
1396 SVN_ERR(start_callback(start_callback_baton));
1398 /* Try to overwrite with its own contents. We do this only to
1399 verify that we can, because we don't want to actually bump the
1400 format of the repository until our underlying filesystem claims
1401 to have been upgraded correctly. */
1402 format_path = svn_path_join(repos->path, SVN_REPOS__FORMAT, subpool);
1403 SVN_ERR(svn_io_read_version_file(&format, format_path, subpool));
1404 SVN_ERR(svn_io_write_version_file(format_path, format, subpool));
1406 /* Try to upgrade the filesystem. */
1407 SVN_ERR(svn_fs_upgrade(repos->db_path, subpool));
1409 /* Now overwrite our format file with the latest version. */
1410 SVN_ERR(svn_io_write_version_file(format_path, SVN_REPOS__FORMAT_NUMBER,
1411 subpool));
1413 /* Close shop and free the subpool, to release the exclusive lock. */
1414 svn_pool_destroy(subpool);
1416 return SVN_NO_ERROR;
1420 svn_error_t *
1421 svn_repos_delete(const char *path,
1422 apr_pool_t *pool)
1424 const char *db_path = svn_path_join(path, SVN_REPOS__DB_DIR, pool);
1426 /* Delete the filesystem environment... */
1427 SVN_ERR(svn_fs_delete_fs(db_path, pool));
1429 /* ...then blow away everything else. */
1430 SVN_ERR(svn_io_remove_dir2(path, FALSE, NULL, NULL, pool));
1432 return SVN_NO_ERROR;
1436 /* Repository supports the capability. */
1437 static const char *capability_yes = "yes";
1438 /* Repository does not support the capability. */
1439 static const char *capability_no = "no";
1441 svn_error_t *
1442 svn_repos_has_capability(svn_repos_t *repos,
1443 svn_boolean_t *has,
1444 const char *capability,
1445 apr_pool_t *pool)
1447 const char *val = apr_hash_get(repos->repository_capabilities,
1448 capability, APR_HASH_KEY_STRING);
1450 if (val == capability_yes)
1452 *has = TRUE;
1454 else if (val == capability_no)
1456 *has = FALSE;
1458 /* Else don't know, so investigate. */
1459 else if (strcmp(capability, SVN_REPOS_CAPABILITY_MERGEINFO) == 0)
1461 svn_error_t *err;
1462 svn_fs_root_t *root;
1463 svn_mergeinfo_catalog_t ignored;
1464 apr_array_header_t *paths = apr_array_make(pool, 1,
1465 sizeof(char *));
1467 SVN_ERR(svn_fs_revision_root(&root, repos->fs, 0, pool));
1468 APR_ARRAY_PUSH(paths, const char *) = "";
1469 err = svn_fs_get_mergeinfo(&ignored, root, paths, FALSE, FALSE, pool);
1471 if (err)
1473 if (err->apr_err == SVN_ERR_UNSUPPORTED_FEATURE)
1475 svn_error_clear(err);
1476 apr_hash_set(repos->repository_capabilities,
1477 SVN_REPOS_CAPABILITY_MERGEINFO,
1478 APR_HASH_KEY_STRING, capability_no);
1479 *has = FALSE;
1481 else if (err->apr_err == SVN_ERR_FS_NOT_FOUND)
1483 /* Mergeinfo requests use relative paths, and anyway we're
1484 in r0, so we're likely to get this error -- but it
1485 means the repository supports mergeinfo! */
1486 svn_error_clear(err);
1487 apr_hash_set(repos->repository_capabilities,
1488 SVN_REPOS_CAPABILITY_MERGEINFO,
1489 APR_HASH_KEY_STRING, capability_yes);
1490 *has = TRUE;
1492 else
1494 return err;
1497 else
1499 apr_hash_set(repos->repository_capabilities,
1500 SVN_REPOS_CAPABILITY_MERGEINFO,
1501 APR_HASH_KEY_STRING, capability_yes);
1502 *has = TRUE;
1505 else
1507 return svn_error_createf(SVN_ERR_UNKNOWN_CAPABILITY, 0,
1508 _("unknown capability '%s'"), capability);
1511 return SVN_NO_ERROR;
1515 svn_fs_t *
1516 svn_repos_fs(svn_repos_t *repos)
1518 if (! repos)
1519 return NULL;
1520 return repos->fs;
1524 /* For historical reasons, for the Berkeley DB backend, this code uses
1525 * repository locking, which is motivated by the need to support the
1526 * Berkeley DB error DB_RUN_RECOVERY. (FSFS takes care of locking
1527 * itself, inside its implementation of svn_fs_recover.) Here's how
1528 * it works:
1530 * Every accessor of a repository's database takes out a shared lock
1531 * on the repository -- both readers and writers get shared locks, and
1532 * there can be an unlimited number of shared locks simultaneously.
1534 * Sometimes, a db access returns the error DB_RUN_RECOVERY. When
1535 * this happens, we need to run svn_fs_recover() on the db
1536 * with no other accessors present. So we take out an exclusive lock
1537 * on the repository. From the moment we request the exclusive lock,
1538 * no more shared locks are granted, and when the last shared lock
1539 * disappears, the exclusive lock is granted. As soon as we get it,
1540 * we can run recovery.
1542 * We assume that once any berkeley call returns DB_RUN_RECOVERY, they
1543 * all do, until recovery is run.
1546 svn_error_t *
1547 svn_repos_recover3(const char *path,
1548 svn_boolean_t nonblocking,
1549 svn_error_t *(*start_callback)(void *baton),
1550 void *start_callback_baton,
1551 svn_cancel_func_t cancel_func, void *cancel_baton,
1552 apr_pool_t *pool)
1554 svn_repos_t *repos;
1555 apr_pool_t *subpool = svn_pool_create(pool);
1557 /* Fetch a repository object; for the Berkeley DB backend, it is
1558 initialized with an EXCLUSIVE lock on the database. This will at
1559 least prevent others from trying to read or write to it while we
1560 run recovery. (Other backends should do their own locking; see
1561 lock_repos.) */
1562 SVN_ERR(get_repos(&repos, path, TRUE, nonblocking,
1563 FALSE, /* don't try to open the db yet. */
1564 subpool));
1566 if (start_callback)
1567 SVN_ERR(start_callback(start_callback_baton));
1569 /* Recover the database to a consistent state. */
1570 SVN_ERR(svn_fs_recover(repos->db_path, cancel_func, cancel_baton, subpool));
1572 /* Close shop and free the subpool, to release the exclusive lock. */
1573 svn_pool_destroy(subpool);
1575 return SVN_NO_ERROR;
1579 svn_error_t *
1580 svn_repos_recover2(const char *path,
1581 svn_boolean_t nonblocking,
1582 svn_error_t *(*start_callback)(void *baton),
1583 void *start_callback_baton,
1584 apr_pool_t *pool)
1586 return svn_repos_recover3(path, nonblocking,
1587 start_callback, start_callback_baton,
1588 NULL, NULL,
1589 pool);
1592 svn_error_t *
1593 svn_repos_recover(const char *path,
1594 apr_pool_t *pool)
1596 return svn_repos_recover2(path, FALSE, NULL, NULL, pool);
1599 svn_error_t *svn_repos_db_logfiles(apr_array_header_t **logfiles,
1600 const char *path,
1601 svn_boolean_t only_unused,
1602 apr_pool_t *pool)
1604 svn_repos_t *repos;
1605 int i;
1607 SVN_ERR(get_repos(&repos, path,
1608 FALSE, FALSE,
1609 FALSE, /* Do not open fs. */
1610 pool));
1612 SVN_ERR(svn_fs_berkeley_logfiles(logfiles,
1613 svn_repos_db_env(repos, pool),
1614 only_unused,
1615 pool));
1617 /* Loop, printing log files. */
1618 for (i = 0; i < (*logfiles)->nelts; i++)
1620 const char ** log_file = &(APR_ARRAY_IDX(*logfiles, i, const char *));
1621 *log_file = svn_path_join(SVN_REPOS__DB_DIR, *log_file, pool);
1624 return SVN_NO_ERROR;
1627 /** Hot copy structure copy context.
1629 struct hotcopy_ctx_t {
1630 const char *dest; /* target location to construct */
1631 unsigned int src_len; /* len of the source path*/
1634 /** Called by (svn_io_dir_walk).
1635 * Copies the repository structure with exception of @c SVN_REPOS__DB_DIR,
1636 * @c SVN_REPOS__LOCK_DIR and @c SVN_REPOS__FORMAT.
1637 * Those directories and files are handled separetly.
1638 * @a baton is a pointer to (struct hotcopy_ctx_t) specifying
1639 * destination path to copy to and the length of the source path.
1641 * @copydoc svn_io_dir_walk()
1643 static svn_error_t *hotcopy_structure(void *baton,
1644 const char *path,
1645 const apr_finfo_t *finfo,
1646 apr_pool_t *pool)
1648 const struct hotcopy_ctx_t *ctx = ((struct hotcopy_ctx_t *) baton);
1649 const char *sub_path;
1650 const char *target;
1652 if (strlen(path) == ctx->src_len)
1654 sub_path = "";
1656 else
1658 sub_path = &path[ctx->src_len+1];
1660 /* Check if we are inside db directory and if so skip it */
1661 if (svn_path_compare_paths
1662 (svn_path_get_longest_ancestor(SVN_REPOS__DB_DIR, sub_path, pool),
1663 SVN_REPOS__DB_DIR) == 0)
1664 return SVN_NO_ERROR;
1666 if (svn_path_compare_paths
1667 (svn_path_get_longest_ancestor(SVN_REPOS__LOCK_DIR, sub_path, pool),
1668 SVN_REPOS__LOCK_DIR) == 0)
1669 return SVN_NO_ERROR;
1671 if (svn_path_compare_paths
1672 (svn_path_get_longest_ancestor(SVN_REPOS__FORMAT, sub_path, pool),
1673 SVN_REPOS__FORMAT) == 0)
1674 return SVN_NO_ERROR;
1677 target = svn_path_join(ctx->dest, sub_path, pool);
1679 if (finfo->filetype == APR_DIR)
1681 SVN_ERR(create_repos_dir(target, pool));
1683 else if (finfo->filetype == APR_REG)
1686 SVN_ERR(svn_io_copy_file(path, target, TRUE, pool));
1689 return SVN_NO_ERROR;
1693 /** Obtain a lock on db logs lock file. Create one if it does not exist.
1695 static svn_error_t *
1696 lock_db_logs_file(svn_repos_t *repos,
1697 svn_boolean_t exclusive,
1698 apr_pool_t *pool)
1700 const char * lock_file = svn_repos_db_logs_lockfile(repos, pool);
1702 /* Try to create a lock file, in case if it is missing. As in case of the
1703 repositories created before hotcopy functionality. */
1704 svn_error_clear(create_db_logs_lock(repos, pool));
1706 SVN_ERR(svn_io_file_lock2(lock_file, exclusive, FALSE, pool));
1708 return SVN_NO_ERROR;
1712 /* Make a copy of a repository with hot backup of fs. */
1713 svn_error_t *
1714 svn_repos_hotcopy(const char *src_path,
1715 const char *dst_path,
1716 svn_boolean_t clean_logs,
1717 apr_pool_t *pool)
1719 svn_repos_t *src_repos;
1720 svn_repos_t *dst_repos;
1721 struct hotcopy_ctx_t hotcopy_context;
1723 /* Try to open original repository */
1724 SVN_ERR(get_repos(&src_repos, src_path,
1725 FALSE, FALSE,
1726 FALSE, /* don't try to open the db yet. */
1727 pool));
1729 /* If we are going to clean logs, then get an exclusive lock on
1730 db-logs.lock, to ensure that no one else will work with logs.
1732 If we are just copying, then get a shared lock to ensure that
1733 no one else will clean logs while we copying them */
1735 SVN_ERR(lock_db_logs_file(src_repos, clean_logs, pool));
1737 /* Copy the repository to a new path, with exception of
1738 specially handled directories */
1740 hotcopy_context.dest = dst_path;
1741 hotcopy_context.src_len = strlen(src_path);
1742 SVN_ERR(svn_io_dir_walk(src_path,
1744 hotcopy_structure,
1745 &hotcopy_context,
1746 pool));
1748 /* Prepare dst_repos object so that we may create locks,
1749 so that we may open repository */
1751 dst_repos = create_svn_repos_t(dst_path, pool);
1752 dst_repos->fs_type = src_repos->fs_type;
1753 dst_repos->format = src_repos->format;
1755 SVN_ERR(create_locks(dst_repos, pool));
1757 SVN_ERR(svn_io_dir_make_sgid(dst_repos->db_path, APR_OS_DEFAULT, pool));
1759 /* Exclusively lock the new repository.
1760 No one should be accessing it at the moment */
1761 SVN_ERR(lock_repos(dst_repos, TRUE, FALSE, pool));
1763 SVN_ERR(svn_fs_hotcopy(src_repos->db_path, dst_repos->db_path,
1764 clean_logs, pool));
1766 /* Destination repository is ready. Stamp it with a format number. */
1767 SVN_ERR(svn_io_write_version_file
1768 (svn_path_join(dst_repos->path, SVN_REPOS__FORMAT, pool),
1769 dst_repos->format, pool));
1771 return SVN_NO_ERROR;
1774 /* Return the library version number. */
1775 const svn_version_t *
1776 svn_repos_version(void)
1778 SVN_VERSION_BODY;
1783 svn_error_t *
1784 svn_repos_stat(svn_dirent_t **dirent,
1785 svn_fs_root_t *root,
1786 const char *path,
1787 apr_pool_t *pool)
1789 svn_node_kind_t kind;
1790 svn_dirent_t *ent;
1791 const char *datestring;
1792 apr_hash_t *prophash;
1794 SVN_ERR(svn_fs_check_path(&kind, root, path, pool));
1796 if (kind == svn_node_none)
1798 *dirent = NULL;
1799 return SVN_NO_ERROR;
1802 ent = apr_pcalloc(pool, sizeof(*ent));
1803 ent->kind = kind;
1805 if (kind == svn_node_file)
1806 SVN_ERR(svn_fs_file_length(&(ent->size), root, path, pool));
1808 SVN_ERR(svn_fs_node_proplist(&prophash, root, path, pool));
1809 if (apr_hash_count(prophash) > 0)
1810 ent->has_props = TRUE;
1812 SVN_ERR(svn_repos_get_committed_info(&(ent->created_rev),
1813 &datestring,
1814 &(ent->last_author),
1815 root, path, pool));
1816 if (datestring)
1817 SVN_ERR(svn_time_from_cstring(&(ent->time), datestring, pool));
1819 *dirent = ent;
1820 return SVN_NO_ERROR;
1823 svn_error_t *
1824 svn_repos_remember_client_capabilities(svn_repos_t *repos,
1825 apr_array_header_t *capabilities)
1827 repos->client_capabilities = capabilities;
1828 return SVN_NO_ERROR;